
    var geocoder;
    var address;
    var label;
    var address1;
    var address2;
    var city;
    var rings;
    var directionsPanel;
    var directions;
    var homeaddress;
    var saddr;
    var daddr;
    var homepoint;
    var homeroutepoint;
    var hasService;
    var hasHomeMarker = "0";
    var homemarker;

    document.getElementById("rangerings").checked = false;
    document.getElementById("rangerings").disabled = true;

    geocoder = new GClientGeocoder();



    if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventualkly be placed in the side_bar
      var side_bar_html = "";

      // arrays to hold copies of the markers used by the side_bar
      var gmarkers = [];
      var gicons = [];
      var i = 0;

      // arrays to hold variants of the info window html with get direction forms open
      var htmls = [];
      var to_htmls = [];
      var from_htmls = [];

          // Marker icon information
          var AllinaIcon = new GIcon();
          AllinaIcon.image = '/clinicfinder/icons/mm_20_blue.png';
          AllinaIcon.shadow = '/clinicfinder/icons/mm_20_shadow.png';
          AllinaIcon.iconSize = new GSize(12, 20);
          AllinaIcon.shadowSize = new GSize(22, 20);
          AllinaIcon.iconAnchor = new GPoint(6, 20);
          AllinaIcon.infoWindowAnchor = new GPoint(5, 1)

          var AllinaIcon2 = new GIcon();
          AllinaIcon2.image = '/clinicfinder/icons/mm_20_red.png';
          AllinaIcon2.shadow = '/clinicfinder/icons/mm_20_shadow.png';
          AllinaIcon2.iconSize = new GSize(12, 20);
          AllinaIcon2.shadowSize = new GSize(22, 20);
          AllinaIcon2.iconAnchor = new GPoint(6, 20);
          AllinaIcon2.infoWindowAnchor = new GPoint(5, 1)

          var AllinaIcon3 = new GIcon();
          AllinaIcon3.image = '/clinicfinder/icons/mm_20_green.png';
          AllinaIcon3.shadow = '/clinicfinder/icons/mm_20_shadow.png';
          AllinaIcon3.iconSize = new GSize(12, 20);
          AllinaIcon3.shadowSize = new GSize(22, 20);
          AllinaIcon3.iconAnchor = new GPoint(6, 20);
          AllinaIcon3.infoWindowAnchor = new GPoint(5, 1)

          var AllinaIcon4 = new GIcon();
          AllinaIcon4.image = '/clinicfinder/icons/mm_20_purple.png';
          AllinaIcon4.shadow = '/clinicfinder/icons/mm_20_shadow.png';
          AllinaIcon4.iconSize = new GSize(12, 20);
          AllinaIcon4.shadowSize = new GSize(22, 20);
          AllinaIcon4.iconAnchor = new GPoint(6, 20);
          AllinaIcon4.infoWindowAnchor = new GPoint(5, 1)
		  
		  var AllinaIcon5 = new GIcon();
          AllinaIcon5.image = '/clinicfinder/icons/mm_20_orange.png';
          AllinaIcon5.shadow = '/clinicfinder/icons/mm_20_shadow.png';
          AllinaIcon5.iconSize = new GSize(12, 20);
          AllinaIcon5.shadowSize = new GSize(22, 20);
          AllinaIcon5.iconAnchor = new GPoint(6, 20);
          AllinaIcon5.infoWindowAnchor = new GPoint(5, 1)

          // Set up our GMarkerOptions object
          markerOptions = { icon:AllinaIcon5 };

      // Associate marker icon with correct catergory
      gicons["HOS"] = new GIcon(AllinaIcon);
      gicons["PHR"] = new GIcon(AllinaIcon5);
      gicons["URG"] = new GIcon(AllinaIcon3);
      gicons["CLI"] = new GIcon(AllinaIcon4);
    //  gicons["SPC"] = new GIcon(AllinaIcon2);

      // A function to create the marker and set up the event window
      function createMarker(point,name,html,category,loc_srv) {
        var marker = new GMarker(point,gicons[category]);

        // The info window version with the "to here" form open 
        to_htmls[i] = name+"@"+ point.lat() + ',' + point.lng();
        // The info window version with the "to here" form open
        from_htmls[i] = name+"@"+ point.lat() + ',' + point.lng();

        // The inactive version of the direction info
        html = html + '<br><br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></span>';

        // === Store the category and name info as a marker properties ===
        marker.mycategory = category;
        marker.myname = name;
        marker.myservice = loc_srv;  // switch to service eventually

        // === store the name so that the tooltip function can use it ===
        marker.tooltip = '<div class="tooltip">'+name+'</div>';

        GEvent.addListener(marker, "click", function() {
           marker.openInfoWindowHtml(html);
           map.setCenter(point,12);
        });

        //  ======  The new marker "mouseover" and "mouseout" listeners  ======
        GEvent.addListener(marker,"mouseover", function() {
           showTooltip(marker);
        });
        GEvent.addListener(marker,"mouseout", function() {
           tooltip.style.visibility="hidden"
        });

        i++;

        gmarkers.push(marker);
        return marker;
      }

       // allows user to press enter on address text field
       function onEnter(){
        if(event.keyCode==13)
        SetHomeLocation();
       }


      // ====== This function displays the tooltip ======
      // it can be called from an icon mousover or a side_bar mouseover
      function showTooltip(marker) {
        tooltip.innerHTML = marker.tooltip;
        var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
        var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
        var anchor=marker.getIcon().iconAnchor;
        var width=marker.getIcon().iconSize.width;
        var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y));
        pos.apply(tooltip);
        tooltip.style.visibility="visible";
      }

      // ===== This function is invoked when the mouse goes over an entry in the side_bar =====
      // It launches the tooltip on the icon
      function mymouseover(i) {
        showTooltip(gmarkers[i])
      }
      // ===== This function is invoked when the mouse leaves an entry in the side_bar =====
      // It hides the tooltip
      function mymouseout() {
        tooltip.style.visibility="hidden";
      }

 // == for each location that has category checked, check if it has desired service  ==
      function showservice(category,service) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            var serviceString=gmarkers[i].myservice;
            if (serviceString.indexOf(service)==-1) { 
              //alert("service not found");
              gmarkers[i].hide();
            } else {
              gmarkers[i].show();
            }
         }
        }
      }

      // == shows all markers of a particular category, and ensures the checkbox is checked ==
      function show(category) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            gmarkers[i].show();
          }
        }
        // == check the checkbox ==
        document.getElementById(category+"box").checked = true;
      }

      // == hides all markers of a particular category, and ensures the checkbox is cleared ==
      function hide(category) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            gmarkers[i].hide();
          }
        }
        // == clear the checkbox ==
        document.getElementById(category+"box").checked = false;
        // == close the info window, in case its open on a marker that we just hid
        map.closeInfoWindow();
      }

      // == a checkbox has been clicked ==
      function boxclick(box,category) {

        switch(category)
        {
             case "HOS":
             case "CLI":
             case "URG":
           //  case "SPC":
             case "PHR":
                if (box.checked) {
                    show(category);
                } else {
                    hide(category);
                }
                // == rebuild the side bar
                makeSidebar();
                serviceclick();  // just in case a service is selected. 
                break;
             case "range":
                if (box.checked) {
                    map.addOverlay(rings);
                } else {
                    map.removeOverlay(rings);
                }
                break;
        }

      }

      function resetservice() {
            // show("SPC");
            if (document.getElementById("PHRbox").checked) { 
               show("PHR");
            }
            if (document.getElementById("CLIbox").checked) {
              show("CLI");
            }
            if (document.getElementById("URGbox").checked) {
              show("URG");
            }
            if (document.getElementById("HOSbox").checked) {
              show("HOS");
            }
            document.getElementById("service").value="0";
      }
 
      function removetext() {
            document.getElementById("homeaddr").value="";
      }


      // == a service has been selected ==
      function serviceclick() {
          // for selected categories, check to see if has those services
        var service=document.getElementById("service").value;
        //alert(service);
        if (service!=0) {

          if (document.getElementById("HOSbox").checked) {
           showservice("HOS",service); 
          } 
          if (document.getElementById("CLIbox").checked) {
           showservice("CLI",service);
          }
          if (document.getElementById("URGbox").checked) {
           showservice("URG",service);
          }
     //   if (SPCbox.checked) {
     //      //check all hospitals for specified service
     //      showservice("SPC",service);
     //   }
          if (document.getElementById("PHRbox").checked) {
           showservice("PHR",service);
          }
        } 
      }

