/* Rabih Raffoul added on January 8th
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
	  // We have a string with leading blank(s)...

	  var j=0, i = s.length;

	  // Iterate from the far left of string until we
	  // don't have any more whitespace...
	  while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		 j++;

	  // Get the substring from the first non-whitespace
	  // character to the end of the string...
	  s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	  // We have a string with trailing blank(s)...

	  var i = s.length - 1;       // Get length of string

	  // Iterate from the far right of string until we
	  // don't have any more whitespace...
	  while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		 i--;


	  // Get the substring from the front of the string to
	  // where the last non-whitespace character is...
	  s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or 
trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}


//This functions validates all entries to ensure entered information is correct	to perform
//the search
function validatePage(lang) 
{ 
	var missingInfo = "";
	
	//Populate the checkBoxList field before submitting the form
	//GetCheckedBoxes();
	
	if (document.search.streetnumber.value == "" && document.search.streetname.value == "" && document.search.streettype.selectedIndex == 0 && document.search.municipality.value == "" && document.search.province.selectedIndex == 0) 
	{
		if (document.search.postalcode.value == "")
		{
			if (lang == "english") 
			{
				alert ("You must enter a Postal Code or an Address to proceed.");
				return false;
			}
			else 
			{
				alert("Vous devez entrer un code postal ou une adresse pour poursuivre.");
				return false;
			}
		}
		if (!validatePostalCode(lang))
		{
			return false;
		}
	}

	//only the postal code OR the address fields must be filled in.
	if (document.search.postalcode.value != "") 
	{
		if (document.search.streetnumber.value != "" || document.search.streettype.selectedIndex != 0 || document.search.streetname.value != "" || document.search.municipality.value != "" || document.search.province.selectedIndex != 0) 
		{
			if (lang == "english") 
			{
				alert("Only the postal code OR the address can be filled in.");
			}
			else 
			{
				alert("Seuls les champs Code postal OU Adresse peuvent être définis.");
			}
			return false;
		}
	}
	//If the postal code OR the address fields are filled in, then validate that data
	
	if (document.search.postalcode.value=="") 
	{
		if (document.search.streetnumber.value=="") 
		{
			if (lang == "english") 
			{
				missingInfo += "\n - Street Number";
			}
			else 
			{
				missingInfo += "\n - No municipal";
			}
		}
		if (document.search.streetname.value=="") 
		{
			if (lang == "english") 
			{
				missingInfo += "\n - Street Name";
			}
			else 
			{
				missingInfo += "\n - Nom de la rue";
			}
		}
		if (document.search.municipality.value=="") 
		{
			if (lang == "english") 
			{
				missingInfo += "\n - Municipality";
			}
			else 
			{
				missingInfo += "\n - Municipalité";
			}
		}
		if (document.search.province.selectedIndex == 0) 
		{
			missingInfo += "\n - Province";
		}
		if (missingInfo != "")
		{
			if (lang == "english")
			{ 
				missingInfo = "The following required address fields are missing:\n" +
				missingInfo + "\n\nPlease re-enter and submit again!";
			}
			else
			{
				missingInfo = "Les champs d'adresses obligatoires suivants manquent:\n" +
				missingInfo + "\n\nVeuillez entrer les données de nouveau et resoumettre la demande!";
			}
			alert(missingInfo);
			return false;
		}
		if (!checkStreetNum(lang))
		{
			return false;
		}
	}

	document.search.postalcode.value = document.search.postalcode.value.toUpperCase();
	document.search.submit();
}

function validateSearchPage(lang) 
{ 
	var missingInfo = "";
	
	//Populate the checkBoxList field before submitting the form
	//GetCheckedBoxes();
	
	if (document.search.streetnumber.value == "" && document.search.streetname.value == "" && document.search.streettype.selectedIndex == 0 && document.search.municipality.value == "" && document.search.province.selectedIndex == 0) 
	{
		if (document.search.postalcode.value == "")
		{
			if (lang == "english") 
			{
				alert ("You must enter a Postal Code or an Address to proceed.");
				return false;
			}
			else 
			{
				alert("Vous devez entrer un code postal ou une adresse pour poursuivre.");
				return false;
			}
		}
		if (!validatePostalCode(lang))
		{
			return false;
		}
	}

	//only the postal code OR the address fields must be filled in.
	if (document.search.postalcode.value != "") 
	{
		if (document.search.streetnumber.value != "" || document.search.streettype.selectedIndex != 0 || document.search.streetname.value != "" || document.search.municipality.value != "" || document.search.province.selectedIndex != 0) 
		{
			if (lang == "english") 
			{
				alert("Only the postal code OR the address can be filled in.");
			}
			else 
			{
				alert("Seuls les champs Code postal OU Adresse peuvent être définis.");
			}
			return false;
		}
	}
	//If the postal code OR the address fields are filled in, then validate that data
	
	if (document.search.postalcode.value=="") 
	{
		if (document.search.streetnumber.value=="") 
		{
			if (lang == "english") 
			{
				missingInfo += "\n - Street Number";
			}
			else 
			{
				missingInfo += "\n - No municipal";
			}
		}
		if (document.search.streetname.value=="") 
		{
			if (lang == "english") 
			{
				missingInfo += "\n - Street Name";
			}
			else 
			{
				missingInfo += "\n - Nom de la rue";
			}
		}
		if (document.search.municipality.value=="") 
		{
			if (lang == "english") 
			{
				missingInfo += "\n - Municipality";
			}
			else 
			{
				missingInfo += "\n - Municipalité";
			}
		}
		if (document.search.province.selectedIndex == 0) 
		{
			missingInfo += "\n - Province";
		}
		if (missingInfo != "")
		{
			if (lang == "english")
			{ 
				missingInfo = "The following required address fields are missing:\n" +
				missingInfo + "\n\nPlease re-enter and submit again!";
			}
			else
			{
				missingInfo = "Les champs d'adresses obligatoires suivants manquent:\n" +
				missingInfo + "\n\nVeuillez entrer les données de nouveau et resoumettre la demande!";
			}
			alert(missingInfo);
			return false;
		}
		if (!checkStreetNum(lang))
		{
			return false;
		}
	}

	document.search.postalcode.value = document.search.postalcode.value.toUpperCase();
	
	return true;
}

//*********************************************************************************

//Validate format of Postal Code and verify that it is not a government postal code
function validatePostalCode(lang)
{
	if (document.search.streetnumber.value == "" && document.search.streetname.value == "" && document.search.streettype.selectedIndex == 0 && document.search.municipality.value == "" && document.search.province.selectedIndex == 0) 
	{
		var digits = "0123456789";
		var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var postalCode = document.search.postalcode.value.toUpperCase();
		var postalCodeObject = document.search.postalcode;
		var fourthChar;
		var firstThreeChar;

		//Rabih Raffoul added on January 8th
		postalCode = Trim(postalCode);
		if (postalCode.length < 6) {
			if (lang == "english") 
			{
				alert("The postal code is invalid.\n  Please use the following format for the postal code: Z9Z 9Z9.");
			}
			else 
			{
				alert("Le code postal est invalide.\n  Veuillez utiliser le format suivant pour entrer le code postal: Z9Z 9Z9.");
			}

			postalCodeObject.value ="" ;
			return false;		
		}

		//Postal code has 7 characters
		if (postalCode.length == 7)
		{
			fourthChar = postalCode.substr(3,1);
			if ((fourthChar != " ") && (fourthChar != "-"))
			{
				if (lang == "english") 
				{
					alert("The postal code is invalid.\n  Please use the following format for the postal code: Z9Z 9Z9.");
				}
				else 
				{
					alert("Le code postal est invalide.\n  Veuillez utiliser le format suivant pour entrer le code postal: Z9Z 9Z9.");
				}

			postalCodeObject.value ="" ;
			return false;
			}
			else
			{
				if (fourthChar == " ")
				{
					postalCode = postalCode.replace(" ", "");			
				}
				else
				{
					postalCode = postalCode.replace("-", "");
				
				}
				postalCodeObject.value = postalCode;
			}
		}
	
		if ((letters.indexOf(postalCode.charAt(0)) < 0) ||
			(letters.indexOf(postalCode.charAt(2)) < 0) ||
			(letters.indexOf(postalCode.charAt(4)) < 0))
		{
			if (lang == "english") 
			{
				alert("The postal code is invalid.\n  Please use the following format for the postal code: Z9Z 9Z9.");
			}
			else 
			{
				alert("Le code postal est invalide.\n  Veuillez utiliser le format suivant pour entrer le code postal: Z9Z 9Z9.");
			}
			postalCodeObject.value ="" ;
			//objPCtext.focus();
			return false;
		}
		if ((digits.indexOf(postalCode.charAt(1)) < 0) ||
			(digits.indexOf(postalCode.charAt(3)) < 0) ||
			(digits.indexOf(postalCode.charAt(5)) < 0))
		{
			if (lang == "english") 
			{
				alert("The postal code is invalid.\n  Please use the following format for the postal code: Z9Z 9Z9.");
			}
			else 
			{
				alert("Le code postal est invalide.\n  Veuillez utiliser le format suivant pour entrer le code postal: Z9Z 9Z9.");
			}
			postalCodeObject.value ="" ;
			return false;
		}
		
		firstThreeChar = postalCode.substr(0,3);

		if ( firstThreeChar == "K1A" ) 
		{
			if (lang == "english") 
			{
				alert("You have selected a Federal Government or Crown Corporation " +
				"postal code which begins with K1A.\n We are unable to provide you with " + 
				"the closest outlet because these postal codes represent several locations.");
			}
			else 
			{
				alert("Vous avez sélectionné un code postal de gouvernement fédéral ou de société" +
				" d'État qui commence par K1A. Nous ne pouvons vous fournir le nom du comptoir postal" +
				" le plus près puisque ces codes postaux représentent plusieurs emplacements.");
			}
			return false;
		}
		
		//valid postal code
		return true; 
	}
}

//*********************************************************************************

//Verify whether the street number has a letter in it.  It can only contain numbers.
function checkStreetNum(lang) 
{      
   var validNum = "0123456789";  
   var i;
    
	for (i = 0; i < document.search.streetnumber.value.length; i++)
	{
		if (document.search.streetnumber.value.substring(i, i+1) == " ")
		{
			document.search.streetnumber.value = "";
			if (lang == "english") 
			{
				alert("You have entered a blank space.\nPlease re-enter your number.");
			}
			else 
			{
				alert("Vous avez entré un espace vide.\nVeuillez entrer le numéro de nouveau.");
			}
   			return false;
		}
	}
			
	for (i = 0; i < document.search.streetnumber.value.length; i++)
	{
		if (validNum.indexOf(document.search.streetnumber.value.substring(i, i+1)) == "-1")
        {
			document.search.streetnumber.value = "";
			if (lang == "english") 
			{
				alert("Please enter only numeric values for Street Number. Try Street Number without a letter.");
			}
			else 
			{
				alert("Vous avez entré une valeur non numérique ou négative.\nVeuillez entrez le numéro de nouveau.");
			}
  			return false;
		}
	}
	return true;
}