gogoGoogleMap = 
{
  
  auto_attach_picker: function(form) 
  {
    // Search the document input elements for something named Latitude or Longitude
    // When found one, search for the matching other half
    // Look for any applicable Country, City, Street fields
    //  (to use for geocoding)
    // When we have everything attach a button to the second co-ordinate field (Longitude Probably)
    // which opens the picker window with the current Lat, Lon and Address, and callback for 
    // saving the values
    
    if(!form)
    {
      var forms = document.getElementsByTagName('form');
      for(var x = 0; x < forms.length; x++)
      {
        this.auto_attach_picker(forms[x]);
      }      
      return;
    }
    
    // Find input elements
    var inputs = form.getElementsByTagName('input');
    
    // Look for lat or lon    
    for(var x = 0; x < inputs.length; x++)
    {
      var fields = { Latitude: null, Longitude: null, Street: null, City: null, Country: null };
      var prefix = null;
      var suffix = null;
      var attachTo = null;
      
      if(inputs[x].name.match(/^(.*)(Latitude|Longitude)(.*)$/))
      {
        prefix = RegExp.$1;                
        suffix = RegExp.$3;
        fields[RegExp.$2] = inputs[x];
                
        // And find the matching
        var lookingFor = prefix + (RegExp.$2 === 'Latitude' ? 'Longitude' : 'Latitude') + suffix; 
        for(var i = x+1; i < inputs.length; i++)
        {
          if(inputs[i].name == lookingFor)
          {
            fields[(RegExp.$2 === 'Latitude' ? 'Longitude' : 'Latitude')] = inputs[i];
            attachTo = inputs[i];
            break;
          }
        }
        
        if(fields.Longitude && fields.Latitude)
        {          
          // Got both, try and find street city and country data
          // note region doesn't work so well because Google gets confused on like
          // "South Island" as a region
          for(var i = 0; i < inputs.length; i++)
          {
            if(fields.Country == null && inputs[i].name === (prefix + 'Country' + suffix))
            {
              fields.Country = inputs[i];
            }
            else if(fields.City == null && inputs[i].name === (prefix + 'City' + suffix))
            {
              fields.City = inputs[i];
            }
            else if (
              fields.Street == null &&
              (
                    inputs[i].name === (prefix + 'Street' + suffix)
                ||  inputs[i].name === (prefix + 'Address' + suffix)
                ||  inputs[i].name === (prefix + 'Street1' + suffix)
              )
            )
            {
              fields.Street = inputs[i];
            }
          }

          this.attach_picker(attachTo, fields);          
          
        }                
      }
    }    
    
  },
  
  attach_picker: function(attachTo, fields)
  {
    fields.button_callback = function(Latitude, Longitude)
    {
      this.Latitude.value  = Latitude;
      this.Longitude.value = Longitude;
    }
    
    var afi = this._active_fields.length;
    this._active_fields[this._active_fields.length] = fields;    
    
    var button = document.createElement('input');
    button.type = 'button';
    button.value = 'Map';
    button.gogoGoogleMapFieldIndex = afi;
    
    button.onclick             = function()
      {
        var fields = gogoGoogleMap._active_fields[this.gogoGoogleMapFieldIndex];
        
        // Build an address string
        var address = [ ];
        if(fields.Street.value)
        {
          address[address.length] = fields.Street.value;
        }
        
        if(fields.City.value)
        {
          address[address.length] = fields.City.value;  
        }
        
        if(fields.Country.value)
        {
          address[address.length] = fields.Country.value;
        }
        
        address = address.join(', ');
           
        var url = '/__classpath/gogo/gogoGoogleMap/picker.html?Callback=gogoGoogleMap._active_fields[' + this.gogoGoogleMapFieldIndex + '].button_callback&Address=' + encodeURIComponent(address) + '&Latitude=' + encodeURIComponent(fields.Latitude.value) + '&Longitude=' + encodeURIComponent(fields.Longitude.value);
        
        var win = window.open(url, 'MapWindow', 'width=570,height=540');
        win.focus();
        
      }
      
      attachTo.parentNode.insertBefore(button, attachTo.nextSibling);
  },
      
  _active_fields: [ ]
}