
// search map functions
//====================================================
function addtown(zName) {
//====================================================
    // if (tobox.options.length == 0) {
    tobox=document.getElementById('searchform').selectedtowns;
    // alert("Town: " + zName);
    var bct=tobox.length;
            var newoption = new Option();
            newoption.value = zName;
            newoption.text = zName;
            tobox.options[bct] = newoption ;
            tobox.length++;
    // }
    KillSpace(tobox);
}


//====================================================
function removetown(zName) {
//====================================================
    // alert(zName);
    frombox=document.getElementById('searchform').selectedtowns;

    for(var i=0; i<frombox.options.length; i++) {
        // alert(frombox.options[i].text);
        if(frombox.options[i].text == zName) {
            frombox.options[i].value = "";
            frombox.options[i].text = "";
           }
    }

    KillSpace(frombox);

}
function addlake(lake) {
    // alert(lake);
    box = document.getElementById('searchform').waterbody;
    for(var i=0; i<box.options.length; i++) {
        if(box.options[i].value == lake) {
            box.options[i].selected=true;
        }
    }
    selectwf();

}

function selectwf() {
    var frm = document.getElementById("searchform");
    frm.waterfront.checked=true;
}
//====================================================
function removeitem(frombox) {
//====================================================
    for(var i=0; i<frombox.options.length; i++) {
        if(frombox.options[i].selected && frombox.options[i].value != "") {
            frombox.options[i].value = "";
            frombox.options[i].text = "";
           }
    }
    KillSpace(frombox);
}


//====================================================
function cleartowns() {
//====================================================
       frombox=document.getElementById('selectedtowns');

       for(var i=0; i<frombox.options.length; i++) {
        frombox.options[i].value = "";
        frombox.options[i].text = "";
    }

    KillSpace(frombox);

    if (frombox.name != "townbox") {
        // addtown ("All Towns")
    }

}


sortem = 1;  //true

//====================================================
function SortThem(lbox)  {
//====================================================
    var t_opts = new Array();
    var temp = new Object();

    for(var i=0; i<lbox.options.length; i++)  {
        t_opts[i] = lbox.options[i];
    }

    for(var x=0; x<t_opts.length-1; x++)  {
        for(var y=(x+1); y<t_opts.length; y++)  {
            if(t_opts[x].text > t_opts[y].text)  {
                temp = t_opts[x].text;
                t_opts[x].text = t_opts[y].text;
                t_opts[y].text = temp;
                temp = t_opts[x].value;
                t_opts[x].value = t_opts[y].value;
                t_opts[y].value = temp;
            }
           }
    }

    for(var i=0; i<lbox.options.length; i++)  {
        lbox.options[i].value = t_opts[i].value;
        lbox.options[i].text = t_opts[i].text;
   }
}

//====================================================
function selecttowns() {
    // selects all towns in the thetowns list
    // alert("selecttowns");

    // if there are no towns selected, then we need to
    // add ALL of the towns.
    var box =document.getElementById('selectedtowns');
    for(var i=0; i<box.options.length; i++) {
        box.options[i].selected=true;
    }
}

//====================================================
function KillSpace(box)  {
    for(var i=0; i<box.options.length; i++) {
        if(box.options[i].value == "")  {
            for(var j=i; j<box.options.length-1; j++)  {
                box.options[j].value = box.options[j+1].value;
                box.options[j].text = box.options[j+1].text;
            }
            var ln = i;
            break;
        }
    }
    if(ln < box.options.length)  {
        box.options.length -= 1;
        KillSpace(box);
   }
}

function showarea(area) {
    // document.location="areasearch.php?area=" + area;
    //use AJAX to replace the map.
   // alert("Test: " + area);
    doAjaxFlash(area);
}

function shownhmap() {
    //document.location="jsresults.php?area=nh";
    doAjaxFlash("nh");
}


// AJAX for mapping results
function doAjaxFlash(area)
{
var xmlHttp;

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  ajaxGood=true;
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    ajaxGood=true;
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      ajaxGood=true;
      }
    catch (e)
      {
          // use qs to return content
          // if we return true, then the link will be followed by non js browsers and SEO.

          return true;
      }
    }
  }

  // if we are here, then we have a valid xmlHttp object and we can use Ajax to serve the content.
document.getElementById('searchmain').innerHTML = "<h2>Getting Flash Map...</h2>";

xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
      // document.myForm.time.value=xmlHttp.responseText;
      rv = xmlHttp.responseText;
      document.getElementById('searchmain').innerHTML = rv;
    }
}

xmlHttp.open("GET","ajaxflashmap.php?area="+area+"&v="+Math.round(),true);
xmlHttp.send(null);
    // by returning false, the original click of the link will not be followed..
    return false;
} // end of doAjax
