/***** JAVASCRIPT VALIDATION ****

Checks required fields (i.e. not empty) and email fields (a@b.c)
and displays alerts with the field names - so NAME YOUR FIELDS PROPERLY!

In each page you want to validate include this javascript:
	function validateForm() {
		requiredFields = new Array("fieldName1","fieldName2",...);
		emailFields = new Array("fieldName3",...);
		if (checkRequired(requiredFields) && checkEmail(emailFields))
			return true;
		else
			return false;
	}

if you have no email fields to validate just amend the if statement as follows:
	if (checkRequired(requiredFields))

To call the validateForm function add the ONSUBMIT METHOD to your form thus:
	<FORM ONSUBMIT="return validateForm()">

*******************************/

function checkRequired(fieldArray) {
	emptyFields = "";
	for (i = 0; i < fieldArray.length; i++) {
		fieldToCheck = fieldArray[i];
		fieldValue = document.forms[0].elements[fieldToCheck].value;
		if (isEmpty(fieldValue)) {
			emptyFields = emptyFields + "\n" + fieldToCheck;
		}
	}
	if (emptyFields == "") {
		return true;
	} else {
		alert("Please fill in the following required fields:" + emptyFields);
		return false;
	}
}

function checkEmail(fieldArray) {
	emailFields = "";
	for (i = 0; i < fieldArray.length; i++) {
		fieldToCheck = fieldArray[i];
		fieldValue = document.forms[0].elements[fieldToCheck].value;
		if (!isEmail(fieldValue)) {
			emailFields = emailFields + "\n" + fieldToCheck;
		}
	}
	if (emailFields == "") {
		return true;
	} else {
		alert("Please enter a valid email address in the following fields:" + emailFields);
		return false;
	}
}

function isEmpty(string) {
	return (string == null || string.length == 0);
}

function isEmail(string) {
	atPos = string.indexOf("@",1);
	dotPos = string.indexOf(".",atPos+2);
	if (atPos > 0 && dotPos > atPos && string.length > dotPos)
		return true;
	else
		return false;
	// this is what we're checking for:
	// (1) @ char with at least 1 letter in front of it
	// (2) . after @ char with at least 1 letter between @ and .
	// (3) make sure there is at least 1 char after the dot (below)
}
