/****************************************************************************/
/* Created by: Lola saadi x 14143                                           */
/* Created on: January 29, 2009                                             */
/* Reason: Validates that a string a Canadian postal code in 6 character 
  		   format. Z5Z 5Z5 or Z5Z5Z5 It can also check if the postal code 
  		   is an Ottawa Postal Code or Not                                  */
/****************************************************************************/
function Postalvalidate( strValue, lang, local) 
{
	var error="";
	var objRegExp  =  /^\D{1}\d{1}\D{1}\s?\d{1}\D{1}\d{1}$/ ;
	var santaClausPC = /H0H\s?0H0/;
	// Not valid Postal Code Letters
	var invalidLetter = /[DFIOQU]/;
	var ottawaStation = /^[K]/;
	strValue = trimAll(strValue);
	local = local.toUpperCase();  // in case of lowercase
	strValue = strValue.toUpperCase();  // in case of lowercase
	
	
	//check for valid Canadian Postal Code	
	if (!validateNotEmpty(strValue))
	{
		if (lang == "fr")
		{
			error += "Veuillez entrer votre code postal.\n";
		}
		else
		{
			error += "Please enter your postal code.\n";
		}
	}		
	else
	{	
		if (!objRegExp.test(strValue) || santaClausPC.test(strValue) || invalidLetter.test(strValue))
		{  
			if (lang == "fr")
			{
				error += "Veuillez entrer un code postal valide.\n";
			}
			else
			{
				error += "Please enter a valid postal code.\n";
			}	
		}
		else
		{
			if (local == "y")
			{
				if (! ottawaStation.test(strValue))
				{
					if (lang == "fr")
					{
						error += "Votre indicatif régional doit se trouver dans la région d’Ottawa.\n" + "<p>S’il vous plait essayer de nouveau.  Si vous continuez à avoir de la difficulté avec votre code postal, communiquez avec nous <a href='arbre@ottawa.ca'>arbre@ottawa.ca</a>.</p>";
					}
					else
					{
						error += "Postal code must be from the Ottawa Area.\n" + "<p>Please try again.  If you continue having difficulties entering your postal code, please contact <a href='mailto:tree@ottawa.ca'>tree@ottawa.ca</a>.</p>";
					}	
				}	
			}
		}
	}	
	return error;
}



