// -------------------------------------------------------------------
// Begin global variables
// -------------------------------------------------------------------

var clk_cursel = 0;
var clk_prev_btn = 0;
var clk_getdir_btn = 0;

var the_timeout;
// Set the initial starting and ending animation coordinates.
var the_coords = new Array("100:200","600:10");

var mousex = 0;
var mousey = 0;

// -------------------------------------------------------------------
// Begin helper functions for getting coordinates of the map bounds
// -------------------------------------------------------------------

// Converts a bounds to coordinates, sort of V1 minx, maxx... style
function boundsToCoords(tmpBounds){
  var north=tmpBounds.getNorthEast().lat();
  var east=tmpBounds.getNorthEast().lng();
  var south=tmpBounds.getSouthWest().lat();
  var west=tmpBounds.getSouthWest().lng();
  return {north : north, east : east, west : west, south : south};
}

// Convert bounds to polyline (for drawing bounds boxes etc)
function boundsToPolyLine(tmpBounds){
  var tmpCoords = boundsToCoords(tmpBounds);
  var points = [];
  points.push(tmpBounds.getNorthEast());
  points.push(new GLatLng(tmpCoords.south,tmpCoords.east));
  points.push(tmpBounds.getSouthWest());
  points.push(new GLatLng(tmpCoords.north,tmpCoords.west));
  points.push(tmpBounds.getNorthEast());
  return points;
}

// Scales a bounds object
function scaleBounds(tmpBounds,scale,bnd){
  var tmpCoords = boundsToCoords(tmpBounds);
  var midLat = (tmpCoords.north+tmpCoords.south)/2;
  var midLng = (tmpCoords.east+tmpCoords.west)/2;
  tmpCoords.north = midLat + (tmpCoords.north - midLat)*scale;
  tmpCoords.south = midLat + (tmpCoords.south - midLat)*scale;
  tmpCoords.east  = midLng + (tmpCoords.east -  midLng)*scale;
  tmpCoords.west  = midLng + (tmpCoords.west -  midLng)*scale;
  if (tmpCoords.north > 85.932567920988){
    tmpCoords.north = 85.932567920988
  }
  if (tmpCoords.east > 180){
    tmpCoords.east = +180
  }
  if (tmpCoords.south < -128.8235826668448){
    tmpCoords.south = -128.8235826668448
  }
  if (tmpCoords.west < -180){
    tmpCoords.west = -180
  }
  var newBounds = new GLatLngBounds(new GLatLng(tmpCoords.south, tmpCoords.west), new GLatLng(tmpCoords.north, tmpCoords.east));
  // alert("North: "+tmpCoords.north+"\nEast: "+tmpCoords.east+"\nSouth: "+tmpCoords.south+"\nWest: "+tmpCoords.west);
  if (bnd == "n") {
    return (tmpCoords.north);
  }
  else if (bnd == "e") {
    return (tmpCoords.east);
  }
  else if (bnd == "s") {
    return (tmpCoords.south);
  }
  else if (bnd == "w") {
    return (tmpCoords.west);
  }
  else {
    return (newBounds);
  }
}

// -------------------------------------------------------------------
// Begin ajax functions for getting data from the server
// -------------------------------------------------------------------

// Returns coordinates from user supplied zip code.
function chgZipCode(ref,obj) {

  var theurl = "getCoordsFromZipCode.cfm?zc="+obj;
  var request = GXmlHttp.create();

  document.getElementById("cursel").style.visibility = 'hidden';
  
  request.open("GET", theurl, true);  
  
  request.onreadystatechange = function() {

    if (request.readyState == 4) {

      var xml = GXml.parse(request.responseText);
      var crds = xml.documentElement.getElementsByTagName("crd");

      for (var i = 0; i < crds.length; i++) {
        var lat = parseFloat(crds[i].getAttribute("lat"));
        var lng = parseFloat(crds[i].getAttribute("lng"));
        var zc = crds[i].getAttribute("zc");
      }

      if (lat.toString() != "NaN") {
        // map.savePosition();
        map.setCenter(new GLatLng(lat, lng), 10);
        layerTracker(0,0,0);
        layerVisibilityMgr(1);
        welcloseBtn();
      }
      else {
        if (ref == "zipcode") {
          document.getElementById("error").style.visibility = 'visible';
        }
        else if (ref == "wzipcode") {
          document.getElementById("welcome_zc_invalid").innerHTML = '<img src="images/exclamation_small.gif" width="18" height="18" hspace="5" vspace="0" align="absmiddle" alt="" />The postal code you entered did not returned any results. Try another postal code.';
        }
      }

    }

  }
  request.send(null);
}

