var http;
var twhid_vote;
var mriver_vote;
var twhid_vote_str;
var mriver_vote_str;
var votes;
var mr_count;
var tw_count;
var mr_actual_count;
var tw_actual_count;
var expired = 0;

function sendVote(id,round) {
	http = false;
	var url = '/kdm100/vote/?id='+id+"&round="+round;
	//alert(url);

	if (window.XMLHttpRequest) { // Mozilla, Safari, etc
		http = new XMLHttpRequest();
		if (http.overrideMimeType) {
			http.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // MSIE
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http) {
		var errorStr 
			= "Your browser isn't supported for voting.\n\nTry using Internet "
			+ "Explorer 6, Firefox, Opera 8+ or Safari 1.3.1/2.0.1."
		alert(errorStr);
		return false;
	}
	
	http.onreadystatechange = handleResponse;
	http.open('GET', url, true);
	http.send(null);
}

function handleResponse() {
	displayMessage("wait");
	if ( http.readyState == 4 ) {
		if ( http.status == 200 ) {
			var response = http.responseText.split(",");
			mr_count = response[1];
			tw_count = response[2];
			displayMessage(response[0]);
		} else {
			alert('There was a problem with the vote. You can try again');
			displayMessage("reset");
		}
	}
}

function displayMessage(kind) {
	if ( mr_count != null || mr_count != undefined ) { // from server
		mr_actual_count = mr_count;
	}
	if ( mr_actual_count != null ) {
		var mrv = mr_actual_count + " votes";
		if (mr_actual_count == "1") mrv = mrv.substring(0,mrv.length-1);
	} 
	
	if ( tw_count != null || tw_count != undefined ) { // from server
		tw_actual_count = tw_count;
	}
	if ( tw_actual_count != null ) {
		var twv = tw_actual_count + " votes";
		if ( tw_actual_count == "1" ) twv = twv.substring(0,twv.length-1);
	}
	
	if ( document.getElementById ) {
		switch(kind) {
			case "wait":
				for (i=0;i<votes.length;i++) {
					votes[i].className = "register_state";
					votes[i].innerHTML = "registering vote&hellip;";
					votes[i].onclick = null;
				}
				break;
			case "mriver_vote":
				for ( i=0; i<votes.length; i++ ) {
					votes[i].onclick = null;
				}
				mriver_vote.className = "vote_complete";
				mriver_vote.innerHTML = "You voted for M.River!";
				if (mr_actual_count != null ) {
					mriver_vote.innerHTML += "<br />He has " + mrv + " for this round.";
				}
				twhid_vote.className = "register_state";
				twhid_vote.innerHTML = "You think T.Whid is a loser!";
				if (tw_actual_count != null ) {
					twhid_vote.innerHTML += "<br />He has " + twv + " for this round.";
				}
				break;
			case "twhid_vote":
				for (i=0;i<votes.length;i++) {
					votes[i].onclick = null;
				}
				mriver_vote.className = "register_state";
				twhid_vote.className = "vote_complete";
				twhid_vote.innerHTML = "You voted for T.Whid!";
				if ( tw_actual_count != null ) {
					twhid_vote.innerHTML += "<br />He has " + twv + " for this round.";
				}
				mriver_vote.innerHTML = "You think M.River is a loser!";
				if ( mr_actual_count != null ) {
					mriver_vote.innerHTML += "<br />He has " + mrv + " for this round.";
				}
				break;
			case "error":
				alert('There was a problem with the vote. You can try again');
				mriver_vote.className = "vote_action";
				mriver_vote.innerHTML = mriver_vote_str;
				twhid_vote.className = "vote_action";
				twhid_vote.innerHTML = twhid_vote_str;
				break;
			case "reset":
				mriver_vote.className = "vote_action";
				mriver_vote.innerHTML = mriver_vote_str;
				twhid_vote.className = "vote_action";
				twhid_vote.innerHTML = twhid_vote_str;
				break;
		}
	}
}

function displayExpired(mvote,tvote) {
	tvote = parseInt(tvote);
	mvote = parseInt(mvote);
	
	var mrv = mvote + " votes";
	if (mr_actual_count == "1") mrv = mrv.substring(0,mrv.length-1);
	var twv = tvote + " votes";
	if ( tw_actual_count == "1" ) twv = twv.substring(0,twv.length-1);
	
	for ( i=0; i<votes.length; i++ ) {
		votes[i].onclick = null; // killing onclick
	}
	
	// display results for expired round
	if ( tvote > mvote ) { 
		// twhid won
		twhid_vote.className = "vote_complete";	
		mriver_vote.className = "register_state";
		twhid_vote.innerHTML = "T.Whid rules! He won this round with " + twv + "!";
		mriver_vote.innerHTML = "M.River sucks! He lost this round with " + mrv + ".";
	} else if ( tvote < mvote ) { 
		// mriver won
		mriver_vote.className = "vote_complete";	
		twhid_vote.className = "register_state";
		twhid_vote.innerHTML = "T.Whid sucks! He lost this round with " + twv + ".";
		mriver_vote.innerHTML = "M.River rules! He won this round with " + mrv + "!";
	} else { 
		// tie
		mriver_vote.className = "vote_complete";	
		twhid_vote.className = "vote_complete";
		twhid_vote.innerHTML = "Argh! It's a tie with " + twv + "!";
		mriver_vote.innerHTML = "Argh! It's a tie with " + mrv + "!";
	}
}