function
daysArray ()
{
	for (var i = 1 ; i <= 12 ; i++) {
		this[i] = 31 ;
		if (i == 4 || i == 6 || i == 9 || i == 11) {
			this[i] = 30 ;
			}
		else if (i == 2) {
			this[i] = 29 ;
			}
		} 
	return this
}

function
daysInFeb (year)
{
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 ) ;
}

function
realDate ()
{
	var frm = document.forms["form"] ;
	var split = frm.birthDate.value.split ("-") ;
	var year = parseInt (split[0], 10) ;
	var month = parseInt (split[1], 10) ;
	var day = parseInt (split[2], 10) ;
	var daysInMonth = daysArray () ;
	var minAge = 22 ;
	var maxAge = 100 ;
	var today = new Date () ;
	var minYear = today.getFullYear () - maxAge ;
	var maxYear = today.getFullYear () - minAge ;

	if (!frm.birthDate.value.match ("^\s*\\d{4}-\\d{2}-\\d{2}\s*$")) {
		alert ("Please enter Birth Date in the form YYYY-MM-DD") ;
		return false ;
		}
	if (month < 1 || month > 12) {
		alert ("Please enter a Birth Date month between 1 and 12") ;
		return false ;
		}
	if (month == 2 && day > daysInFeb (year)) {
		alert ("Please enter a Birth Date day between 1 and " + daysInFeb (year)) ;
		return false ;
		}
	else if (day > daysInMonth[month]) {
		alert ("Please enter a Birth Date day between 1 and " + daysInMonth[month]) ;
		return false ;
		}
	else if (day < 1 || day > 31) {
		alert ("Please enter a Birth Date day between 1 and 31") ;
		return false ;
		}
	else if (year < minYear || year > maxYear) {
		alert ("Please enter a Birth Date year between " + minYear + " and " + maxYear) ;
		return false ;
		}

	return true ;
}

function
realPhone (phone, field)
{
	var frm = document.forms["form"] ;

	if (phone == "") {
		return (true) ;
		}
	if ((frm.country.value == "US" || frm.country.value == "CA")) {
		if (!phone.match ("^\\s*(1-)?\\d{3}-\\d{3}-\\d{4}\\s*$")) {
			alert ("Please enter " + field + " in the form (1-)?ddd-ddd-dddd.\nFor example, 818-555-1212 or 1-818-555-1212") ;
			return false ;
			}
		}
	else if (!phone.match ("^[-0-9() +]+$")) {
		alert ("Please include only the characters [-0-9() }] in " + field) ;
		return false ;
		}

	return true ;
}

function
realZip ()
{
	var frm = document.forms["form"] ;
	var zip = frm.zip.value ;

	if (frm.country.value == "US") {
		if (!zip.match ("^\\s*\\d{5}(-\\d{4})?\\s*$")) {
			alert ("Please enter Postal Code in the form ddddd(-dddd)?") ;
			return false ;
			}
		var split = zip.split ("-") ;
		var zipMinus4 = parseInt (split[0], 10) ;
		if (zipMinus4 == 0) {
			alert ("Please enter a non-zero Postal Code") ;
			return false ;
			}
		return true ;
		}
	else if (frm.country.value == "CA") {
		if (!zip.match ("^\\s*[A-Z]\\d[A-Z] \\d[A-Z]\\d\\s*$")) {
			alert ("Please enter Postal Code in the form AOA OAO, where A is an uppercase letter and O is single digit") ;
			return false ;
			}
		return true ;
		}

	return true ;
}

function
addlValidation ()
{
	var frm = document.forms["form"] ;

	if (!realDate ()) {
		return false ;
		}
	if (!realPhone (frm.phone.value, "Phone")) {
		return false ;
		}
	if (!realPhone (frm.altPhone.value, "Alternate Phone")) {
		return false ;
		}
	if (!realZip ()) {
		return false ;
		}
	if (frm.country[frm.country.selectedIndex].value == "US") {
		stateIndex = frm.USState.selectedIndex ;
		if (stateIndex == 0 || frm.USState[stateIndex].value == "") {
			alert ("Please select your State") ;
			return (false) ;
			}
		}
	else {
		if (frm.foreignState.value == "") {
			alert ("Please select your State") ;
			return (false) ;
			}
		}
	//ethnicityIndex = frm.ethnicity.selectedIndex ;
	//if (frm.ethnicity[ethnicityIndex].value == "") {
	//	alert ("Please select your Ethnicity") ;
	//	return (false) ;
	//	}

	return true ;
}

var validator = new Validator ("form") ;
validator.addValidation ("firstName", "required", "Please enter your First Name") ;
validator.addValidation ("lastName", "required", "Please enter your Last Name") ;
validator.addValidation ("birthDate", "required", "Please enter your Birth Date") ;
validator.addValidation ("address", "required", "Please enter your Street Address") ;
validator.addValidation ("city", "required", "Please enter your City") ;
validator.addValidation ("zip", "required", "Please enter your Postal Code") ;
validator.addValidation ("foreign", "required", "Please Answer 'Yes' or 'No'") ;
validator.addValidation ("phone", "required", "Please enter your Phone") ;
validator.addValidation ("email", "required", "Please enter your Email Address") ;
validator.addValidation ("email", "email", "Please correct your Email Address") ;
//validator.addValidation ("ethnicity", "required", "Please select your Ethnicity") ;
validator.setAddnlValidationFunction ("addlValidation") ;

