function resetForm() {
	document.getElementById("Address").value = "";
	document.getElementById("City").value = "";
	document.getElementById("State").selectedIndex = 0;
	document.getElementById("distance").selectedIndex = 1;
}
function load(btnClick) {
	preloadMarkers();
	if (GBrowserIsCompatible()) {
		map = new GMap(document.getElementById("map1"));
		map.clearOverlays();
		point = new GPoint(-96.399729,42.489695);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.centerAndZoom(point, 5);	
		
		baseIcon = new GIcon();
		baseIcon.shadow = "./images/shadow50.png";
		baseIcon.iconSize = new GSize(12, 20);
		baseIcon.shadowSize = new GSize(22, 20);
		baseIcon.iconAnchor = new GPoint(6, 20);
		baseIcon.infoWindowAnchor = new GPoint(6, 1);
		baseIcon.infoShadowAnchor = new GPoint(13, 13);
		baseIcon.image = "./images/mm_20_red.png";
		
		document.getElementById("updatingMap").style.display = "block";
		document.getElementById("mapOverlay").style.display = "block";
		document.getElementById("resultText").style.display = "none";
		
		var address = document.getElementById("Address").value;
		var city = document.getElementById("City").value;
		var state = document.getElementById("State").options[document.getElementById("State").selectedIndex].value;
		var distance = document.getElementById("distance").options[document.getElementById("distance").selectedIndex].value;
		var includeSold = document.Form1.includeSold;
		var bSold = false;
		
		if (includeSold[0].checked == true) {
			bSold = true;
		}
		
		var loadDoc = true;
		if (trim(address) == "" || trim(city) == "" || trim(state) == "" || distance == "") {
			loadDoc = false;
		}
		
		if (btnClick && loadDoc == false) {
			alert("You must enter all of the address information to search for properties.");
		}
		
		if (loadDoc == true) {
			request = GXmlHttp.create();
			//request.open('Get', 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=ERCompass&street=' + encode(address) + '&city=' + encode(city) + '&state=' + encode(state), true);
			request.open('Get', 'yahooGeocodeProxy.cfm?address=' + encode(address) + '&city=' + encode(city) + '&state=' + encode(state), true);
			//request.open('GET', 'getPropertiesForMap.cfm?age=' + age + '&minBeds=' + minBeds + '&minBaths=' + minBaths + '&minSqFt=' + minSqFt + '&price1=' + price1 + '&price2=' + price2, true);
			
			request.onreadystatechange = function() {
				if (request.readyState == 4) {
					/*
					plotHomes();
					var centerLat = parseFloat(totalLat / totalPoints);
					var centerLng = parseFloat(totalLng / totalPoints);
					var centerpoint = new GPoint(centerLng, centerLat);
					document.getElementById("updatingMap").style.display = "none";
					document.getElementById("mapOverlay").style.display = "none";
					document.getElementById("resultText").style.display = "block";
					*/
					var firstResult = request.responseXML.getElementsByTagName("Result")[0];
					var precision = firstResult.getAttribute("precision");
					if (precision != "address") {
						alert("Sorry, we were unable to locate that address. Please try again.");
						document.getElementById("updatingMap").style.display = "none";
						document.getElementById("mapOverlay").style.display = "none";
					}
					else {
						// plot the address they entered
						var lat = getElementTextNS("", "Latitude", firstResult, 0);
						var lng = getElementTextNS("", "Longitude", firstResult, 0);
						var a = getElementTextNS("", "Address", firstResult, 0);
						var c = getElementTextNS("", "City", firstResult, 0);
						var s = getElementTextNS("", "State", firstResult, 0);
						var z = getElementTextNS("", "Zip", firstResult, 0);
						var thisPoint = new GPoint(lng, lat);
						centerpoint = thisPoint;
						
						var theHtml = "<b>" + a + "</b><br>";
						theHtml += c + ", " + s + " " + z;
						var mkr = createAndPlotMarker(thisPoint, 1, theHtml);
						
						// now, load another doc to pull in the property results
						deltaLat = distance / 4555.648143174525 * degreesPerRadian;
						deltaLng = distance / (Math.cos(lat * radiansPerDegree) * 4555.648143174525) * degreesPerRadian; 
						// so, we have our delta points now.  Build the bounding box
						// northern latitudes are higher; therefore, add the delta lat
						top = parseFloat(lat) + parseFloat(deltaLat);
						left = parseFloat(lng) - parseFloat(deltaLng);
						right = parseFloat(lng) + parseFloat(deltaLng);
						bottom = parseFloat(lat) - parseFloat(deltaLat);
						
						request2 = GXmlHttp.create();
						request2.open('Get', 'getPropertiesForRadiusSearch.cfm?maxdistance=' + distance + '&lat=' + lat + '&lng=' + lng + '&sold=' + bSold + '&left=' + encode(left) + '&right=' + encode(right) + '&bottom=' + encode(bottom) + '&top=' + encode(top), true);
				
						request2.onreadystatechange = function() {
							if (request2.readyState == 4) {
								// plot the results
								plotHomes(request2.responseXML, true);
								
								drawRadiusCircle(centerpoint, distance, "#ff0000", 5, 1);
								/*
								var theZoom = 4;
								switch(distance) {
									case ".25":
										theZoom = 2;
										break;
									case ".5":
										theZoom = 3;
										break;
									case ".75":
										theZoom = 3;
										break;
									
								}
								map.centerAndZoom(centerpoint, theZoom);
								*/
								
								document.getElementById("updatingMap").style.display = "none";
								document.getElementById("mapOverlay").style.display = "none";
								document.getElementById("resultText").style.display = "block";
								
							}
						}	
						
						request2.send(null);
						
					}
				}
			}		
			
			request.send(null);		
		}
		else {
			document.getElementById("updatingMap").style.display = "none";
			document.getElementById("mapOverlay").style.display = "none";
			document.getElementById("resultText").style.display = "none";
		}	
	}						
}

