//
// LTrim(), RTrim(), trim()
// Removes leading whitespaces
// Function found at Code Snippets - http://www.bigbold.com/snippets/posts/show/701
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

function contact_bind(){
	document.getElementById("form-contact").onsubmit=function(){
		bail=false;
		emailbail=false;
		author=document.getElementById("author");
		email=document.getElementById("email");
		message=document.getElementById("message");
		errorborder="1px solid #333;"
		normalborder="1px solid #ccc;"
		emailregex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/
		
		author.value=trim(author.value);
		email.value=trim(email.value);
		message.value=trim(message.value);
		
		if(author.value.length==0){
			author.style.border=errorborder;
			bail=true;
		}else{
			author.style.border=normalborder;
		}

		if(email.value.length==0){
			email.style.border=errorborder;
			bail=true;
		}else if(!emailregex.test(email.value)){
			emailbail=true;
		}else{
			email.style.border=normalborder;
		}
		
		if(message.value.length==0){
			message.style.border=errorborder;
			bail=true;
		}else{
			message.style.border=normalborder;
		}				
		
		if(emailbail==true){
			alert("Please enter a valid Email address.");
			return false;
		}else if(bail==true){
			alert("Please fill out all required fields.");
			return false;
		}else{
			return true;
		}
	}
}
addLoadEvent(contact_bind);