// variables for use in the script
var FormName = "CommentForm";
var loLight = "#003366";
var hiLight = "#EE0000";

checkContact = function() {
	Errors = "";
	
	var theForm = document.getElementById( FormName );
	
	if ( theForm.FullName.value == "" ) {
		Errors += "\n\n\tPlease supply your name";
		markField( theForm.FullName );
	}

	Flag = 0;
	if ( theForm.EmailAddress.value == "" ) {
		Errors += "\n\n\tPlease supply your Email";
		Flag = 1;
		markField( theForm.EmailAddress );
		markField( theForm.ConfirmEmailAddress );
	}
	
	if ( Flag == 0 && document.CommentForm.EmailAddress.value != theForm.ConfirmEmailAddress.value ) {
		Errors += "\n\n\tYour email does not match the confirmation email";
		Flag = 1;
		markField( theForm.EmailAddress );
		markField( theForm.ConfirmEmailAddress );
	}
	
	Mail = /[\w\_\-]+[\.\w+]?\@[\w\_\-]+\.\w{2,3}/i;
	if ( Flag == 0 && !Mail.test( theForm.EmailAddress.value ) ) {
		Errors += "\n\n\tThe email address you supplied is invalid";
		markField( theForm.EmailAddress );
		markField( theForm.ConfirmEmailAddress );
	}

	if ( theForm.Comments.value == "" ) {
		Errors += "\n\n\tPlease type your comments into the box provided";
		markField( theForm.Comments );
	}
	

	if ( Errors ) {
		alert( "Please correct the following errors before submitting the form:" + Errors );
		return false;
	} else { return true; }
}

function lowLight( thing ) {
	thing.style.borderColor = loLight;
}

function markField( thing ) {
	thing.style.borderColor = hiLight;
}

setupForm = function() {
	var theForm = document.getElementById( FormName );
	// set the onSubmit handler for the form
	theForm.onsubmit = checkContact;
}

addHandler( window, 'load', setupForm , false );