// JavaScript Document

var lang="";     //variable for holding the selected language

//var message_text_exceed = 'Maximum 300 characters allowed in message box';
//This function is used for assign invalid email error-  depends on language selection
function selectedLanguageEmailError(){
       var EmailError;
       if(lang=="cn"){
	EmailError='\u65e0\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740';
       }
       else if(lang=="de"){
	EmailError='Ung&uuml;ltige E-Mail Adresse';
      	}
       else if(lang=="en"){
	EmailError='Invalid email address';
       }
       else if(lang=="es"){
	EmailError='Correo electr&oacute;nico err&oacute;neo';
       }
       else if(lang=="fr"){
	EmailError='Adresse de courriel erron&eacute;e';
       }
       else if(lang=="hk"){
	EmailError='\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u7121\u6548';
       }
       else if(lang=="ja"){
	EmailError='E-mail ID\u304c\u7121\u52b9';
       }
       else if(lang=="ko"){
	EmailError='\ubd88\uba85\ud655\ud55c \uc774\uba54\uc77c \uc8fc\uc18c';
       }
       else{
	EmailError='Endere&ccedil;o de e-mail inv&aacute;lido';
       }
       return EmailError;
  
}
//This function is used for assign mandatory field error- depends on language selection
function selectedLanguageMandatoryError(){

       var MandatoryError;
       if(lang=="cn"){
	MandatoryError= '\u8bf7\u5b8c\u6210\u6240\u6709\u5fc5\u586b\u9009\u9879';
       }
       else if(lang=="de"){
	MandatoryError= 'Bitte f&uuml;llen Sie alle Pflichtfelder aus';
       }
       else if(lang=="en"){
	MandatoryError= 'Please enter all mandatory fields';
     	}
       else if(lang=="es"){
	MandatoryError= 'Rellene todos los campos obligatorios';
      	}
       else if(lang=="fr"){
	MandatoryError= 'Veuillez remplir tous les champs obligatoires';
       }
       else if(lang=="hk"){
	MandatoryError= '\u8acb\u586b\u5beb\u6240\u6709\u5fc5\u586b\u6b04\u4f4d';
       }
       else if(lang=="ja"){
	MandatoryError= '\u5fc5\u9808\u6b04\u3092\u3059\u3079\u3066\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044';
       return MandatoryError;
	}
       else if(lang=="ko"){
	MandatoryError= '\ubaa8\ub4e0 \ud544\uc218\ud56d\ubaa9\uc744 \uae30\uc7ac\ud558\uc2ed\uc2dc\uc624.';
       }
       else{
	MandatoryError= 'Introduza por favor todos os campos obrigat&oacute;rios';
      	}
       return MandatoryError;

}
function displayErrorMessage(Message) {
  var MessageToDisplay;
  switch (Message) {
    case 'EMAIL_INVALID_MESSAGE':
      MessageToDisplay =selectedLanguageEmailError();  //This will execute function called selectedLanguageEmailError() and assign return value to MessageToDisplay
      break;
    case 'MAND_FIELDS_MESSAGE':
      MessageToDisplay = selectedLanguageMandatoryError(); //This will execute function called selectedLanguageMandatoryError() and assign return value to MessageToDisplay

      break;
	case 'MESSAGES_SIZE_LIMIT':
	MessageToDisplay = message_text_exceed;
	break;
  }
  document.getElementById('error').innerHTML = MessageToDisplay;
  document.getElementById('error').className = 'error2';
} 
function echeck(str) {
   var reg_email = /^([A-Za-z0-9_\+\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  
   if(reg_email.test(str) == false) 
   {
     displayErrorMessage('EMAIL_INVALID_MESSAGE');
      return false;
   }		
   return true;					
}
function ValidateSubjectSelection()
{
   if (document.getElementById('dropdownSubj').selectedIndex == 0)
              {
			   displayErrorMessage('MAND_FIELDS_MESSAGE');
			   return false;

            }
	return true;
}

function ValidateMessageText(){ 
if (document.getElementById('textMessage').value.length >300)
	{       
			displayErrorMessage('MESSAGES_SIZE_LIMIT');
			return false;
	}
	return true;
}
function ValidateContactForm(value)
	{
	lang=value;
	//assign language to the variale called "lang"
       var emailID=document.getElementById('email');
			
                 if((document.getElementById('email').value == '') || (document.getElementById('textMessage').value == ''))
	          {
 		    	displayErrorMessage('MAND_FIELDS_MESSAGE');
		    	emailID.focus();
		    	return false;
	 		}
								
			if (!ValidateSubjectSelection())
			   {
				   return false;
			   }
			if (!ValidateMessageText())
			   {
				   return false;
			   }
			document.getElementById('email').getAttribute('value');
			if (echeck(document.getElementById('email').value)==false)
	   	    {
					emailID.value="";
					emailID.focus();
					return false;
			}
		 return true;  
	}


function textCounter(field, countfield, maxlimit) {

if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}


/*
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		//limitField.value = limitField.value.substring(0, limitNum);
		displayErrorMessage('MESSAGES_SIZE_LIMIT');
		
	} else {
		displayErrorMessage('CLEAR_ERROR_MESSAGE');
document.getElementById('error').className = 'error1'; 
		 limitCount.value = limitNum - limitField.value.length;
		 
		//document.getElementById('error').className ='';
		//var emptymessg='';
		//document.getElementById('error').innerHTML = emptymessg;
		
	}
	
}*/

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function textCounter(field, countfield, maxlimit) {
countfield=0;
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

//Pre populate Book and fly subject line for Book and fly tool
function getBookingParam(bookandfly)
{
  var subjectSelector = document.getElementById("dropdownSubj");
  bookandfly = bookandfly.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+bookandfly+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
  	return "";
  else   {
  	subjectSelector.options[2].selected=true;
       document.getElementById("dropdownSubject").innerHTML="Book and Fly";
  }
}
