
var map = null;
var myMarker = null;
var myPoint = null;
var myCenter = null;
var myZoom = 0;
var myAdress = '';
var myName = '';
var tabMarker = new Array();
var tabInfos = new Array();
var tabDetails = new Array();
var tabDist = new Array();
var strPrefixResult1Pre = '';
var strPrefixResult1Post = '';
var strPrefixResultXPre = '';
var strPrefixResultXPost = '';
var strNoResults = '';
var strPrev = '';
var strNext = '';

var defaultLat = 51.40;
var defaultLng = 10.02;
var defaultZoom = 6;

// The allowed region which the whole map must be within
var minLat = 35;
var maxLat = 65;
var minLng = -10;
var maxLng = 21;
var tminLat = minLat;
var tmaxLat = maxLat;
var tminLng = minLng;
var tmaxLng = maxLng;
var allowedBounds = new GLatLngBounds(new GLatLng(minLat,minLng), new GLatLng(maxLat,maxLng));

var wait = 0;
var filtersChange = 0;
var resRank = 0;

var tabFilters = new Array();
tabFilters['a1'] = new Array(); tabFilters['a1'][1] = 0; tabFilters['a1'][2] = '01inkjet';
tabFilters['a2'] = new Array(); tabFilters['a2'][1] = 0; tabFilters['a2'][2] = '02multifunctioninkjet';
tabFilters['a3'] = new Array(); tabFilters['a3'][1] = 0; tabFilters['a3'][2] = '03monolaser';
tabFilters['a4'] = new Array(); tabFilters['a4'][1] = 0; tabFilters['a4'][2] = '04colorlaser';
tabFilters['a5'] = new Array(); tabFilters['a5'][1] = 0; tabFilters['a5'][2] = '05multifunctionlaser';
tabFilters['a6'] = new Array(); tabFilters['a6'][1] = 0; tabFilters['a6'][2] = '06dotmatrix';
tabFilters['b1'] = new Array(); tabFilters['b1'][1] = 0; tabFilters['b1'][2] = '07lasertoner';
tabFilters['b2'] = new Array(); tabFilters['b2'][1] = 0; tabFilters['b2'][2] = '08inkjetcartridge';
//tabFilters['d1'] = new Array(); tabFilters['d1'][1] = 0; tabFilters['d1'][2] = '09usershome';
//tabFilters['d2'] = new Array(); tabFilters['d2'][1] = 0; tabFilters['d2'][2] = '10usersbusiness';
//tabFilters['f1'] = new Array(); tabFilters['f1'][1] = 0; tabFilters['f1'][2] = '11spareparts';

var baseIcon = new GIcon();
baseIcon.shadow = "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 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.";

var imgMarker = "img/markerLex.png";
var nbDisplay = 5;

String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g,'');
}

function hide(identifiant) {
  document.getElementById(identifiant).className = "hidden";
}

function show(identifiant) {
  document.getElementById(identifiant).className = "visible";
}

function setFilter(ind,nbLocations){
  if (wait == 0) {
    filtersChange = 1;
    wait = 1;
    if (tabFilters[ind][1] == 0) {
      tabFilters[ind][1] = 1;
      document.getElementById(tabFilters[ind][2]).src = 'img/icons/' + tabFilters[ind][2] + 'On.png'
    }
    else {
      tabFilters[ind][1] = 0;
      document.getElementById(tabFilters[ind][2]).src = 'img/icons/' + tabFilters[ind][2] + 'Off.png'
    }
    loadLocation(nbLocations);
  }
  wait = 0;
}

function setFilterImg(ind){
  if (tabFilters[ind][1] == 1)
    document.getElementById(tabFilters[ind][2]).src = 'img/icons/' + tabFilters[ind][2] + 'On.png'
  else
    document.getElementById(tabFilters[ind][2]).src = 'img/icons/' + tabFilters[ind][2] + 'Off.png'
}

function setFilterImgOn(ind){
  document.getElementById(tabFilters[ind][2]).src = 'img/icons/' + tabFilters[ind][2] + 'On.png'
}

