/*
			"Placemark": [
				{
				  "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
				  "AddressDetails": {
					"Country": {
					  "CountryNameCode": "US",
					  "AdministrativeArea": {
						"AdministrativeAreaName": "CA",
						"SubAdministrativeArea": {
						  "SubAdministrativeAreaName": "Santa Clara",
						  "Locality": {
							"LocalityName": "Mountain View",
							"Thoroughfare": {
							  "ThoroughfareName": "1600 Amphitheatre Pkwy"
							},
							"PostalCode": {
							  "PostalCodeNumber": "94043"
							}
						  }
						}
					  }
					},
					"Accuracy": 8
				  },
				  "GX_Point": {
					"coordinates": [-122.083739, 37.423021, 0]
				  }
				}
			  ]
			}
			*/

function GX_Addr(p_street, p_city, p_state, p_lat, p_lon, p_text) {
	this.street = p_street;
	this.city = p_city;
	this.state = p_state;
	this.lat = p_lat;
	this.lon = p_lon;
	this.text = p_text;
	this.geom = "";
}
GX_Addr.prototype.getDisplayText = function() {
	var text = this.street;
	if(this.city != "")
		text += ", " + this.city;
	if(this.state != "")
		text += ", " + this.state;
	return text;
}

function GX_findAddress(addr, stateRestriction, cityRestriction, funcSearchDone) {
	var geocoder = new GClientGeocoder();
	var addresses = [];
	
	if(cityRestriction) {
		addr += ", " + cityRestriction; 
	}
	geocoder.getLocations(addr,  
		function(response) {
			if (!response || response.Status.code != 200) {
				//alert("Sorry, we were unable to geocode that address");
				funcSearchDone(addresses);
			} else {
				var numPoints = response.Placemark.length;				
				
				for(var i=0; i<numPoints; i++) {      				
					var place = response.Placemark[i];

					if(place.AddressDetails.Country.CountryNameCode != "US")
						continue;
						
					var x = place.AddressDetails.Country.AdministrativeArea;
					var showDefault = true;
					
					if(x) {
						var state = x.AdministrativeAreaName;							
						if(stateRestriction) {
							if(state != stateRestriction) {
								continue;
							}
						}

						if(x.SubAdministrativeArea)
							x = x.SubAdministrativeArea;
						
						//alert(x.locality);
						//alert(response);
						if(x.Locality && x.Locality.Thoroughfare) {
							var city = x.Locality.LocalityName;
							if(cityRestriction) {
								if(city != cityRestriction) continue;
							}
							var street = x.Locality.Thoroughfare.ThoroughfareName;
							
							addresses.push(new GX_Addr(street, city, state, place.Point.coordinates[1],
								place.Point.coordinates[0]));
							showDefault = false;
						}
						
					} else {
						if(stateRestriction) {
							continue;
						}
					}
					if(showDefault) {
						var addr = place.address;
						
						addresses.push(new GX_Addr(addr, "", "", place.Point.coordinates[1],
								place.Point.coordinates[0], addr));							
					}
				}
				
				for(var a=0; a<addresses.length; a++) {
					var addr = addresses[a];
					if(addr.street != "") {
						var url = getServer() + "/findParcel" + "?" + 
											"appName=" + getAppName() + "&" +
											"street=" +	URLencode(addr.street) + 
											"&city=" + URLencode(addr.city) + 
											"&state=" + URLencode(addr.state);
						HttpGetRequestSync(url, "/findParcel", function(output) {
							var response = getXMLDocument(output);
							if(response.getElementsByTagName("Latitude").item(0)) {					
								addr.lat = response.getElementsByTagName("Latitude").item(0).firstChild.nodeValue;
								addr.lon = response.getElementsByTagName("Longitude").item(0).firstChild.nodeValue;
								if(response.getElementsByTagName("geom").item(0))
									addr.geom = response.getElementsByTagName("geom").item(0).firstChild.nodeValue;
							}
						});
					}						
				}
				funcSearchDone(addresses);
			}
		});
	
	
	return false;
}