/****************************************************************************/
/* Created by: Lola saadi x 14143                                           */
/* Created on: January 29, 2009                                             */
/* Reason: Validates the tree form fields                                   */
/****************************************************************************/
function validateForm(theForm)
{
	var why = "";
	var radioValue ="";
	var lang="";
	var returnValue="";	
	var returnPostal="";
	var firstnameError="";
	var lastname;
	var streetnumberInvalid;
	var streetnumberSize;
	var streetname;
	var treesize;
	var contact;
	var phonenumber;
	var choiceEmpty;
	var sameChoice;
	var postalError;
	var postalValue;
	
	if (document.getElementById('lang'))
	{
		if (document.getElementById('lang').value =="fr")
		{
			lang = "fr";
			firstnameEmpty="<p>Veuillez entrer votre prénom.</p>";
			firstnameSize="<p>Le prénom doit comprendre au moins deux (2) caractères.</p>";
			firstnameInvalid="<p>Veuillez entrer un prénom valide.</p>";
			lastnameEmpty="<p>Veuillez entrer votre nom de famille.</p>";
			lastnameSize="<p>Le nom de famille doit comprendre au moins deux (2) caractères.</p>";
			lastnameInvalid="<p>Veuillez entrer un nom de famille valide.</p>";
			streetnumberInvalid="<p>Veuillez entrer un numéro municipal valide.</p>";
			streetnumberEmpty="<p>Veuillez entrer votre numéro municipal.</p>";
			streetnameEmpty="<p>Veuillez entrer le nom de votre rue.</p>";
			phonenumberEmpty="<p>Veuillez entrer votre numéro de téléphone à la maison.</p>";
			phonenumberInvalid="<p>Veuillez entrer un numéro de téléphone à la maison valide.</p>";
			treesize="<p>Veuillez sélectionner la taille de l’arbre.</p>";
			choiceEmpty="<p>S.V.P. sélectionner trois espèces différentes.</p>";
			sameChoice="<p>Vous avez sélectionné la même espèce plus qu'une fois.  S.V.P. sélectionner trois espèces différentes.</p>";
		}
		else
		{				
			lang = "en";
			firstnameEmpty="<p>Please enter your given name.</p>";
			firstnameSize="<p>The given name must be at least 2 characters.</p>";			
			firstnameInvalid="<p>Please enter a valid given name.</p>";
			lastnameEmpty="<p>Please enter your surname.</p>";
			lastnameInvalid="<p>Please enter a valid surname.</p>";
			lastnameSize="<p>The Surname must be at least 2 characters.</p>";			
			streetnumberInvalid="<p>Please enter a valid street number.</p>";
			streetnumberEmpty="<p>Please enter your street number.</p>";			
			streetnameEmpty="<p>Please select your street name.</p>";
			phonenumberEmpty="<p>Please enter your home phone number.</p>";			
			phonenumberInvalid="<p>Please enter a valid home phone number.</p>";
			treesize="<p>Please select size of tree.</p>";
			choiceEmpty="<p>Please select three different species.";
			sameChoice="<p>You have selected the same species more than once.  Please select three different species.</p>";
		}

		// Validate Input boxes
		why += checkInputBox(document.getElementById('firstname').value,firstnameEmpty,firstnameInvalid,firstnameSize,"2",lang);
		why += checkInputBox(document.getElementById('lastname').value,lastnameEmpty,lastnameInvalid,lastnameSize,"2",lang);
		why += checkForNumAlpha(document.getElementById('streetnumber').value,streetnumberEmpty,streetnumberInvalid, "","", "b");
		
		if (document.getElementById('streetname'))
		{
			why += checkInputBox(document.getElementById('streetname').value,streetnameEmpty,"","","0",lang);
		}	

		// Validate Postal Code
		returnPostal = Postalvalidate(document.getElementById('postal').value,lang,"y");
		if (returnPostal)
		{
			why += returnPostal;
		}
		else
		{
			postalValue = document.getElementById('postal').value;
			postalValue = postalValue.toUpperCase();
				// if no space is added to the postal code add one
				if (postalValue.length == 6) {
					postalValue= postalValue.substr(0,3)+" "+postalValue.substr(3,3);
					document.getElementById('postal').value=postalValue;
				}			
		}
		

		// Validate Home Phone Number
		returnValue = validatePhone(document.getElementById('phone_home_areacode').value, document.getElementById('phone_home_prefix').value,document.getElementById('phone_home_extension').value,"",lang,phonenumberEmpty,phonenumberInvalid,"y");
		if (returnValue)
		{
			why += returnValue;
		}
		else
		{
			// join the phone number together and put into the hidden field named home_phone
			if (document.getElementById('phone_home_areacode').value)
			{
				document.getElementById('home_phone').value = document.getElementById('phone_home_areacode').value + "-" + document.getElementById('phone_home_prefix').value + "-" + document.getElementById('phone_home_extension').value;
			}
		}
		
		// join the phone number together and put into the hidden field named work_phone
		if (document.getElementById('phone_work_areacode').value !== '')
		{
			document.getElementById('work_phone').value = document.getElementById('phone_work_areacode').value + "-" + document.getElementById('phone_work_prefix').value + "-" + document.getElementById('phone_work_extension').value;

			// Append work extension if given
			if (document.getElementById('phone_work_bus_extension').value !=='')
			{
				document.getElementById('work_phone').value += " Ext. " + document.getElementById('phone_work_bus_extension').value;
			}
		}		
				
		// Validate Radio Button for the pickup form
		//if (document.getElementById('frmPick'))
		//{			
			//why += checkAllRadio(theForm,"r_size", treesize, lang);
		//}	
							
		// Pops up Error Box if any errors occur
		if (why != "") 
		{
			document.getElementById('error').style.display = "block"; 
		    document.getElementById('error').innerHTML= "<br /> " + why + "<br /> ";
			return false;
		}
		// If no choices are selected
		if (document.getElementById('choice1').value == 'select' || document.getElementById('choice2').value == 'select' ||document.getElementById('choice3').value == 'select')
		{
			document.getElementById('error').style.display = "block"; 
		    document.getElementById('error').innerHTML= "<br /> " + choiceEmpty + "<br /> ";		  
			return false;
		}
		
		// If 1st, 2nd, and 3rd choices are the same
		if (document.getElementById('choice1').value == document.getElementById('choice2').value || document.getElementById('choice1').value == document.getElementById('choice3').value || document.getElementById('choice2').value == document.getElementById('choice3').value)
		{
			document.getElementById('error').style.display = "block"; 
		    document.getElementById('error').innerHTML= "<br /> " + sameChoice + "<br /> ";
			return false;
		}
		// Disable the Submit and Reset Button
		//disableButton(theForm);
		return true;
	} //end if
	else
	{
		alert ("Language Field Not Set");
		return false;
	}
}