// ===== check URL for parameters =====
// determine if parameters passed in URL, i.e. from location finder widget in header
q = location.search;
getParam = function(arg) {
if (q.indexOf(arg) >= 0) {
var pntr = q.indexOf(arg) + arg.length + 1;
if (q.indexOf("&", pntr) >= 0) {
return q.substring(pntr, q.indexOf("&", pntr));
} else {
return q.substring(pntr, q.length);
}
} else {
return null;
}
}



      // ===== request the directions =====
      function getDirections() {
        //alert(saddr + ' to ' + daddr);
        document.getElementById("route").innerHTML = " ";
        gdir.load("from: "+saddr+" to: "+daddr);
      }

      // functions that open the directions forms
      function tohere(i) {
        if(document.getElementById("rangerings").disabled) {
          alert("Please Set your Home location");
        } else {
          saddr = "Home@" + homeroutepoint;
          daddr = to_htmls[i];
          getDirections();
        }
      }
      function fromhere(i) {
        if(document.getElementById("rangerings").disabled) {
          alert("Please Set your Home location");
        } else {
          saddr = to_htmls[i];
          daddr = "Home@" + homeroutepoint;
          getDirections();
        }
      }

      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        GEvent.trigger(gmarkers[i], "click");
      }

      // == rebuilds the sidebar to match the markers currently displayed ==
      function makeSidebar() {
        var html = "";

        //alert(gmarkers.length);

        for (var i=0; i<gmarkers.length; i++) {
          if (!gmarkers[i].isHidden()) {
            html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '</a><br>';

          }
        }
        //document.getElementById("side_bar").innerHTML = html;
      }


      function SetHomeLocation() {

        // if there's a home icon alreay on map, remove it.
        if (hasHomeMarker=="1") {
           //alert("Theres a home marker already present");
           homemarker.hide(); 
        }


        homeaddress = document.getElementById("homeaddr").value;
        if (homeaddress.indexOf("Enter")==-1) {
         geocoder.getLatLng(homeaddress, function(latlng) {
          if (!latlng) {
           alert(homeaddress + ' not found');
           document.getElementById("rangerings").checked = false;
           document.getElementById("rangerings").disabled = true;
           map.removeOverlay(rings);
          } else {
           if(!document.getElementById("rangerings").disabled) {
           map.removeOverlay(rings);
           }
           searchLocationsNear(latlng);
           document.getElementById("rangerings").checked = false;
           hasHomeMarker = "1"; 
          }
        });
        } else {
           alert("Please enter your address and/or zip.");
        }
       }

       function searchLocationsNear(center) {

         var searchUrl = 'lat=' + center.lat() + '&lng=' + center.lng();

         //alert(searchUrl);

         //map.setCenter(new GLatLng(center.lat(), center.lng()), 11);

         var HomeIcon = new GIcon();

         HomeIcon.image = "/clinicfinder/icons/house.png";
         HomeIcon.shadow = "/clinicfinder/icons/shadow.png";

         HomeIcon.iconSize = new GSize(21, 31);
         HomeIcon.iconAnchor = new GPoint(7, 27);

         HomeIcon.infoWindowAnchor = new GPoint(16, 13);

         // Set up our GMarkerOptions object
         HomeOptions = { icon:HomeIcon };

         //Set Home Point variable for Directions routing function
         homeroutepoint = center.lat() + "," + center.lng();

         var homepoint = new GLatLng(center.lat(), center.lng());
         homemarker = new GMarker(homepoint, HomeOptions);

         map.addOverlay(homemarker);

         map.setCenter(homepoint,11);

         document.getElementById("rangerings").disabled = false;
         rings = new BdccRangeRings(homepoint, "#0000FF",3,0.5,null,5);

         return;

       }


      // create the map
      var map = new GMap2(document.getElementById("map"));

      //map.addMapType(G_SATELLITE_3D_MAP);

      map.addControl(new GHierarchicalMapTypeControl());
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new GScaleControl());

      map.getDragObject().setDraggableCursor("crosshair");

      map.setCenter(new GLatLng( 44.95, -93.26), 8);

      // === create a GDirections Object ===
      var gdir=new GDirections(map, document.getElementById("route"));

      // === Array for decoding the failure codes ===
      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
      reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

      // === catch Directions errors ===
      GEvent.addListener(gdir, "error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = reasons[code]
        }

        alert("Failed to obtain directions, "+reason);
      });

      GEvent.addListener(map, "singlerightclick", function(point) {
          //document.getElementById("mousetrack").value=point.toUrlValue();

           //alert(map.fromDivPixelToLatLng(point));
           document.getElementById("mouseTrack").innerHTML = '<font size="1">Mouse Lat/Long: ' + map.fromContainerPixelToLatLng(point) + '</font>';


      });

      // ====== set up marker mouseover tooltip div ======
      var tooltip = document.createElement("div");
      document.getElementById("map").appendChild(tooltip);
      tooltip.style.visibility="hidden";

      // Read the data from xml file 
      var request = GXmlHttp.create();
      request.open("GET", "/ahs/mapfinder.nsf/mapxml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          // are parameters being passed in the URL - added next line by CLARK 2/22/09
          var haveParameters = getParam("opendatabase");

          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");

          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            address = markers[i].getAttribute("address");
            label = markers[i].getAttribute("label");
            var category = markers[i].getAttribute("category");
            var loc_url = markers[i].getAttribute("loc");
            var loc_desc = markers[i].getAttribute("desc");
            var loc_phone = markers[i].getAttribute("phone"); 
            var loc_srv = markers[i].getAttribute("srv");
         
            //alert('Marker Loop = ' + address);

            // create the marker
            var html = '<span class="contenttext"><a href="http://' + loc_url + '">' + label + '</a><br>' + address + '<br>' + loc_phone + '<br><br>' + loc_desc;

            // create the marker
            var marker = createMarker(point,label,html,category,loc_srv);
            map.addOverlay(marker);
            if (haveParameters) {  // if no parameters then don't hide. Added by CLARK 2/22/09
              marker.hide();
            }
          }
      // == show or hide the categories initially ==
    // If parameters passed in URL then need to check each parameter added by CLARK 2/22/09
    if (haveParameters) {
      var HOSparam = getParam("hospitals");
      var CLIparam = getParam("clinics");
      var URGparam = getParam("urgents");
      var PHRparam = getParam("pharmacies");
      if (PHRparam == "on") {
         show("PHR");
       }
      if (CLIparam == "on") {
         show("CLI");
       }
      if (URGparam == "on") {
         show("URG");
       }
      if (HOSparam == "on") {
         show("HOS");
       }
    } else {
     // Everything turned on if no parameters
     document.getElementById("HOSbox").checked = true;
     document.getElementById("CLIbox").checked = true;
     document.getElementById("URGbox").checked = true;
     document.getElementById("PHRbox").checked = true;
    }

      // == create the initial sidebar ==
      //makeSidebar();
      // Using the timeout function due to some wonky IE behavior..surprise
      var t=setTimeout("makeSidebar()",100);

        }
      }
      request.send(null);

    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
     // If parameters passed in URL check if home address passed. Added CLARK 2/22/09 
     var haveParameters = getParam("opendatabase");
     if (haveParameters) {
        var Locationparam = getParam("location");
        var newHomeLocation = Locationparam.indexOf("Zip");
        if (newHomeLocation == "-1") {
          SetHomeLocation();
        }
     }