// Google geoocoder
function moveToZipCode(ref,address) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        // alert(address + " not found");
        if (ref == "zipcode") {
          document.getElementById("error").style.visibility = 'visible';
        }
        else if (ref == "wzipcode") {
          document.getElementById("welcome_zc_invalid").innerHTML = '<img src="images/exclamation_small.gif" width="18" height="18" hspace="5" vspace="0" align="absmiddle" alt="" />The postal code you entered did not returned any results. Try another postal code.';
        }
      } 
      else {
        map.setCenter(point, 10);
        // var marker = new GMarker(point);
        // map.addOverlay(marker);
        // marker.openInfoWindowHtml(address);
        layerTracker(0,0,0);
        layerVisibilityMgr();
        // welcloseBtn();
      }
    }
  );
}

// Returns coordinates from user supplied zip code.
function chgState(obj) {

  var theurl = "getCoordsFromState.cfm?s="+obj.value;
  var request = GXmlHttp.create();

  document.getElementById("cursel").style.visibility = 'hidden';
  
  request.open("GET", theurl, true);  
  
  request.onreadystatechange = function() {

    if (request.readyState == 4) {

      var xml = GXml.parse(request.responseText);
      var crds = xml.documentElement.getElementsByTagName("crd");

      for (var i = 0; i < crds.length; i++) {
        var lat = parseFloat(crds[i].getAttribute("lat"));
        var lng = parseFloat(crds[i].getAttribute("lng"));
        var zl = crds[i].getAttribute("zl");
      }

      if (lat.toString() != "NaN") {
        // map.savePosition();
        map.setCenter(new GLatLng(lat, lng), parseInt(zl));
        layerTracker(0,0,0);
        layerVisibilityMgr(1);
        welcloseBtn();
      }
      else {
        if (ref == "zipcode") {
          document.getElementById("error").style.visibility = 'visible';
        }
        else if (ref == "wzipcode") {
          document.getElementById("welcome_zc_invalid").innerHTML = '<img src="images/exclamation_small.gif" width="18" height="18" hspace="5" vspace="0" align="absmiddle" alt="" />The postal code you entered did not returned any results. Try another postal code.';
        }
      }

    }

  }
  request.send(null);
}

