//Global variables for the zip code, postal code, and area code regular expression 
   var zipReg 		    = /\d{5}/;								  //Zip Code
   var postalReg 	    = /[A-Z][0-9][A-Z]\s[0-9][A-Z][0-9]/;     //canadian postal code
   var areaCodeReg 		= /\d{3}/;  						      //area code  
   
   
/**
* This function validates the user entered a valid value for the zip field.
* validatezipForm(formName)
* @param formName An unique form name
* @return flag A true/false condition.
*/	 
   function validateZipForm(formName) 
   {      
          formName.productLine.value = document.dealerForm.productLine.options[document.dealerForm.productLine.selectedIndex].value; 
       
          if(formName.zip.value == "")
	      {	     
			     alert("Please enter a 5 digit zip code or a canadian postal code.");			
				 formName.zip.focus();				 
				 return false;
	      }
		  if ((formName.zip.value).indexOf("-") != -1)
	      {
	         formName.zip.value = (formName.zip.value).substring(0, 5);
	      }
		  if ((formName.zip.value).length == 6)
	      {
	         formName.zip.value = (formName.zip.value).substring(0, 3) + " " + (formName.zip.value).substring(3,6);
	      }	
		  if (("" + formName.zip.value).length == 7)
		  {
		     formName.zip.value = (formName.zip.value).toUpperCase();
		  }
		  if((formName.zip.value).length != 5 && (formName.zip.value).length != 7)
		  {
		       alert("Invalid zip code or postal code.");			   	 
		       formName.zip.focus();
		       return false;		  
		  }
		  if ((formName.zip.value).length == 5 && (formName.zip.value).match(zipReg) == null )
		  {
		       alert("Invalid zip code or postal code.");			   	 
		       formName.zip.focus();
		       return false;
	      }
		  else if ((formName.zip.value).length == 7 && (formName.zip.value).match(postalReg) == null)
		  {
		        alert("Invalid zip code or postal code.");			   	 
		        formName.zip.focus();
		        return false;
		  }		 
		  if (!hasValidProductLine(formName.productLine.value))
		  {		      
		      return false;			   
		  }
		  return true;
	}
	  
   /**
	* This function validates the user entered a valid value for the areaCode field.
	* validatezipForm(formName)
	* @param formName An unique form name
	* @return flag A true/false condition.
	*/	 
	function validateAreaCodeForm(formName) 
    { 
	     formName.productLine.value = document.dealerForm.productLine.options[document.dealerForm.productLine.selectedIndex].value; 
       
	     if(formName.areaCode.value == "")
	     {
		    alert("Please enter a 3 digit area code.");			
		 	formName.areaCode.focus();
			return false;
	     }
		 if ((formName.areaCode.value).match(areaCodeReg) == null)
		 {
		     alert("Invalid area code.");			 
			 formName.areaCode.focus();
		     return false;
	     }
		 if (!hasValidProductLine(formName.productLine.value))
		 {		      
		      return false;
		 }
	     return true;
	} 
	
   /**
	* This function validates the user entered a valid value for the city and state fields.
	* validatezipForm(formName)
	* @param formName An unique form name
	* @return flag A true/false condition.
	*/	 
	function validateCityStateForm(formName) 
    { 
	     formName.productLine.value = document.dealerForm.productLine.options[document.dealerForm.productLine.selectedIndex].value; 
       
		 if(formName.city.value == "")
	     {
		    alert("Please enter a city.");			
			formName.city.focus();
			return false;
	     }	
		 else if(isSpaces(formName.city.value))
		 {
		       alert ("Invalid city name.");			   
		       formName.city.focus();
		       return false;	
		 }		      		 
	     if (formName.state.selectedIndex == 0)
		 {
		     alert("Please select a state or province.");			 
			 formName.state.focus();
			 return false;		 
		 }
		 if (!hasValidProductLine(formName.productLine.value))
		 {		      
		      return false;
		 }			
	     return true;
   } 
   /**
	* This function validates the user entered a valid value for the state fields.
	* validatezipForm(formName)
	* @param formName An unique form name
	* @return flag A true/false condition.
	*/	 
   function validateStateForm(formName)
   {
        formName.productLine.value = document.dealerForm.productLine.options[document.dealerForm.productLine.selectedIndex].value; 
       
        if (formName.state.selectedIndex == 0)
		 {
		     alert("Please select a state or province.");			 
			 formName.state.focus();
			 return false;		 
		 }	
		 if (!hasValidProductLine(formName.productLine.value))
		 {		      
		      return false;
		 }		
	     return true;   
   }
   /**
	* This function validates the user entered a valid value for the dealer name fields.
	* validatezipForm(formName)
	* @param formName An unique form name
	* @return flag A true/false condition.
	*/	 
   function validateDealerNameForm(formName)
   {
        formName.productLine.value = document.dealerForm.productLine.options[document.dealerForm.productLine.selectedIndex].value; 
        if (formName.dealerName.value == "")
		{
		     alert("Please enter a dealer name.");			 
			 formName.dealerName.focus();
			 return false;		 
		}	
		else if(isSpaces(formName.dealerName.value))
		{
		       alert ("Invalid dealer name.");			   
		       formName.dealerName.focus();
		       return false;	
		}	
		if (!hasValidProductLine(formName.productLine.value))
		{		      
		      return false;
		}
	    return true;   
   }
   
       
/**
* This function is used to validate if a field value are all spaces .
* isSpaces(fieldValue)
* @param fieldValue An unique field value
* @return true/false.
*/
function isSpaces(fieldValue)
{
    for (i = 0; i<fieldValue.length; i++)
	{
	   if (fieldValue.substring(i, i+1) != ' ')
	   {
	       return false;
	   }
	}  
	return true;
} 

  
/**
* This function is used to validate if a a valid product line value is entered.
* hasValidProductLine(fieldValue)
* @param fieldValue An unique field value
* @return true/false.
*/
function hasValidProductLine(fieldValue)
{
    if (fieldValue == "0")
	{
	     alert("Please select a product line.");		
		 return false;		 
	}		
	return true;
} 



/**
* This function is used to set the productline values for all the other forms.
* setProductLine(formName)
* @param formName An unique form name
*/
function setProductLine(formName)
{
    var productLineValue = formName.productLine.options[formName.productLine.selectedIndex].value; 
	
	//set the other form's productline data
	document.zipSearch.productLine.value = productLineValue;	
	document.cityStateSearch.productLine.value = productLineValue;
	document.stateSearch.productLine.value = productLineValue;
	document.areaCodeSearch.productLine.value = productLineValue;
    document.dealerNameSearch.productLine.value = productLineValue;
} 



/**
* This function is used to set the serviceType field then submit the form.
* submitForm()
*/
function submitForm(aValue)
{
    if(aValue=="0")
	{ 
	    document.dealerForm.serviceType.value = "SALES";
	}else{	
	    document.dealerForm.serviceType.value = "SERVICE";
	}		
	document.dealerForm.submit();
	
} 

