
function trim(stringToTrim) {//remove spaces
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//validation function before submission
function check(){
	//trim left and right
	document.contact_form.email.value = trim(document.contact_form.email.value);
	document.contact_form.msg.value = trim(document.contact_form.msg.value);

	//check for space in sender email
	if (document.contact_form.email.value.indexOf(" ")>0){
		alert("Please remove spaces from sender email address.");
		document.contact_form.email.focus();
		return false;
	}
	
	//check if sender email is valid
	if (document.contact_form.email.value=="" || document.contact_form.email.value.indexOf("@")<0 || document.contact_form.email.value.indexOf(".")<0){
		alert("Invalid Email Adress.");
		document.contact_form.email.focus();
		return false;
	}
	//check for message body
	if (document.contact_form.msg.value==""){
		alert ("Blank message.");
		document.contact_form.msg.focus();
		return false;
	}
	
	//check image verification code provided
	if (document.contact_form.txtNumber.value == ""){
		alert ("No image verification code provided!");
		document.contact_form.txtNumber.focus();
		return false;
	}
}

function captcha_update(){
	//refreshes the captcha image located in img tag with id captcha_img when user can't read the image, refreshes the hidden sid form variable too
	var now = new Date();
	
	if( document.getElementById ) {//get image node
		img = document.getElementById( "captcha_img" );//get captcha img object
		img.src = "../includes/randomimg.php?sid="+now.getTime().toString();;//refresh image, use cache buster technique to force refresh
	}
	if(document.contact_form.sid){//update sid in hidden form variable sid to be passed on submission to identify the image session id to compare to user number input
		document.contact_form.sid.value = now.getTime().toString();
	}
}

function init(){
	clock_tick();
}
window.onload=init;
