function checkMainForm(form) {
	var age_yr=18;
	var incomplete=0;
	var alertMsg = "The following fields are required:\n";
	var friendsemail;
	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.EMAIL.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"E-mail Address\n";
	}
	if(allTrim(form.FRIENDSEMAIL1.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Your Friend's E-mail Address #1\n";
	}

	if (incomplete > 0){
		alert(alertMsg);
		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;
 }
 
 //Given an email, make sure it's in proper email format.
 boolValidateEmail = bwwValidateEmailAddress(form.FRIENDSEMAIL1.value);
   
 //If the email isn't formatted properly, throw out this error:
 if (boolValidateEmail == false)
 {
  form.FRIENDSEMAIL1.focus();
  alert("The E-mail Address you entered is not in the proper format\n");
		return false;
 }
if(form.FRIENDSEMAIL2.value != "") { 
  //Given an email, make sure it's in proper email format.
 boolValidateEmail = bwwValidateEmailAddress(form.FRIENDSEMAIL2.value);
   
 //If the email isn't formatted properly, throw out this error:
 if (boolValidateEmail == false)
 {
  form.FRIENDSEMAIL2.focus();
  alert("The E-mail Address you entered is not in the proper format\n");
		return false;
 }
}
if(form.FRIENDSEMAIL3.value != "") {
  //Given an email, make sure it's in proper email format.
 boolValidateEmail = bwwValidateEmailAddress(form.FRIENDSEMAIL3.value);
   
 //If the email isn't formatted properly, throw out this error:
 if (boolValidateEmail == false)
 {
  form.FRIENDSEMAIL3.focus();
  alert("The E-mail Address you entered is not in the proper format\n");
		return false;
 }
}
 if(form.FRIENDSEMAIL4.value != "") {
  //Given an email, make sure it's in proper email format.
 boolValidateEmail = bwwValidateEmailAddress(form.FRIENDSEMAIL4.value);
   
 //If the email isn't formatted properly, throw out this error:
 if (boolValidateEmail == false)
 {
  form.FRIENDSEMAIL4.focus();
  alert("The E-mail Address you entered is not in the proper format\n");
		return false;
 }
}
 if(form.FRIENDSEMAIL5.value != "") {
  //Given an email, make sure it's in proper email format.
 boolValidateEmail = bwwValidateEmailAddress(form.FRIENDSEMAIL5.value);
   
 //If the email isn't formatted properly, throw out this error:
 if (boolValidateEmail == false)
 {
  form.FRIENDSEMAIL5.focus();
  alert("The E-mail Address you entered is not in the proper format\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; }
   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;
}
 