// CREATING THE REQUEST
function createRequestObject(){
	try	{
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}	catch(e)	{
		alert('Sorry, but your browser doesnt support XMLHttpRequest.');
	}
	return xmlhttp;
}

var http = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg(){
	var url = 'image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback){
	sess.open('POST', '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 = 'process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback){
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck(){
	if(http.readyState == 4)	{
		var showcheck = http.responseText;
		if(showcheck == '1')		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
		}
		if(showcheck == '0')		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';

		}
	}
}


function checkfinish(){
	var all = 0;
	if( document.getElementByName('captcha').style.background != '#ffbcbc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[0]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[1]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[2]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[3]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[4]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[5]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[6]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end
	if( document.getElementByName('log[7]').style.background != '#fcc'){
		all = 1;
	}else{// if end
		all = 0;
	}// else end

	// vége
	if( all = 1){
		document.regform.submit();
	}// if all okéj

}// fn end

