var addUmpireNextAction = "";
var deleteUmpireNextAction = "";
var editUmpireNextAction = "";
var addUmpirePopupCompId = -1;
var deleteUmpireCompId = -1;

function addUmpirePopup(compid) {
	addUmpirePopupCompId = compid;
	addUmpireNextAction = "listUmpiresForComp";
	sendAJAX("xml/getplayers.php", null, addUmpirePopup2);
}

function addUmpirePopup2(xmlDoc) {
	players = parseGetPlayersResponseToArray(xmlDoc.getElementsByTagName('player'));
	html = "<div class=\"popup\" id=\"addUmpirePopup\">\n";
	html += "	<div class=\"line1col1\">Add Umpire</div>\n";
	html += "	<div class=\"line2col1\"><span id=\"messagelabel\">Please choose an umpire</span></div>\n";
	html += "	<div class=\"line3col1\"><span id=\"umpirelabel\">Umpire:</span></div>\n";
	html += "	<div class=\"line3col2\"><select name=\"umpire\" id=\"umpire\">\n";
	for (a = 0; a < players.length; a++) {
		html += "<option value=\"" + players[a][0] + "\">" + players[a][1] + " " + players[a][2] + "</option>\n";
	}
	html += "   </select>\n";
	html += "   </div>\n";
	html += "	<div class=\"line7col1\">\n";
	html += "   	<span class=\"button\"><a href=\"javascript:addUmpireRequest(-1, " + addUmpirePopupCompId + ");\">Add</a></span>\n";
	html += "		<span class=\"button\"><a href=\"javascript:removeAddUmpirePopup();\">Cancel</a></span>\n";
	html += "	</div>\n";
	html += "</div>\n";
	document.getElementById('addUmpireHolder').innerHTML = html;
}

function removeAddUmpirePopup() {
	document.getElementById('addUmpireHolder').innerHTML = "";
}

function addUmpireRequest(id, compid) {
	if (id == -1) {
		id = document.getElementById('umpire').value;
	}
	addUmpirePopupCompId = compid;
	removeAddUmpirePopup();
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/addumpire.php";
		urlString += "?id=" + id;
		urlString += "&compid=" + compid;
		//alert(urlString);
		httpObject.open("GET",urlString, true);
		httpObject.send(null);
		httpObject.onreadystatechange = addUmpireResponse;
	}
}

function addUmpireResponse() {
	//alert("Starting setOutput - readyState is "+httpObject.readyState);
	if(httpObject.readyState == 4) {
		//show the results and format the page for another add
		//alert(httpObject.responseText);
		try { // Internet Explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(httpObject.responseText);
		} catch (e) {
			try { //Firefox Mozilla etc
				parser = new DOMParser();
				xmlDoc = parser.parseFromString(httpObject.responseText,"text/xml");
			} catch (e) {
				alert(e.message)
			}
		}
		status = xmlDoc.getElementsByTagName('response')[0].getAttribute('status');
		if (status == "0") {
			// need to do whatever we were supposed to be doing
			if (addUmpireNextAction == "listUmpiresForComp") {
				addumpireNextAction = "";
				getUmpiresForCompRequest(addUmpirePopupCompId);
			} else if (addUmpireNextAction == "listUmpiresForComp") {
				//alert("Need to show players again");
				addUmpireNextAction = "";
			}
		} else {
			// an error occurred
		}
	}
}

function getUmpiresForCompRequest(compid) {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/getumpiresforcomp.php?compid=" + compid;
		httpObject.open("GET",urlString, true);
		httpObject.onreadystatechange = getUmpiresForCompResponse;
		httpObject.send(null);
	}
}

// Only used in the results screen, as it will call the get stand-ins AJAX as well
function getUmpiresForCompResponse() {
	//alert("Build player array - readyState is "+httpObject.readyState);
	if(httpObject.readyState == 4) {
		//show the results and format the page for another add
		//alert(httpObject.responseText);
		try { // Internet Explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(httpObject.responseText);
		} catch (e) {
			try { //Firefox Mozilla etc
				parser = new DOMParser();
				xmlDoc = parser.parseFromString(httpObject.responseText,"text/xml");
			} catch (e) {
				alert(e.message)
			}
		}
		status = xmlDoc.getElementsByTagName('response')[0].getAttribute('status');
		if (status == "0") {
			compId = xmlDoc.getElementsByTagName('comp')[0].getAttribute('id');
			umpires = xmlDoc.getElementsByTagName('umpire');
			//alert("players.length = " + players.length);
			umpireList = new Array();
			for (a = 0; a < umpires.length; a++) {
				umpireList[a] = new Array();
				umpireList[a]["id"] = umpires[a].getElementsByTagName('id')[0].childNodes[0].nodeValue;
				umpireList[a]["fname"] = umpires[a].getElementsByTagName('firstName')[0].childNodes[0].nodeValue;
				umpireList[a]["lname"] = umpires[a].getElementsByTagName('lastName')[0].childNodes[0].nodeValue;
				umpireList[a]["umpireId"] = umpires[a].getElementsByTagName('umpireId')[0].childNodes[0].nodeValue;
			}
			showUmpireList(umpireList);
		} else if (status == "2") {
			compId = xmlDoc.getElementsByTagName('comp')[0].getAttribute('id');
			// no umpires were found
			umpireList = new Array();
			showUmpireList(umpireList);
		} else {
			compId = xmlDoc.getElementsByTagName('comp')[0].getAttribute('id');
			//we have an error here
			messages = xmlDoc.getElementsByTagName('errorMessage');
			message = messages[0].childNodes[0].nodeValue;
			alert(message);
		}
		getStandInsForCompRequest(compId);
	}
}

