// JavaScript Document

function validateFormFields(formObj) {
	//reset any previous errors
	resetValidationFields()
	
	//track number of errors
	var errors = 0;
	
	//check fields
	if(!isValidApplicantLastName(formObj.applicantLastName.value)) {
		errors++;
		document.getElementById("formLastNameError").style.color = "#ff0000";
	}
	
	if(!isValidApplicantFirstName(formObj.applicantFirstName.value)) {
		errors++;
		document.getElementById("formFirstNameError").style.color = "#ff0000";				
	}	
	
	if(!isValidApplicantBirthDate(formObj.applicantBirthMonth.value, formObj.applicantBirthDay.value, formObj.applicantBirthYear.value)) {
		errors++;
		document.getElementById("formBirthError").style.color = "#ff0000";
		document.getElementById("formBirthError").innerHTML = "(Invalid date)";
	}
	
	if(!isValidCurrentMailingAddress(formObj.applicantCurrentMailingAddress.value)) {
		errors++;
		document.getElementById("formCurrentMailingAddressError").style.color = "#ff0000";		
	}
	
	if(!isValidApplicantEmailAddress(formObj.applicantEmailAddress.value)) {
		errors++;
		document.getElementById("formApplicantEmailAddressError").style.color = "#ff0000";
		document.getElementById("formApplicantEmailAddressError").innerHTML = ((formObj.applicantEmailAddress.value.length > 0) ? "(Invalid email address)" : "(Required)");
	}
	
	//tally results and notify user if any errors are found
	if(errors == 0) {
		return true;
	}
	else {
		displayValidationResults(errors);
		return false;
	}
}

function isValidApplicantLastName(formLastNameField) {
	var validationResult = ((formLastNameField.length > 0) ? true : false);
	return validationResult;
}

function isValidApplicantFirstName(formFirstNameField) {
	var validationResult = ((formFirstNameField.length > 0) ? true : false);
	return validationResult;	
}

function isValidApplicantBirthDate(formBirthMonthField, formBirthDayField, formBirthYearField) {
	var validationResult = true;
	
	if((formBirthMonthField.length > 0) && (formBirthDayField.length > 0) && (formBirthYearField.length > 0)) {
		if(!isValidYear(formBirthYearField)) {
			return false;			
		}
		else if(((formBirthMonthField == "3") || (formBirthMonthField == "5") || (formBirthMonthField == "8") || (formBirthMonthField == "10")) && (formBirthDayField == "31")) {
			return false;
		}
		else if((formBirthMonthField == "1") && ((formBirthDayField == "30") || (formBirthDayField == "31"))) {
			return false;
		}
		else if((formBirthMonthField == "1") && (formBirthDayField == "29") && (!isLeapYear(parseInt(formBirthYearField)))) {
			return false;			
		}
	}
	else if((formBirthMonthField.length > 0) || (formBirthDayField.length > 0) || (formBirthYearField.length > 0)) {
		validationResult = false;
	}

	return validationResult;
}

function isLeapYear(formBirthYearField) {
	if((formBirthYearField % 400) == 0) {
		return true;
	}
	else if((formBirthYearField % 100) == 0) {
		return false;
	}
	else if((formBirthYearField % 4) == 0) {
		return true;		
	}
	else {
		return false;		
	}
}

function isValidYear(formBirthYearField) {
	if((formBirthYearField.length == 4) && (parseInt(formBirthYearField) >= 1800) && (parseInt(formBirthYearField) <= 2008)) {
		return true;
	}
	else {
		return false;
	}
}

function isValidCurrentMailingAddress(formCurrentMailingAddressField) {
	var validationResult = ((formCurrentMailingAddressField.length > 0) ? true : false);
	return validationResult;		
}

function isValidApplicantEmailAddress(formEmailField) {
	var validationResult = (((formEmailField.length > 0) && (formEmailField.indexOf("@") > 0) && (formEmailField.indexOf(".") > 2) && (formEmailField.lastIndexOf(".") > formEmailField.indexOf("@"))) ? true : false);
	return validationResult;		
}

function displayValidationResults(errors) {
	varErrorSummaryMsg = errors + ((errors > 1) ? " errors were " : " error was ") + "found in your application!\nPlease review the form and correct any indicated fields then resubmit.";
	alert(varErrorSummaryMsg);
}

function resetValidationFields() {
	document.getElementById("formLastNameError").style.color = "#000000";
	document.getElementById("formFirstNameError").style.color = "#000000";
	document.getElementById("formBirthError").style.color = "#000000";
	document.getElementById("formBirthError").innerHTML = "";
	document.getElementById("formCurrentMailingAddressError").style.color = "#000000";
	document.getElementById("formApplicantEmailAddressError").style.color = "#000000";
	document.getElementById("formApplicantEmailAddressError").innerHTML = "(Required)";
}
