
/**
 * 	File:	func.js
 * 	Author:	Damien Vouillamoz <vouillamoz@gmail.com>
 *  Date:	17.01.2006
 *  Desc:	This file provides a couple of JavaScript functions to support the application
**/

//-------------------------------------------------------------------------------------------

/*
* 	Ajax-Initialisation
*/
var ajaxRequest;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
		}
	}
}


/**
* 	Function to redirect from index to a specific form
*	
*	@param element Country Name
*	
*	@return 0
**/
function redirect_toSpecific_form(element) {
	var choice_str = element.value;
	var formpath = "forms/";
	
	formScritpPath = "";
	
	switch(choice_str) {
	
		case "Austria":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-austria.php?frmName=Austria";
		formScritpPath = "contact/forms/austria.php";
		break;
		
		case "Bahamas":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-bahamas.php?frmName=Bahamas";
		formScritpPath = "contact/forms/bahamas.php";
		break;
		
		case "Belgium":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "contact/forms/belgium.php";
		break;
		
		/*case "Channel Islands":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "form-jursey.php";
		break;*/
		
		case "China":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-china.php";
		formScritpPath = "contact/forms/china.php";
		break;
		
		case "Croatia":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-croatia.php";
		formScritpPath = "contact/forms/croatia.php";
		break;
		
		case "Commonwealth of Dominica":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "contact/forms/commonwealth_of_dominica.php";
		break;
		
		case "Hong Kong":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-china.php";
		formScritpPath = "contact/forms/hong_kong.php";
		break;
		
		/*case "Ireland":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "form-geninfo2.php?frmName=Ireland";
		break;*/
		
		/*case "Italy":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "form-geninfo2.php?frmName=Italy";
		break;*/

		case "Jersey":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-jursey.php";
		formScritpPath = "contact/forms/jersey.php";
		break;
		
		case "Liechtenstein":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-liechtenstein.php";
		formScritpPath = "contact/forms/liechtenstein.php";
		break;

		case "Malta":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-malta.php";
		formScritpPath = "contact/forms/malta.php";
		break;

		case "Monaco":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-geninfo2.php?frmName=Monaco";
		formScritpPath = "contact/forms/monaco.php";
		break;
		
		case "Panama":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-panama.htm";
		formScritpPath = "contact/forms/panama.php";
		break;
		
		case "Spain":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-geninfo2.php?frmName=Spain";
		formScritpPath = "contact/forms/spain.php";
		break;
					
		case "St. Kitts and Nevis":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "contact/forms/stkitts_and_nevis.php";
		break;
		
		case "Switzerland":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-switzerland.php";
		formScritpPath = "contact/forms/switzerland.php";
		break;
		
		case "United Kingdom":
		formpath = "http://www.henleyglobal.com/";
		//formScritpPath = "form-geninfo2.php?frmName=United Kingdom";
		formScritpPath = "contact/forms/united_kingdom.php";
		break;
		
		case "Humanitarian":
		formpath = "http://www.henleyglobal.com/";
		formScritpPath = "contact/forms/humanitarian.php";
		break;
		
		default:
		break;
	}
	
	
	if(formScritpPath.length > 0) {
		//alert(formScritpPath);
		document.getElementById("chosenform").value = formScritpPath;
		document.forms['contactform'].submit();
	}
	
	return 0;
}

    
/*
*	Function: 		chkContactInformation
*	Description: 	Checks a contact form for the required details
*
*	@param form		Form element
*	@param returnedEmailData	Returned value from the AJAX-Routine
*/
function chkContactInformation(form, returnedEmailData)
{

	var checkContentInformation = true;
	
	//Check Salutation
	if (document.forms[form].contactInfo_salutation.value == "")
	{
		document.getElementById('contactInfo_salutation').style.backgroundColor = "#F9CB7A";
		checkContentInformation = false;
	}
	else
	{
		document.getElementById('contactInfo_salutation').style.backgroundColor = "";
	}
	
	
	//Check FirstName
	if (document.forms[form].contactInfo_firstName.value == "")
	{
		document.getElementById('contactInfo_firstName').style.backgroundColor = "#F9CB7A";
		checkContentInformation = false;
	}
	else
	{
		document.getElementById('contactInfo_firstName').style.backgroundColor = "";
	}
	
	//Check LastName
	if (document.forms[form].contactInfo_lastName.value == "")
	{
		document.getElementById('contactInfo_lastName').style.backgroundColor = "#F9CB7A";
		checkContentInformation = false;
	}
	else
	{
		document.getElementById('contactInfo_lastName').style.backgroundColor = "";
	}
	
	
	/**Check email**/
	//E-mail not valid
	if(returnedEmailData[0] == "0") {
		
		//email (first field) empty
		if(document.getElementById('contactInfo_email_field').value == "") {
			document.getElementById('contactInfo_email').style.backgroundColor = "#F9CB7A";
			document.getElementById('contactInfo_emailError').innerHTML = "";
		}
		else {
		//email (first field) invalid
			document.getElementById('contactInfo_email').style.backgroundColor = "#F9CB7A";
			document.getElementById('contactInfo_emailError').innerHTML = "Invalid e-mail address";
		}
		
		//Set the retype email (second field) emtpy and without a color
		document.getElementById('contactInfo_emailRetype_field').value = "";
		document.getElementById('contactInfo_emailRetypeError').innerHTML = "";
		
		//set flat of an error
		checkContentInformation = false;
	}
	
	//Email (first field) is valid
	if(returnedEmailData[0] == "1") {
		
		//fields are not identic
		if(document.getElementById('contactInfo_email_field').value != document.getElementById('contactInfo_emailRetype_field').value) {
			
			document.getElementById('contactInfo_email').style.backgroundColor = "";
			document.getElementById('contactInfo_emailError').innerHTML = "";
			
			if(document.getElementById('contactInfo_emailRetype_field').value == "") {
				document.getElementById('contactInfo_emailRetype').style.backgroundColor = "#F9CB7A";
				document.getElementById('contactInfo_emailRetypeError').innerHTML = "";
			}
			else {
				document.getElementById('contactInfo_emailRetype').style.backgroundColor = "#F9CB7A";
				document.getElementById('contactInfo_emailRetypeError').innerHTML = "Invalid e-mail address";
			}
			
			//set flag of an error
			checkContentInformation = false;
		}
		else {
		// fields are identic
			document.getElementById('contactInfo_email').style.backgroundColor = "";
			document.getElementById('contactInfo_emailError').innerHTML = "";
			
			document.getElementById('contactInfo_emailRetype').style.backgroundColor = "";
			document.getElementById('contactInfo_emailRetypeError').innerHTML = "";
		}
	}
	
	
	if(form == "Press/Media") {
		checkContentInformation = chkMediaForm(form, checkContentInformation);
	}
		
	else if(form == "Humanitarian") {
		if (document.getElementById("humanitarianQuestion").value == "")
		{
			document.getElementById("humanitarianQuestion").style.backgroundColor = "#F9CB7A";
			checkContentInformation = false;
		}
		else
		{
			document.getElementById("humanitarianQuestion").style.backgroundColor = "";
		}
	}
	
	return checkContentInformation;
}


