/***************************************************************************
 *                           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 strEmailAddress = form.EMAIL.value;
	var strEmailAddress2 = form.CONFIRMEMAIL.value;
	var strGender = form.GENDER.options[form.GENDER.selectedIndex].value;
//	var strAge = form.AGE.options[form.AGE.selectedIndex].value;
	var strMaritalStatus = form.MARITAL_STATUS.options[form.MARITAL_STATUS.selectedIndex].value;
	var strChildren = form.CHILDREN.options[form.CHILDREN.selectedIndex].value;
	var strChildrenAge = new Array();;
	strChildrenAge[1] = form.CHILD_AGE_1;
	strChildrenAge[2] = form.CHILD_AGE_2;
	strChildrenAge[3] = form.CHILD_AGE_3;
	strChildrenAge[4] = form.CHILD_AGE_4;
	strChildrenAge[5] = form.CHILD_AGE_5;
	strChildrenAge[6] = form.CHILD_AGE_6;
	strChildrenAge[7] = form.CHILD_AGE_7;
	var strQuestion = form.QUESTION.options[form.QUESTION.selectedIndex].value;
	var strAdvisoryOptout = form.ADVISORY_OPTOUT.value
	 var strDOBMO = form.DOBMO.value;
	 var strDOBDD = form.DOBDD.value;
	 var strDOBYR = form.DOBYR.value;
	 var strDOB = strDOBMO+"/"+strDOBDD+"/"+strDOBYR;

	 //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";
	 }

	 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(strGender == "")
	 {
			intIncomplete++;
			strAlertMsg = strAlertMsg +"Gender\n";
	 }
/*	 if(strAge == "")
	 {
			intIncomplete++;
			strAlertMsg = strAlertMsg +"Age\n";
	 }
*/
	 if(strMaritalStatus == "")
	 {
			intIncomplete++;
			strAlertMsg = strAlertMsg +"Marital Status\n";
	 }
	 if(strChildren == "")
	 {
			intIncomplete++;
			strAlertMsg = strAlertMsg +"Children\n";
	 }
	
	 if(strQuestion == "")
	 {
			intIncomplete++;
			strAlertMsg = strAlertMsg +"Answer for question. \n";
	 }

	if (intIncomplete > 0)
	{
		alert(strAlertMsg);
		return false;
	}

	var regexSpace=/\s/;
      
	 if (regexSpace.test(strFNAME)){
			form.FNAME.focus();
			alert("Please remove space from First Name.");
			return false;
	 }

	 if (regexSpace.test(strLNAME)){
			form.LNAME.focus();
			alert("Please remove space from Last Name.");
			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 last name was entered, check to see if it has any special characters
	 validateFirst = bwwValidateLNAME(strLNAME);
   
	 //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 (bwwValidateLNAME(strLNAME) == false)
	 {
	  form.LNAME.focus();
	  alert("Please leave out any special characters in your last name\n");
			return false;
	 }

	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 registering with Pringles.com. Unfortunately, we cannot accept messages from people in your age group.");
				window.location="/pages/index.shtml";
           		return false;  
   	   	  }
		*/
	  } 
 }



	 if((strChildren != "0") && (strChildren != ""))
	 {
		var childErrStr="";	
		var testRegExp=new RegExp("^\d+$");
		var reg = /^[\d]+$/;

		for(i=1; i <=strChildren ; i++){


			if(strChildrenAge[i].value == "")
			{
				childErrStr += "Enter the age for child "+i+"\n";	 
			}
			else{
				var val = strChildrenAge[i].value;
				if(val.search( reg )==-1)
					childErrStr += "Enter valid values for age of child "+i+"\n";

			}

		}

		if(childErrStr != ""){
			alert(childErrStr);
			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;
	 }

	 if ((form.ADVISORY_OPTOUT[0].checked == false )&&(form.ADVISORY_OPTOUT[1].checked == false))
//	 if (form.ADVISORY_OPTOUT.value =="" )
	{
		 alert("Select the your choice of participating in the Advisory Panel.\n");
		  return false;
	}
  
	return checkCounter();

}   
 
function bwwValidateEmailAddress(emailAddress)
{
	 var regexProperEmail=/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/
		  
	 if (emailAddress.search(regexProperEmail)==-1)
	 {  
	  return false;
	 }
	 return true;
}
 




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;
}

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 bwwValidateFNAME(FNAME)
{

 var regexProperFirst=/^[a-zA-Z]+((\')[a-zA-Z]+)?$/
      
	 if (FNAME.search(regexProperFirst)==-1)
	 {  
	  return false;
	 }
  return true;
}


 function bwwValidateLNAME(LNAME){

 //Regular expression that only allows letters,apostrophe and hyphens for last name
 var regexProperFirst=/^[a-zA-Z]+((\'|\s|\-)[a-zA-Z]+)?$/
      
	 if (LNAME.search(regexProperFirst)==-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;
}


function setCookie(underAge)
{
  document.cookie = "underage=" + underAge + ";";
}


function getCookie(name)
{
    var cookieValue = " ";
 if (document.cookie.length>0)
     {
    var cookieArray = document.cookie.split(';');
         for(i=0;i < cookieArray.length;i++) 
   {
   
   var nameFound = cookieArray[i].indexOf(name + "=");
   if( nameFound != -1)
   {
     cookieValue = 
     cookieArray[i].substring(name.length+1,cookieArray[i].length);
   }
    }
       }
     
    return cookieValue;
}