// Returns jeweler data based on map bound coordinates.
function getData() {

  var theurl = "getJewelersByCoords.cfm?n="+scaleBounds(map.getBounds(),bndscale,'n')+"&e="+scaleBounds(map.getBounds(),bndscale,'e')+"&s="+scaleBounds(map.getBounds(),bndscale,'s')+"&w="+scaleBounds(map.getBounds(),bndscale,'w');
  
  // Debugging Information
  document.getElementById("map_center").innerHTML = map.getCenter(); // Centerpoint
  document.getElementById("map_zoomlvl").innerHTML = map.getZoom(); // Zoom Level
  document.getElementById("map_bnds").innerHTML = "n = "+scaleBounds(map.getBounds(),bndscale,'n')+"\ne = "+scaleBounds(map.getBounds(),bndscale,'e')+"\ns = "+scaleBounds(map.getBounds(),bndscale,'s')+"\nw = "+scaleBounds(map.getBounds(),bndscale,'w'); // Map Bounds (box)
  document.getElementById("map_bnds_xml").innerHTML = "<a href="+theurl+" target='_blank'>Get XML</a>"; // Launches a seperate window with the xml data from the current bounds set.
  
  var request = GXmlHttp.create();

  request.open("GET", theurl, true);

  request.onreadystatechange = function() {

    if (request.readyState == 4) {

      map.clearOverlays();
      sidebar_html = "";

      if (request.responseText.length != 0) {

        var xml = GXml.parse(request.responseText);
        var markers = xml.documentElement.getElementsByTagName("marker");

        for (var i = 0; i < markers.length; i++) {

          // Uncomment to use different icons based on the mcolor field from the database.
          /*
          if (markers[i].getAttribute("mcolor") == "") {
            icoimg = "122.png";
          }
          else {
            icoimg = markers[i].getAttribute("mcolor");
          }
          */

          icoimg = "137.png"; // Default icon

          var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          var jid = markers[i].getAttribute("jid");
          var jeweler = markers[i].getAttribute("jeweler")+'<br />';
          var mallname = markers[i].getAttribute("mallname");
          mallname = markers[i].getAttribute("mallname") == "" ? '' : mallname+'<br />';
          var address1 = markers[i].getAttribute("address1")+'<br />';
          var address2 = markers[i].getAttribute("address2");
          address2 = markers[i].getAttribute("address2") == "" ? '' : address2+'<br />';
          var city = markers[i].getAttribute("city")+', ';
          var state = markers[i].getAttribute("state")+' ';
          var zip = markers[i].getAttribute("zip")+'<br />';
          var phone = markers[i].getAttribute("phone")+'<br />';
          var webaddress = markers[i].getAttribute("webaddress");
          webaddress = markers[i].getAttribute("webaddress") == "" ? '' : '<a href='+webaddress+' target=_blank>Website</a><br />';
          var logo = markers[i].getAttribute("logo");
          // logo = markers[i].getAttribute("logo") == "" ? '' : '<img src=images/retailer_logos/lwtpmj_jcpenney_logo_small.gif><br />';
          // logo = markers[i].getAttribute("logo") == "" ? '' : '<img src=images/retailer_logos/'+logo+'><br />';
          var urlstr = 'more_info.cfm?jid='+jid;
          var moreinfo = '<a href=\"javascript:showPopWin(\''+urlstr+'\', 600, 300, null);\">More Info</a>';
          // var marker_display_str = logo+jeweler+mallname+address1+address2+city+state+zip+phone+webaddress+moreinfo; //with logos
          var marker_display_str = jeweler+mallname+address1+address2+city+state+zip+phone+webaddress+moreinfo; //without logos
          var zoom = '<a href="javascript:map.savePosition();map.setCenter(new GLatLng('+lat+', '+lng+'), 15);layerTracker(2,1,2);layerVisibilityMgr(1);">Zoom</a>';
          // var sidebar_display_str = jeweler+address1+city+state+zip+zoom;

          // Display the zoom link if the zoom level is less than 15.
          if (map.getZoom() >= 15) {
            var sidebar_display_str = jeweler+mallname+address1+city+state+zip;
          }
          else {
            var sidebar_display_str = jeweler+mallname+address1+city+state+zip+zoom;
          }
          
          // Show the prev/back button if there is 1 marker.
          if (markers.length == 1 && map.getZoom() >= 15) {
            layerTracker(2,1,2);
            layerVisibilityMgr(1);
          }
          // Hide the prev/back button of their is more than one marker.
          else if (markers.length > 1) {
            layerTracker(2,0,2);
            layerVisibilityMgr(1);
          }

          // create the marker
          var marker = createMarker(point,jid,marker_display_str,sidebar_display_str,icoimg);
          map.addOverlay(marker);
          document.getElementById("loadmsg").style.visibility = 'hidden';
          
          backToTop("results");

        }

      }
      else {
        document.getElementById("loadmsg").style.visibility = 'hidden';
        // Hide the prev/back button if there is no markers.
        layerTracker(2,0,2);
        layerVisibilityMgr(1);
        sidebar_html += '<div class="results_data">No Results</div>'
      }
      // put the assembled sidebar_html contents into the sidebar div
      document.getElementById("results").innerHTML = sidebar_html;

    }

  }
  request.send(null);
}

// -------------------------------------------------------------------
// Begin mouse capture functions
// -------------------------------------------------------------------

// This function loads on startup.
function init() {

  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
  getData();

}

function wheelZoom(a) {
  if (a.detail) { // Firefox
    if (a.detail < 0) { 
      map.zoomIn();
    }
    else if (a.detail > 0) { 
      map.zoomOut();
    }
  }
 	else if (a.wheelDelta) { // IE
    if (a.wheelDelta > 0) { 
      map.zoomIn();
    }
    else if (a.wheelDelta < 0) { 
      map.zoomOut();
    }
  }
}

function getMouseXY(e) { // works on IE6,FF,Moz,Opera7

  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

  if (e) {
    if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft;
      mousey = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }

  }

}