/**
*
*	Function:		chkMediaForm
*	Description:	Checks all additional Fields/Conditions of the Media-Form
*
*	@param	form	The form element
*	@param	checkContentInformation Flag wether an error has already occured
*
*	@return	If checkContentInformation already false -> false, else only false if error occurs
**/
function chkMediaForm(form, checkContentInformation) {
	
	var checkMediaSpecificInformation = true;
	
	//Check Position
	if(document.forms[form].contactInfo_position.value == "") {
		document.getElementById('contactInfo_position').style.backgroundColor = "#F9CB7A";
		checkMediaSpecificInformation = false;
	}
	else {
		document.getElementById('contactInfo_position').style.backgroundColor = "";
	}

	
	//Check Media Organisation
	if(document.forms[form].contactInfo_mediaOrganization.value == "") {
		document.getElementById('contactInfo_mediaOrganization').style.backgroundColor = "#F9CB7A";
		checkMediaSpecificInformation = false;
	}
	else {
		document.getElementById('contactInfo_mediaOrganization').style.backgroundColor = "";
	}
	
	
	
	if(checkMediaSpecificInformation == false)
		return checkMediaSpecificInformation;
	else 
		return checkContentInformation;
}


/*
*	Function: 		chkForm
*	Description:	Checks all mandatory fields in the form
*	
*	@param	form	The form element
*/
function chkForm(form)
{	
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
			}
		}
	}
	
	//onreadystatehange
	ajaxRequest.onreadystatechange = function ()
	{
		// trim the email-addresses
		document.getElementById('contactInfo_email_field').value = trim(document.getElementById('contactInfo_email_field').value);
		document.getElementById('contactInfo_emailRetype_field').value = trim(document.getElementById('contactInfo_emailRetype_field').value);

		if(ajaxRequest.readyState == 4)
		{
			/*
			* excluded by Arno Grüter <arno.grueter@gmail.com>
			* What was this function for? -> didn't work!
			*/
			// var returnedEmailData = ajaxRequest.responseText.split(",");
			
			// added new by Arno Grüter <arno.grueter@gmail.com>
			var returnedEmailData = new Array();
			returnedEmailData[0] = validate_email(document.getElementById('contactInfo_email_field'));
			
			var checkBoxCheck = chkAllCheckBoxes(form);
			var checkContentInformation = chkContactInformation(form, returnedEmailData);
						
			if (checkBoxCheck && checkContentInformation)
			{	
				document.getElementById('sec_check').value = 1;
				// document.location.href = "#top";
				document.forms[form].submit();
			}
			else
			{
				document.getElementById('errorMessage').innerHTML = "<strong>Required Information</strong><br /><br />Please note the highlighted areas - kindly complete those sections where information or a selection is required. Your information will help us to provide you with excellent service.";
				document.getElementById('errorMessage').style.visibility = 'visible';
				document.getElementById('errorMessage').style.align = 'left';
				// document.location.href = "#top";
			}
		}
	}
	

	var email = document.getElementById('contactInfo_email_field').value;
	ajaxRequest.open("POST", "http://www.henleyglobal.com/fileadmin/contact/res/ajax/check_email.php?email=" + escape(email), true);
	ajaxRequest.send(null);
	
}


