// 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(type)
{
	var url = jQuery("#root_path").val()+'library/captcha/'+'image_req.php';
	jQuery.post(url, 
 					{  }, 
     				function(data){
						if (type == '1') {
							document.getElementById('captchaimage').innerHTML = data;
						} else if (type == '2') {
							document.getElementById('homecaptcha').innerHTML = data;
						}
     				});
}

function dorefresh(url, callback, type)
{
	sess.open('POST', jQuery("#root_path").val()+'library/captcha/'+'newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = displayimg(type);
	http.send(null);
}

function displayimg(type)
{
	if(http.readyState == 4)
	{
		var showimage = http.responseText;
		if (type == '1') {
			document.getElementById('captchaimage').innerHTML = showimage;
		} else if (type == '2') {
			document.getElementById('homecaptcha').innerHTML = showimage;
		}
	}
}

// SUBMISSION

function check()
{
	var submission = document.getElementById('captcha').value;
	var url = jQuery("#root_path").val()+'library/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()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1')
		{
			return '1';
		}
	}
	return '0';
}
