var addStandInNextAction = "";
var deleteStandInNextAction = "";
var editStandInNextAction = "";
var addStandInPopupCompId = -1;
var deleteStandInCompId = -1;

function addStandInPopup(compid) {
	addStandInPopupCompId = compid;
	addStandInNextAction = "listStandInsForComp";
	sendAJAX("xml/getplayers.php", null, addStandInPopup2);
}

function addStandInPopup2(xmlDoc) {
	players = parseGetPlayersResponseToArray(xmlDoc.getElementsByTagName('player'));
	html = "<div class=\"popup\" id=\"addStandInPopup\">\n";
	html += "	<div class=\"line1col1\">Add Stand In</div>\n";
	html += "	<div class=\"line2col1\"><span id=\"messagelabel\">Please choose a stand in</span></div>\n";
	html += "	<div class=\"line3col1\"><span id=\"StandInlabel\">Stand In:</span></div>\n";
	html += "	<div class=\"line3col2\"><select name=\"StandIn\" id=\"StandIn\">\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=\"line5col1\"><span id=\"StandForlabel\">Stand In For:</span></div>\n";
	html += "	<div class=\"line5col2\"><select name=\"StandFor\" id=\"StandFor\">\n";
	for (a = 0; a < competitorList.length; a++) {
		html += "<option value=\"" + competitorList[a][0] + "\">" + competitorList[a][1] + " " + competitorList[a][2] + "</option>\n";
	}
	html += "   </select>\n";
	html += "   </div>\n";
	html += "	<div class=\"line7col1\">\n";
	html += "   	<span class=\"button\"><a href=\"javascript:addStandInRequest(-1, " + addStandInPopupCompId + ");\">Add</a></span>\n";
	html += "		<span class=\"button\"><a href=\"javascript:removeAddStandInPopup();\">Cancel</a></span>\n";
	html += "	</div>\n";
	html += "</div>\n";
	document.getElementById('addStandInHolder').innerHTML = html;
}

function removeAddStandInPopup() {
	document.getElementById('addStandInHolder').innerHTML = "";
}

function addStandInRequest(id, compid) {
	if (id == -1) {
		id = document.getElementById('StandIn').value;
	}
	infor = document.getElementById('StandFor').value;
	addStandInPopupCompId = compid;
	addStandInNextAction = "listStandInsForComp";
	removeAddStandInPopup();
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/addstandin.php";
		urlString += "?id=" + id;
		urlString += "&compid=" + compid;
		urlString += "&infor=" + infor;
		//alert(urlString);
		httpObject.open("GET",urlString, true);
		httpObject.send(null);
		httpObject.onreadystatechange = addStandInResponse;
	}
}

function addStandInResponse() {
	//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 (addStandInNextAction == "listStandInsForComp") {
				addStandInNextAction = "";
				getStandInsForCompRequest(addStandInPopupCompId);
			}
		} else {
			// an error occurred
		}
	}
}

function getStandInsForCompRequest(compid) {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/getstandinsforcomp.php?compid=" + compid;
		//alert(urlString);
		httpObject.open("GET",urlString, true);
		httpObject.onreadystatechange = getStandInsForCompResponse;
		httpObject.send(null);
	}
}

function getStandInsForCompResponse() {
	//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") {
			standIns = xmlDoc.getElementsByTagName('standin');
			//alert("standIns.length = " + standIns.length);
			standInList = new Array();
			for (a = 0; a < standIns.length; a++) {
				standInList[a] = new Array();
				standInList[a]["id"] = standIns[a].getElementsByTagName('id')[0].childNodes[0].nodeValue;
				standInList[a]["fname"] = standIns[a].getElementsByTagName('firstName')[0].childNodes[0].nodeValue;
				standInList[a]["lname"] = standIns[a].getElementsByTagName('lastName')[0].childNodes[0].nodeValue;
				standInList[a]["standInId"] = standIns[a].getElementsByTagName('standInId')[0].childNodes[0].nodeValue;
				standInList[a]["standInForFname"] = standIns[a].getElementsByTagName('standInForFirstName')[0].childNodes[0].nodeValue;
				standInList[a]["standInForLname"] = standIns[a].getElementsByTagName('standInForLastName')[0].childNodes[0].nodeValue;
				standInList[a]["standInForId"] = standIns[a].getElementsByTagName('standInForId')[0].childNodes[0].nodeValue;
			}
			showStandInList(standInList);
		} else if (status == "2") {
			//alert("no StandIns were found");
			standInList = new Array();
			showStandInList(standInList);
		} else {
			//we have an error here
			messages = xmlDoc.getElementsByTagName('errorMessage');
			message = messages[0].childNodes[0].nodeValue;
			alert("Error: "+message);
		}
	}
}
function getStandInsForPlayerRequest(id) {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		urlString = "xml/getstandinsforplayer.php?id=" + id;
		httpObject.open("GET",urlString, true);
		httpObject.onreadystatechange = getStandInsForCompResponse;
		httpObject.send(null);
	}
}

function getStandInsForPlayerResponse() {
	//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 deleteStandInForComp(id, compid) {
	deleteStandInNextAction = "showStandInsByComp";
	deleteStandInCompId = compid;
	deleteStandInRequest(id);
}

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

function deleteStandInResponse() {
	//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 (deleteStandInNextAction == "showStandInsByComp") {
			deleteStandInNextAction = "";
			getStandInsForCompRequest(deleteStandInCompId);
		}
	}
}