/*
*	Function: 		chkCheckBox
*	Description: 	Checks if all required fields next to the checkboxes are filled out
*	
*	@param	form	The form element we are going to check
*	@param  checkboxName	Name of the checkbox that has been checked
*	@param 	fieldName	Name of the field which has to be filled out
*
*	@return	true/false If the checked checkbox's field is not empty
*/
function chkCheckBox(form,checkboxName,fieldName)
{		
		if(document.getElementById(checkboxName)) {
			
			//um sicherzustellen, dass beim buisness-formular mind. eine checkbox angekreuzt wird.
			if(checkboxName.substring(0, 18) == "business_checkbox_") {
				var count = 0;
				
				if(document.forms[form].business_checkbox_proposalsTrustFiduciaryAndCorporatedServices.checked) { count++; }
				if(document.forms[form].business_checkbox_proposalsResidenceAndCitizenshipPrograms.checked) { count++; }
				if(document.forms[form].business_checkbox_proposalsAnotherAreaOfHenleyAndPartnersExpertise.checked) { count++; }
				
				if(count == 0) {
					document.getElementById(fieldName).style.backgroundColor = "#F9CB7A";
					return false;
				}
				else {
					document.getElementById(fieldName).style.backgroundColor = "";
					return true;
				}
				
			}
			else {
			
				if(document.forms[form].elements[checkboxName].checked && document.forms[form].elements[fieldName].value == ''){
					document.getElementById(checkboxName).style.backgroundColor = "#F9CB7A";
					return false;
				}
				else
				{
					document.getElementById(checkboxName).style.backgroundColor = "";
					return true;
				}
			}
		}
		else {
			return true;
	}
}


