     // Google Variables
     var geocoder = new GClientGeocoder();

     var markerArray = [];
     var markerTextArray = [];
     var markerToArray = [];
     var markerFromArray = [];

     // Create a base icon for all of our markers that specifies the
     // shadow, icon dimensions, etc.
     var baseIcon = new GIcon();
     baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
     baseIcon.iconSize = new GSize(20, 34);
     baseIcon.shadowSize = new GSize(37, 34);
     baseIcon.iconAnchor = new GPoint(9, 34);
     baseIcon.infoWindowAnchor = new GPoint(9, 2);
     baseIcon.infoShadowAnchor = new GPoint(18, 25);


     var address  = "";

     function buildAddressString(attributeFormIdPrefix)
     {
       var street = document.getElementById(attributeFormIdPrefix+"street").value;
       var city   = document.getElementById(attributeFormIdPrefix+"city").value;
       var state  = document.getElementById(attributeFormIdPrefix+"state").value;
       var zip    = document.getElementById(attributeFormIdPrefix+"zip").value;

       var address = street+" "+city+", "+state+" "+zip;

       return address;
     }

     var mapDivId;
     var addressDivId;
     var addressTableId; 
     var coordinateFieldId;
     var addressFieldId;
     var buttonId;

     function getCoordinates(attributeFormIdPrefix, _mapDivId, _addressDivId, 
       _addressTableId, _coordinateFieldId, _buttonId, _addressFieldId)
     {
       address = buildAddressString(attributeFormIdPrefix);
   
       mapDivId = _mapDivId;
       addressDivId = _addressDivId;
       addressTableId = _addressTableId;
       coordinateFieldId = _coordinateFieldId;
       buttonId = _buttonId;
       addressFieldId = _addressFieldId;

       geocoder.getLocations(address, findAddressMatch);
     }

     function findAddressMatch(response)
     {
       if (!response || response.Status.code != 200)
       {
	     alert("Google Maps was not able to locate address \"" + address + "\"");
       }
       else
       {
         var map = new GMap2(document.getElementById(mapDivId));
         
         map.addControl(new GSmallMapControl());
         map.addControl(new GMapTypeControl());

         var bounds = new GLatLngBounds();

         map.setCenter(new GLatLng(0,0),0);

         var addressDiv = document.getElementById(addressDivId);
         var addressTable = document.getElementById(addressTableId);

         while (addressTable.hasChildNodes())
	     {
	       addressTable.removeChild(addressTable.firstChild);
	     }


         var placeMarkArray = response.Placemark;

         for (var i=0; i<placeMarkArray.length; i++)
         {

           var letter = String.fromCharCode("A".charCodeAt(0) + i);

           placeMark = placeMarkArray[i];

           var tr = document.createElement('tr');
           var td = document.createElement('td');

           tr.appendChild(td);

           var tbody = document.createElement('tbody');
           tbody.appendChild(tr);
           addressTable.appendChild(tbody);

           var point =
             new GLatLng(placeMark.Point.coordinates[1], placeMark.Point.coordinates[0]);

           var coord1 = placeMark.Point.coordinates[1];
           var coord2 = placeMark.Point.coordinates[0];

           bounds.extend(point);

           var icon = new GIcon(baseIcon);
           icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
           var marker = createMarker(point,icon, i, placeMark.address);


           // The inactive version of the direction info
           var addressString = placeMark.address + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';


           GEvent.addListener(marker, "click", function() {
             marker.openInfoWindowHtml(addressString);
           });


           var optionString =
             "<button onclick='storeCoordinates(\""+coordinateFieldId+"\", \""+placeMark.address+"\", \""+coord1+"\", \""+coord2+"\")'>Select</button>&nbsp;&nbsp;<a style='cursor:pointer;' href='javascript:markerClick("+i+")'><b>"+
             letter+"</b></a> &nbsp;&nbsp;"+placeMark.address;;

           td.innerHTML = optionString;

           markerArray[i] = marker;
           markerTextArray[i] = addressString;

           map.addOverlay(marker);

         }

         if (map.getBoundsZoomLevel(bounds) > 15)
         {
           map.setZoom(15);
         }
         else
         {
           map.setZoom(map.getBoundsZoomLevel(bounds));
         }

         map.setCenter(bounds.getCenter());

         var submitButton = document.getElementById(buttonId);
         submitButton.style.cursor = 'pointer';
         addressDiv.style.visibility="visible";
         map.style.visibility="visible";
       }
     }

     function storeCoordinates(coordinateFieldId, address, coord1, coord2)
     {
       coordinateInput = document.getElementById(coordinateFieldId); 
       coordinateInput.value = coord1+","+coord2;
       addressInput = document.getElementById(addressFieldId); 
       addressInput.value = address;
       
       var addressDiv = document.getElementById(addressDivId);
       var map = document.getElementById(mapDivId);

       addressDiv.style.visibility="hidden";
       map.style.visibility="hidden";
     }

      function markerClick(i) {
        markerArray[i].openInfoWindowHtml(markerTextArray[i]);
      }

      // functions that open the directions forms
      function tohere(i) {
        markerArray[i].openInfoWindowHtml(markerToArray[i]);
      }
      function fromhere(i) {
        markerArray[i].openInfoWindowHtml(markerFromArray[i]);
      }


      // A function to create the marker and set up the event window
      function createMarker(point,icon, index, addressString)
      {
        var marker = new GMarker(point, icon);

        var name = "Marker "+index;

        // The info window version with the "to here" form open
        markerToArray[index] = addressString + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + index + ')">From here</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/>';
        // The info window version with the "to here" form open
        markerFromArray[index] = addressString + '<br>Directions: <a href="javascript:tohere(' + index + ')">To here</a> - <b>From here</b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" +
           '"/>';

        return marker;
      }


