    var map;
    var geocoder;
	var form;
    var url;

    function geocode_initialize() {
      map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(34, 0), 1);
	  
	  geocoder = new GClientGeocoder();
	  
	  if(typeof(geocode_page_init) != "undefined")
		form = geocode_page_init();
    }
	
	 function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
      }


    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to locate that address");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        
		originalSearchTerm = form.location.value;
		
		
		form.LatLng.value = place.Point.coordinates[1] + ' ' + place.Point.coordinates[0];
        form.CountryCode.value = place.AddressDetails.Country.CountryNameCode;
		
		if (place.AddressDetails.Country.AdministrativeArea)
		  form.region.value = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
		else
		  form.region.value = "";
          
        var params = "search=" + originalSearchTerm + "&address=" + place.address + "&region=" + form.region.value + "&country_code=" + form.CountryCode.value;
		
        ajax(url, params, processAddress);
		
		//form.location.value = cleanAddress(place.address, form.region.value, originalSearchTerm);  
		
		marker = new GMarker(point, {title:place.address});
		
		map.addOverlay(marker);
		map.panTo(marker.getLatLng());
		
        //marker.openInfoWindowHtml(place.address);
		//+ '<br>' +'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
      }
      locChanged = false;
    }
    
    function processAddress(response)
    {
      form.location.value = response;
    }
	
	function addPointToMap(name, lat, lng) {
      map.clearOverlays();
      point = new GLatLng(lat, lng);
      marker = new GMarker(point);
		
	  map.addOverlay(marker);
      marker.openInfoWindowHtml(name);
    }

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation(address) {
      address = trim(address);
      if (address.length>0)
        geocoder.getLocations(address, addAddressToMap);
    }
	
	
	function cleanAddress(address, region, originalSearchTerm)
	{
	  addressItems = address.split(',');
	  
	  searchTerm = getWordsBeforeFirstComma(originalSearchTerm);
	  searchTerm = trim(searchTerm.toLowerCase());
	  
	  addressCount = addressItems.length;
	 
	  i = 0;
	  
	  // cut out stuff from address that appears before the search term 
	  if (addressCount > 2)
	  {
		foundSearchTerm = false;
		for (i=0; i<addressCount; i++)
		{
		  if (searchTerm == trim(addressItems[i].toLowerCase()))
		  {
			foundSearchTerm = true;
			name = trim(addressItems[i]);
			break;
		  }			  
		}
		if (!foundSearchTerm)
		  i = 0;
	  }
	  
	  // remove region from the first part of the address
	  address = removeSupplement(trim(addressItems[i]), region);
            
	  // reconstruct new address using just the parts after and including the search term
	  for (j=i+1; j<addressCount; j++)
	  {
		  address += ',' + addressItems[j];
	  }	  
	  
	  return address;	  
	}
	
	function removeSupplement(name, extra)
	{	  
	  // remove extra stuff if it appears at the end of name
	  if (extra.length > 0)
	  {
		nameArray = name.split(' ');
		
		lastIndex = nameArray.length - 1;
		if (lastIndex>0)
		{
		  if (extra.toLowerCase() == trim(nameArray[lastIndex].toLowerCase()))
		  {
			nameArray.splice(lastIndex,1);
			name = nameArray.join(' ');
		  }
		}
	  }
	  return name;
	} 
	
	function trim(str)
	{
	  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');  
	}
	
	function getWordsBeforeFirstComma(str) {
	  pos = str.indexOf(',');
	  if (pos==-1||pos==0)
		return str;
	  else
		return str.substr(0, pos);
	}
	
	function countWords(str) {
	  return str.split(',').length;
	}

	function getWordsBeforeLastComma(str) {
	  pos = str.lastIndexOf(',');
	  if (pos==-1||pos==0)
		return str;
	  else
		return str.substr(0, pos);
	}

    var locChanged = false;
    function locationChanged()
    {
      locChanged = true;
    }
    