var xmlHttp;
var xmlHttpCalc;


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function do_update(poll_id, force_show)
{
	var strShow;
	if (force_show == true)
		strShow = "&show=1";
	else
		strShow = "";
	xmlHttp = XmlHttpQuery("/inc/poll.php?poll_id=" + poll_id + strShow, null, update_view);
}

function update_view()
{
	if (XmlHttpIsStateReady(xmlHttp))
		XmlHttpSetRegion("view", XmlHttpGetResponseText(xmlHttp));
}

function post_data()
{
	var comment;
	var answer;
	var poll;
	comment = document.getElementById("comment").value;
	answer = getCheckedValue(document.forms['radio_answer'].elements['answer']);
	poll = document.getElementById("poll").value;
	xmlHttp = XmlHttpPost("/inc/poll.php", "poll="+poll+"&comment="+comment+"&answer="+answer, update_view);	
}


/* FIXME: move me */
function do_calc()
{
	var score = document.getElementById("score").value;
	var players = document.getElementById("players").value;
	var place = document.getElementById("place").value;
	
	document.getElementById("calc_view").innerHTML = "<p>Loading...</p>";
	
	xmlHttpCalc = XmlHttpQuery("/inc/scorecalc.php?score=" + score + "&players=" + players + "&place=" + place, null, update_calc_view);
}

function update_calc_view()
{
	if (XmlHttpIsStateReady(xmlHttpCalc))
		XmlHttpSetRegion("calc_view", XmlHttpGetResponseText(xmlHttpCalc));
}
