var selectUpdateTarget = "div_communities";

function makeRequest(dataSource) {
	
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
	
	if (xhr) {
		xhr.onreadystatechange = showContents;
		xhr.open("GET", dataSource, true);
		xhr.send(null);
	}
	else {
		document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
	}
}

function fileBuilder(whichBoxSelected, whichValuePassed) {
	switch (whichBoxSelected) {
		case "states":
			var xmlFileToGet = "p/q_communities.php?state=" + whichValuePassed
			selectUpdateTarget = "div_communities";
			break;
		case "communities":
			var xmlFileToGet = "p/q_publications.php?community=" + whichValuePassed
			selectUpdateTarget = "div_publications";
			break;
		case "publications":
			var xmlFileToGet = "ignore"
			if (whichValuePassed != "") {
window.open(whichValuePassed);
}
			break;
		default : alert("Sorry, an error handling your value selection has occurred.");
	}
	if (xmlFileToGet != "ignore") {makeRequest(xmlFileToGet);}
}

function showContents() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var outMsg = xhr.responseText;
		}
		else {
			var outMsg = "There was a problem with the request " + xhr.status;
		}
		document.getElementById(selectUpdateTarget).innerHTML = outMsg;
		if (selectUpdateTarget == "div_communities") {
			document.getElementById("div_publications").innerHTML = "<select id=\"publications\" size=\"7\" class=\"myinputstyles\" style=\"width:133px\" onchange=\"fileBuilder(this.id, this.value)\"><option value=\"Choose\">Choose Community</option></select>";
		}
	}
}