var what = '';

function DoCallback(data, url, callback)
{
	if (window.XMLHttpRequest)  // branch for native XMLHttpRequest object
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		what = callback;
		req.open('POST', url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(data);
	}
	else if (window.ActiveXObject)  // branch for IE/Windows ActiveX version
	{
		req = new ActiveXObject('Microsoft.XMLHTTP')
		if (req) {
			req.onreadystatechange = processReqChange;
			what = callback;
			req.open('POST', url, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.send(data);
		}
	}
	else
	{
		alert('Your browser is not supported. Please upgrade to the newest version of your browser so that we can provide you with better service.');	
	}
}

function processReqChange() {
	// only if req shows 'loaded'
	if (req.readyState == 4) {
		// only if 'OK'
		if (req.status == 200)
			eval(what);
		else
			alert('There was a problem retrieving the XML data: ' +	req.responseText);
	}
}

function SendVote(user, id, vote)
{
	DoCallback("user=" + user + "&id=" + id + "&vote=" + vote, "/voting/server.php", "SetVote(req.responseText, '" + id + "', '" + vote + "')");
}

function SetVote(text, id, vote)
{
	var arr = text.split('|', 2);
	if(arr[0] == 'g')
	{
		if(vote == 1)
		{
			document.getElementById('cell' + id).style.backgroundImage = 'url(/voting/yes.png)';
		}
		else
		{
			if(vote == -1)
			{
				document.getElementById('cell' + id).style.backgroundImage = 'url(/voting/no.png)';
			}
		}
		document.getElementById('cellNum' + id).innerHTML = arr[1];

		document.getElementById('voteRecorded' + id).innerHTML = '<center>Your vote has been cast. Thank you.</center>';
		document.getElementById('voteRecorded' + id).className = 'voteGood';
		document.getElementById('voteRecorded' + id).style.display = '';
		setTimeout("closeBox('" + id + "')", 3000);
	}
	else
	{
		document.getElementById('voteRecorded' + id).innerHTML = arr[1];
		document.getElementById('voteRecorded' + id).className = 'voteError';
		document.getElementById('voteRecorded' + id).style.display = '';
		setTimeout("closeBox('" + id + "')", 8000);
	}
}

function closeBox(id)
{
	document.getElementById('voteRecorded' + id).style.display = 'none'	;
}