/*Universal Javascript Form Validator*/

/*This function checks to make sure the form field has been filled in and sends a warning message if a required field is empty.*/
/*This function requires the 'onSubmit="return formValidator()"' event to be attached to the form requiring validation.*/
function formValidator() {
	/*Requires the form's name and the name of the field.*/
	/*Each field that needs to be checked must be defined as a variable here.*/
	var nameField = document.contact_form.name;
	var emailField = document.contact_form.email;
	
	/*Checks to see if the field is empty; if so, the function runs a warning message.*/
	/*Repeat this code for each field that needs to be checked.*/
	if (nameField.value == "") {
		/*The actual message is built by the "alertMsg" function.*/
		alertMsg("Please include your full name.",nameField);
		/*This returns the function as false, which stops the form from processing.*/
		return false;
	}
	if (emailField.value == "") {
		/*The actual message is built by the "alertMsg" function.*/
		alertMsg("Please include your e-mail address.",emailField);
		/*This returns the function as false, which stops the form from processing.*/
		return false;
	}
	if (! checkEmail(emailField.value)) {
		/*The actual message is built by the "alertMsg" function.*/
		alertMsg("Please include a valid e-mail address.",emailField);
		/*This returns the function as false, which stops the form from processing.*/
		return false;
	}
}

function checkEmail (emailVal) {
	if (
		emailVal.search("@") >= 0 && 
		emailVal.search(".") >= 0 && 
		emailVal.search(" ") < 0 && 
		emailVal.length >= 6 && 
		emailVal.indexOf("@") > 0 &&
		emailVal.indexOf("@") < (emailVal.length - 4) &&
		emailVal.indexOf(".") > 0 &&
		emailVal.indexOf(".") < (emailVal.length - 2)
	) {
		return true;
	} else {
		return false;
	}
}

/*Creates the validator warning message and then places the focus on the empty field.*/
/*Requires two arguments, the message to display and the field that triggered the warning.*/
function alertMsg (msg,field) {
		/*Displays the message.*/
		alert(msg);
		/*Places the cursor on the offending field.*/
		field.focus();
}


