// CPATCHA LOCATION

var captcha_location="http://artapress.gr/scripts/captcha";

// 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_location+'/image_req.php';
	dorefresh(url, displayimg);
        document.getElementById('captcha').value="";
}

function dorefresh(url, callback)
{
	sess.open('POST', captcha_location+'/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(formname)
{
        var validation_result=validate(formname);
        if (validation_result!=false) {
	var submission = document.getElementById('captcha').value;
	var url = captcha_location+'/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.comments_form.method="post";
                        document.comments_form.action="../scripts/submit.php";
                        document.comments_form.submit();
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.background = '#FF4F4F';
                        document.getElementById('captcha').value = 'Λανθασμένος Κωδικός';

		}
	}
}