// JavaScript Document

function validateFormFields(formObj) {
	//reset any previous errors
	resetValidationFields()
	
	//track number of errors
	var errors = 0;
	
	//check fields
	if(formObj.contactName.value.length == 0) {
		errors++;
		document.getElementById("contactNameErrorField").style.color = "#ff0000";		
	}
	
	if(!isValidEmailAddress(formObj.emailAddress.value)) {
		errors++;
		document.getElementById("emailErrorField").style.color = "#ff0000";
		document.getElementById("emailErrorField").innerHTML = ((formObj.emailAddress.value.length > 0) ? "(Invalid email address)" : "(Required)");
	}
	
	if(formObj.messageBody.value.length == 0) {
		errors++;		
		document.getElementById("messageErrorField").style.color = "#ff0000";		
	}	
	
	//tally results and notify user if any errors are found
	if(errors == 0) {
		return true;
	}
	else {
		displayValidationResults(errors);
		return false;
	}
}

function isValidEmailAddress(emailAddress) {
	var validationResult = (((emailAddress.length > 0) && (emailAddress.indexOf("@") > 0) && (emailAddress.indexOf(".") > 2) && (emailAddress.lastIndexOf(".") > emailAddress.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("contactNameErrorField").style.color = "#000000";
	document.getElementById("emailErrorField").style.color = "#000000";
	document.getElementById("emailErrorField").innerHTML = "(Required)";	
	document.getElementById("messageErrorField").style.color = "#000000";

}
