/***************************************************************************
 *                           Validators.js
 *                            -------------------
 *  
 *
 *   purpose              : form validation for html pages
 *   dependencies         : javascript must be turned on for the pop ups to
 *                          display.  Other wise, these scripts will be 
 *                          the cgi file as well.
 *
 *                
 *
 ***************************************************************************/

function checkMainForm(form){
 var intIncomplete=0;
 var strAlertMsg = "The following fields are required:\n";
 
 var strFNAME = form.FNAME.value;
 var strLNAME = form.LNAME.value;
 var strAddress = form.ADDR1.value;  
 var strCity = form.CITY.value;  
 var strState = form.STATE[form.STATE.selectedIndex].value;  
 var strZipCode = form.ZIP.value;
 //var strCountry = form.COUNTRY.selectedIndex;
 var strDOBMO = form.DOBMO.value;
 var strDOBDD = form.DOBDD.value;
 var strDOBYR = form.DOBYR.value;
 var strDOB = strDOBMO+"/"+strDOBDD+"/"+strDOBYR;
 var strEmailAddress = form.EMAIL.value;
 var strEmailAddress2 = form.CONFIRMEMAIL.value;
  
 //This regular expression will make sure the date of birth is a valid date.  February 29th will be valid in leap years and not valid in non leap years.  Other false dates like November 31st and February 30th will not be accepted at any time.
 var regexProperDate=/^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/   
var alertMsg = "The following fields are required:\n";

 // First Name check
 if (strFNAME == "")
 {
  intIncomplete++;
	 strAlertMsg = strAlertMsg + "First Name\n";
 }
 
 // Last Name check 
 if (strLNAME == "")
 {
  intIncomplete++;
	 strAlertMsg = strAlertMsg + "Last Name\n";
 }
  
 // Address check
 if (strAddress == "")
 {
  intIncomplete++;
  strAlertMsg = strAlertMsg + "Address 1\n";
 }
   
 // City check
 if (strCity == "")
 {
  intIncomplete++;
  strAlertMsg = strAlertMsg + "City\n";
 }
   
 // State check
 if (strState.length !=2)
 {
	 intIncomplete++;
		strAlertMsg = strAlertMsg +"State\n";
} 
  
 // Zip Code check
 if (strZipCode == "")
 {
	 intIncomplete++;
		strAlertMsg = strAlertMsg +"Zip\n";
 }
 
 // Country check
// if (strCountry == 0)
// {
//		intIncomplete++;
//		strAlertMsg = strAlertMsg +"Country\n";
// }
 
 if (strDOB.search(regexProperDate)==-1)
	  { 
	   intIncomplete++;
	   strAlertMsg = strAlertMsg + "Date of Birth\n";
	  }
 
 // Email Address check
 if(strEmailAddress == "")
 {
		intIncomplete++;
		strAlertMsg = strAlertMsg +"Email\n";
 }
 //CONFIRMEMAIL Email Address2
 if(strEmailAddress2 == "")
 {
		intIncomplete++;
		strAlertMsg = strAlertMsg +"Confirm Email\n";
 }
 
  if (form.EMAIL.value != form.CONFIRMEMAIL.value){
    alert('Both e-mail address entries must match.')
    return false;      
   }
 

 // Date check
 
 if (strDOBMO && strDOBDD && strDOBYR)
 {
  //if DOB match failed 
	  if (strDOB.search(regexProperDate)==-1)
	  { 
	  }
	  else
	  {
		  	  //pass the date value to make sure the age is within the proper age requirement
	   intAgeYear = bwwApproveAge(strDOBYR,strDOBMO,strDOBDD); 
	  	  if(intAgeYear == 0)
	 	  {
	 		alert ("Thanks for your interest in the Pringles Wii Giveaway. Unfortunately, you are not eligible per the Official Rules.");
      		window.location="/pages/index.shtml";
           	return false;  
   	   	  }
	  } 
 }

//Type Email to be sent
//if(form.emailtype[0].checked == false && form.emailtype[1].checked == false)
//{
	//intIncomplete++;
	//strAlertMsg = strAlertMsg + "Email type to be sent \n";
//}

 //If any of the required fields are empty, bring up a message telling
 //the user to fill in those fields
 if (intIncomplete > 0)
 {
		alert(strAlertMsg);
		return false;
	}
 
 //If the age given by the user was too young
 if (intAgeYear != 1)
 {
  alert("You are too young for this!");
  return false;
 }
  
 //Given a first name was entered, check to see if it has any special characters
 validateFirst = bwwValidateFNAME(strFNAME);
   
 //If the first name field contains any numbers or special characters, reject
 //Also, if there is a hyphenated name, check to see that there are letters
 //after the hyphen.
 if (bwwValidateFNAME(strFNAME) == false)
 {
  form.FNAME.focus();
  alert("Please leave out any special characters in your first name\n");
		return false;
 }
   
 //Given a zip code was entered, check to see if its in 5 or 5-4 digit
 //format for US or A5W 4W2 format for Canadian Zip codes.
 boolValidateZip = bwwValidateZipCode(strZipCode);
   
 //If the zip code is not in the required format, throw out this error.
 if (boolValidateZip == false)
 {
  form.ZIP.focus();
  alert("Please enter your Zip Code in one of the following formats:\n55555\n55555-5555\n");
		return false;
 }   
    
 //Given an email, make sure it's in proper email format.
 boolValidateEmail = bwwValidateEmailAddress(strEmailAddress);
   
 //If the email isn't formatted properly, throw out this error:
 if (boolValidateEmail == false)
 {
  form.EMAIL.focus();
  alert("The Email Address you entered is not in the proper format\n");
		return false;
 }
  alert(strPhoneNumber);
 if(!isNum(strPhoneNumber) || !isNum(strPhoneNumber) || !isNum(strPhoneNumber) )	{
	alert("Phone Number must be numeric.");
	return false;
 }
 if ((form.PHONE1.value.length < 3) || (form.PHONE2.value.length < 3) || (form.PHONE3.value.length < 4))
{ 
	alert("Please check your phone number.");
	return false;
}
	return checkCounter();

}   
 