function setupMap(pResult1Pre,pResult1Post,pResultXPre,pResultXPost,pPrev,pNext,pNoResults,pDisplay) {
  // Check to see if this browser can run the Google API
  if (GBrowserIsCompatible()) {
    nbDisplay = pDisplay;
    strPrefixResult1Pre = pResult1Pre;
    strPrefixResult1Post = pResult1Post;
    strPrefixResultXPre = pResultXPre;
    strPrefixResultXPost = pResultXPost;
    strNoResults = pNoResults;
    strPrev = pPrev;
    strNext = pNext;

    map = new GMap2(document.getElementById("map"));

    // ====== Restricting the range of Zoom Levels =====
    // Get the list of map types
    var mt = map.getMapTypes();
    // Overwrite the getMinimumResolution() methods
    for (var i=0; i<mt.length; i++) {
      mt[i].getMinimumResolution = function() {return 5;}
    }
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

    // Add a move listener to restrict the bounds range
    GEvent.addListener(map, "move", function() {
      checkBounds();
    });

    map.setCenter(new GLatLng(defaultLat,defaultLng),defaultZoom);
    map.enableDoubleClickZoom();
    //map.enableScrollWheelZoom();
  }
  else
    alert("Sorry, the Google Maps API is not compatible with this browser");
}

// If the map position is out of range, move it back
function checkBounds() {
  // Perform the check and return if OK
  if (allowedBounds.contains(map.getCenter())) return;
  // It`s not OK, so find the nearest allowed point and move there
  var C = map.getCenter();
  var X = C.lng();
  var Y = C.lat();

  var AmaxX = allowedBounds.getNorthEast().lng();
  var AmaxY = allowedBounds.getNorthEast().lat();
  var AminX = allowedBounds.getSouthWest().lng();
  var AminY = allowedBounds.getSouthWest().lat();
//alert(AminX+' '+AmaxX+' '+AminY+' '+AmaxY);
  if (X < AminX) {X = AminX;}
  if (X > AmaxX) {X = AmaxX;}
  if (Y < AminY) {Y = AminY;}
  if (Y > AmaxY) {Y = AmaxY;}

  //alert ("Restricting "+Y+" "+X);
  map.setCenter(new GLatLng(Y,X));
}

function submitenter(myfield,e,nbRes) {
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;
  if (keycode == 13) {
    loadLocation(nbRes);
    return false;
  }
  else
    return true;
}

function loadLocation(nbLocations) {
  hide("list");
  show('wait');
  setTimeout("checkLocation("+nbLocations+")",10);
}

function checkLocation(nbLocations) {
  map.closeInfoWindow();
  document.formaddr.add.value = document.formaddr.add.value.trim();
  document.formaddr.zip.value = document.formaddr.zip.value.trim();
  document.formaddr.cit.value = document.formaddr.cit.value.trim();
  var address = document.formaddr.add.value + ' ' + document.formaddr.zip.value + ' ' + document.formaddr.cit.value;
  address = address.trim();

  document.formaddr.nam.value = document.formaddr.nam.value.trim();
  var name = document.formaddr.nam.value;
//  var name = '';

  if ((myAdress != address) || (myName != name) || (filtersChange == 1)) {
    show('wait');
    clearMap();
    myAdress = address;
    myName = name;
    filtersChange = 0;
    //if (myMarker != null) map.removeOverlay(myMarker);
    myMarker = null;
    myPoint = null;

    if ((address != "") || (name != "")) {

      lat = '';
      lng = '';

      if (address != "") {
        geocoder = new GClientGeocoder();
        if (geocoder) {
          geocoder.setBaseCountryCode("DE");
          geocoder.getLatLng(
            address,
            function(point) {
              if (!point) {
                hide('wait');
                // ==Look to see if the query was cached ==
                var result=geocoder.getCache().get(address);
                if (result) {
                  var reason="Code "+result.Status.code;
                  if (reasons[result.Status.code]) {
                    if (result.Status.code != G_GEO_SUCCESS){
                        reason = reasons[result.Status.code];
                        alert(reason);
                    }
                  }
                }
              }
              else {
                myPoint = point;
                var icon = new GIcon(baseIcon);
                icon.image = 'img/marker.png';
                lat = point.y;
                lng = point.x;

                if ((lat > minLat) && (lat < maxLat) && (lng > minLng) && (lng < maxLng)) {
                  //clearMap();
                  //map.setCenter(new GLatLng(lat,lng), 0);
                  myMarker = createMarker(point,"<div class=\"popupinfos\">" + address + "</div>",icon);
                  map.addOverlay(myMarker);
//alert(name+"-"+lat+"-"+lng);
                  getData(name,lat,lng,nbLocations);
                }
                else
                  hide('wait');
              }
            }
          );
        }
      }
      else
        getData(name,lat,lng,nbLocations);
    }
    else {
      //document.getElementById("infos").innerHTML = '';
      allowedBounds = new GLatLngBounds(new GLatLng(minLat,minLng), new GLatLng(maxLat,maxLng));
      // ====== Restricting the range of Zoom Levels =====
      // Get the list of map types
      var mt = map.getMapTypes();
      // Overwrite the getMinimumResolution() methods
      for (var i=0; i<mt.length; i++) {
        mt[i].getMinimumResolution = function() {return 5;}
      }
      map.setCenter(new GLatLng(defaultLat,defaultLng), defaultZoom);
      hide('wait');
    }
  }
  else {
    if ((myCenter != null) && (myZoom >= 0)) map.setCenter(myCenter, myZoom);
    hide('wait');
  }
}

