var xmlHttp = createXmlHttpRequestObject();

function getNextWR1(ownerID)  {
	if (document.picklineup.wr1.options[document.picklineup.wr1.selectedIndex].value == "x")  {
		clearSelectWR1();
	} else {
		processWR1(ownerID);
	}
}

function getNextWR2(ownerID)  {
	if (document.picklineup.wr2.options[document.picklineup.wr2.selectedIndex].value == "x")  {
		clearSelectWR2();
	} else {
		processWR2(ownerID);
	}
}


function processWR2(ownerID)  {
	if (xmlHttp)  {
		try  {
			var player1 = document.picklineup.wr1.options[document.picklineup.wr1.selectedIndex].value;
			var player2 = document.picklineup.wr2.options[document.picklineup.wr2.selectedIndex].value;
			xmlHttp.open("GET","getWR.php?ownerID="+ownerID+"&player1="+player1+"&player2="+player2,true);
			xmlHttp.onreadystatechange = handleRequestStateChangeWR2;
			xmlHttp.send(null);
		}
		catch (e)  {
			alert("Can't connect to the server: \n" + e.toString());
		}
	}
}

function processWR1(ownerID)  {
	if (xmlHttp)  {
		try  {
			var player1 = document.picklineup.wr1.options[document.picklineup.wr1.selectedIndex].value;
			xmlHttp.open("GET","getWR.php?ownerID="+ownerID+"&player1="+player1+"&player2=",true);
			xmlHttp.onreadystatechange = handleRequestStateChangeWR1;
			xmlHttp.send(null);
		}
		catch (e)  {
			alert("Can't connect to the server: \n" + e.toString());
		}
	}
}

function handleRequestStateChangeWR1()  {
	if (xmlHttp.readyState == 4)  {
		if (xmlHttp.status == 200)  {
			try  {
				handleServerResponseWR1();
			}
			catch (e)  {
				alert("Error reading the response: " + e.toString());
			}
		} else {
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleRequestStateChangeWR2()  {
	if (xmlHttp.readyState == 4)  {
		if (xmlHttp.status == 200)  {
			try  {
				handleServerResponseWR2();
			}
			catch (e)  {
				alert("Error reading the response: " + e.toString());
			}
		} else {
			alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponseWR1()  {
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)  {
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")  {
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	}
	xmlRoot = xmlResponse.documentElement;
	valueArray = xmlRoot.getElementsByTagName("value");
	var html = "";
	clearSelectWR1();
	document.getElementById("wr2").options[document.getElementById("wr2").options.length] = new Option("Pick...","x");
	for (var x=0;x<valueArray.length;x++)  {
		var thisOption=new Array();
		thisOption["name"] = valueArray.item(x).firstChild.data;
		x++;
		thisOption["id"] = valueArray.item(x).firstChild.data;
		document.getElementById("wr2").options[document.getElementById("wr2").options.length] = new Option(thisOption["name"],thisOption["id"]);
	}
}

function handleServerResponseWR2()  {
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)  {
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")  {
		throw("Invalid XML structure:\n" + xmlHttp.responseText);
	}
	xmlRoot = xmlResponse.documentElement;
	valueArray = xmlRoot.getElementsByTagName("value");
	var html = "";
	clearSelectWR2();
	document.getElementById("wr3").options[document.getElementById("wr3").options.length] = new Option("Pick...","x");
	for (var x=0;x<valueArray.length;x++)  {
		var thisOption=new Array();
		thisOption["name"] = valueArray.item(x).firstChild.data;
		x++;
		thisOption["id"] = valueArray.item(x).firstChild.data;
		document.getElementById("wr3").options[document.getElementById("wr3").options.length] = new Option(thisOption["name"],thisOption["id"]);
	}
}


function clearSelectWR1()  {
	var selectList3 = document.getElementById("wr3");
	if (selectList3.options.length != 0)  {
		while(selectList3.lastChild){
    		selectList3.removeChild(selectList3.lastChild);
		}
	}
	var selectList2 = document.getElementById("wr2");
	if (selectList2.options.length != 0)  {
		while(selectList2.lastChild){
    		selectList2.removeChild(selectList2.lastChild);
		}
	}
}

function clearSelectWR2()  {
	var selectList3 = document.getElementById("wr3");
	while(selectList3.lastChild){
    	selectList3.removeChild(selectList3.lastChild);
	}
}

/*
function createXmlHttpRequestObject()  {
	var xmlHttp;
	try  {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)  {
		var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
		for (var i=0;i<xmlHttpVersions.length && !xmlHttp; i++)  {
			try  {
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			}
			catch (e)  { }
		}
	}
	if (!xmlHttp)  {
		alert("Error creating the XMLHttpRequest object.");
	} else {
		return xmlHttp;
	}
}*/
