// 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 = 'http://factory.ldf.ru/typo3conf/ext/buyout/pi1/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback)
{
	sess.open('POST', 'http://factory.ldf.ru/typo3conf/ext/buyout/pi1/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 = 'http://factory.ldf.ru/typo3conf/ext/buyout/pi1/process1.php?captcha=' + submission;
	docheck(url, displaycheck);
	
	return true;
	
	alert(document.getElementById('captcha').getAttributeNode('title').nodeValue);
	
	
//	if(tr=='1'){
//		alert(tr);
//		return true;
//	}else{
//		return false;
//	}
}

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;
		var newattr = document.createAttribute("title");
		if(showcheck == '1')
		{	
			newattr.nodeValue = "1";
			document.getElementById('captcha').setAttributeNode(newattr);
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
		}
		if(showcheck == '0')
		{
			newattr.nodeValue = "0";
			document.getElementById('captcha').setAttributeNode(newattr);
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
		}
	}
	
}