// CREATING THE REQUEST

function createRequestObject() {
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
	}
	return xmlhttp;
}

var http = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg() {
	var url = 'captcha/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback) {
	sess.open('POST', 'captcha/newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = displayimg;
	http.send(null);
}

function displayimg() {
	if(http.readyState == 4) {
		var showimage = http.responseText;
		document.getElementById('captchaimage').innerHTML = showimage;
	}
}

// SUBMISSION

function check() {
	var submission = document.getElementById('captcha').value;
	var url = 'captcha/process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback) {
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck() {
	document.canSubmite = false;
	if(http.readyState == 4) {
		var showcheck = http.responseText;
		if(showcheck == '1') {
			document.getElementById('captcha').style.border = '1px solid #49c249';
			document.getElementById('captcha').style.background = '#bcffbc';
			submite();
		}
		if(showcheck == '0') {
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
		}
	}
}

function submite() {
	var formSubmitCaptcha = "";
	if (tabNameCaptcha.length>0) {
		
		formSubmitCaptcha = "<form name=\"submitCaptcha\" action=\""+pageNameCaptcha+"\" method=\""+methodeCaptcha+"\">";
		for (i=0;i<tabNameCaptcha.length;i++) {		
			var val=document.getElementById(tabNameCaptcha[i]).value;
			var reg=new RegExp("(')", "gi");
			val=val.replace(reg,"\\'");
			formSubmitCaptcha = formSubmitCaptcha + "<input type=\"hidden\" name=\""+tabNameCaptcha[i]+"\" value=\""+val+"\" />";
		}
		formSubmitCaptcha = formSubmitCaptcha + "</form>";
		document.getElementById('divSubmitCaptcha').innerHTML = formSubmitCaptcha;
		var test= document.getElementById('divSubmitCaptcha').innerHTML;
		
		document.submitCaptcha.submit();
	} 
}