function bwwValidateFNAME(FNAME)
{
 //Regular expression that only allows letters and hyphens for first names
 var regexProperFirst=/^[a-zA-Z]+((\'|\s|\-)[a-zA-Z]+)?$/
      
 if (FNAME.search(regexProperFirst)==-1)
 {  
  return false;
 }
  return true;
}
 
function bwwValidateZipCode(zipCode)
{
 var regexProperZip=/^\d{5}-\d{4}$|^\d{5}$|[A-Z]\d[A-Z] \d[A-Z]\d$/
      
 if (zipCode.search(regexProperZip)==-1)
 {  
  return false;
 }
 return true;
}
 
function bwwValidateEmailAddress(emailAddress)
{
 var regexProperEmail=/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/
      
 if (emailAddress.search(regexProperEmail)==-1)
 {  
  return false;
 }
 return true;
}
 

//This function will takes a validated date and makes sure it's over a certain age.  Eventually,
//I want to set something up where the user can check for an age above, below, and in bewteen
//a given age set.
function bwwApproveAge(strBirthYear,strBirthMonth,strBirthDay)
{
 //This code will get the current server date.  It will use its month, day, and year to compare to 
 //the user has entered.
 datNow = new Date();
 datNowYear = datNow.getFullYear();
 datNowMonth = datNow.getMonth() + 1;
 datNowDay = datNow.getDate();

 //Adjust this variable for the age of allowance.
 var intPassAge = 13;
 
 //By default, intPassed is set to zero, meaning the test will fail.
 //If the test passes, this variable will be changed to 1.
 var intPassed = 0;
 var diff=datNowYear - strBirthYear;
 if (strBirthDay >= 1 && strBirthDay <= 31 && strBirthMonth >= 1 && strBirthMonth <= 12 && strBirthYear >= 1899 && strBirthYear <= datNow.getFullYear())
 {
  //This section checks for a month and day if the year is the same as the allowed year.
  if ((datNowYear - strBirthYear) == intPassAge)
  {
	   if (strBirthMonth < datNowMonth)
		{
		  intPassed = 1;
		} 
	   else if (strBirthMonth == datNowMonth && strBirthDay <= datNowDay)
		{
		  intPassed = 1;
		}
		else
	   {
		intPassed = 0;
	   }
  }
  else if (datNowYear - strBirthYear > intPassAge)
  {
		 intPassed = 1;
  } 
  else 
  {
   intPassed = 0;
  }
 }
 else
 {
  intPassed = 0;
 }


 return intPassed;
}


var iSubmitCounter=0;
function checkCounter(){

	iSubmitCounter++;

	if (iSubmitCounter==1) {

		return true;

	} else {

		alert('Please Wait.'); 

		return false;

	}

}
function isNum(p)
{

for(i=0;i<p.length;i++)
{
a=parseInt(p.charAt(i));
if(a>=0 || a<=9)
return true;
}
return false;
}