function drawRadiusCircle(center, radius, color, width, complexity) { 
	// delta longitude = k * cos( latitude(x1,y1) ) / RadiusOfEarth * DegreesPerRadian 
	// var longitudeOffset = radius / (Math.cos(center.y * radians) * 111325); 
	
	// 6076.11549 feet = 1 nautical mile
	// A minute of longitude is one nautical mile x cosine(latitude). 
	
	//radius = radius * 10;
	//var points = []; 
	points = [];
	var radians = Math.PI / 180; 
	var degs = 180 / Math.PI;
	var nm = 6076.11549; // feet
	// convert radius to nautical miles
	radius = radius * 1.15077944886;
	var longitudeOffset = parseFloat(radius) / (Math.cos(center.y * parseFloat(radians)) * 4555.648143174525) * parseFloat(degs); 

	//alert(longitudeOffset);
	//var latitudeOffset = radius / 3958.75; 
	var latitudeOffset = parseFloat(radius) / 4555.648143174525 * parseFloat(degs);
	
	//alert(latitudeOffset);
	bounds = new GLatLngBounds();
	for (var i = 0; i < 360; i += complexity) { 
		var lngToAdd = parseFloat((longitudeOffset * Math.cos(i * radians)));
		var latToAdd = parseFloat((latitudeOffset * Math.sin(i * radians)))
		//var point = new GPoint(parseFloat(center.x) + parseFloat(lngToAdd), parseFloat(center.y) + parseFloat(latToAdd)); 
		var point = new GLatLng(parseFloat(center.y) + parseFloat(latToAdd), parseFloat(center.x) + parseFloat(lngToAdd));
		points.push(point); 
		bounds.extend(point);
	} 
	points.push(points[0]);         // close the circle 
	var polyline = new GPolyline(points, color, width); 
	map.addOverlay(polyline); 	
	
	/*
	bounds = new GLatLngBounds();
	for (var i=0; i < points.length; i++) {
		bounds.extend(points[i]);
	}
	*/
	//map.centerAndZoom(centerpoint, map.getBoundsZoomLevel(bounds));
	
	map.setZoom(map.getBoundsZoomLevel(bounds));
	var newcenter = new GLatLng(parseFloat(centerpoint.y), parseFloat(centerpoint.x));
	map.setCenter(newcenter);
	
} 