 var url = "getCitiesZips.cgi?county="; // 
 var curl = "getCounties.cgi";
 var zurl = "getZipcodes.cgi";
 var aurl = "getAreas.cgi";
 var nzurl = "getCities.cgi";
 var surl = "getSuburbs.cgi?city=";
 var kurl = "getAreasK.cgi?county="; 
 var turl = "getTransit.cgi?system=";
 var murl = "getMapPoints.cgi?id=";
 var nurl = "getNeighborhoods.cgi?city=";
 var purl = "getPolygons.cgi";  
 var gurl = "getPriceAverage.cgi?";
 var map;
 function handleMapResponse() {
	 var point = [];

	 var bounds = new GLatLngBounds();

	 if (http.readyState == 4 && http.responseXML) {
		var i;
		var j;
		var myPolygons = http.responseXML.getElementsByTagName("polygon");
		for (j = 0; j < myPolygons.length; j++) {
			//var polyPointLats = http.responseXML.getElementsByTagName("polygonpointlat");	 		
			//var polyPointLongs = http.responseXML.getElementsByTagName("polygonpointlong");	
			var myPolygon = myPolygons[j];
			var polyPointLats = myPolygon.getElementsByTagName("polygonpointlat");			
			var polyPointLongs = myPolygon.getElementsByTagName("polygonpointlong");						
			for (i = 0; i < polyPointLats.length; i++) {
			//var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
				var Lat = polyPointLats[i].firstChild.nodeValue;
				var Long = polyPointLongs[i].firstChild.nodeValue;
				point.push(new GLatLng(Lat, Long));
				bounds.extend(new GLatLng(Lat, Long));
			}
			var lngCenter = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
			var latCenter = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
			var center = new GLatLng(latCenter,lngCenter);
			map.setCenter(center, map.getBoundsZoomLevel(bounds));
			var box = new GPolygon(point,'#FF0000', 3, 1,'#0000FF',0.2);
			map.addOverlay(box);
			point = [];
		}
	 }
}
function handleHttpResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var cityPick = http.responseXML.getElementsByTagName("city");
		 for (i = 0; i < cityPick.length; i++) {
		 var cityChoice = cityPick[i].firstChild.nodeValue;
		 document.mlssearch.city.options[i] = new Option(cityChoice,cityChoice, false, false);
		 }
	 }
}
function handleCountyResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var countyPick = http.responseXML.getElementsByTagName("county");
		 for (i = 0; i < countyPick.length; i++) {
			 var countyChoice = countyPick[i].firstChild.nodeValue;
			 document.mlssearch.county.options[i] = new Option(countyChoice,countyChoice, false, false);
		 }
	 }
}
function handlePolyResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var polyId = http.responseXML.getElementsByTagName("id");
		 var polyName = http.responseXML.getElementsByTagName("polygon");		 
		 var polyDisable = http.responseXML.getElementsByTagName("disabled");		 		 
		 for (i = 0; i < polyId.length; i++) {
			var id = polyId[i].firstChild.nodeValue;
			var name = polyName[i].firstChild.nodeValue;			 
			var dis = polyDisable[i].firstChild.nodeValue;
			document.mlssearch.poly.options[i] = new Option(name, id, false, false);
			if (dis == 1) {
				var opt = document.mlssearch.poly.options[i];
				opt.disabled = true;
			}
		 }
	 }
}
function handleSuburbResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var suburbPick = http.responseXML.getElementsByTagName("suburb");
		 for (i = 0; i < suburbPick.length; i++) {
			 var suburbChoice = suburbPick[i].firstChild.nodeValue;
			 document.mlssearch.advertised_subdivision.options[i] = new Option(suburbChoice,suburbChoice, false, false);
		 }
	 }

}
function handleAreaResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var areaPick = http.responseXML.getElementsByTagName("area");
		 for (i = 0; i < areaPick.length; i++) {
			 var areaChoice = areaPick[i].firstChild.nodeValue;
			 document.mlssearch.areas.options[i] = new Option(areaChoice,areaChoice, false, false);
		 }
	 }
}
function handleAreaKResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var areaPick = http.responseXML.getElementsByTagName("area");
		 for (i = 0; i < areaPick.length; i++) {
			 var areaChoice = areaPick[i].firstChild.nodeValue;
			 document.mlssearch.areas.options[i] = new Option(areaChoice,areaChoice, false, false);
		 }
	 }
}
function handleZipResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var zipPick = http.responseXML.getElementsByTagName("zipcode");
		 var zipName = http.responseXML.getElementsByTagName("displayname");		 
		 for (i = 0; i < zipPick.length; i++) {
			 var zipChoice = zipPick[i].firstChild.nodeValue;
			 var zipDisplayName = zipName[i].firstChild.nodeValue;			 
			 // build the full display name
			 var zipDisplay = zipChoice + " - " + zipDisplayName;
			 document.mlssearch.zipcodes.options[i] = new Option(zipDisplay,zipChoice, false, false);
		 }
	 }
}
function handleTransitResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var transitPick = http.responseXML.getElementsByTagName("stationid");
		 var transitName = http.responseXML.getElementsByTagName("displayname");		 
		 for (i = 0; i < transitPick.length; i++) {
			 var transitChoice = transitPick[i].firstChild.nodeValue;
			 var transitDisplayName = transitName[i].firstChild.nodeValue;			 
			 // build the full display name
			// var transitDisplay = transitChoice + " - " + transitDisplayName;
			 document.mlssearch.stations.options[i] = new Option(transitDisplayName,transitChoice, false, false);
		 }
	 }
} 
function handleNhoodResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var i;
		 var cityPick = http.responseXML.getElementsByTagName("neighborhood");
		 var ncountObj = http.responseXML.getElementsByTagName("neighborhoodcount");
		 var ncount = ncountObj[0].firstChild.nodeValue;
		// alert("ncount = " + ncount);
		 if (ncount == 0) { 
			 clearSelect('advertised_subdivision');
		 	 var cityChoice = '(No Neighborhoods Available For This City)';
			 document.mlssearch.advertised_subdivision.options[0] = new Option(cityChoice,cityChoice, false, false);
		 }
		 else {		 
			 for (i = 0; i < cityPick.length; i++) {
				 var cityChoice = cityPick[i].firstChild.nodeValue;
				 document.mlssearch.advertised_subdivision.options[i] = new Option(cityChoice,cityChoice, false, false);
			 }
	     }
	 }
 }
