function ajaxRequestObject() {

	var obj;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		obj = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				obj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				// alert("Your browser broke!");
				return false;
			}
		}
	}
	return obj;
	
}

function ajaxFunction(url, selName){

	// clear the appropriate selector cells
	switch (selName) {
		case "Pdf":
			// print pdf links;
			break;
		case "Zip":
			document.getElementById("tPdf").innerHTML = "";
			break;
		case "City":
			currentCity = "";
			document.getElementById("tPdf").innerHTML = "";
			document.getElementById("tZip").innerHTML = "";
			break;
		case "State":
			currentZip = "";
			currentCity = "";
			document.getElementById("tPdf").innerHTML = "";
			document.getElementById("tZip").innerHTML = "";
			document.getElementById("tCity").innerHTML = "";
			break;
	}
	
	
	// exit function if the user selected the "select" option
	var currentSelector = document.getElementById(selName);
	if (currentSelector) {
		if (currentSelector.options[currentSelector.selectedIndex].value == "") {
			return;
		}
	}
	
	// Create an ajax request object
	var ajaxRequest = new ajaxRequestObject();  // The variable that makes Ajax possible!
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// show the whirlygig
		document.getElementById("ajaxLoader").style.visibility = "visible";

		// Uninitialized - open() has not been called yet.
		if(ajaxRequest.readyState == 0){
		}

		// Loading -		send() has not been called yet.
		if(ajaxRequest.readyState == 1){
		}

		// Loaded -		send() has been called, headers and status are available.
		if(ajaxRequest.readyState == 2){
		}

		// Interactive -	Downloading, responseText holds the partial data.
		if(ajaxRequest.readyState == 3){
		}

		// Completed -		Finished with all operations.
		if(ajaxRequest.readyState == 4){
			document.getElementById("ajaxLoader").style.visibility = "hidden";
			printForm(ajaxRequest.responseText)
		}
	}

	// open url and send
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 

}

function printForm(objData) {
	
	var formDiv = document.getElementById("zipForm");
	
	var tempNameValuePair = objData.split("=");
	var tempName = tempNameValuePair[0];
	var tempValues = tempNameValuePair[1].split(",");
	
	// set current variables
	switch (tempName) {
		case "State":
			break;
		case "City":
			currentState = document.getElementById("State").options[document.getElementById("State").selectedIndex].value;
			break;
		case "Zip":
			currentCity = document.getElementById("City").options[document.getElementById("City").selectedIndex].value;
			break;
		case "Pdf":
			currentZip = document.getElementById("Zip").options[document.getElementById("Zip").selectedIndex].value;
			break;
	}
	
	if (tempName != "Pdf") {
		// create the appropriate select object
		var tempSel = document.createElement("select");
		tempSel.name = tempName;
		tempSel.id = tempName;
		
		// pre populate select object with a "select whatever" option
		var tempOption = document.createElement("option");
		tempOption.innerHTML = "Select " + tempName;
		tempOption.value = "";
		tempSel.appendChild(tempOption);
		
		// populate select object with the appropriate option items
		for (var j=0;j<tempValues.length-1;j++) {
			tempOption = document.createElement("option");
			tempOption.innerHTML = tempValues[j];
			tempOption.value = tempValues[j];
			tempSel.appendChild(tempOption);
		}
		
		// add onchange event handler
		tempSel.onchange = function () { 
			ajaxFunction("jbProxy.php?action=" + this.name + "&" + this.name + "=" + this.options[this.selectedIndex].value + "&s=" + currentState + "&c=" + currentCity + "&z=" + currentZip + "", this.name);
		}
	
		// place new selector
		document.getElementById("t" + tempName).innerHTML = "";
		document.getElementById("t" + tempName).appendChild(tempSel);
	} else {
		// print the pdf link container
		var tPdf = document.getElementById("tPdf");
		
		// create found text header
		var tFoundText = document.createElement("div");
		tFoundText.id = "tFound";
		tPdf.appendChild(tFoundText);
		
		var foundCount = 0;
		
		for (var i=0;i<tempValues.length-1;i++) {
			
			// create the pdf links
			if(tempValues[i] != "unknown.pdf") {
			
				var tTitle = document.createElement("div");
				tTitle.style.width = "100%";
				
				// view link
				var tempViewLink = document.createElement("a");
				tempViewLink.href = "channelLineups/" + tempValues[i];
				tempViewLink.target = "_blank";
				tempViewLink.innerHTML = "(view)";
				tempViewLink.title = "Click here to view the document online.";
				tTitle.appendChild(tempViewLink);
				
				// download link
				var tempDLLink = document.createElement("a");
				tempDLLink.href = "jbDownload.php?file=" + tempValues[i];
				tempDLLink.target = "_self";
				tempDLLink.innerHTML = "(download)";
				tempDLLink.title = "Click here to download the document to your computer.";
				tTitle.appendChild(tempDLLink);
				
				// document title
				var tSpan = document.createElement("span");
				tSpan.innerHTML = tempValues[i];
				tTitle.appendChild(tSpan);
				
				tPdf.appendChild(tTitle);
				
				foundCount++;
			}
			
		}
		
		// set the found text header text
		var plurality = "s";
		if (foundCount == 1) {
			plurality = "";
		}
		
		document.getElementById("tFound").innerHTML = "Found: (" + foundCount + ") document" + plurality + ".";
	}

}

// persistent variables
var currentState = "";
var currentCity = "";
var currentZip = "";

// tell the browser to create the state selector when the page first loads
window.onload = function() { ajaxFunction("jbProxy.php?action=","State"); }