
var FormProcessor = {

	/* (c) CopySense.co.uk 2010 ~ Martin Latter */

	sNameOfForm: "contactform",
	sNameOfClass: "errorfield",
	sErrorBgCol: "#ff0",
	sClearBgCol: "#f7ffff",

	checkForm: function(oForm) {

		function errorDisplay(oId, oErrFld, sText) {
			oId.style.background = FormProcessor.sErrorBgCol;
			oId.focus();
			var oEf = document.getElementById(oErrFld);
			oEf.style.display = "block";
			oEf.innerHTML = sText;
		}

		var rVEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

			// clear form backgrounds
		var aFormElems = [oForm.name, oForm.email, oForm.message];
		var iFormElemsLen = aFormElems.length;
		for (var i = 0; i < iFormElemsLen; i++) {
			aFormElems[i].style.background = FormProcessor.sClearBgCol;
		}
			// clear error boxes
		var oErrorCont = document.getElementById(FormProcessor.sNameOfForm);
		var oErrorDivs = oErrorCont.getElementsByTagName("div");
		var iErrorDivsLen = oErrorDivs.length;
			// clear error fields
		for (i = 0; i < iErrorDivsLen; i++) {
			if (oErrorDivs[i].className == FormProcessor.sNameOfClass) {
				oErrorDivs[i].innerHTML = "";
				oErrorDivs[i].style.display = "none";
			}
		}
			// error messages
		if (oForm.name.value === "") {
			errorDisplay(oForm.name, "ename", "Please enter your name.");
			return false;
		}
		else if (oForm.email.value === "") {
			errorDisplay(oForm.email, "eemail", "Please enter an email address.");
			return false;
		}
		else if (!rVEmail.test(oForm.email.value)) {
			errorDisplay(oForm.email, "eemail", "The email address is not valid.");
			return false;
		}
		else if (oForm.message.value === "") {
			errorDisplay(oForm.message, "emessage", "The message field is blank.");
			return false;
		}
		else {
			return true;
		}
	}
};


window.onload = function() {

	cbsElemTxt("title");
	if (document.getElementById("contactform")) {
		document.getElementById("contactform").onsubmit = function() {return FormProcessor.checkForm(this);};
	}
};


window.onunload = function() {
	FormProcessor = null;
};
