
var url = '_captcheck.php?code=';
var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed

function getHTTPObject(){
	try {
		req = new XMLHttpRequest();
	} 
	catch (err1){
		try {
			req = new ActiveXObject("Msxml12.XMLHTTP");
		} 
		catch (err2){
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (err3){
				req = false;
			}
		}
	}
	return req; 
}

var http = getHTTPObject(); // We create the HTTP Object        

function handleHttpResponse() {
	if (http.readyState == 4) {
		captchaOK = http.responseText;
		if(captchaOK != 1) {
			alert('The validation code was not correct. Please try again');
			document.myform.code.value='';
			document.myform.code.focus(); 
			return false;
		}
		document.myform.checkcode.value=http.responseText
		document.myform.submit();
	} 
}

function checkcode(thecode) {
	http.open("GET", url + escape(thecode), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

function checkform() {
	if(document.myform.firstname.value=='') {
		alert('Please enter your firstname');
		document.myform.firstname.focus();
		return false;
	}
	if(document.myform.lastname.value=='') {
		alert('Please enter your lastname');
		document.myform.lastname.focus();
		return false;
	}
	if(document.myform.email.value=='') {
		alert('Please enter your email');
		document.myform.email.focus();
		return false;
	}
	var emailPat = /^\w+([\'\.\-]\w+)*\@\w+([\'\.\-]\w+)*\.[a-z]{2,4}$/i;
	var matchArray = document.myform.email.value.match(emailPat);
	if (document.myform.email.value.length > 1) {
		if (matchArray == null) {
			alert('Please enter a valid email');
			document.myform.email.focus(); 
			return false;
		}
	}	
	if(document.myform.comment.value.length > 2000) {
		alert('Please limit your comment to 2000 characters');
		document.myform.comment.focus();
		return false;
	}
	checkcode(document.myform.code.value);
	return false;
}      
