// JavaScript Document

function getStreets() {
	var index = document.getElementById("communities").selectedIndex;
	var community = document.getElementById("communities").options[index].value;	
	window.location = 'records.php?community=' + community;	
}

function getStreets() {
	var index = document.getElementById("communities").selectedIndex;
	var community = document.getElementById("communities").options[index].value;	
	var url = "streetsXML.php?&community="+community;
	request2.open("GET", url, true);
	request2.onreadystatechange = setStreets;
	request2.send(null);
	
	var streetEl = document.getElementById("streets");
	streetEl.selectedIndex = 0;
}

function setStreets() {
  	if (request2.readyState == 4) {
		if (request2.status == 200) {
			var xmlDoc = request2.responseXML;
			
			var x=xmlDoc.documentElement.getElementsByTagName("street");
			var streetEl = document.getElementById("streets");

			// remove all options except the first one "All"
			streetEl.length = 1;
			
			for (i=0;i<x.length;i++) {
				cElement = document.createElement("option");
  				xx=x[i].getElementsByTagName("name");
				cElement.text = xx[0].firstChild.nodeValue;

  				xx=x[i].getElementsByTagName("abrv");
				cElement.value = xx[0].firstChild.nodeValue;
				
				streetEl.appendChild(cElement);
			}
			
			streetEl.selectedIndex = 0;
		}
	}
}

function getRecords() {
	var index = document.getElementById("communities").selectedIndex;
	var community = document.getElementById("communities").options[index].value;	
	index = document.getElementById("streets").selectedIndex;
	var street = document.getElementById("streets").options[index].value;
	var id = document.getElementById("record_id").value;
	var air = document.getElementById("record_air").checked;
		if (!air) air = null;
	var soils = document.getElementById("record_soils").checked;
		if (!soils) soils = null;
	var wells = document.getElementById("record_wells").checked;
		if (!wells) wells = null;
	var septic = document.getElementById("record_septic").checked;
		if (!septic) septic = null;
	var wq = document.getElementById("record_wq").checked;
		if (!wq) wq = null;
	var other = document.getElementById("record_other").checked;
		if (!other) other = null;

	var url = "recordsXML.php?" + 
				"community=" + escape(community) +
				"&id=" + escape(id) +
				"&street=" + escape(street) +
				"&air=" + escape(air) + 
				"&soils=" + escape(soils) + 
				"&wells=" + escape(wells) + 
				"&septic=" + escape(septic) + 
				"&wq=" + escape(wq) + 
				"&other=" + escape(other);	
			
	request3.open("GET", url, true);
	request3.onreadystatechange = setRecords;
	request3.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request3.send(null);
}

function resetPage() {
	document.records.reset();
	resultPages = new Array();
	currentPage = 1;
	
	var resultsEl = document.getElementById("recordResults");
	resultsEl.innerHTML = "";
	
	var totalRecordsEl = document.getElementById("totalRecords");
	totalRecordsEl.innerHTML = "";
	
	var spanEl = document.getElementById("pageNumbers");
	spanEl.innerHTML = "";
	
	var spanEl2 = document.getElementById("pageNumbersTop");
	spanEl2.innerHTML = "";
	
	//getCommunities();
	getStreets();
}

function setRecords() {
  	if (request3.readyState == 4) {
		if (request3.status == 200) {
			resultPages = new Array();
			
			var xmlDoc = request3.responseXML;
			var divEl = document.getElementById("recordResults");
			var x=xmlDoc.documentElement.getElementsByTagName("house");
			
		//	var query = xmlDoc.documentElement.getElementsByTagName("query");
		//	alert(query[0].firstChild.nodeValue);
		//	var theRequest = xmlDoc.documentElement.getElementsByTagName("theRequest");
		//	alert("theRequest: " + theRequest[0].firstChild.nodeValue);
		//	var args = xmlDoc.documentElement.getElementsByTagName("args");
		//	alert("args: " + args[0].firstChild.nodeValue);
		
			var index = x.length;
			var pages = Math.ceil(index/15);
			
			var totalRecordsEl = document.getElementById("totalRecords");
			totalRecordsEl.innerHTML = index + " records found - " + pages + " page";
			
			if (index > 0) {
				if (pages > 1) {
					totalRecordsEl.innerHTML += 's';
				}
			
				var tags = new Array();
					tags[0] = "id";
					tags[1] = "street_nm";
					tags[2] = "community_nm";
					tags[3] = "air";
					tags[4] = "soils";
					tags[5] = "wells";
					tags[6] = "septic";
					tags[7] = "wq";
					tags[8] = "other";
			
				for(h=0;h<pages;h++) {
					resultsEl = createResultsTableHeader();
					start = h*15;
					if (start+15 > index) {
						end = index;
					} else {
						end = start+15;
					}
					for (i=start;i<end;i++) {
						var newRow = resultsEl.insertRow(-1);
						newRow.setAttribute("height","30");
						newRow.setAttribute("valign","middle");
						
						for (j=0; j<tags.length; j++) {
							var newCell = newRow.insertCell(-1);
							xx=x[i].getElementsByTagName(tags[j]);
							if ( xx[0].firstChild != null) {
								var theNode = xx[0].firstChild.nodeValue;
								if ( j <= 2 || ((j > 2) && (theNode.length > 1)) ) {
									if (j>2) {
										newCell.align = "center";
										newCell.innerHTML = '<a href=pdfs/' + theNode + ' target=_blank><img src=images/record_' + tags[j] + '.gif border=0 /></a>';
									}else {
										newCell.innerHTML = theNode;
									}
								}
							}
						}
					}
					resultPages[h] = resultsEl;
				}
				
				if (divEl.firstChild == null) {
					divEl.appendChild(resultPages[0]);
				}else {
					divEl.replaceChild(resultPages[0],divEl.firstChild);
				}
				currentPage = 1;
				pageNumbers();
			} else {
				divEl.innerHTML = "";	
			}
		}
	}
}

