
// CREATING THE REQUEST
var root = 'http://www.afla.ro/captchalist/';
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 http2 = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg(params, vote)
{
	var url = root + 'image_req.php';
	dorefresh(url, params, vote, displayimg);
	
}

function dorefresh(url, params, vote, callback)
{
	sess.open('POST', root + 'newsession.php?article_id='+params+'&vote='+vote, true);
	sess.send(null);
	http.open('POST', url+'?article_id='+params+'&vote='+vote, true);
	http.onreadystatechange = function() {
		if(http.readyState == 4)
		{
			displayimg(params, vote);
		}
	};
	http.send(null);
}

function displayimg(params, vote)
{
		var showimage = http.responseText;
		var divcaptcha = 'captchaimage' + params;
		var divtext	   = 'captchatext' + params;
		var dividcaptcha = 'captcha' + params;
		var mana;
		mana = document.getElementById('mana' + params);

		if (vote < 0)
		{
			  mana.setAttribute("src", '/images/icon_nu_big.gif');  
		} else {
			mana.setAttribute("src", '/images/icon_da_big.gif');  
		}
		document.getElementById('vote' + params).value = vote;
		document.getElementById('q' + params).style.display = "block";
		document.getElementById(divcaptcha).innerHTML = showimage;
		document.getElementById(divcaptcha).style.display = "inline";
		document.getElementById(dividcaptcha).value = '';
		document.getElementById(divtext).style.display = "inline";
		
}

// SUBMISSION
function check(params, vote)
{
	var divcaptcha	   = 'captcha' + params;
	var submission = document.getElementById(divcaptcha).value;
	var url = root + 'process.php?captcha=' + submission + '&article_id=' + params;
	docheck(url, params, vote, submission, displaycheck);
}

function docheck(url, params, vote, submission, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = function () {
		if(http.readyState == 4) 
		{
			displaycheck(params, vote, submission);
		}
	}
	http.send(null);
}

function displaycheck(params, vote, submission)
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1')
		{
			document.getElementById('q' + params).style.display = 'none';	
			document.getElementById('captchaimage'+params).style.display = 'none';
			document.getElementById('captchatext'+params).style.display = 'none';
			for (var i=0; i<document.forms.length; i++)
			{
			
				if (document.forms[i].name == 'siteform'+params)
				{
					setVotes(params, vote, submission);
				}
			}
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha'+params).style.border = '1px solid #c24949';
			document.getElementById('captcha'+params).style.background = '#ffbcbc';
			document.getElementById('score'+params).innerHTML = "Codul de validare este incorect introdus!";
			//alert('Codul de validare este incorect introdus!');
		}
	}
}

function setVotes(params, vote, submission) {
	
	http2.open('GET', './?m=ajax&o=top&article_id='+params+'&vote='+vote+'&captcha='+submission, true);
	
	http2.onreadystatechange = function() {
		
		if(http2.readyState == 4)
		{
			 if (http2.status == 200) {
				
				displayVotes(params);
			 }
		}		
	}
	http2.send(null);
	
}

function displayVotes(params) {
	
	var showVotes = http2.responseText;
	document.getElementById('score'+params).innerHTML = showVotes;
	
}