var InternetBrowser = "";

if (window.XMLHttpRequest){// code for IE7, Firefox, Opera, etc.
	InternetBrowser = "IE7+FF";
}else if (window.ActiveXObject){// code for IE6, IE5
	InternetBrowser = "IE5+IE6";
}



function CountyInit(){
	var counties=[];
	counties[0]='Barbour';
	counties[1]='Berkeley';
	counties[2]='Boone';
	counties[3]='Braxton';
	counties[4]='Brooke';
	counties[5]='Cabell';
	counties[6]='Calhoun';
	counties[7]='Clay';
	counties[8]='Doddridge';
	counties[9]='Fayette';
	counties[10]='Gilmer';
	counties[11]='Grant';
	counties[12]='Greenbrier';
	counties[13]='Hampshire';
	counties[14]='Hancock';
	counties[15]='Hardy';
	counties[16]='Harrison';
	counties[17]='Jackson';
	counties[18]='Jefferson';
	counties[19]='Kanawha';
	counties[20]='Lewis';
	counties[21]='Lincoln';
	counties[22]='Logan';
	counties[23]='Marion';
	counties[24]='Marshall';
	counties[25]='Mason';  
	counties[26]='McDowell';
	counties[27]='Mercer';
	counties[28]='Mineral';
	counties[29]='Mingo';
	counties[30]='Monongalia';
	counties[31]='Monroe';
	counties[32]='Morgan';
	counties[33]='Nicholas';
	counties[34]='Ohio';
	counties[35]='Pendleton';
	counties[36]='Pleasants';
	counties[37]='Pocahontas';
	counties[38]='Preston';
	counties[39]='Putnam';
	counties[40]='Raleigh';
	counties[41]='Randolph';
	counties[42]='Ritchie';
	counties[43]='Roane';
	counties[44]='Summers';
	counties[45]='Taylor';
	counties[46]='Tucker';
	counties[47]='Tyler';
	counties[48]='Upshur';
	counties[49]='Wayne';
	counties[50]='Webster';
	counties[51]='Wetzel';
	counties[52]='Wirt';
	counties[53]='Wood';
	counties[54]='Wyoming';  
	
	return counties;
}

