var http = new XMLHttpRequest();

var is_ok = 'tbd';
var the_form;
var form_wait;

function handleHttpResponse_captcha() {
	if (http.readyState == 4) {
		is_ok = http.responseText;
		//alert(is_ok);
	}
}

function check_captcha(form){
	the_form = form;
	input = form['vercode'].value;
	var url = "captcha/captcha_ajax.php";
	var inputValue = "input=" + input;
	http.open("POST", url, true);
	http.onreadystatechange = handleHttpResponse_captcha;

	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", inputValue.length);
	http.setRequestHeader("Connection", "close");

	http.send(inputValue);
}

function submit_if_ok(){
	if (is_ok == 'good'){
		clearInterval(form_wait);
		the_form.submit();
	}
	if (is_ok == 'bad'){
		clearInterval(form_wait);
		the_form['vercode'].value = '';
		the_form['vercode'].focus();
		the_form['vercode'].style.backgroundColor = 'orange';
		document.getElementById('imgCaptcha').src = 'captcha/captcha.php?' + Math.random();
		alert('Image verification failed. Please try again');
	}
	//or do it again if is_ok is still 'tbd'
}
