function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function isFilled(field) {
	if (field.value.length < 1) {
		return false;
	} else {
		return true;
	}
}

function isEmail(field) {
	if(field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
		return false;
	} else {
		return true;
	}
}

function validateForm(whichform) {
	for (var i=0; i < whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1) {
			if (!isFilled(element)) {
				var label = element.previousSibling.previousSibling.firstChild.nodeValue;
				alert("\"" + label + "\" is a required field.");
				element.focus();
				return false;
			}
		}
		if(element.className.indexOf("email") != -1) {
			if (!isEmail(element)) {
				var label = element.previousSibling.previousSibling.firstChild.nodeValue;
				alert("\"" + label + "\" field must be a valid email address.");
				element.focus();
				return false;
			}
		}
	}
	return true;
}

function prepareForms() {
	for (var i=0; i < document.forms.length; i++) {
		var thisform = document.forms[i];
		thisform.onsubmit = function() {
			return validateForm(this);
		}
	}
}

addLoadEvent(prepareForms);