/*
*	Function:		chkAllCheckBoxes
*	Description: 	Runs the function chkCheckBox() for all checkboxes
*					If there is an error (a checkbox is checked but no value is selected) the var CheckBoxTemp will be false
*	
*	@param	form	The form element whe are checking
*
*	@return true/false	If all the checked checkbox's field are not empty
*/
function chkAllCheckBoxes(form)
{
	var CheckBoxTemp = true;
	
	/*****************
	* Länderformular *
	*****************/
	//Yacht/Ship Registration and Marine Services
	if(!chkCheckBox(form, 'checkbox_yachtShipMarine_other', 'yachtShipMarine_other_content')) { CheckBoxTemp = false; }
		
	/*******************
	* General Formular *
	*******************/
	//Residence
	if(!chkCheckBox(form,'checkbox_residence_financiallyIndependentAndInterestedInResidencePermit','residence_financiallyIndependentAndInterestedInResidencePermitIn_country')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_residence_interestedInInvestementBasedImmigrationToCOUNTRYNAMEAsInvestorsEntrepreneurs','residence_interestedInInvestementBasedImmigrationToCOUNTRYNAMEAsInvestorsEntrepreneurs_country')) { CheckBoxTemp = false; }

	//Citizenship
	if(!chkCheckBox(form,'checkbox_citizenship_IAmWeAreOfNATIONALITYDescentAndInterestedInObtainingCitizenshipOnBasisOfAncestry','citizenship_IAmWeAreOfNATIONALITYDescentAndInterestedInObtainingCitizenshipOnBasisOfAncestry_nationality')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_citizenship_citizenshipByInvestment','citizenship_citizenshipByInvestment_country')) { CheckBoxTemp = false; }
	
	//Real Estate
	if(!chkCheckBox(form,'checkbox_realEstate_acquiringRealEstate','realEstate_acquiringRealEstateIn_country')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_realEstate_alreadyOwnRealEstateAndNeedAdvice','realEstate_alreadyOwnRealEstateIn_country_AndNeedAdvice')) { CheckBoxTemp = false; }
	
	//Corporate Services and Business Relocation
	if(!chkCheckBox(form,'checkbox_corporateServicesBusinessRelocation_establishingInternationalTradingReInvoicingHoldingOrAuxiliaryCompany','corporateServicesBusinessRelocation_establishingInternationalTradingReInvoicingHoldingOrAuxiliaryCompanyIn_country') ) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_corporateServicesBusinessRelocation_establishingCompanyWithActiveOperations','corporateServicesBusinessRelocation_establishingCompanyWithActiveOperationsIn_country')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_corporateServicesBusinessRelocation_relocatingProfessionalStaff','corporateServicesBusinessRelocation_relocatingProfessionalStaffTo_country')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_corporateServicesBusinessRelocation_relocatingBusiness','corporateServicesBusinessRelocation_relocatingBusinessTo_country')) { CheckBoxTemp = false; }
	
	//Tax and Estate Planning
	if(!chkCheckBox(form,'checkbox_taxEstate_propertyAndOrBusinessInterestsLocatedInCountryOrCountries','taxEstate_propertyAndOrBusinessInterestsLocatedInCountryOrCountries_content')) { CheckBoxTemp = false; }
	
	//Trust and Fiduciary Services
	if(!chkCheckBox(form,'checkbox_trustFiduciary_other','trustFiduciary_other_content')) { CheckBoxTemp = false; }

	//How did you hear about us?
	if(!chkCheckBox(form,'checkbox_hearAbout_refferedBy','hearAbout_refferedBy_content')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_hearAbout_internetSearch','hearAbout_internetSearch_content')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_hearAbout_ad','hearAbout_ad_content')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_hearAbout_article','hearAbout_article_content')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_hearAbout_other','hearAbout_other_content')) { CheckBoxTemp = false; }

	/********************
	* Business Formular *
	********************/
	if(!chkCheckBox(form,'business_checkbox_proposalsTrustFiduciaryAndCorporatedServices','business_proposals')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'business_checkbox_proposalsResidenceAndCitizenshipPrograms','business_proposals')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'business_checkbox_proposalsAnotherAreaOfHenleyAndPartnersExpertise','business_proposals')) { CheckBoxTemp = false; }
	//hier muss nicht ueberprueft werde ob ein text eingegeben wurde, sonder ob minds. 1e der checkboxen angedreuzt wurde
	//darum wird als parameter fuer die funktion chckCheckBox, die checkbox und als 2ten parameter die id des zu einfaerbenden tr-tag
	//die felder werden business_checkbox_ genannt, damit sie im formmailer nicht wie alle anderen checkboxes ignoeirt werden.
	
	/*****************
	* Media Formular *
	******************/
	if(!chkCheckBox(form,'checkbox_media_writingAboutAnotherAreaOfHenelyAndPartners','media_checkbox_writingAboutAnotherAreaOfHenelyAndPartners_value')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_media_writingAboutConductInterviewWithSeniorPartner','media_checkbox_writingAboutConductInterviewWithSeniorPartner_value')) { CheckBoxTemp = false; }
	if(!chkCheckBox(form,'checkbox_media_deadline','media_checkbox_deadline_value')) { CheckBoxTemp = false; }

	
	/*****************************
	* Humanitarian Fund Formular *
	*****************************/
	if(form == "Humanitarian"){
		CheckBoxTemp = true;
	}
	
	return CheckBoxTemp;
}


