// Panasonic Oxyride Sweeps Form Validator, version 1.0
// Last modified on 06/09/2005
// By: kfleischer@iandD.com

function Validate() {
	var result;
	setPhoneNumber();
	result = validateForm();
	if (result) {
	document.forms[0].submit();
	}
}

function setPhoneNumber()
{
	strPhone1 = document.forms[0].phone1.value;
	strPhone2 = document.forms[0].phone2.value;
	strPhone3 = document.forms[0].phone3.value;

	//Format the Phone Number
	strPhone = strPhone1 + strPhone2 + strPhone3;

	//let's update the Phone Number
	document.forms[0].phoneNumber.value = strPhone;
}

function validateForm() {
	var msg = "";
	var atest;
	var anum=/(^\d+$)/;
	
	if (document.forms[0].firstname.value == "") {
		msg += " - First Name\n";
	}	
	if (document.forms[0].lastname.value == "") {
		msg += " - Last Name\n";
	}	
	if (document.forms[0].address.value == "") {
		msg += " - Street Address\n";
	}
	if (document.forms[0].city.value == "") {
		msg += " - City\n";
	}
	if (document.forms[0].state.value == "") {
		msg += " - State\n";
	}
	sZip = document.forms[0].zip.value;
	sZipLen = (sZip.length);
	if (sZipLen < 5) {
		msg += " - Zipcode\n";
		} else {
		if (anum.test(sZip)) {}
		else {
		msg += " - A Valid Zipcode\n";
		}
	}
	
	sPhone = document.forms[0].phoneNumber.value;
	sPhoneLen = (sPhone.length);
	if (sPhoneLen < 10) {
		msg += " - A Valid Phone Number\n";
	} else {
		if (anum.test(sPhone)) {}
		else {
		msg += " - A Valid Phone Number\n";
		}
	}
	
	sEmail = document.forms[0].email.value;
	if (sEmail == "" || sEmail.indexOf("@") == -1  || sEmail.indexOf(".") == -1) {
		msg += " - Valid Email Address\n";
	} else {
		nEmailDot = (sEmail.length - sEmail.lastIndexOf("."));
		if (nEmailDot > 2 && nEmailDot < 5)  {
		} else {
			msg += " - Valid Email Address\n";
		}
	}
	if (document.forms[0].email_confirm.value == "") {
		msg += " - Confirm Email\n";
	} else {
		if (document.forms[0].email_confirm.value != document.forms[0].email.value) {
			msg += " - Email Confirmation Mismatch!\n";
		}
	}
	
	strBirthMonth = document.forms[0].month.value;
	strBirthDay = document.forms[0].day.value;
	strBirthYear = document.forms[0].year.value;

	//Format the date of birth
	strBirthDate = strBirthMonth + "/" + strBirthDay + "/" + strBirthYear;
	//Update the birth date
	document.forms[0].dateofbirth.value = strBirthDate;
	//Validate DOB
	sDOBLen = (strBirthDate.length);
	if (sDOBLen < 10) {
		msg += " - Valid Date of Birth\n";
	} else {
		
		if (anum.test(strBirthMonth)) {
			if (strBirthMonth > 12) {msg += " - Valid Date of Birth Month\n";}
		} else {
			msg += " - Valid Date of Birth Month\n";
		}
		if (anum.test(strBirthDay)) {
			if (strBirthDay > 31) {msg += " - Valid Date of Birth Day\n";}
		} else {
			msg += " - Valid Date of Birth Day\n";
		}
		if (anum.test(strBirthYear)) {} else {
			msg += " - Valid Date of Birth Year\n";
		}
		//Confirm Age
		var today = new Date()
		var tmonth = today.getMonth() + 1
		var tday = today.getDate()
		var tyear = today.getFullYear()
		CurrentAge = tyear - strBirthYear;
		if (strBirthMonth > tmonth) {
				CurrentAge = CurrentAge - 1;
		}
		if (strBirthMonth == tmonth) {
			if (strBirthDay > tday) {
				CurrentAge = CurrentAge - 1;
			}
		}
		//eot	
		if (CurrentAge < 14) {
			msg += " - Age Ineligable!\n(Under 14 years old)\n";
		}
	}
	
	if (msg != "") 	{
			alert ("Please correct the following fields:\n\n" + msg);
		return false;
		} else {
		return true;
	}
}