function pageNumbers() {
	var spanEl = document.getElementById("pageNumbers");
	spanEl.innerHTML = '';
	var spanEl2 = document.getElementById("pageNumbersTop");
	spanEl2.innerHTML = '';
	
	for(i=0;i<resultPages.length;i++) {
		if ((i+1) == currentPage) {
			spanEl.innerHTML += "<b>" + (i+1) + "</b>  ";
			spanEl2.innerHTML += "<b>" + (i+1) + "</b>  ";
		} else {
			pageNum = i+1;
			spanEl.innerHTML +=  "<b><a onClick='goToPage(" +pageNum + ")'>"+pageNum+"</a><b>  ";
			spanEl2.innerHTML +=  "<b><a onClick='goToPage(" +pageNum + ")'>"+pageNum+"</a><b>  ";
		}
	}
}

function goToPage(pageNum) {
	var divEl = document.getElementById("recordResults");
	divEl.replaceChild(resultPages[pageNum-1],divEl.firstChild);
	currentPage = pageNum;
	pageNumbers();
}

function createResultsTableHeader() {
	// creates <table> and <tbody> elements
	var table1 = document.createElement("table");
	table1.id = "recoreResults1";
	table1.className = "recordResults";
	table1.cellspacing = "5";
	table1.width = "100%";
	table1.border = "0";
	
	var rowEl = document.createElement("tr");
	rowEl.valign = "bottom";
	
	var headEL1 = document.createElement("th");
	var innerText = document.createTextNode("Fire Number");
	headEL1.appendChild(innerText);
	
	var headEL2 = document.createElement("th");
	innerText = document.createTextNode("Street");
	headEL2.appendChild(innerText);
	
	var headEL3 = document.createElement("th");
	innerText = document.createTextNode("Community");
	headEL3.appendChild(innerText);
	
	var headEL4 = document.createElement("th");
	headEL4.width = "40";
	headEL4.align = "center";
	innerText = document.createTextNode("Air");
	headEL4.appendChild(innerText);
	
	var headEL5 = document.createElement("th");
	headEL5.width = "40";
	headEL5.align = "center";
	innerText = document.createTextNode("Soils");
	headEL5.appendChild(innerText);
	
	var headEL6 = document.createElement("th");
	headEL6.width = "40";
	headEL6.align = "center";
	innerText = document.createTextNode("Wells");
	headEL6.appendChild(innerText);
	
	var headEL7 = document.createElement("th");
	headEL7.width = "40";
	headEL7.align = "center";
	innerText = document.createTextNode("Septic");
	headEL7.appendChild(innerText);
	
	var headEL8 = document.createElement("th");
	headEL8.width = "40";
	headEL8.align = "center";
	innerText = document.createTextNode("Water Quality");
	headEL8.appendChild(innerText);
	
	var headEL9 = document.createElement("th");
	headEL9.width = "40";
	headEL9.align = "center";
	innerText = document.createTextNode("Other");
	headEL9.appendChild(innerText);
	
	rowEl.appendChild(headEL1);
	rowEl.appendChild(headEL2);
	rowEl.appendChild(headEL3);
	rowEl.appendChild(headEL4);
	rowEl.appendChild(headEL5);
	rowEl.appendChild(headEL6);
	rowEl.appendChild(headEL7);
	rowEl.appendChild(headEL8);
	rowEl.appendChild(headEL9);
	
	table1.appendChild(rowEl);
	
	var rowEl2 = document.createElement("tr");
	var cell1 = document.createElement("td");
	cell1.setAttribute("colspan","9");
	var hr1 = document.createElement("hr");
	cell1.appendChild(hr1);
	rowEl2.appendChild(cell1);
	
	table1.appendChild(rowEl2);
	
	return table1;
}
