 
  // GLOBALS:
  var deptIdx = 0;

  //---------------------------------------------------------------------------
  // initForm - checks to see if any parameters were passed in to this page,
  // and if so, it attempts to set the related form field values.
  //---------------------------------------------------------------------------
  function initForm()
  {
    // Shared list params:
    setDroplistValue("rpp", getUrlParam("rpp"));
    setRadioValue("rf", getUrlParam("rf"));
    setDroplistValue("sb", getUrlParam("sb"));
    setDroplistValue("sd", getUrlParam("sd"));
    
    toggleKsrchButton();
    
    // Advanced search:

    // special handling for department/classification settings:    
    setDroplistValue("dp", getUrlParam("dp"));
    var idx = document.getElementById("dp").selectedIndex;    
    departmentChanged(idx);
    setDroplistValue("cl"+deptIdx, getUrlParam("cl"));    

    setTextValue("by",getUrlParam("by"));
    setTextValue("ey",getUrlParam("ey"));
    setRadioValue("byr",getUrlParam("byr"));
    setRadioValue("eyr",getUrlParam("eyr"));

    setLocationCheckbox("lv",getUrlParam("lv"));
    setLocationCheckbox("la",getUrlParam("la"));
    setLocationCheckbox("ls",getUrlParam("ls"));
    
    // Note: all text fields are handled via the template 
    // to simplify quoting issues in URIs
  }
  
  //---------------------------------------------------------------------------
  // getUrlParam - returns the value of a parameter from the url of the
  // current page, or "" if it's not defined.
  //---------------------------------------------------------------------------
  function getUrlParam(param)
  {
    param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    
    var regexS = "[\\?&]"+param+"=([^&#]*)";
    var regex = new RegExp( regexS );
    
    var results = regex.exec(window.location.href);
    
    if( results == null )
    {
      return "";
    }
    else
    {
      return unescape(results[1]);
    }
  }

  //---------------------------------------------------------------------------
  // departmentChanged - Handles the display of the appropriate classifications
  // specific to a given department.  
  //---------------------------------------------------------------------------
  function departmentChanged(idx)
  {
    // get a pointer to the 'active' classification droplist (prior to dept change):
    var elem = document.getElementsByName("cl")[deptIdx];

    // get the value of the selected classification option:
    var cval = elem.options[elem.selectedIndex].value;

    // reset the (old) classification droplist that we're about to hide
    // (so that when we come back to it next time it's already reset)
    elem.selectedIndex = 0;

    // hide all:
    for (var i=0; i<document.getElementsByName("cl").length; i++)
    {
      document.getElementsByName("cl")[i].style.display = "none";
    }

    // un-hide the classification field for the selected department:
    document.getElementsByName("cl")[idx].style.display = "inline";

    // Now set the new classification droplist to match the old, assuming
    // that option exists:
    setClassification(document.getElementsByName("cl")[idx], cval);

    // and finally update the global
    deptIdx = idx;

    // Oh, and update the label so that accesskey will work:
    document.getElementById("classificationLabel").htmlFor = "cl"+idx;
    
  }

  //---------------------------------------------------------------------------
  // setClassification - handles the selection of a given item when switching
  // from one department to another.  
  //---------------------------------------------------------------------------
  function setClassification(obj, val)
  {
    for (var i=0; i<obj.options.length; i++)
    {
      if ( obj.options[i].value == val )
      {
        obj.selectedIndex = i;
      }
    }

  }

  //---------------------------------------------------------------------------
  // toggleHints - shows/hides the keyword search hints 
  //---------------------------------------------------------------------------
  function toggleHints()
  {
    var cur = document.getElementById("searchHints").style.display;
    if ( cur == "inline" )
    {
      document.getElementById("searchHints").style.display = "none";
    }
    else
    {
      document.getElementById("searchHints").style.display = "inline";
    }
  }


  //---------------------------------------------------------------------------
  // Checks each keystroke in the keyword search field, and watches for 
  // enter press, which triggers the keyword search call.
  //---------------------------------------------------------------------------
  function ksrchKeyup(evt)
  {
    toggleKsrchButton();

    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    if ( charCode == 13 )
    {
      keywordSearch();
    }
  }

  //---------------------------------------------------------------------------
  // enables/disables the keyword search button 
  //---------------------------------------------------------------------------
  function toggleKsrchButton()
  {
    var val = document.getElementById("ksrch").value;
    
    document.getElementById("ksrchButton").disabled = true;
    if ( val != "" )
    {
      document.getElementById("ksrchButton").disabled = false;
    }
  }

  //---------------------------------------------------------------------------
  // keywordSearch - submits the keyword search 
  //---------------------------------------------------------------------------
  function keywordSearch()
  {
    var val = document.getElementById("ksrch").value;
    if ( val != "" )
    {
      // Strip off double quotes
      val = val.replace('"','');
      
      // force all letters to lower case
      val = val.toLowerCase();
      
      // Build the url for the search:
      var url = "";
      
      url += "results.htm?ksrch="+val;
      url += "&rf=" + getRadioValue("rf");
      url += "&rpp=" + getDroplistValue("rpp");
      url += "&sb=" + getDroplistValue("sb");       
      url += "&sd=" + getDroplistValue("sd");       
       
      // reset the page number to 1:
      url += "&pn=1";
      
      document.location.href = url;     
    }
  }
  
  //---------------------------------------------------------------------------
  //  
  //---------------------------------------------------------------------------
  function advancedReset()
  {
    // special handling for department/classification settings:    
    document.getElementById("dp").selectedIndex = 0;
    departmentChanged(0);
    document.getElementById("cl0").selectedIndex = 0;

    setTextValue("ar","");
    setTextValue("ti","");
    setTextValue("me","");
    setTextValue("cu","");

    setTextValue("by","");
    setTextValue("ey","");
    document.getElementById("byrad").checked = "checked";
    document.getElementById("eyrad").checked = "checked";

    setTextValue("ge","");
    setTextValue("an","");

    setLocationCheckbox("lv",0);
    setLocationCheckbox("la",1);
    setLocationCheckbox("ls",2);

    setDroplistValue("rpp", "25");
    setRadioValue("rf","0");
    setDroplistValue("sb", "objectNumber");
    setDroplistValue("sd", "0");
  }
  
  
  //---------------------------------------------------------------------------
  //  
  //---------------------------------------------------------------------------
  function advancedSearch()
  {
    // Ensure at least one of the location checkboxes is checked:
    var lv = getLocationCheckboxValue("lv");       
    var la = getLocationCheckboxValue("la");       
    var ls = getLocationCheckboxValue("ls");
    
    if ( lv == -1 && la == -1 && ls == -1 )
    {
      alert("You must select at least one location type");
      document.getElementById("lv").focus();
      return;
    }

    var url = "";
      
    url += "results.htm";
    url += "?rf=" + getRadioValue("rf");
    url += "&rpp=" + getDroplistValue("rpp");
    url += "&sb=" + getDroplistValue("sb");       
    url += "&sd=" + getDroplistValue("sd");       
    url += "&pn=1";
    url += "&dp=" + getDroplistValue("dp");
    
    // Special handling for classification...
    var idx = document.getElementById("dp").selectedIndex;
    url += "&cl=" + getDroplistValue("cl"+idx);       
    
    url += "&ar=" + getTextValue("ar");       
    url += "&ti=" + getTextValue("ti");       
    url += "&me=" + getTextValue("me");       
    url += "&cu=" + getTextValue("cu");       
    url += "&by=" + getTextValue("by");       
    url += "&byr=" + getRadioValue("byr");       
    url += "&ey=" + getTextValue("ey");       
    url += "&eyr=" + getRadioValue("eyr");       
    url += "&ge=" + getTextValue("ge");       
    url += "&an=" + getTextValue("an"); 

    if ( lv != null ) url += "&lv=" + getLocationCheckboxValue("lv");       
    if ( la != null ) url += "&la=" + getLocationCheckboxValue("la");       
    if ( ls != null ) url += "&ls=" + getLocationCheckboxValue("ls");       
    
    document.location.href = url;     
  }
  
  function getClassification(deptIdx)
  {
    var obj = document.getElementByName("cl"+deptIdx);
    
  }
  

  //---------------------------------------------------------------------------
  // Gets the value of a text field 
  //---------------------------------------------------------------------------
  function getTextValue(fieldId)
  {
    var val = document.getElementById(fieldId).value;
    return val;
  }
  
  
  //---------------------------------------------------------------------------
  // Sets text field to the specified value, or blank if undefined.
  //---------------------------------------------------------------------------
  function setTextValue(fieldId, val)
  {
    if ( ! val ) val = "";
    document.getElementById(fieldId).value = val;
  }


  //---------------------------------------------------------------------------
  // Sets checkbox field to the specified value, or blank if undefined.
  //---------------------------------------------------------------------------
  function setLocationCheckbox(fieldId, val)
  {
    if ( ! val || val == "" )
    {
      // By default (on load only) every location is checked
      document.getElementById(fieldId).checked = "checked"; 
    }
    else if ( val == -1 || val == "-1")
    {
      // -1 forces it to be blank (primarily for 'refine this search' logic)
      document.getElementById(fieldId).checked = ""; 
    }
    else
    {
      document.getElementById(fieldId).checked = "checked"; 
    }
  }

  //---------------------------------------------------------------------------
  // Returns the checkbox value
  //---------------------------------------------------------------------------
  function getLocationCheckboxValue(fieldId)
  {
    var cb = document.getElementById(fieldId)
    if ( cb != null && cb.checked )
    {
      return cb.value;
    }
    else
    {
      return -1;
    } 
  }
  

  //---------------------------------------------------------------------------
  //
  //---------------------------------------------------------------------------
  function getDroplistValue(droplistId)
  {
    var obj = document.getElementById(droplistId);
    return obj[obj.selectedIndex].value;
  }

  //---------------------------------------------------------------------------
  //
  //---------------------------------------------------------------------------
  function setDroplistValue(droplistId, val)
  {
    if ( ! val ) return;
    if ( val.indexOf("+") != -1 )
    {
      val = val.replace(/\+/g," ");
    }
    var obj = document.getElementById(droplistId);
    for (var i=0; i<obj.options.length; i++)
    {
      if ( obj.options[i].value == val )
      {
        obj.selectedIndex = i;
      }
    }
  }
  

  //---------------------------------------------------------------------------
  // Returns the value assigned to whichever radio option is checked
  //---------------------------------------------------------------------------
  function getRadioValue(radioName)
  {
    var rfs = document.getElementsByName(radioName);
    for (var i=0; i<rfs.length; i++)
    {
      if ( rfs[i].checked ) 
      {
        return rfs[i].value;
      }
    }
  }
  
  //---------------------------------------------------------------------------
  // Iterates thru the radio fields of the specified name and checks the first
  // item whose value matches 'val'. 
  //---------------------------------------------------------------------------
  function setRadioValue(radioName, val)
  {
    if ( ! val ) return;
    
    var rfs = document.getElementsByName(radioName);
    for (var i=0; i<rfs.length; i++)
    {
      if ( rfs[i].value == val ) 
      {
        return rfs[i].checked = "checked";
      }
    }
  }

  //---------------------------------------------------------------------------
  //
  //---------------------------------------------------------------------------
  function checkNumeric(evt, fld)
  {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));

    var val = fld.value;

    if ( (charCode > 47 && charCode < 58) || (charCode > 95 && charCode < 106) || charCode == 13)
    {
      // valid keystrokes, so just leave them alone
      return;
    }
    else
    {
      // everything else (that's not a control char) gets stripped:
      if ( charCode >= 32 )
      {
        var pgnum = fld.value;
        pgnum = pgnum.substring(0,(pgnum.length - 1))
        fld.value = pgnum;
      }
    }
  }
  
  
  //---------------------------------------------------------------------------
  // Checks each keystroke in the header's keyword search field, and watches  
  // for enter press, which triggers the keyword search call.
  //---------------------------------------------------------------------------
  function headerCheckForEnter(evt)
  {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    if ( charCode == 13 )
    {
      headerKeywordSearch();
    }
  }
  
  //---------------------------------------------------------------------------
  // headerKeywordSearch - submits the keyword search from the header 
  //---------------------------------------------------------------------------
  function headerKeywordSearch()
  {
    var ksrch = document.getElementById("hdr_ksrch").value;
    if ( ksrch != null && ksrch != "" )
    {
      var url = "results.htm?ksrch="+ksrch;
      url += "&rf="+document.getElementById("hdr_rf").value;
      url += "&rpp="+document.getElementById("hdr_rpp").value;
      url += "&sb="+document.getElementById("hdr_sb").value;       
      url += "&sd="+document.getElementById("hdr_sd").value;       
      url += "&pn=1";
      
      document.location.href = url;     
    }
  }


  //---------------------------------------------------------------------------
  // Waits for enter key press, then validates the data in the page number
  // input field.  If it's numeric, and it's between 1 and totalpagecount, 
  // the specified url is called with the requested page number appended.
  // 
  // Note that the url MUST NOT CONTAIN &pn=xx
  // 
  //---------------------------------------------------------------------------
  function resultsPageFieldKeyed(evt, fld, currentPage, totalpages, url)
  {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    var val = fld.value;

    // If enter pressed, validate the field and either
    // submit the request or display an error: 
    if ( charCode == 13)
    {
      if ( isNaN(val) )
      {
        alert("Invalid page number");
        fld.value = currentPage;
        return;
      }
      else
      {
        // Ensure the number is between 1 and totalpages:
        var num = Number(val);
        if ( num > 0 && num <= totalpages )
        {
          document.location.href = url + "&pn="+val;
        }
        else
        {
          alert("Invalid page number");
          fld.value = currentPage;
          return;
        }
      }
    }
  }
  
