// JavaScript Document


//include("../js/utility.js");


/* ---------------------------- */
/* XMLHTTPRequest Enable
/* 
/* This is what allows asynchronous communication between client and server.
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();


/* Required: var nocache is a random number to add to request. This value solves an Internet Explorer cache issue */
var nocache = 0;



/* -------------------------- */
/* HELLOWORLD
/* -------------------------- */

function helloworld() {

	// Set the random number to add to URL request
	nocache = Math.random();

	// Pass the variables like URL variable
	http.open('get', 'helloworld.php?nocache='+nocache);
	http.onreadystatechange = insertReply;
	http.send(null);
}

function insertReply() {
	if (http.readyState == 4) {
		var response = http.responseText;
		// change the text in the source page to the "Hello World!" response
		document.getElementById('response').innerHTML = response;
	}
}

/* -------------------------- */
/* vote
/* -------------------------- */

/* When the user votes for a monster, enable the button for the winner, show win percentages,
   update the database, and allow them to play again.
*/

function vote(winner, winnerID, loserID) {
	
	if (has_voted) {
//		alert("Already voted!  Click PLAY AGAIN.");
		return;
	}
	has_voted = true;
	
	// Set the random number to add to URL request
	nocache = Math.random();

	// invoke the PHP code to do the work
	http.open('get', 'vote.php?winner=' + winner + '&winnerID=' + winnerID + '&loserID=' + loserID + '&nocache=' + nocache);
	http.onreadystatechange = voteHandler;
	http.send(null);
}

function voteHandler() {
	if (http.readyState == 4) {
		var response = eval('(' + http.responseText + ')');
		var changes = response.changes;
		for (c in changes) {
//			alert("In loop, changes[c].id = " + changes[c].id);
			document.getElementById( changes[c].id ).innerHTML = changes[c].code;
		}
	
//	alert("before toggle call");
	// reveal the "vote again" button
	toggleVisibility("PlayAgainButton");
	
	// switch "pick a winner arrows" to "play again" arrows and button
	document.getElementById('PickOrPlayAgainImg').style.display = "none";
	document.getElementById('PlayAgainArrows').style.display = "block";
	
		
		// debug printout
//	document.getElementById("Percentage1").innerHTML = "voteHandler ran!  changes[0] = " + changes[0];
	}
}


/* toggleVisibility -- change whether an item named "label" is visible or not */
function toggleVisibility(label)
{
	if (document.layers)
	{
//		alert("case 1");
		vista = (document.layers[label].visibility == 'hide') ? 'show' : 'hide'
		document.layers[label].visibility = vista;
	}
	else if (document.all)
	{
//		alert("case 2");
		vista = (document.all[label].style.visibility == 'hidden') ? 'visible'	: 'hidden';
		document.all[label].style.visibility = vista;
	}
	else if (document.getElementById)
	{
//		alert("case 3");
		vista = (document.getElementById(label).style.visibility == 'hidden') ? 'visible' : 'hidden';
		document.getElementById(label).style.visibility = vista;

	}
//	alert("after if statement");
}