function update(e) {

  getMouseXY(e); // NS is passing (event), while IE is passing (null)
  document.getElementById('span_mousex').innerHTML = mousex;
  document.getElementById('span_mousey').innerHTML = mousey;

}

// -------------------------------------------------------------------
// Begin layer animation functions
// -------------------------------------------------------------------

// This function returns the X/Y coords of an object.
function getPos(obj) {
  var pos = {x: obj.offsetLeft||0, y: obj.offsetTop||0};
  while(obj = obj.offsetParent) {
    pos.x += obj.offsetLeft||0;
    pos.y += obj.offsetTop||0;
  }
  return pos;
  //alert("X = "+getPos(document.getElementById("cursel2")).x+" and Y = "+getPos(document.getElementById("cursel2")).y);
}

// This function gets the two anchor points of a segment of the path and then calls moveDiv() to move the DIV along that segment.
function getAnchors(array_position) {
  var first_anchor = the_coords[array_position];
  var second_anchor = the_coords[array_position+1];

  array_position++;

  if (array_position == the_coords.length-1) {
    array_position = 0;
  }

  moveDiv(array_position, first_anchor, second_anchor, 0, 0);

}

// This function moves the DIV along a segment of the path based on the anchor points. It will keep calling itself with a setTimeout() until the DIV is at the end of the segment.
function moveDiv(array_position, anchor_one, anchor_two, horizontal_step_size, vertical_step_size, loop) {

  var the_style = getStyleObject("cursel");

  if (the_style) {

    // Get the first anchor.
    var first_points = anchor_one.split(":");
    var first_left = parseInt(first_points[0]);
    var first_top = parseInt(first_points[1]);

    // Get the second anchor.
    var second_points = anchor_two.split(":");
    var second_left = parseInt(second_points[0]);
    var second_top = parseInt(second_points[1]);

    // If we don't know the step sizes to move the DIV, figure them out.
    if ((horizontal_step_size == 0) && (vertical_step_size == 0)) {
      horizontal_step_size = getStepSize(anchor_one, anchor_two, 0);
      vertical_step_size = getStepSize(anchor_one, anchor_two, 1);
    }

    // Figure out the new coordinates.
    var new_left = first_left + horizontal_step_size;
    var new_top = first_top + vertical_step_size;

    // If we're at the end of the segment, set the coordinates to move the DIV to the end.
    if (atEndOfPath(horizontal_step_size, second_left, new_left) || (atEndOfPath(vertical_step_size, second_top, new_top))) {
      new_left = second_left;
      new_top = second_top;
    }

    // Add the px or don't, depending on the browser.
    if (!document.layers) {
      new_left = new_left + "px";
      new_top = new_top + "px";
    }

    // Now actually move the DIV.
    the_style.left = new_left;
    the_style.top = new_top;

    // If we're at the end of the segment, get new anchors otherwise, call moveDiv() again with a new starting point along the segment.
    if ((parseInt(new_left) == parseInt(second_left)) && (parseInt(new_top) == parseInt(second_top))) {
      // Uncomment getAnchors() and comment out clearTimeout() to loop the animation.
      // getAnchors(array_position);
      clearTimeout(the_timeout);
    } 
    else {
      var new_anchor_one = new_left + ":" + new_top;
      var timeout_string = "moveDiv(" +
        array_position + ", '" + new_anchor_one + "', '" +
        anchor_two + "', " + horizontal_step_size + "," + 
        vertical_step_size + ");";
      the_timeout = setTimeout(timeout_string, 50);
    }

  }

}

// This figures out how much to move the DIV each time.
function getStepSize(anchor_one, anchor_two, position) {

  var first_points = anchor_one.split(":");
  var first_number = parseInt(first_points[position]);
  var second_points  = anchor_two.split(":");
  var second_number = parseInt(second_points[position]);
  var step_size = Math.round((second_number - first_number)/10);

  return step_size;

}

// If the DIV is about to be moved past the end point of the segment, this will return true. Otherwise, it will return false.
function atEndOfPath(the_step_size, second_number, new_number) {

  var the_end = false;

  if (((the_step_size > 0) && (new_number > second_number)) || ((the_step_size < 0) && (new_number < second_number))) {
    the_end = true;
  }

  return the_end;

}

