   var map = null;
   var point = null;
   var geocoder = null;
   var marker = null;

   function create_post_list()
   {
      var i;
      var list = '';
      for (i = 0; i < document.forms['gpx'].length; i++)
      {
         if (document.forms['gpx'].elements[i].type == 'text' ||
             document.forms['gpx'].elements[i].type == 'textarea')
         {
            if (i > 1)
            {
               list += '&';
            }

            list += document.forms['gpx'].elements[i].id;
            list += '=';
            list += document.forms['gpx'].elements[i].value;
         }
      }

      list += '&term=';
      list += '';

      if (document.forms['gpx'].crlf.checked)
      {
         list += 'crlf';
      }

      if (document.forms['gpx'].lf.checked)
      {
         list += 'lf';
      }

      if (document.forms['gpx'].cr.checked)
      {
         list += 'cr';
      }

      return list;
   }

   function toFixedLatLon()
   {
      var lon = parseFloat(document.forms['gpx'].lon.value);
      if (!isNaN(lon))
      {
         document.forms['gpx'].lon.value = lon.toFixed(6);
      }

      var lat = parseFloat(document.forms['gpx'].lat.value);
      if (!isNaN(lat))
      {
         document.forms['gpx'].lat.value = lat.toFixed(6);
      }
   }

   function updateLatLon()
   {
      var point = marker.getLatLng();
      document.forms['gpx'].lon.value = point.lng().toFixed(6);
      document.forms['gpx'].lat.value = point.lat().toFixed(6);
   }

   function print_lat_lon(marker)
   {
      var oStatusDiv = document.getElementById("map_latlon");
      if (oStatusDiv)
      {
         var point = marker.getLatLng();
         document.getElementById("map_latlon").innerHTML = '<form>'
                                                         + '<label class="geocode" for="update">Click on map or drag marker to find new coordinates.<br/>Press the Update button to update the waypoint.</label>'
                                                         + "<input class='geocode' type='button' value='Update' id='update' onclick='updateLatLon();'/>"
                                                         + '<br/>'
                                                         + '<br/>'
                                                         + 'Longitude: ' + point.lng().toFixed(6)
                                                         + '<br/>'
                                                         + 'Latitude: ' + point.lat().toFixed(6)
                                                         + '</form>';
      }
   }

   function addListener_dragend(marker)
   {
      GEvent.addListener(marker, 'dragend', function()
      {
         print_lat_lon(marker);
      });
   }

   function addListener_click(map)
   {
      GEvent.addListener(map, 'click', function(overlay, point)
      {
         if (point)
         {
            map.removeOverlay(marker);
            marker = new GMarker(point, {draggable: true});
            map.addOverlay(marker);

            print_lat_lon(marker);

            addListener_dragend(marker);
         }
      });
   }

   function showAddress(place)
   {
      var lon = place.Point.coordinates[0];
      if (lon != null)
      {
         document.forms['gpx'].lon.value = lon.toFixed(6);
      }

      var lat = place.Point.coordinates[1];
      if (lat != null)
      {
         document.forms['gpx'].lat.value = lat.toFixed(6);
      }

      // alert(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);

      var country = place.AddressDetails.Country.CountryNameCode;
      if (country != null)
      {
         document.forms['gpx'].country.value = country;
      }

       var state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
       if (state != null)
       {
          document.forms['gpx'].state.value = state;
       }

      // if (country == 'US')
      if (place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea == undefined)
      {
         var staddr = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
         var city = place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
         var postalcode = place.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
      }
      else
      {
         var staddr = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
         var city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
         var postalcode = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
      }

      if (staddr != null)
      {
         document.forms['gpx'].staddr.value = staddr;
      }

      if (city != null)
      {
         document.forms['gpx'].city.value = city;
      }

      if (postalcode != null)
      {
         document.forms['gpx'].postalcode.value = postalcode;
      }
   }

   function initialize()
   {
      if (GBrowserIsCompatible())
      {
         map = new GMap2(document.getElementById("map_canvas"));
         geocoder = new GClientGeocoder();

         point = new GLatLng(17, 0);
         map.setCenter(point, 1);
         marker = new GMarker(point, {draggable: true});
         map.addOverlay(marker);

         map.addControl(new GMapTypeControl());
         map.addControl(new GSmallMapControl());

         print_lat_lon(marker);

         addListener_click(map);
         addListener_dragend(marker);
      }
   }

   function geocode_addr()
   {
      var address = document.forms['gpx'].staddr.value + ','
                  + document.forms['gpx'].city.value + ','
                  + document.forms['gpx'].state.value + ' '
                  + document.forms['gpx'].postalcode.value + ','
                  + document.forms['gpx'].country.value;

      if (geocoder)
      {
         geocoder.getLocations(
         address,
         function(point)
         {
            if (!point || point.Status.code != 200)
            {
               alert("Address not found: " + address);

               return false;
            }
            else
            {
               place = point.Placemark[0];
               point = new GLatLng(place.Point.coordinates[1],
                                   place.Point.coordinates[0]);
               map.setCenter(point, 13);

               map.removeOverlay(marker);
               marker = new GMarker(point, {draggable: true});
               map.addOverlay(marker);

               print_lat_lon(marker);

               addListener_dragend(marker);

               showAddress(place);

               return true;
            }
         });
      }
   }