function getUmpiresForPlayerRequest(id) {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/getumpiresforplayer.php?id=" + id;
		httpObject.open("GET",urlString, true);
		httpObject.onreadystatechange = getUmpiresForCompResponse;
		httpObject.send(null);
	}
}

function getUmpiresForPlayerResponse() {
	//alert("Build player array - readyState is "+httpObject.readyState);
	if(httpObject.readyState == 4) {
		//show the results and format the page for another add
		//alert(httpObject.responseText);
		try { // Internet Explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(httpObject.responseText);
		} catch (e) {
			try { //Firefox Mozilla etc
				parser = new DOMParser();
				xmlDoc = parser.parseFromString(httpObject.responseText,"text/xml");
			} catch (e) {
				alert(e.message)
			}
		}
		status = xmlDoc.getElementsByTagName('response')[0].getAttribute('status');
		if (status == "0") {
			players = xmlDoc.getElementsByTagName('player');
			//alert("players.length = " + players.length);
			if (getPlayersNextAction == "updatePlayerIdSelect") {
				getPlayersNextAction == "";
				html = "";
				for (a = 0; a < players.length; a++) {
					player = players[a];
					html += "<option value=\"" + player.getElementsByTagName('id')[0].childNodes[0].nodeValue + "\">" + player.getElementsByTagName('firstName')[0].childNodes[0].nodeValue + " " + player.getElementsByTagName('lastName')[0].childNodes[0].nodeValue + "</option>\n";
				}
				document.getElementById('playerid').innerHTML = html;
			} else if (getPlayersNextAction == "updatePlayersPage") {
				getPlayersNextAction = "";
				allPlayersArray = new Array(players.length);
				for (a = 0; a < players.length; a++) {
				//alert("Iteration " + a);
					player = players[a];
					try {
						allPlayersArray[a] = new Array(10);
						allPlayersArray[a][0] = player.getElementsByTagName('id')[0].childNodes[0].nodeValue;
						allPlayersArray[a][1] = player.getElementsByTagName('firstName')[0].childNodes[0].nodeValue;
						allPlayersArray[a][2] = player.getElementsByTagName('lastName')[0].childNodes[0].nodeValue;
						allPlayersArray[a][3] = player.getElementsByTagName('IWFID')[0].childNodes[0].nodeValue;
						allPlayersArray[a][4] = player.getElementsByTagName('numComps')[0].childNodes[0].nodeValue;
						allPlayersArray[a][5] = player.getElementsByTagName('bestPlace')[0].childNodes[0].nodeValue;
						allPlayersArray[a][6] = player.getElementsByTagName('worstPlace')[0].childNodes[0].nodeValue;
						allPlayersArray[a][7] = player.getElementsByTagName('rarName')[0].childNodes[0].nodeValue;
						allPlayersArray[a][9] = player.getElementsByTagName('armies')[0].childNodes[0].nodeValue;
					} catch (e) {
						alert (e.message);
					}
				}
				buildPlayerList(allPlayersArray);
			} else {
				getPlayersNextAction == "";
				for (a = 0; a < players.length; a++) {
				//alert("Iteration " + a);
					player = players[a];
					try {
						allPlayersArray[a] = new Array(2);
						allPlayersArray[a][0] = player.getElementsByTagName('id')[0].childNodes[0].nodeValue;
						allPlayersArray[a][1] = player.getElementsByTagName('firstName')[0].childNodes[0].nodeValue + " " + player.getElementsByTagName('lastName')[0].childNodes[0].nodeValue;
					} catch (e) {
						alert (e.message);
					}
				}
			}
			//alert("players.length = " + players.length);
			//alert(allPlayersArray.length);
		} else {
			//we have an error here
			messages = xmlDoc.getElementsByTagName('errorMessage');
			message = messages[0].childNodes[0].nodeValue;
			alert(message);
		}
	}
}

function deleteUmpireForComp(id, compid) {
	deleteUmpireNextAction = "showUmpiresByComp";
	deleteUmpireCompId = compid;
	deleteUmpireRequest(id);
}

function deleteUmpireRequest(id) {
	//alert("Starting deletePlayerRequest()");
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/deleteumpire.php";
		urlString += "?id=" + id;
		//alert(urlString);
		httpObject.open("GET",urlString, true);
		httpObject.send(null);
		httpObject.onreadystatechange = deleteUmpireResponse;
	}
}

function deleteUmpireResponse() {
	//alert("Starting setOutput - readyState is "+httpObject.readyState);
	if(httpObject.readyState == 4) {
		//show the results and format the page for another add
		//alert(httpObject.responseText);
		try { // Internet Explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(httpObject.responseText);
		} catch (e) {
			try { //Firefox Mozilla etc
				parser = new DOMParser();
				xmlDoc = parser.parseFromString(httpObject.responseText,"text/xml");
			} catch (e) {
				alert(e.message)
			}
		}
		//id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		//document.getElementById('statusMessage').innerHTML = httpObject.responseText;
		// need to do whatever we were supposed to be doing
		if (deleteUmpireNextAction == "showUmpiresByComp") {
			deleteUmpireNextAction = "";
			getUmpiresForCompRequest(deleteUmpireCompId);
		}
	}
}