function handlePriceResponse() {
	 if (http.readyState == 4 && http.responseXML) {
		 var average = http.responseXML.getElementsByTagName("average");
		 var average = average[0].firstChild.nodeValue;
		 document.getElementById("average").innerHTML = average;
	 }
}
function updateNeighborhoods() {
	 var cityValue = document.getElementById('city').value;
	//alert(nurl + ' ' + cityValue);
	 http.open("GET", nurl + escape(cityValue), true);
	 http.onreadystatechange = handleNhoodResponse;
	 http.send(null);
}
function updateTransit(systemId) {
	// null causes trouble, so we're adding this.  4-Jan-2007
	if (systemId > 0) {	 http.open("GET", turl + systemId, true); 
	 http.onreadystatechange = handleTransitResponse;
	 http.send(null); 
	}
}
function updateAreas() {
 	 http.open("GET", aurl, true);
	 http.onreadystatechange = handleAreaResponse;
	 http.send(null); 
}
function updateAreasK() {
	// written for knoxville
	// they want a county list to put the area list,
	// BUT they also don't want county included in the
	// search for stuff.
 	 var countyValue = document.getElementById('county').value;
	 clearSelect('areas');
	 http.open("GET", kurl + escape(countyValue), true);
	 http.onreadystatechange = handleAreaKResponse;
	 http.send(null);
}
function updatePolygons() {
	 http.open("GET", purl, true); 	  
	 http.onreadystatechange = handlePolyResponse;
	 http.send(null); 
}
function updateZipcodes() {
 	 http.open("GET", zurl, true);
	 http.onreadystatechange = handleZipResponse;
	 http.send(null); 
}
function updateCities() {
 	 http.open("GET", nzurl, true);
	 http.onreadystatechange = handleHttpResponse;
	 http.send(null); 
}
function updateSuburbs() {
	 var cityValue = document.getElementById('city').value;
	 clearSelect('advertised_subdivision');
 	 http.open("GET", surl + escape(cityValue), true);
	 http.onreadystatechange = handleSuburbResponse;
	 http.send(null); 
}
function updateCounties() {
	 http.open("GET", curl, true);
	 http.onreadystatechange = handleCountyResponse;
	 http.send(null); 
}
function updateMap() {
	var polyValues = document.getElementById('poly');
	map = new GMap2(document.getElementById("map"));	 
	map.addControl(new GSmallMapControl());
	map.disableDragging();
	var len = document.mlssearch.poly.length;
	var i = 0;
	var chosen = "";

	for (i = 0; i < len; i++) {
		if (document.mlssearch.poly[i].selected) {
			chosen = chosen + document.mlssearch.poly[i].value + ":"
		}
	}
//	alert('chosen is ' + chosen);
//	alert('length is ' + len);
	http.open("GET", murl + chosen, true);	 
	http.onreadystatechange = handleMapResponse;
	http.send(null); 
}
function updateCityState() {
	 var countyValue = document.getElementById('county').value;
	 clearSelect('city');
	 http.open("GET", url + escape(countyValue), true);
	 http.onreadystatechange = handleHttpResponse;
	 http.send(null);
}
function updatePriceAvg() {

 	http = getHTTPObject(); // We create the HTTP Object
	var minBeds = document.getElementById('min_beds').value;	 
	var minBaths = document.getElementById('min_baths').value;	 

	var params;

	if (document.getElementById('county')) {
		var countyValue = document.getElementById('county').value;	
		params = 'county=' + escape(countyValue);
	}
	if (document.getElementById('city')) {
         var cityValues = '';
         for (var intLoop = 0; intLoop < document.getElementById('city').length; intLoop++) {
			if ((document.getElementById('city')[intLoop].selected) ||
			   (document.getElementById('city')[intLoop].checked)) {
				var cityVal = document.getElementById('city')[intLoop].value;
				if (cityVal.length > 0) {
					cityValues = cityValues + '&city=' + escape(cityVal);
				}
			}
		}
		params = params + cityValues;
	//	alert(params);
	}
	if (document.getElementById('zipcodes')) {
		var zipcodes = document.getElementById('zipcodes').value;	
		params = params + '&zipcodes=' + escape(zipcodes);
	}
	if (document.getElementById('PropertyType')) {
		var typeValues = '';
		for (var x = 0; x < document.getElementById('PropertyType').length; x++) {
			if ((document.getElementById('PropertyType')[x].selected) ||
			   (document.getElementById('PropertyType')[x].checked)) {
				var typeVal = document.getElementById('PropertyType')[x].value;
				if (typeVal.length > 0) {
					typeValues = typeValues + '&propType=' + escape(typeVal);
				}
			}
		}
		params = params + typeValues;
	}
	if (document.getElementById('areas')) {
		var areas = document.getElementById('areas').value;
		params = params + '&areas=' + escape(areas);
	}
	if (document.getElementById('advertised_subdivision')) {
		var neighborhoods = document.getElementById('advertised_subdivision').value;			
		params = params + '&advertised_subdivision=' + escape(neighborhoods);
	}

	var params =  params + '&min_beds=' + minBeds + '&min_baths=' + minBaths;
	http.open("GET", gurl + params, true);
	http.onreadystatechange = handlePriceResponse;
	http.send(null);
}
function clearSelect() {
	 for (var i = 0; i < arguments.length; i++) {
		 var element = arguments[i];
		 if (typeof element == 'string')
			 element = document.getElementsByName(element)[0];
		 if (element && element.options) {
			 element.options.length = 0;
		 element.selectedIndex = -1;
	  }
	}
 }
 function getHTTPObject() {
	 var xmlhttp = null;
	 var success = false;
	 // List of MS XMLHTTP versions - newest first
	 var MSXML_XMLHTTP_PROGIDS = new Array(
						   'MSXML2.XMLHTTP.5.0',
						   'MSXML2.XMLHTTP.4.0',
						   'MSXML2.XMLHTTP.3.0',
						   'MSXML2.XMLHTTP',
						   'Microsoft.XMLHTTP'
						   );
	 // test for IE implementations first
		 for (var i = 0; i < MSXML_XMLHTTP_PROGIDS.length &&
		  !success; i++) {
		 try  {
			 xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
			 success = true;
			 return xmlhttp;
		 }
		 catch (e) {
			 xmlhttp = false;
		 }
		 }
	 
	 // Now test for non-IE implementations
	 if (!xmlhttp &&
		 typeof XMLHttpRequest != 'undefined')  {
		 try {
			 xmlhttp = new XMLHttpRequest();
		 }
		 catch (e) {
			 xmlhttp = false;
		 }
	 }
	 return xmlhttp;
 }
 var http = getHTTPObject(); // We create the HTTP Object