

// Form validation for the comment form
function checkCommentForm(){
	var msg = "";
	var commentBox = document.getElementById("comment");
	var emailBox = document.getElementById("email");
	var authorBox = document.getElementById("author");
	var emailRegexp = new RegExp("[^@]+@.+");
	
	if(commentBox.value == "YOUR QUESTION")
		msg += "Please type in a question\n";
	if(emailBox.value == "EMAIL")
		msg += "Please type in your email address\n";
	else if(!emailRegexp.test(emailBox.value))
		msg += "Please enter a valid email address\n";
	if(authorBox.value == "NAME")
		msg += "Please type in your name\n";
	
	
	var valid = msg.length == 0; 
	if(!valid) alert(msg);
	return valid;
}
// To avoid having javascript in the page, using jQuery to initialise event listeners
function init_form(){
	var commentForm = document.getElementById("commentform");
	// if commentForm == null, there is no comment form on this page
	if(commentForm==null) return null;
	// Set onsubmit action
	commentForm.onsubmit = checkCommentForm;
	
	var commentBox = document.getElementById("comment");
	var emailBox = document.getElementById("email");
	var authorBox = document.getElementById("author");
	
	commentBox.onblur = function (){
		if(this.value=='')this.value='YOUR QUESTION';
	}
	commentBox.onfocus = function (){
		if(this.value=='YOUR QUESTION')this.value='';
	}
	authorBox.onblur = function (){
		if(this.value=='')this.value='NAME';
	}
	authorBox.onfocus = function (){
		if(this.value=='NAME')this.value='';
	}
	emailBox.onblur = function (){
		if(this.value=='')this.value='EMAIL';
	}
	emailBox.onfocus = function (){
		if(this.value=='EMAIL')this.value='';
	}
} 
jQuery(document).ready(init_form);

