var candidates;

function init()
{
	// Disable autocomplete on form
	document.voteForm.setAttribute('autocomplete', 'off');

	// Check if the voting button should be enabled
	if(isVoteComplete()) {
		document.getElementById('submitNotice').innerHTML = "";
		document.getElementById('submit').disabled = false;
		document.getElementById('submit').className = "voteButton";
	}
}

function loadCandidates(categoryID)
{
	var parameters = "categoryID="+categoryID;
	var ajax = new AjaxCall("getCandidatesJSON.php", parameters, loadCandidatesCallback, categoryID);
	ajax.doGET();
}
function loadCandidatesCallback(json, readyState, categoryID)
{
	if(readyState == 4) {
		if(json == -1) return;	// No procesar si no hubieron resultados

		var curCandidate = document.getElementById('vote_cat_'+categoryID).value;

		candidates = eval(json);
		var html = "<ul>";
		html += "<span id='rubro'>Rubro: </span><span id='rubroCat'>"+document.getElementById('category_'+categoryID).innerHTML+"</span>";
		for(var i in candidates) {
			html += "<div class='dropshadow'>"+
			        "	<li class='candidateItem' onmouseover='showCandidateInfo("+i+");'>"+
					"		<img class='portrait' src='portraits/"+candidates[i].portrait+"'>"+
					"		<span>"+candidates[i].name+"</span>"+
					"		<p>"+candidates[i].abstract+"</p>";
					if(candidates[i].id == curCandidate) html += "<img id='votedMarker' src='img/checkmark.gif'>";
			html +=	"		<input class='button' type='button' value='ELEGIR' onclick='selectCandidate("+categoryID+", "+i+");'>"+
					"	</li>"+
					"</div>";
		}
		html += "</ul>";
		document.getElementById('candidateList').innerHTML = html;
		document.getElementById('candidateInfo').innerHTML = "";
	}
}

function showCandidateInfo(id)
{
	var html = "<h2>"+candidates[id].name+"</h2>";
	html 	+= "<p>"+candidates[id].bio+"</p>";

	document.getElementById('candidateInfo').innerHTML = html;	
}

function selectCandidate(categoryID, i)
{
	// Set the appropiate hidden fields in the form
	document.getElementById("vote_cat_"+categoryID).value = candidates[i].id;
	var html =  "<img src='portraits/"+candidates[i].portrait+"'>"+
				"<span>"+candidates[i].name+"</span>"+
				"<p>"+candidates[i].abstract+"</p>";
	document.getElementById("voted_candidate_"+categoryID).innerHTML = html;
	
	// Try to save this selection to database. Ignore errors, since this is not critical
	var parameters = "categoryID="+categoryID+"&candidateID="+candidates[i].id;
	var ajax = new AjaxCall("castTempVote.php", parameters, null, null);
	ajax.doGET();
	
	// Check if the voting button should be enabled
	if(isVoteComplete()) {
		document.getElementById('submitNotice').innerHTML = "";
		document.getElementById('submit').disabled = false;
		document.getElementById('submit').className = "voteButton";
	}
	
	loadCandidates(categoryID);
}

function isVoteComplete()
{
	var hidden;
	var i = 1;
	while((hidden = document.getElementById('vote_cat_'+i)) != null) {
		if(hidden.value != "") return true;	// At least one category has been voted on
		i++;
	}
	// No categories voted
	return false;
}
