  var map;
  var gdir;
  var request = null;
  var filename = null;
  var baseIcon = null;
  var icons = [];
  var ggmarkers = [];
  var currentMarker = 0;
  var baseLat = 29.520929;
  var baseLng = -98.500940;

  google.load("maps", "2.x");

  function gLoad()
  {
    if (document.getElementById("map"))
    {
	    if (GBrowserIsCompatible())
	    {
		    baseIcon = new google.maps.Icon();
		    baseIcon.iconSize = new google.maps.Size(59, 50);
		    baseIcon.iconAnchor = new google.maps.Point(29, 50);
		    baseIcon.infoWindowAnchor = new google.maps.Point(18, 6);
		    show("map");
		    map = new google.maps.Map2(document.getElementById("map"));
		    map.addControl(new google.maps.LargeMapControl());
		    map.addControl(new google.maps.MapTypeControl());
		    google.maps.Event.addListener(map, "load", initializeMap);
		    map.setCenter(new google.maps.LatLng(baseLat,baseLng), 10);
	    }
    }
  }

  function setDirections(fromAddress, toAddress, locale)
  {
    if (gdir == null)
    {
	    gdir = new google.maps.Directions(map, document.getElementById("directions"));
	    google.maps.Event.addListener(gdir, "load", onGDirectionsLoad);
	    google.maps.Event.addListener(gdir, "error", handleErrors);
    }
    show("directions");		
    gdir.load("from: " + fromAddress + " to: " + toAddress,
              { "locale": locale });

  }

  function getDirections()
  {
    var _addr = document.getElementById("streetAddress").value;
    var _city = document.getElementById("city").value;
    var _state = document.getElementById("state").value;
    var _zip = document.getElementById("zip").value;
    var _location = document.getElementById("startAddress").value;
    if (_location == 'n/a')
    {
	    alert('Please select a Destination Address.');
	    return;
    }

    if ((_addr == '') && (_city == '') && (_state == '') && (_zip == ''))
    {
	    alert('Please enter a starting address.');
	    document.getElementById("streetAddress").focus();
	    return;
    }

    var _toAddress = _addr + " " + _city + ", " + _state + " " + _zip;
    		
    setDirections(_toAddress, _location, "en_US");
  }

  function show(object)
  {
    if (document.getElementById && document.getElementById(object) != null)
	    node = document.getElementById(object).style.display='block';
    else if (document.layers && document.layers[object] != null)
	    document.layers[object].display = 'block';
    else if (document.all)
	    document.all[object].style.display = 'block';
  }

  function hide(object)
  {
    if (document.getElementById && document.getElementById(object) != null)
	    node = document.getElementById(object).style.display='none';
    else if (document.layers && document.layers[object] != null)
	    document.layers[object].display = 'none';
    else if (document.all)
	    document.all[object].style.display = 'none';
  }


  function handleErrors()
  {
     if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
       alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
     else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
       alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
     
     else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
       alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

    //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
    //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
       
     else if (gdir.getStatus().code == G_GEO_BAD_KEY)
       alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

     else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
       alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
      
     else alert("An unknown error occurred.");
   
  }

  function onGDirectionsLoad()
  { 
    // Use this function to access information about the latest load()
    // results.

    // e.g.
    // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
    // and yada yada yada...
  }

  //	Toggle the markers
  function toggleGGMarkers(_display)
  {
    hideCurrentMarkers();
    displayMarkers(_display);
  }


  //	Hide Current Markers
  function hideCurrentMarkers()
  {
    map.getInfoWindow().hide();
    var _markers = ggmarkers[currentMarker];
    if (_markers != null)
    {
	    for(var i=0; i < _markers.length; i++)
	    {
		    map.removeOverlay(_markers[i]);
	    }
    }
  }

  //	Show Markers
  function displayMarkers(_display)
  {
    var _markers = ggmarkers[_display];
    for(var i = 0; i < _markers.length; i++)
    {
	    map.addOverlay(_markers[i]);
    }
    currentMarker = _display;
  }

  //	Create an XmlHttpRequest
  function createRequest(fileName, processRequest)
  {
    request = google.maps.XmlHttp.create(); 
    request.open("GET", fileName, true); 
    request.onreadystatechange = processRequest;
    request.send(null);
  }

  //	Create a marker but don't display
  function buildMarker(type, point, image, html)
  {
    var icon = createIcon(type, image);
    var marker = new google.maps.Marker(point, icon);
    google.maps.Event.addListener(marker, "click", function()
    {
	    marker.openInfoWindowHtml(html);
    });
    return marker;
  }

  //	Create Icon
  function createIcon(iconType, imageName)
  {
    if (!icons[iconType])
    {
	    var icon = new google.maps.Icon(baseIcon);
	    icon.image = imageName;
	    icons[iconType] = icon;
    }
    return icons[iconType];
  }
  
  google.setOnLoadCallback(gLoad);