/**
*	Special CheckForm-Method to check the Real-Estate Form
*
*	@param	form	The form-Element we are checking
**/
function chkRealEstateForm(form) {
	
	//onreadystatehange
	ajaxRequest.onreadystatechange = function ()
	{
		// trim the email-addresses
		document.getElementById('contactInfo_email_field').value = trim(document.getElementById('contactInfo_email_field').value);
		document.getElementById('contactInfo_emailRetype_field').value = trim(document.getElementById('contactInfo_emailRetype_field').value);
				
		if(ajaxRequest.readyState == 4)
		{
			var returnedEmailData = ajaxRequest.responseText.split(",");
								
			var checkContentInformation = chkContactInformation(form, returnedEmailData);
			
			var checkBoxCheck = true;
			
			//->check the requiered fields
			
				//general_lookingForRealEstateIn
				if(document.getElementById('general_lookingForRealEstateIn').value == '') { 
					document.getElementById('select_general_lookingForRealEstateIn').style.backgroundColor = "#F9CB7A";
					checkBoxCheck = false; 
				}
				else {
					document.getElementById('select_general_lookingForRealEstateIn').style.backgroundColor = "#EEEED9";
				}
				
				//general_priceRange
				if(document.getElementById('general_priceRange').value == '') { 
					document.getElementById('select_general_priceRange').style.backgroundColor = "#F9CB7A";
					checkBoxCheck = false; 
				}
				else {
					document.getElementById('select_general_priceRange').style.backgroundColor = "#EEEED9";
				}
				
				//general_minimumSquareMetersLivingSpace
				if(document.getElementById('general_minimumSquareMetersLivingSpace').value == '') { 
					document.getElementById('select_general_minimumSquareMetersLivingSpace').style.backgroundColor = "#F9CB7A";
					checkBoxCheck = false; 
				}
				else {
					document.getElementById('select_general_minimumSquareMetersLivingSpace').style.backgroundColor = "#EEEED9";
				}
				
				//general_minimumSquareMetersLandAreaOrLotSpace
				if(document.getElementById('general_minimumSquareMetersLandAreaOrLotSpace').value == '') { 
					document.getElementById('select_general_minimumSquareMetersLandAreaOrLotSpace').style.backgroundColor = "#F9CB7A";
					checkBoxCheck = false; 
				}
				else {
					document.getElementById('select_general_minimumSquareMetersLandAreaOrLotSpace').style.backgroundColor = "#EEEED9";
				}
				
				//general_typeOfRealEstate
				if(document.getElementById('general_typeOfRealEstate').value == '') { 
					document.getElementById('select_general_typeOfRealEstate').style.backgroundColor = "#F9CB7A";
					checkBoxCheck = false; 
				}
				else {
					document.getElementById('select_general_typeOfRealEstate').style.backgroundColor = "#EEEED9";
				}
				
			//-> end of check


			if (checkBoxCheck && checkContentInformation)
			{	
				document.getElementById('sec_check').value = 1;
				// document.location.href = "#top";
				document.forms[form].submit();
			}
			else
			{
				document.getElementById('errorMessage').innerHTML = "<strong>Required Information</strong><br /><br />Please note the highlighted areas - kindly complete those sections where information or a selection is required. Your information will help us to provide you with excellent service.";
				document.getElementById('errorMessage').style.visibility = 'visible';
				document.getElementById('errorMessage').style.align = 'left';
				// document.location.href = "#top";
			}
		}
	}

	var email = document.getElementById('contactInfo_email_field').value;
	ajaxRequest.open("POST", "contact/res/ajax/check_email.php?email=" + escape(email), true);
	ajaxRequest.send(null);
}


/**
*	Desc:		Trims a string
*
*	@param	s	String
*
*	@return trimmed String
**/
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}


/**
*	By Arno Grüter (arno.grueter@gmail.com)
*
*	Checks if the email address is valid
*
*	@param	field	The field where the user enters his email address
**/
function validate_email(field) {
	with (field) {
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			return(0);
		}
		else {
			return(1);
		}
	}
}