function initCntyCoord(){
	var cntyCoord=[];
	
	cntyCoord[0] = "Barbour,-80.00297,39.132907";
	cntyCoord[1] = "Berkeley,-78.02753,39.46404";
	cntyCoord[2] = "Boone,-81.71119,38.02301";
	cntyCoord[3] = "Braxton,-80.71925,38.700023";
	cntyCoord[4] = "Brooke,-80.57671,40.273908";
	cntyCoord[5] = "Cabell,-82.24178,38.420311";
	cntyCoord[6] = "Calhoun,-81.11753,38.844475";
	cntyCoord[7] = "Clay,-81.07507,38.462537";
	cntyCoord[8] = "Doddridge,-80.70692,39.2692";
	cntyCoord[9] = "Fayette,-81.08119,38.028705";
	cntyCoord[10] = "Gilmer,-80.85726,38.924089";
	cntyCoord[11] = "Grant,-79.19551,39.1051";
	cntyCoord[12] = "Greenbrier,-80.4529,37.946869";
	cntyCoord[13] = "Hampshire,-78.61412,39.31704";
	cntyCoord[14] = "Hancock,-80.57428,40.521669";
	cntyCoord[15] = "Hardy,-78.85793,39.007501";
	cntyCoord[16] = "Harrison,-80.37983,39.283474";
	cntyCoord[17] = "Jackson,-81.67474,38.83449";
	cntyCoord[18] = "Jefferson,-77.86271,39.30759";
	cntyCoord[19] = "Kanawha,-81.528,38.336605";
	cntyCoord[20] = "Lewis,-80.50216,38.995807";
	cntyCoord[21] = "Lincoln,-82.07026,38.175443";
	cntyCoord[22] = "Logan,-81.93531,37.831513";
	cntyCoord[23] = "Marion,-80.24333,39.509973";
	cntyCoord[24] = "Marshall,-80.66324,39.860428";
	cntyCoord[25] = "Mason,-82.02654,38.769789";
	cntyCoord[26] = "McDowell,-81.65357,37.378464";
	cntyCoord[27] = "Mercer,-81.11139,37.405485";
	cntyCoord[28] = "Mineral,-78.9437,39.414763";
	cntyCoord[29] = "Mingo,-82.13473,37.726374";
	cntyCoord[30] = "Monongalia,-80.04654,39.630261";
	cntyCoord[31] = "Monroe,-80.55064,37.560276";
	cntyCoord[32] = "Morgan,-78.25779,39.560411";
	cntyCoord[33] = "Nicholas,-80.79921,38.291673";
	cntyCoord[34] = "Ohio,-80.61884,40.09697";
	cntyCoord[35] = "Pendleton,-79.35093,38.680736";
	cntyCoord[36] = "Pleasants,-81.16064,39.370884";
	cntyCoord[37] = "Pocahontas,-80.00781,38.331772";
	cntyCoord[38] = "Preston,-79.66811,39.469284";
	cntyCoord[39] = "Putnam,-81.90901,38.508656";
	cntyCoord[40] = "Raleigh,-81.24868,37.771272";
	cntyCoord[41] = "Randolph,-79.87581,38.774694";
	cntyCoord[42] = "Ritchie,-81.06292,39.178233";
	cntyCoord[43] = "Roane,-81.34827,38.714051";
	cntyCoord[44] = "Summers,-80.85851,37.655852";
	cntyCoord[45] = "Taylor,-80.04615,39.335994";
	cntyCoord[46] = "Tucker,-79.56494,39.113577";
	cntyCoord[47] = "Tyler,-80.88488,39.465275";
	cntyCoord[48] = "Upshur,-80.23338,38.897827";
	cntyCoord[49] = "Wayne,-82.42697,38.146033";
	cntyCoord[50] = "Webster,-80.4219,38.494747";
	cntyCoord[51] = "Wetzel,-80.63912,39.60521";
	cntyCoord[52] = "Wirt,-81.37855,39.022636";
	cntyCoord[53] = "Wood,-81.51521,39.211249";
	cntyCoord[54] = "Wyoming,-81.54915,37.609595";
	
	return cntyCoord;
}

function City_In_County_Init(CityInCounty){
	//alert(CityInCounty);
	// Read the data from xml file
	var xmlhttp=null;
	//alert(CityInCounty);
	if (InternetBrowser == "IE7+FF")	xmlhttp=new XMLHttpRequest();
	else if (InternetBrowser == "IE5+IE6")	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

	
	if (xmlhttp!=null){
		xmlhttp.open("GET", "data/WV_City.xml", false);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				var xmlDoc = xmlhttp.responseXML;
				var city= xmlDoc.documentElement.getElementsByTagName("WV_City");
				var cnty=xmlDoc.getElementsByTagName("County")[0].childNodes[0].nodeValue;
				
				for (var i = 0; i < city.length; i++) {
					cnty=xmlDoc.getElementsByTagName("County")[i].childNodes[0].nodeValue;
					for (var j = 0; j < 55; j++){
						if ( cnty.toUpperCase() == CityInCounty[j][0].toUpperCase()) {
							CityInCounty[j].push(xmlDoc.getElementsByTagName("NAME")[i].childNodes[0].nodeValue);
							CityInCounty[j].push(xmlDoc.getElementsByTagName("Coord_X")[i].childNodes[0].nodeValue);
							CityInCounty[j].push(xmlDoc.getElementsByTagName("Coord_Y")[i].childNodes[0].nodeValue);
							CityInCounty[j].push(xmlDoc.getElementsByTagName("Count")[i].childNodes[0].nodeValue);
							CityInCounty[j].push(xmlDoc.getElementsByTagName("County")[i].childNodes[0].nodeValue);
							CityInCounty[j].push(xmlDoc.getElementsByTagName("County_FIPS")[i].childNodes[0].nodeValue);
							CityInCounty[j].push(xmlDoc.getElementsByTagName("CNTY_FIPS")[i].childNodes[0].nodeValue);
							CityInCounty[j].push("***");
						}
					}
				}
			}
		}
	}
	xmlhttp.send(null);
	//alert(CityInCounty);
	return CityInCounty;
}

