function checkMainForm(form) {
	var age_yr=18;
	var incomplete=0;
	var alertMsg = "The following fields are required:\n";
	var friendsemail;
	var strZipCode = form.ZIP.value;
	if(allTrim(form.FIRSTNAME.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"First Name\n";
	}
	if(allTrim(form.LASTNAME.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Last Name\n";
	}
	if(allTrim(form.ADDR1.value) == "") {
			incomplete++;
			alertMsg = alertMsg +"Address\n";
	}
	if(allTrim(form.CITY.value) == "") {
			incomplete++;
			alertMsg = alertMsg +"City\n";
	}
	if (allTrim(form.STATE.value) == "ERROR") {
			incomplete++;
			alertMsg = alertMsg +"State\n";
	}
	if(allTrim(form.ZIP.value) == "") {
			incomplete++;
			alertMsg = alertMsg +"Zip\n";
	}
	if(allTrim(form.EMAIL.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Email Address\n";
	}
	if(allTrim(form.CONFIRM.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Confirm Email\n";
	}
	if(allTrim(form.COUNTRY.value) == "") {
			incomplete++;
			alertMsg = alertMsg +"Country\n";	
	}
	if(allTrim(form.GENDER.value) == "error") {
			incomplete++;
			alertMsg = alertMsg +"Gender\n";
	}

var DOBDD_valid = 1;
var DOBMO_valid = 1;
var DOBYR_valid = 1;
/*********************************** DOBDD Checks ********************************************************/
	if((allTrim(form.DOBDD.value) == "") || (allTrim(form.DOBDD.value) > 31) || (allTrim(form.DOBDD.value) < 1))  {
			incomplete++;
			DOBDD_valid = 0;
			alertMsg = alertMsg +"Day of Birth\n";
	}
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
    for (var i = 0; i < form.DOBDD.value.length; i++) {
		if ((iChars.indexOf(form.DOBDD.value.charAt(i)) != -1) && (DOBDD_valid))
		{
			incomplete++;
			DOBDD_valid = 0;
			alertMsg = alertMsg +"Day of Birth\n";
        }
    }
	var nonums = /^[0-9]*$/;
	if (!(nonums.test(form.DOBDD.value)) && (DOBDD_valid)) {
	     	incomplete++;
			DOBDD_valid = 0;
			alertMsg = alertMsg +"Day of Birth\n";
	}
/*********************************** DOBMO Checks ********************************************************/
	if((allTrim(form.DOBMO.value) == "") || (allTrim(form.DOBMO.value) > 12) || (allTrim(form.DOBMO.value) < 1))  {
			incomplete++;
			DOBMO_valid = 0;
			alertMsg = alertMsg +"Month of Birth\n";
	}
    for (var i = 0; i < form.DOBMO.value.length; i++) {
		if ((iChars.indexOf(form.DOBMO.value.charAt(i)) != -1) && (DOBMO_valid))
		{
			incomplete++;
			DOBMO_valid = 0;
			alertMsg = alertMsg +"Month of Birth\n";
        }
    }
	if (!(nonums.test(form.DOBMO.value)) && (DOBMO_valid)) {
			incomplete++;
			DOBMO_valid = 0;
			alertMsg = alertMsg +"Month of Birth\n";
	}

/*********************************** DOBYR Checks ********************************************************/
	if((allTrim(form.DOBYR.value) == "") || (allTrim(form.DOBYR.value) > 2007) || (allTrim(form.DOBYR.value) < 1900))  {
			incomplete++;
			DOBYR_valid = 0;
			alertMsg = alertMsg +"Year of Birth\n";
	}
    for (var i = 0; i < form.DOBYR.value.length; i++) {
		if ((iChars.indexOf(form.DOBYR.value.charAt(i)) != -1) && (DOBYR_valid))
		{
			incomplete++;
			DOBYR_valid = 0;
			alertMsg = alertMsg +"Year of Birth\n";
        }
    }
	if (!(nonums.test(form.DOBYR.value)) && (DOBYR_valid)) {
			incomplete++;
			DOBYR_valid = 0;
			alertMsg = alertMsg +"Year of Birth\n";
	}

	// Use the following 'if' condition only if date field for DOB is used
	if ( (form.DOBDD && form.DOBMO && form.DOBYR) && (incomplete == 0) ) {
			age_yr = checkAgeDD(form.DOBDD.value, form.DOBMO.value, form.DOBYR.value); // if date is required use this function
	}


	// Use the following condition when the date filed in DOB field is not required
	/*
	if ( form.DOBMO && form.DOBYR ) {
		if ( form.DOBMO.selectedIndex == 0 || form.DOBYR.selectedIndex == 0 ) {
				incomplete++;
				alertMsg = alertMsg +"Date of Birth\n";	
		}	
		else {
			age_yr = checkAge(form.DOBMO[form.DOBMO.selectedIndex].value, form.DOBYR[form.DOBYR.selectedIndex].value); 
		}
	}
	*/

	if ( age_yr < 18) {
			createCookie('inelligible', 'true', 1); 
			alert ("We're sorry; we can't accept messages or personal information from persons in your age group.  But that doesn't mean you can't explore the rest of our website and learn more about us!");
			if (window.opener && !window.opener.closed) {
				opener.location="/index.shtml";
				window.close();
			}
			else{
				window.location="/index.shtml";
			}
           	return false;
	}
		
	if (incomplete > 0){
		alert(alertMsg);
		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(strZipCode);
  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(form.EMAIL.value);
   
 //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;
 }
  
 //Check if the email and confirm email fields match
 if (form.EMAIL.value != form.CONFIRM.value) 
 {
	form.EMAIL.focus();
	alert("The Email Address and the Confirm Email Address fields do not match\n");
		return false;
 }

	return checkCounter();
}


function checkEmail(addr) {
//  Will check for @, period after @ and text in between
	
	var in_space = addr.indexOf(" ");
	if (in_space != -1)
	{ alert ("Email address should contain no spaces and be of the form jdoe\@domain.com");
			return false;  }

	var len = addr.length;
	var alpha = addr.indexOf("@");
	var last_alpha = addr.lastIndexOf("@");

	if (alpha != last_alpha)
	 {  alert ("Bad email address. Should contain only one @ and be of the form jdoe\@domain.com");
			 return false; }

	// No @, in first position, or name too short
	if (alpha == -1 || alpha == 0 || len<6 )
	 {  alert ("Bad email address. Should contain an @ and be of the form jdoe\@domain.com");
			 return false; }

	var last_p = addr.lastIndexOf(".");
			// Be sure period at least two spaces after @, but not last char.
			
	if (last_p - alpha < 2 || last_p == (len - 1) )
		{  alert ("Bad email address. Should contain a period after the @ and be of the form jdoe\@domain.com");
				return false; }
}

function allTrim(cValue){
  var lDone=false;
  while (lDone==false){
    if (cValue.length==0) {return cValue;}
    if (cValue.indexOf(' ')==0){cValue=cValue.substring(1);lDone=false; continue;}
    else {lDone=true;}
    if (cValue.lastIndexOf(' ')==cValue.length-1){cValue=cValue.substring(0, cValue.length-1);lDone=false;continue;}
    else {lDone=true;}
  }
  return cValue;
}

function isValidAge( year, month, day ) {
// Will check to make sure the user is at least the age set below
   validAge = 13;
   today = new Date();
   bday  = new Date( year+=1900, month, day );
   difference = today - bday;
   if ( Math.floor(difference/(1000*60*60*24*365))-validAge >= 0 ) { return true; }
   else {
	   return false;
   }
}

function checkYear(oYear) {
	oYear.value = allTrim(oYear.value)
	if ( isNaN(oYear.value) ) { // is a number?
		return false;
	}
	//if ( oYear.value.length ==2 ){ // 2 digit year?
	//	oYear.value = "19" + oYear.value.toString();
	//}
	if ( oYear.value.length !=4 ){ // not 4 digit year?
		return false;
	}
	return true;
}

function checkAge(liMonth, liYear) {
	var ldToday = new Date();
	var liAge;
	liAge = ldToday.getFullYear() - liYear;
	if ( liMonth >= ldToday.getMonth()+1 ) {
		liAge = liAge - 1;
	}
	return liAge;
}

function checkAgeDD(liDay,liMonth, liYear) {
	var ldToday = new Date();
	var liAge;
	liAge = ldToday.getFullYear() - liYear;
	if ( liMonth > ldToday.getMonth()+1 ) {
		liAge = liAge - 1;
	}
	else if ( liMonth == ldToday.getMonth()+1 ) {
	  if ( liDay > ldToday.getDate() ) {
		  liAge = liAge - 1;
	  }
	}
	return liAge;
}


var iSubmitCounter=0;

function checkCounter(){
	iSubmitCounter++;
	if (iSubmitCounter==1) {
		return true;
	} else {
		alert('Please Wait.'); 
		return false;
	}
}

function bwwValidateEmailAddress(emailAddress)
{
 var regexProperEmail=/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/
      
 if (emailAddress.search(regexProperEmail)==-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;
}
