/**
 * Used to manage tags
*/

function VotingTool(handlerUrl) {
	this.url = handlerUrl;
}

VotingTool.prototype.post = function(url, parameterString, callback) {
    if (window.XMLHttpRequest) {
	   	xmlReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	//alert(url+"?"+parameterString);
	xmlReq.persistenceTool = this;
	xmlReq.open("POST",url,true); 
	xmlReq.onreadystatechange = callback;
	xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	xmlReq.send(parameterString);
}

VotingTool.prototype.completeVoting = function() {
    if (xmlReq.readyState == 4) {
        if (xmlReq.status == 200) {
		    var response = xmlReq.responseText;
		    var status = (response.split("\n")[0]);
		    if (status == "success" || status == "success") {
				alert(response.split('\n')[1]);
				//alert(response.split('\n')[3]);
				var count = parseInt( document.getElementById('total_votes_'+(response.split('\n')[3])).innerHTML );
				document.getElementById('total_votes_'+(response.split('\n')[3])).innerHTML = (count + 1);
			} else
				alert("Failed: "+response.split('\n')[1]);
		} else {
			alert("The website is having some problems ... the code returned is "+xmlReq.status);
		}
    } else {
		//alert("received file with status "+xmlReq.readyState);
    }
}

VotingTool.prototype.vote = function(submissionid) {
	var str = 'id='+submissionid;
	this.post(this.url, str, VotingTool.prototype.completeVoting);
}

