// GMap2 object
var map = document.getElementById('googleMapNew');
// GReverseGeocoder object
var rg;
// text input fields
var lat;
var lng;
// result div
var info;

function load() {
  if (GBrowserIsCompatible()) {
    lat = document.getElementById("lat");
    lng = document.getElementById("lng");

    rg = new GReverseGeocoder(map);

    // add listners for the results
    GEvent.addListener(rg, "load", goodresult);
    GEvent.addListener(rg, "error", badresult);
    // Clicking on the map fills in the lat and lng fields
    // Just handy
    GEvent.addListener(map, "click", handleClicks);
    GEvent.addListener(map, "click", goodresult);
  }
}

// is called with a placemark if the reverse geocode request was successfull
// sets the result div
function goodresult(placemark) {
  //alert('inside goodresult');
//  var html = placemark.address + '<br />' + '<b>Country code:</b> ' + placemark.AddressDetails.Country.CountryNameCode;
  //info.innerHTML = html;
  var address = placemark.address;
  document.getElementById('search_add').value = address;
  //document.getElementById('search_add_1').value = address;
  //check_address();
  
  //var postalcodenumber = rg.getPlacemarkProperty(placemark,"PostalCodeNumber");
  //if (postalcodenumber != null) alert("Postal Code Number: " + postalcodenumber);
  //else alert("Postal Code Number Unknown");
}

// is called if the reverse geocode request was unsuccessfull
function badresult() {
  //info.innerHTML = "Unable to reverse geocode";
}

// get the input form lat and lng fields and issue a reverse geocode
// request
function reverse(){
  var point = new GLatLng(document.getElementById("lat").value,document.getElementById("lng").value);
  rg.reverseGeocode(point);
}

// handy method to fill in the lat and lng fields by clicking on the map.
function handleClicks(marker, point){
	document.getElementById("lat").value=point.lat();
	document.getElementById("lng").value=point.lng();
}