/****************************************************************************/
/* Created by: Lola saadi x 14143                                           */
/* Created on: January 29, 2009                                             */
/* Reason: This function will return an error if the delivery method option */
/*		   is not selected.                                                 */
/****************************************************************************/
function validateOption(form)
{ 
	  var checked = false; 
	  var opt = document.forms["frmTree"].elements.option;
	  var selectOption;
	  var lang="";
	  
	  if (document.getElementById('lang'))
	{
		if (document.getElementById('lang').value =="fr")
		{
			lang = "fr";			
			selectOption =  "Sélectionnez entre " + "<b>" +"livrer mon arbre " + "</b>" + " ou " + "<b>" +"ramasser mon arbre." + "</b>";
		}//if
		else
		{				
			lang = "en";			
			selectOption = "Select between " + "<b>" +"deliver my tree " + "</b>" + " or " + "<b>" +"pick up my tree." + "</b>";
		}//else
	  	for (var i=0; i<opt.length; i++){  
		    if (opt[i].checked) {  
		      checked = true; 
		      break; 
	     	}	//if	    
	   	}// for
	   	if(!checked){
		    document.getElementById('error').style.display = "block";
			document.getElementById('error').innerHTML= "<br /> " + selectOption +"<br />";
			return false;
		} //if		
	   	else if(opt[i].value == "1"){		   
	   		validateConfirm();	   		  		
	   		document.forms["frmTree"].elements.confim.focus();
   		} //else if
   		else if(opt[i].value == "2"){
	   		
	   		if (document.getElementById('lang').value =="fr")
			{
				lang = "fr";			
				window.open('pickup_fr.html','_self','');
			}//if
			else
			{				
				lang = "en";			
				window.open('pickup_en.html','_self','');
			}  //else
	   		
   		}  //else if
	 }// if
} // function

/****************************************************************************/
/* Created by: Lola saadi x 14143                                           */
/* Created on: January 28, 2009                                             */
/* Reason: This function will show the confirmation box for user to confirm */
/*		   the delivery once delivery option is selected.                   */
/****************************************************************************/
function checkConfiorm(el){	
		document.getElementById('confim').style.display = "none";
		
		if (el.value == "1") {				
			document.getElementById('confim').style.display = "block";
			}					
		else {
			document.getElementById('confim').style.display = "none";
			}
}

/****************************************************************************/
/* Created by: Lola saadi x 14143                                           */
/* Created on: January 28, 2009                                             */
/* Reason: This function will return an error if user did not check select  */
/*		   a confirm option.                                                */
/****************************************************************************/
function validateConfirm(){				
		document.getElementById('error').style.display = "none";		
		var checked = false;		
		var option = document.forms["frmTree"].elements.confim;
		var confirmDelivery;
		var lang="";
	
	if (document.getElementById('lang'))
	{
		if (document.getElementById('lang').value =="fr")
		{
			lang = "fr";			
			confirmDelivery = "Sélectionnez " + "<b>" +"oui " + "</b>" + " ou " + "<b>" +"non" + "</b>";
		}
		else
		{				
			lang = "en";			
			confirmDelivery = "Select " + "<b>" +"Yes " + "</b>" + " or " + "<b>" +"No" + "</b>";
		}
		for (var i=0; i<option.length; i++){  
			if (option[i].checked) {  
			  checked = true;
			  break;  
			}	     
		}
		if(!checked){
			document.getElementById('error').style.display = "block"; 
		    document.getElementById('error').innerHTML= "<br /> " + confirmDelivery + "<br />";		    
		}
		else if(option[i].value == "yes") {
			
			if (document.getElementById('lang').value =="fr")
			{
				lang = "fr";			
				window.open('delivery_fr.html','_self','');
			}
			else
			{				
				lang = "en";			
				window.open('delivery_en.html','_self','');
			} 
			
		}
		else if(option[i].value == "no"){
			if (document.getElementById('lang').value =="fr")
			{
				lang = "fr";			
				window.open('pickup_fr.html','_self','');
			}
			else
			{				
				lang = "en";			
				window.open('pickup_en.html','_self','');
			}
		}
	}
}

/****************************************************************************/
/* Created by: Lola saadi x 14143                                           */
/* Created on: January 28, 2009                                             */
/* Reason: This function will disable the selected option if the option     */
/*		   is set to be disabled.                                           */
/****************************************************************************/
function resetDisabled(el) {
			if (el.options[el.selectedIndex].disabled) {
				el.options.value = 'select';
			}
}
