

    var marker;
    var markerMode;
    var place;
    var map;
    var infoElement;
    var directions;
    var mapZoom = 14;
    var mapEnableZoomControls = true;
    var mapEnableMapTypeControls = true;
    var mapEnableMarker = true;

    //<![CDATA[

    function LoadMap(addr) {
      if (GBrowserIsCompatible()) {
       GetAddress(addr);
      }
    }
    
    function GetAddress(addr)
    {
         var finder = new GClientGeocoder();
        finder.getLocations(addr, OnGotLocations);
    }
    
    function OnGotLocations(/*Placemark[]*/ response)
    {
        if (response
            && response.Status.code == 200)
        {
            map = new GMap2(document.getElementById("map"));
           
            place = response.Placemark[0];
          
            var point = new GLatLng(place.Point.coordinates[1],
                                    place.Point.coordinates[0]);
            map.setCenter(point, mapZoom);

            if (mapEnableMarker)
            {
                marker = new GMarker(point, G_DEFAULT_ICON);
                map.addOverlay(marker);
            }
            map.enableScrollWheelZoom();
            if (mapEnableMapTypeControls)
            {
                map.addControl(new GMapTypeControl(),
                   new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 20)));
            }
            if (mapEnableZoomControls)
            {
                map.addControl(new GSmallZoomControl(),
                   new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10)));
            }
            infoElement = document.createElement("span");
            if (marker)
            {
                marker.bindInfoWindow(infoElement);
                SetMarkerInfo();
            }
        }
        else
        {
            document.getElementById("map").innerHTML = "<div style=\"text-align: left; margin: 20px 20px 20px 20px;\"><p>&nbsp;</p><h1>Invalid Address</h1><p>No map is available for address &quot;"+addr+"&quot;</p><p><b>Try a different address: </b><br/><input type=\"text\" id=\"addr\" style=\"width: 300px;\" value=\""+addr+"\" /> <input type=button onclick=\"addr=document.getElementById('addr').value; GetAddress();\" value=\"   Re-Submit   \" /><br/><span style=\"font-size: 8pt;\">NOTE: Sometimes removing a Building Name, or Postal Code can help google to resolve the address.</span></div>";
        }
    }


    function SetMarkerInfo()
    {
        if (!marker)
            return;
   
        switch(markerMode)
        {
            case "from":
                infoElement.innerHTML = "<div style=\"text-align:left\"><h1>From:</h1>" 
                + place.address + "<h1>To:</h1> <input type=\"text\" id=\"driveAddress\" /> <input type=\"button\" value=\" Get Directions \" onclick=\"GetDirections(false);\"/>"
                + "<br /><a href=\"javascript:SetMarkerMode('');\">cancel</a><div id=\"feedback\"></div></div>";
                break;
            case "to":
                 infoElement.innerHTML = "<div style=\"text-align:left\"><h1>From:</h1>" 
                 + "<input type=\"text\" id=\"driveAddress\" /> "
                 + "<h1>To:</h1> "+ place.address+"<input type=\"button\" value=\" Get Directions \" onclick=\"GetDirections(true);\"/>"
                 + "<br /><a href=\"javascript:SetMarkerMode('');\">cancel</a></div><div id=\"feedback\"></div>";
                break;
            default:
               infoElement.innerHTML = "<div style=\"text-align:left\"><h1>Address:</h1>" 
                + place.address + "<br/><br/>Get Directions: <a href=\"javascript:SetMarkerMode('to');\">To Here</a>, <a href=\"javascript:SetMarkerMode('from');\">From Here</a></div>";
               break;
        }
       
    }
    function GetDirections(goingTo)
    {
        document.getElementById('directions').style.display = 'none';
        if (document.getElementById('feedback'))
           document.getElementById('feedback').innerHTML = "Fetching directions ... ";

         if (directions==null)
         {
            directions = new GDirections(map, document.getElementById("directions"));
            GEvent.addListener(directions, "load", OnDirectionsSuccess);
            GEvent.addListener(directions, "error", OnDirectionsError);
         }
         
         if (goingTo)
            directions.load("from:"+document.getElementById("driveAddress").value+" to:"+place.address);
         else
            directions.load("from:"+place.address+" to:"+document.getElementById("driveAddress").value);
    }
    
    function OnDirectionsSuccess()
    {
        document.getElementById('directions').style.display = 'block';
        if (document.getElementById('feedback'))
           document.getElementById('feedback').innerHTML = "";
    }
    
    function OnDirectionsError()
    {
        document.getElementById('directions').style.display = 'none';
        var error;
        switch(directions.getStatus().code)
        {
            case G_GEO_UNKNOWN_ADDRESS:
                error = "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: " + directions.getStatus().code;
                break;
            case G_GEO_SERVER_ERROR:
                error = "A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code;
                break;
            case G_GEO_MISSING_QUERY:
                error = "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;
                break;
            case G_GEO_BAD_KEY:
                error = "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: " + directions.getStatus().code;
                break;
            case G_GEO_BAD_REQUEST:
                error = "A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code;
                break;
            default:
                error = "An unknown error occurred.";
                break;
        }

         if (document.getElementById('feedback'))
           document.getElementById('feedback').innerHTML = error;
    }
    
    function SetMarkerMode(mode)
    {
        markerMode = mode;
        SetMarkerInfo();
    }