function getStyleObject(objectId) {
  // cross-browser function to get an object's style object given its
  if(document.getElementById && document.getElementById(objectId)) {
    // W3C DOM
    return document.getElementById(objectId).style;
  }
  else if (document.all && document.all(objectId)) {
    // MSIE 4 DOM
    return document.all(objectId).style;
  }
  else if (document.layers && document.layers[objectId]) {
    // NN 4 DOM.. note: this won't find nested layers
    return document.layers[objectId];
  }
  else {
    return false;
  }
}

function animateDivFromPoint(html) {

  // Populate the hidden <span> tags with the mouse's current x/y position.
  var mouse_x = document.getElementById("span_mousex").innerHTML;
  var mouse_y = document.getElementById("span_mousey").innerHTML;
  var cur_div_posX = getPos(document.getElementById("curselxy")).x;
  var cur_div_posY = getPos(document.getElementById("curselxy")).y+20;
  // Set the starting coordinates to the current mouse position and set the default ending coordinates.
  the_coords = new Array(mouse_x+":"+mouse_y,cur_div_posX+":"+cur_div_posY);
  var thelayer = document.getElementById("cursel");
  thelayer.innerHTML = unescape(html);
  thelayer.style.visibility = 'visible';
  getAnchors(0);

}

// -------------------------------------------------------------------
// Begin Misc. functions
// -------------------------------------------------------------------

// Positions a layer to a certain coordinate relative to the window size.
function positionLayers() {

  var cur_div_posX = getPos(document.getElementById("wrapper")).x;
  var cur_div_posY = getPos(document.getElementById("wrapper")).y;

  document.getElementById("welcome").style.left = cur_div_posX+149+'px';
  document.getElementById("welcome").style.top = cur_div_posY+195+'px';
  document.getElementById("loadmsg").style.left = cur_div_posX+212+'px';
  document.getElementById("loadmsg").style.top = cur_div_posY+226+'px';
  document.getElementById("cursel").style.left = cur_div_posX+657+'px';
  document.getElementById("cursel").style.top = cur_div_posY+129+'px';
  document.getElementById("getdir_btn").style.left = cur_div_posX+787+'px';
  document.getElementById("getdir_btn").style.top = cur_div_posY+248+'px';
  document.getElementById("prev_btn").style.left = cur_div_posX+652+'px';
  document.getElementById("prev_btn").style.top = cur_div_posY+344+'px';
  document.getElementById("error").style.left = cur_div_posX+149+'px';
  document.getElementById("error").style.top = cur_div_posY+217+'px';
}

function rollOver(obj,args) {

  if (args == 1) {
    obj.style.background='#cad9ea';
  }
  else {
    obj.style.background='#ffffff';
  }

}

function prevBtn() {

  map.returnToSavedPosition();
  layerTracker(2,0,2);
  layerVisibilityMgr(1);

}

function chgBgImage(obj,dec,img,color) {

  if (dec == 1) {
    obj.style.backgroundImage = 'url('+img+')';
    // obj.style.color = color;
  }
  else {
    obj.style.backgroundImage = 'url('+img+')';
    // obj.style.color = color;
  }

}

function displayJewelersByZipCodeLayers(toggle) {
	
	if (toggle == 1) {
    document.getElementById("cursel").style.visibility = 'hidden';
    document.getElementById("prev_btn").style.visibility = 'hidden';
  }
  else {
    document.getElementById("cursel").style.visibility = 'visible';
    document.getElementById("prev_btn").style.visibility = 'visible';
  }

}

function setJID(jid) {

  document.getElementById("getdir_btn").style.visibility = 'visible';
  document.getElementById("jid_data").innerHTML = jid;

}

function getDirections() {

  var jid = document.getElementById("jid_data").innerHTML;
  var urlstr = 'get_directions.cfm?jid='+jid;

  showPopWin(urlstr, 600, 260, null);

}

function layerTracker(obj1,obj2,obj3) {
  
  if (obj1 != 2) {
    clk_cursel = obj1;
  }
  if (obj2 != 2) {
    clk_prev_btn = obj2;
  }
  if (obj3 != 2) {
    clk_getdir_btn = obj3;
  }

}

