function validateMail() {
	// get the elements to be checked
	var senderName = document.getElementById("sendername");
	var senderEmail = document.getElementById("senderemail");
	var subject = document.getElementById("subject");
	var antispam = document.getElementById("antispam");
	// the form is invalid until checked
	var validName = false;
	var emailHasInput = false;
	var validEmail = false;
	var validSubject = false;
	var validAntiSpam = false;
	// check the name
	if (senderName.value != '') {
		validName = true;
	}
	// check the subject
	if (subject.value != '') {
		validSubject = true;
	}
	// check that the antispam question was answered, but don't give the answer here
	if (antispam.value != '') {
		validAntiSpam = true;
	}
	// now check the email, make sure its a valid email address
	if (senderEmail.value != '') {
		emailHasInput = true;
		// do a regexp check to make sure the mail format is correct	
		var pattern = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
		var checkEmail = pattern.test(senderEmail.value);
		if (checkEmail === true) {
			validEmail = true;
		}
	}
	// if everything checks out let the form go for processing
	if (validName === true && validEmail === true && validSubject === true) {
		return true;
	} else {
		// oh no something went wrong better let the user know
		var messageText = "Input did not validate!";
		if (validName === false) {
			messageText = messageText + "\nName: Input Required";	
		}
		if (emailHasInput === false) {
			messageText = messageText + "\nEmail: Input Required";	
		}
		if (emailHasInput === true && validEmail === false) {
			messageText = messageText + "\nEmail: The email address supplied is not a valid address";	
		}
		if (validSubject === false) {
			messageText = messageText + "\nSubject: Input Required";	
		}
		alert(messageText);
		// don't submit the form until they fix their mistakes
		return false;
	}
}

function initValidateMail() {
	// make sure there is a form to validate
	if (!document.getElementById("contactform")) return false;
	// check the form when submitted
	document.forms[0].onsubmit = validateMail;
	return true;
}

addLoadListener(initValidateMail);