function SchoolInit(CntySchl){
	// Read the data from example.xml
	var xmlhttp=null;
	if (InternetBrowser == "IE7+FF")	xmlhttp=new XMLHttpRequest();
	else if (InternetBrowser == "IE5+IE6")	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

	if (xmlhttp!=null){
		xmlhttp.open("GET", "data/school.xml", false);
		
		var tempCntyName = "";

		xmlhttp.onreadystatechange = function(tempCntySchool) {
			// state  0: The request is not initialized; 1 The request has been set up; 2 The request has been sent; 3 The request is in process; 4 The request is complete
			if (xmlhttp.readyState == 4) {
				//alert(xmlhttp.responseText);
				var xmlDoc = xmlhttp.responseXML;	
				var Schools= xmlDoc.documentElement.getElementsByTagName("WV_Schools");

				//alert(Schools.length);
				//alert(window.navigator.appName);
				if (window.navigator.appName=="Microsoft Internet Explorer") {
					for (var i = 0; i < Schools.length; i++) {
						// obtain the attribues of each marker
						for (var j = 0; j < 55 ; j++) {
							if ( (Schools[i].childNodes[5].text).toUpperCase() == CntySchl[j][0].toUpperCase()) {
								CntySchl[j].push(Schools[i].childNodes[0].text);							
								CntySchl[j].push(Schools[i].childNodes[6].text);
								CntySchl[j].push(Schools[i].childNodes[7].text);
								CntySchl[j].push(Schools[i].childNodes[1].text);
								CntySchl[j].push(Schools[i].childNodes[2].text);
								CntySchl[j].push(Schools[i].childNodes[3].text);
								CntySchl[j].push(Schools[i].childNodes[4].text);
								CntySchl[j].push(Schools[i].childNodes[5].text);
								CntySchl[j].push(Schools[i].childNodes[8].text);
								CntySchl[j].push(";");
							}
						}
					}
				} else {
					for (var i = 0; i < Schools.length; i++) {
						// obtain the attribues of each marker
						for (var j = 0; j < 55; j++) {
							tempCntyName = 	(Schools[i].getElementsByTagName("COUNTY"))[0].firstChild.nodeValue;
							if ( tempCntyName.toUpperCase() == CntySchl[j][0].toUpperCase()) {
								CntySchl[j].push((Schools[i].getElementsByTagName("FEATURE_NA"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("LATITUDE"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("LONGITUDE"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("FEATURE_TY"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("SUBTYPE"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("COUNTRY"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("STATE"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("COUNTY"))[0].firstChild.nodeValue);
								CntySchl[j].push((Schools[i].getElementsByTagName("CITY"))[0].firstChild.nodeValue);
								CntySchl[j].push(";");
							}						
						}
					}
				}
			}
		}
	}
	
	//alert(CntySchl);
	xmlhttp.send(null);
	return CntySchl;
}
//  ========Functions in this segment are used to trim the leading and the ending whitespaece ========
//  ============================Beginning of this segment=====================================
		
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
		
// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
		
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

function CreateCountyDropdownList(counties){
	
	var quickFindEle;
	quickFindEle = document.createElement("option");
	
	for (var i=0; i<55; i++){
		quickFindEle.setAttribute("Value",i);
		var theText = document.createTextNode(counties[i]);
		var quickFind = document.getElementById("quickFind");
		quickFindEle.appendChild(theText);
		quickFind.appendChild(quickFindEle);
	}
}

function SearchOfficefromMemory(){
	var searchResults = document.getElementById("searchResults");
	searchResults.innerHTML = "";
	var strName = trim(document.getElementById("offName").value).toLowerCase();
	var strCity = trim(document.getElementById("offCity").value).toLowerCase();
	var searchMethod = document.getElementById("searchmethod");
	//strCity = strCity.toLowerCase();
	//strName = strName.toLowerCase();
		
	var searchRecords="";
	
	var tempnum=0;
	var lengName=strName.length;
	var lengCity=strCity.length;
			
	// If both two input boxes are empty, do nothing
	if ( (lengName==0) && (lengCity==0) ) {
		alert("At least one of boxes, Office Name and Office City, can't be left empty.");
		searchResults.style.height="";
		searchResults.style.overflow="auto";
		searchResults.innerHTML = searchRecords;
		return;
	} else  if ( (lengName==0) && (lengCity>0) ){
		for (var i=0; i < 2; i++){
			// This segment is used to extract the every office infomation from the whole set
			var tempPos = htmls[i].indexOf("Office Name:") + 12; 	// 12 is the length of  string "Office Name:"
			var officeinfo = htmls[i].substring(tempPos);
			tempPos=officeinfo.indexOf("(Latitude, Longitude)") -4;
			officeinfo =(officeinfo.substring(0,tempPos)).toLowerCase();
					
			var temp = new Array();
			temp = officeinfo.split('<br>'); 	// string "<br>" split officeinfo into 3 parts. First part includes the office name.
												// Office name is bolded, like this: <b>office name;
												// Second part includes street address; Thirs part includes city, state, and zip;
			// re-extract the office name and excludes the HTML code
			tempPos = temp[0].indexOf("<b>") + 3;
			temp[0] = temp[0].substring(tempPos);
			tempPos = temp[0].indexOf("</b>");
			temp[0] = temp[0].substring(0,tempPos);
				
			tempPos = temp[2].indexOf(",");
			temp[2] = temp[2].substring(0,tempPos);
					
			var cityname = temp[2];
			var officename = temp[0];
			// end of the segment
					
			var j1=cityname.indexOf(strCity);
			if ( j1 != -1) {
				tempnum += 1;
				searchRecords += "<div style='white-space:nowrap;'>";
				searchRecords += "<font color='blue'><b>(" + tempnum + ")</b></font>";
				searchRecords += htmls[i];
				searchRecords += "</div><br>";
				//searchResults.innerHTML = htmls[i];
			}
		}			
	} else  if ( (lengName>0) && (lengCity==0) ){
		for (var i=0; i < htmls.length; i++){
			// This segment is used to extract the every office infomation from the whole set
			var tempPos = htmls[i].indexOf("Office Name:") + 12; 	// 12 is the length of  string "Office Name:"
			var officeinfo = htmls[i].substring(tempPos);
			tempPos=officeinfo.indexOf("(Latitude, Longitude)") -4;
			officeinfo =(officeinfo.substring(0,tempPos)).toLowerCase();
					
			var temp = new Array();
			temp = officeinfo.split('<br>'); 	// string "<br>" split officeinfo into 3 parts. First part includes the office name.
												// Office name is bolded, like this: <b>office name;
												// Second part includes street address; Thirs part includes city, state, and zip;
			// re-extract the office name and excludes the HTML code
			tempPos = temp[0].indexOf("<b>") + 3;
			temp[0] = temp[0].substring(tempPos);
			tempPos = temp[0].indexOf("</b>");
			temp[0] = temp[0].substring(0,tempPos);
			
			tempPos = temp[2].indexOf(",");
			temp[2] = temp[2].substring(0,tempPos);
				
			var cityname = temp[2];
			var officename = temp[0];
			// end of the segment

			var j2=officename.indexOf(strName);
			if ( j2 != -1 ) {
				tempnum += 1;
				searchRecords += "<div style='white-space:nowrap;'>";
				searchRecords += "<font color='blue'><b>(" + tempnum + ")</b></font>";
				searchRecords += htmls[i];
				searchRecords += "</div><br>";
			}
		}
	}else if( (lengName>0) && (lengCity>0) ){
		for (var i=0; i < htmls.length; i++){
			// This segment is used to extract the every office infomation from the whole set
			var tempPos = htmls[i].indexOf("Office Name:") + 12; 	// 12 is the length of  string "Office Name:"
			var officeinfo = htmls[i].substring(tempPos);
			tempPos=officeinfo.indexOf("(Latitude, Longitude)") -4;
			officeinfo =(officeinfo.substring(0,tempPos)).toLowerCase();
				
			var temp = new Array();
			temp = officeinfo.split('<br>'); 	// string "<br>" split officeinfo into 3 parts. First part includes the office name.
												// Office name is bolded, like this: <b>office name;
												// Second part includes street address; Thirs part includes city, state, and zip;
			// re-extract the office name and excludes the HTML code
			tempPos = temp[0].indexOf("<b>") + 3;
			temp[0] = temp[0].substring(tempPos);
			tempPos = temp[0].indexOf("</b>");
			temp[0] = temp[0].substring(0,tempPos);
					
			tempPos = temp[2].indexOf(",");
			temp[2] = temp[2].substring(0,tempPos);
			
			var cityname = temp[2];
			var officename = temp[0];
			// end of the segment
				
			var j1=cityname.indexOf(strCity);
			var j2=officename.indexOf(strName);
				
			// for the option of searchMethod:
			// If OR is checked, the value of searchMethod.checked is "true";
			// If AND is checked, the value of searchMethod.checked is "false".
			if (searchMethod.checked == false) {
				if ( (j1 != -1) && (j2 != -1)) {
					tempnum += 1;
					searchRecords += "<div style='white-space:nowrap;'>";
					searchRecords += "<font color='blue'><b>(" + tempnum + ")</b></font>";
					searchRecords += htmls[i];
					searchRecords += "</div><br>";
					//searchResults.innerHTML = htmls[i];
				}
			} else {
				if ( (j1 != -1) && (j2 != -1)) {
					tempnum += 1;
					searchRecords += "<div style='white-space:nowrap;'>";
					searchRecords += "<font color='blue'><b>(" + tempnum + ")</b></font>";
					searchRecords += htmls[i];
					searchRecords += "</div><br>";
					//searchResults.innerHTML = htmls[i];
				}
				if ( (j1 != -1) && (j2 == -1)) {
					tempnum += 1;
					searchRecords += "<div style='white-space:nowrap;'>";
					searchRecords += "<font color='blue'><b>(" + tempnum + ")</b></font>";
					searchRecords += htmls[i];
					searchRecords += "</div><br>";
					//searchResults.innerHTML = htmls[i];
				}
				if ( (j1 == -1) && (j2 != -1)) {
					tempnum += 1;
					searchRecords += "<div style='white-space:nowrap;'>";
					searchRecords += "<font color='blue'><b>(" + tempnum + ")</b></font>";
					searchRecords += htmls[i];
					searchRecords += "</div><br>";
					//searchResults.innerHTML = htmls[i];
				}
			}
		}
	}
	
	if (tempnum == 0){ 
		searchResults.style.height="";
		searchResults.style.overflow="auto";
		searchResults.innerHTML = "<font color='red'><b>Sorry. No record found!</b></font>";
	}
	else if (tempnum < 5){
		searchResults.style.height="";
		searchResults.style.overflow="auto";
		searchResults.innerHTML = searchRecords;
	}
	else if (tempnum > 4) {
		searchResults.style.height="400px";
		searchResults.style.overflow="auto";
		searchResults.innerHTML = searchRecords;
	}
	
	return;
}