function clearMap() {
  map.clearOverlays();
  myCenter = null;
  myZoom = 0;
  document.getElementById("infos").innerHTML = '';
}

function createMarker(point, str, icon) {
  var marker = new GMarker(point, icon);
  if (str != "") {
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(str);
    });
  }
  return marker;
}

function openInfosWindowFromIndex(i) {
  var imarker = tabMarker[i];
  if (imarker != null) imarker.openInfoWindowHtml('<div class="popupinfos">'+tabInfos[i]+tabDetails[i]+'</div>');
}

function nextRes() {
  resRank+=nbDisplay;
  var tmpDisplay = nbDisplay;
  if (resRank + tmpDisplay > tabMarker.length) tmpDisplay = tabMarker.length - resRank;

  var infosStr = '';

  var bounds = new GLatLngBounds();
  if (myPoint != null) bounds.extend(myPoint);
  for (var i = 0; i < resRank; i++) {
    var point = tabMarker[i].getPoint();
    bounds.extend(point);
  }

  for (var i = resRank; i < (resRank+tmpDisplay); i++) {
    //infosStr+= ' <div >&raquo; ' + tabDist[i] + ' km<br />';
    infosStr+= '<a onmouseover="this.style.cursor=\'pointer\'" onclick="openInfosWindowFromIndex(' + i + ');">' + tabInfos[i] + '</a></div><hr noshade, size=1, color="#FF0000" />';
    var point = tabMarker[i].getPoint();
    bounds.extend(point);
  }
  map.setZoom(map.getBoundsZoomLevel(bounds));
  map.setCenter(bounds.getCenter());

  if (resRank > 0) {
    infosStr = infosStr + '<div style="float: left"><a onmouseover="this.style.cursor=\'pointer\'" onclick="prevRes();">'+strPrev+'</a></div>';
  }
  if ((resRank + nbDisplay) < tabMarker.length) {
    infosStr = infosStr + '<div style="float: right"><a onmouseover="this.style.cursor=\'pointer\'" onclick="nextRes();">'+strNext+'</a></div>';
  }

  document.getElementById("infos").innerHTML = infosStr;
  show("infosC");
}

function prevRes() {
  resRank-=nbDisplay;
  var tmpDisplay = nbDisplay;
  if (resRank + tmpDisplay > tabMarker.length) tmpDisplay = tabMarker.length - resRank;

  var infosStr = '';
  if (resRank == 0) {
    if (tmpDisplay > 1)
      infosStr = strPrefixResultXPre + tabMarker.length + strPrefixResultXPost;
    else
      infosStr = strPrefixResult1Pre + tabMarker.length + strPrefixResult1Post;
  }

  var bounds = new GLatLngBounds();
  if (myPoint != null) bounds.extend(myPoint);
  for (var i = 0; i < resRank; i++) {
    var point = tabMarker[i].getPoint();
    bounds.extend(point);
  }
  for (var i = resRank; i < (resRank + tmpDisplay); i++) {
    //infosStr+= ' <div >&raquo; ' + tabDist[i] + ' km<br />';
    infosStr+= '<a onmouseover="this.style.cursor=\'pointer\'" onclick="openInfosWindowFromIndex(' + i + ');">' + tabInfos[i] + '</a></div><hr noshade, size=1, color="#FF0000" />';
    var point = tabMarker[i].getPoint();
    bounds.extend(point);
  }
  map.setZoom(map.getBoundsZoomLevel(bounds));
  map.setCenter(bounds.getCenter());

  if (resRank > 0) {
    infosStr = infosStr + '<div style="float: left"><a onmouseover="this.style.cursor=\'pointer\'" onclick="prevRes();">'+strPrev+'</a></div>';
  }
  if ((resRank + nbDisplay) < tabMarker.length) {
    infosStr = infosStr + '<div style="float: right"><a onmouseover="this.style.cursor=\'pointer\'" onclick="nextRes();">'+strNext+'</a></div>';
  }
  document.getElementById("infos").innerHTML = infosStr;
  show("infosC");
}