function layerVisibilityMgr(tab) {

  var cursel = document.getElementById("cursel").style.visibility;
  var prev_btn = document.getElementById("cursel").style.visibility;
  var getdir_btn = document.getElementById("cursel").style.visibility;

  if (tab == 1) {
    
    if (clk_cursel == 1) {
      document.getElementById("cursel").style.visibility = 'visible';
    }
    else {
      document.getElementById("cursel").style.visibility = 'hidden';
    }
    
    if (clk_prev_btn == 1) {
      document.getElementById("prev_btn").style.visibility = 'visible';
    }
    else {
      document.getElementById("prev_btn").style.visibility = 'hidden';
    }
    
    if (clk_getdir_btn == 1) {
      document.getElementById("getdir_btn").style.visibility = 'visible';
    }
    else {
      document.getElementById("getdir_btn").style.visibility = 'hidden';
    }
    
  }

  else {
    document.getElementById("cursel").style.visibility = 'hidden';
    document.getElementById("prev_btn").style.visibility = 'hidden';
    document.getElementById("getdir_btn").style.visibility = 'hidden';
    document.getElementById("loadmsg").style.visibility = 'hidden';
    document.getElementById("welcome").style.visibility = 'hidden';
    document.getElementById("error").style.visibility = 'hidden';
  }

}

function openGMD() {

  var frm = document.getdir;
  var addr = frm.address;
  var city = frm.city;
  var is_state = frm.state.selectedIndex;
  var state = frm.state.options[is_state].value;
  var zip = frm.zip;

  if (addr.value == "") {
    alert("Address is required!");
    addr.focus();
    return false;
  }
  else if (city.value == "") {
    alert("City is required!");
    city.focus();
    return false;
  }
  else if (state == "") {
    alert("State is required!");
    frm.state.focus();
    return false;
  }
  else {

    var frm = document.getdir;
    var saddress = frm.address.value;
    var scity = frm.city.value;
    var sstate = frm.state.value;
    var szip = frm.zip.value;
    var baseurl = 'http://maps.google.com/maps?';
    var daddr = frm.daddr.value; // destination address (jeweler)
    var saddr = saddress+',+'+scity+',+'+sstate+',+'+szip; // starting address (users)
    var miscstr = '&f=li&hl=en&cid=&ie=UTF8&om=1'
    var urlstr = baseurl+'daddr='+daddr+'&saddr='+saddr+miscstr;
    // MM_openBrWindow(urlstr,'directions','resizable=yes,width=750,height=410');
    MM_openBrWindow(urlstr,'directions','resizable=yes,width=800,height=600');

  }

}

function welcloseBtn() {

  document.getElementById("welcome").style.visibility = 'hidden';

}

function errorcloseBtn() {

  document.getElementById("error").style.visibility = 'hidden';

}

function captureSubmit(ref) {

  if(event.keyCode==13) {
    if (ref == "zipcode") {
      chgZipCode('zipcode',document.zipcode.zipcode.value);
    }
    else if (ref == "wzipcode") {
      chgZipCode('wzipcode',document.wzipcode.wzipcode.value);
    }
    return false;
  }

}

function backToTop(id){
document.getElementById(id).scrollTop=0
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// -------------------------------------------------------------------
// Begin inactive functions
// -------------------------------------------------------------------

function chgBgColor(obj,dec,color) {

  if (dec == 0) {
    obj.style.background = color;
  }
  else {
    obj.style.background = color;
  }

}

function drawCircle(lng,lat) {

  var Cradius = 19;               // mile radius
  var Ccolor = '#0000ff';         // color blue
  var Cwidth = 3;                 // width pixels
  var d2r = Math.PI/180;          // degrees to radians
  var r2d = 180/Math.PI;          // radians to degrees
  var Clat = (Cradius/3963)*r2d;  // using 3963 as earth's radius
  var Clng = Clat/Math.cos(lat*d2r);
  var Cpoints = [];
  var Cx;
  var Cy;
  
  for (var i=0; i < 33; i++) {
    var theta = Math.PI * (i/16);
    Cx = lng + (Clng * Math.cos(theta));
    Cy = lat + (Clat * Math.sin(theta));
    Cpoints.push(new GPoint(Cx,Cy));
  };
  map.addOverlay(new GPolyline(Cpoints,Ccolor,Cwidth));
}
