// JavaScript Document

//used to verify number is entered on zipcode search
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function isMobile()
{
	return true;
}

function isTelephone(aTeleField)
{
	var stripped = aTeleField.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped)))
	{
		return false;
	}
	else if (!(stripped.length == 10)) 
	{
		return false;
	}
	else
	{ 
		return true;
	}
}

//verify field has not been left empty
function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null))
   {
      return true;
   }
   else
   { 
      return false;
   }
}	

//verify email address has basic characteristics
function isValidEmail(str)
{
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function isCorrectLength(str)
{
   return (str.length == 5);	
}

function clearTxt()
{
      form.zipcode.value = '';
}

//validation function called from form
function ValidateForm(form)
{

   if (IsEmpty(form.zipcode)) 
   { 
      alert('You must enter a zipcode to continue'); 
      form.zipcode.focus();
	  return false; 
   } 
   
   if (!isCorrectLength(form.zipcode.value))
   {
	   alert('Please enter a numeric 5-digit zip code');
	   form.zipcode.focus();
	   return false;
   }
 
   if (!IsNumeric(form.zipcode.value)) 
   { 
      alert('Please enter only numbers in the zipcode field'); 
      form.zipcode.focus();
      return false; 
   } 
 
return true;
 
} 

function ValidateStepOne(form)
{
   if (IsEmpty(form.name))
   { 
      alert('All fields are required'); 
      form.name.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.email))
   { 
      alert('All fields are required'); 
      form.email.focus();
	  return false; 
   } 

   if (!isValidEmail(form.email.value))
   { 
      alert('Please double check your email and try again'); 
      form.email.focus();
	  return false; 
   }  
   
return true;
   
}

function ValidateStepTwo(form)
{

   if (IsEmpty(form.address))
   { 
      alert('A delivery address is required'); 
      form.address.focus();
	  return false; 
   } 

   if (IsEmpty(form.telephone) && IsEmpty(form.mobile))
   { 
      alert('A delivery telephone is required');
      form.telephone.focus();
	  return false; 
   }
 
 
 if (IsEmpty(form.telephone))
 {
   if (!isTelephone(form.mobile.value))
   {
	  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
	  form.mobile.focus();
	  return false;
   }	 
 }
 else if (IsEmpty(form.mobile))
 {
   if (!isTelephone(form.telephone.value))
   {
	  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
	  form.telephone.focus();
	  return false;
   }	 
 }
 else if (!(IsEmpty(form.telephone) && IsEmpty(form.mobile)))
 {
   if (!isTelephone(form.telephone.value))
   {
	  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
	  form.telephone.focus();
	  return false;
   }		   

   if (!isTelephone(form.mobile.value))
   {
	  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
	  form.mobile.focus();
	  return false;
   }		 
 }

return true;
   
}

function ValidateZipResults(form)
{
   if (IsEmpty(form.name))
   { 
      alert('All fields are required'); 
      form.name.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.email))
   { 
      alert('All fields are required'); 
      form.email.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.telephone))
   { 
      alert('A delivery telephone is required');
      form.telephone.focus();
	  return false; 
   }     

   if (!isValidEmail(form.email.value))
   { 
      alert('Please double check your email and try again'); 
      form.email.focus();
	  return false; 
   }  
   
   if (!isTelephone(form.telephone.value))
   {
	  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
	  form.telephone.focus();
	  return false;
   }		      
   
return true;
   
}

function ValidateLearnNoDel(form)
{
   if (IsEmpty(form.telephone))
   { 
      alert('A delivery telephone is required');
      form.telephone.focus();
	  return false; 
   }  
   
   if (!isTelephone(form.telephone.value))
   {
	  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
	  form.telephone.focus();
	  return false;
   }		      
   
return true;   
      
}

function ValidateLearnStepOne(form)
{
   if (IsEmpty(form.name))
   { 
      alert('All fields are required'); 
      form.name.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.email))
   { 
      alert('All fields are required'); 
      form.email.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.zipcode)) 
   { 
      alert('You must enter a zipcode to continue'); 
      form.zipcode.focus();
	  return false; 
   }    

   if (!isValidEmail(form.email.value))
   { 
      alert('Please double check your email and try again'); 
      form.email.focus();
	  return false; 
   }  
   
   if (!isCorrectLength(form.zipcode.value))
   {
	   alert('Please enter a numeric 5-digit zip code');
	   form.zipcode.focus();
	   return false;
   }
 
   if (!IsNumeric(form.zipcode.value)) 
   { 
      alert('Please enter only numbers in the zipcode field'); 
      form.zipcode.focus();
      return false; 
   } 
 
return true;
   
}

function ValidateSpree(form)
{
   if (IsEmpty(form.name))
   { 
      alert('All fields are required'); 
      form.name.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.email))
   { 
      alert('All fields are required'); 
      form.email.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.address))
   { 
      alert('All fields are required'); 
      form.email.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.zipcode))
   { 
      alert('All fields are required'); 
      form.email.focus();
	  return false; 
   } 
   
   ValidateForm(form);

   if (!isValidEmail(form.email.value))
   { 
      alert('Please double check your email and try again'); 
      form.email.focus();
	  return false; 
   }       

	 if (IsEmpty(form.telephone))
	 {
	   if (!isTelephone(form.mobile.value))
	   {
		  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
		  form.mobile.focus();
		  return false;
	   }	 
	 }
	 else if (IsEmpty(form.mobile))
	 {
	   if (!isTelephone(form.telephone.value))
	   {
		  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
		  form.telephone.focus();
		  return false;
	   }	 
	 }
	 else if (!(IsEmpty(form.telephone) && IsEmpty(form.mobile)))
	 {
	   if (!isTelephone(form.telephone.value))
	   {
		  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
		  form.telephone.focus();
		  return false;
	   }		   
	
	   if (!isTelephone(form.mobile.value))
	   {
		  alert('The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.');
		  form.mobile.focus();
		  return false;
	   }		 
 }
return true;
   
}

function ValidateContact(form)
{

   if (IsEmpty(form.name))
   { 
      alert('All fields are required') ;
      form.name.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.email))
   { 
      alert('All fields are required');
      form.email.focus();
	  return false; 
   } 
   
   if (IsEmpty(form.comment))
   { 
      alert('All fields are required'); 
      form.comment.focus();
	  return false; 
   }    
   
   if (!isValidEmail(form.email.value))
   { 
      alert('Please double check your email and try again');
      form.email.focus();
	  return false; 
   }       
 
return true;
 
} 