function list(start) {
  aURL = "dl-de-list.php?s="+start;
  var request = GXmlHttp.create();
  request.open("GET", aURL, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = GXml.parse(request.responseText);
      var cpt = xmlDoc.documentElement.getAttribute("c");
      var resellers = xmlDoc.documentElement.getElementsByTagName("reseller");
      var rStr = '';

      var nbItemsOnLine = 0;
      for (var i = 0; i < resellers.length; i++) {
        if (nbItemsOnLine == 0) {
          rStr+= '<tr>';
          rStr+= '<td><hr noshade size=1 color="#FF0000" /></td>';
          rStr+= '<td><hr noshade size=1 color="#FF0000" /></td>';
          rStr+= '<td><hr noshade size=1 color="#FF0000" /></td>';
          rStr+= '</tr><tr>';
        }
        rStr+= '<td width="250" valign="top">'+resellers[i].getAttribute("s")+resellers[i].getAttribute("sd")+'</td>';
        nbItemsOnLine++;
        if (nbItemsOnLine == 3) {
          nbItemsOnLine = 0;
          rStr+= '</tr>';
        }
      }
      var tmpInt = nbItemsOnLine;
      if ((nbItemsOnLine > 0) && (nbItemsOnLine < 3)) {
        while (nbItemsOnLine < 3) {
          nbItemsOnLine++;
          rStr+= '<td width="250">&nbsp;</td>';
        }
        rStr+= '</tr>';
      }
      if (tmpInt == 0) tmpInt = 3;
      rStr+= '<tr>';
      for (var i = 0; i < tmpInt; i++) {
        rStr+= '<td width="250"><hr noshade size=1 color="#FF0000" /></td>';
      }
      while (tmpInt < 3) {
        tmpInt++;
        rStr+= '<td width="250">&nbsp;</td>';
      }
      rStr+= '</tr>';

      if (rStr != '') {
        var nav = '';
        if (start > 0)
          nav+= '<div style="float: left"><a onmouseover="this.style.cursor=\'pointer\'" onclick="list('+(start - resellers.length)+');">'+strPrev+'</a></div>';
        if (cpt > (start + resellers.length))
          nav+= '<div style="float: right"><a onmouseover="this.style.cursor=\'pointer\'" onclick="list('+(start + resellers.length)+');">'+strNext+'</a></div>';
        if (nav != '')
          rStr+= '<tr><td colspan="3">'+nav+'</td></tr>';
        rStr = '<table width="750" border="0" align="center" cellpadding="0" cellspacing="5">'+rStr+'</table>';
      }
      document.getElementById("listitems").innerHTML = rStr;
      hide("infosC");
      show("list");
    }
  }
  request.send(null);
}

function is_numeric(num){
  return (!((isNaN(num)) || (num.length == 0)));
}

function getData(name,lat,lng,nbLocations) {

  var gps = (is_numeric(lat) && is_numeric(lng))

  // ===== Start with an empty GLatLngBounds object =====     
  var bounds = new GLatLngBounds();
  if (gps) {
    var point = new GLatLng(lat,lng);
    bounds.extend(point);
    tminLat = lat;
    tmaxLat = lat;
    tminLng = lng;
    tmaxLng = lng;
  }
  else {
    var point = new GLatLng(defaultLat,defaultLng);
    bounds.extend(point);
    tminLat = defaultLat;
    tmaxLat = defaultLat;
    tminLng = defaultLng;
    tmaxLng = defaultLng;
  }

  aURL = "dl-de-data.php?";
  if (gps) aURL+= "lat="+lat+"&lng="+lng+"&";
  if (name != "") aURL+= "nam="+name+"&";
  aURL+= "nb="+nbLocations;

  aURL+= "&a1="+tabFilters['a1'][1];
  aURL+= "&a2="+tabFilters['a2'][1];
  aURL+= "&a3="+tabFilters['a3'][1];
  aURL+= "&a4="+tabFilters['a4'][1];
  aURL+= "&a5="+tabFilters['a5'][1];
  aURL+= "&a6="+tabFilters['a6'][1];
  aURL+= "&b1="+tabFilters['b1'][1];
  aURL+= "&b2="+tabFilters['b2'][1];
//  aURL+= "&d1="+tabFilters['d1'][1];
//  aURL+= "&d2="+tabFilters['d2'][1];
//  aURL+= "&f1="+tabFilters['f1'][1];

//alert(aURL);
  var request = GXmlHttp.create();
  request.open("GET", aURL, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = GXml.parse(request.responseText);
      // obtain the array of markers and loop through it
      var resellers = xmlDoc.documentElement.getElementsByTagName("reseller");
      var tmpDisplay = nbDisplay;

      if (resellers.length < tmpDisplay) tmpDisplay = resellers.length;

      resRank = 0;
      if (tmpDisplay == 0) {

        tminLat = minLat;
        tminLng = minLng;
        tmaxLat = maxLat;
        tmaxLng = maxLng;
        allowedBounds = new GLatLngBounds(new GLatLng(tminLat,tminLng), new GLatLng(tmaxLat,tmaxLng));

        // ====== Initializing the range of Zoom Levels =====
        // Get the list of map types
        var mt = map.getMapTypes();
        // Overwrite the getMinimumResolution() methods
        for (var i=0; i<mt.length; i++) {
          mt[i].getMinimumResolution = function() {return defaultZoom;}
        }
        map.setCenter(new GLatLng(defaultLat,defaultLng),defaultZoom);
        document.getElementById("infos").innerHTML = strNoResults;
        show("infosC");
      }
      else {
        var infosStr = '';
        var str = '';

        if (gps) {
          if (lat < tminLat) tminLat = lat;
          if (lat > tmaxLat) tmaxLat = lat;
          if (lng < tminLng) tminLng = lng;
          if (lng > tmaxLng) tmaxLng = lng;
        }

        var icon = new GIcon(baseIcon);
        icon.image = 'img/markerLex.png';

        tabMarker.length = 0;
        for (var i = 0; i < tmpDisplay; i++) {
          // obtain the attribues of each marker
          var alat = parseFloat(resellers[i].getAttribute("lat"));
          var alng = parseFloat(resellers[i].getAttribute("lng"));

//          if ((alat >= minLat) && (alat <= maxLat) && (alng >= minLng) && (alng <= maxLng)) {
            if (alat < tminLat) tminLat = alat;
            if (alat > tmaxLat) tmaxLat = alat;
            if (alng < tminLng) tminLng = alng;
            if (alng > tmaxLng) tmaxLng = alng;

            var point = new GLatLng(alat,alng);
            myCenter = point;
            // ==== Each time a point is found, extent the bounds ato include it =====
            bounds.extend(point);

            tabInfos[i] = resellers[i].getAttribute("s");
            tabDetails[i] = resellers[i].getAttribute("sd");
            tabDist[i] = resellers[i].getAttribute("d");
            // create the marker
            str = '<div class="popupinfos">';
            str+=tabInfos[i];
            str+=tabDetails[i];
            str+='</div>';
            var amarker = createMarker(point,str,icon);
            map.addOverlay(amarker);
            // build html string
            if (gps) infosStr+= ' <div >&raquo; '+tabDist[i]+' km<br />';
            infosStr+= '<a onmouseover="this.style.cursor=\'pointer\'" onclick="openInfosWindowFromIndex(' + i + ');">'+tabInfos[i]+'</a></div><hr noshade size=1 color="#FF0000" />';
            // save marker ref
            tabMarker[i] = amarker;
//          }
        }

        if ((resRank + nbDisplay) < resellers.length) {
            infosStr = infosStr + '<div style="float: right"><a onmouseover="this.style.cursor=\'pointer\'" onclick="nextRes();">'+strNext+'</a></div>';
        }

        // ===== determine the zoom level from the bounds =====
        myZoom = map.getBoundsZoomLevel(bounds);
        // ===== determine the centre from the bounds ======
        if ((tmpDisplay > 1) || gps) myCenter = bounds.getCenter();

        if (resellers.length > 1)
          document.getElementById("infos").innerHTML = strPrefixResultXPre + resellers.length + strPrefixResultXPost + infosStr;
        else
          document.getElementById("infos").innerHTML = strPrefixResult1Pre + resellers.length + strPrefixResult1Post + infosStr;

        show("infosC");

        for (var i = tmpDisplay; i < resellers.length; i++) {
          // obtain the attribues of each marker
          var alat = parseFloat(resellers[i].getAttribute("lat"));
          var alng = parseFloat(resellers[i].getAttribute("lng"));

          var point = new GLatLng(alat,alng);
          if (alat < tminLat) tminLat = alat;
          if (alat > tmaxLat) tmaxLat = alat;
          if (alng < tminLng) tminLng = alng;
          if (alng > tmaxLng) tmaxLng = alng;
          // ==== Each time a point is found, extent the bounds ato include it =====
          bounds.extend(point);

          tabInfos[i] = resellers[i].getAttribute("s");
          tabDetails[i] = resellers[i].getAttribute("sd");
          tabDist[i] = resellers[i].getAttribute("d");
          // create the marker
          str = '<div class="popupinfos">';
          str+=tabInfos[i];
          str+=tabDetails[i];
          str+='</div>';
          var amarker = createMarker(point,str,icon);
          map.addOverlay(amarker);
          // save marker ref
          tabMarker[i] = amarker;
        }

        if ((tminLat >= tmaxLat) || (tminLng >= tmaxLng)) {
          tminLat = minLat;
          tminLng = minLng;
          tmaxLat = maxLat;
          tmaxLng = maxLng;
        }
        allowedBounds = new GLatLngBounds(new GLatLng(tminLat,tminLng), new GLatLng(tmaxLat,tmaxLng));

        if (gps && (resellers.length > nbDisplay)) {
          // === The array of points for the polyline ===
          var points = [ new GLatLng(tminLat,tminLng), new GLatLng(tminLat,tmaxLng),new GLatLng(tmaxLat,tmaxLng),new GLatLng(tmaxLat,tminLng), new GLatLng(tminLat,tminLng)];
          // === Create the polyline
          //map.addOverlay(new GPolyline(points));
          map.addOverlay(new GPolygon(points,'#333333',2,0.5,'',0));

          points = [ new GLatLng(tmaxLat,minLng-20), new GLatLng(tmaxLat,maxLng+20),new GLatLng(maxLat+20,maxLng+20),new GLatLng(maxLat+20,minLng-20), new GLatLng(tmaxLat,minLng-20)];
          map.addOverlay(new GPolygon(points,'#333333',0,0.5,'#666666',0.5));

          points = [ new GLatLng(tminLat,minLng-20), new GLatLng(tminLat,maxLng+20),new GLatLng(minLat-20,maxLng+20),new GLatLng(minLat-20,minLng-20), new GLatLng(tminLat,minLng-20)];
          map.addOverlay(new GPolygon(points,'#333333',0,0.5,'#666666',0.5));

          points = [ new GLatLng(tminLat,minLng-20), new GLatLng(tminLat,tminLng),new GLatLng(tmaxLat,tminLng),new GLatLng(tmaxLat,minLng-20), new GLatLng(tminLat,minLng-20)];
          map.addOverlay(new GPolygon(points,'#333333',0,0.5,'#666666',0.5));

          points = [ new GLatLng(tminLat,tmaxLng), new GLatLng(tminLat,maxLng+20),new GLatLng(tmaxLat,maxLng+20),new GLatLng(tmaxLat,tmaxLng), new GLatLng(tminLat,tmaxLng)];
          map.addOverlay(new GPolygon(points,'#333333',0,0.5,'#666666',0.5));
        }

        // Add a move listener to restrict the bounds range
        GEvent.addListener(map, "move", function() {
          checkBounds();
        });

        // ====== Restricting the range of Zoom Levels =====
        // Get the list of map types
        var mt = map.getMapTypes();
        // Overwrite the getMinimumResolution() methods
        for (var i=0; i<mt.length; i++) {
          mt[i].getMinimumResolution = function() {return map.getBoundsZoomLevel(bounds);}
        }

        map.setZoom(myZoom);
        map.setCenter(myCenter);
      }
      hide('wait');
    }
  }
  request.send(null);
}

