// SearchForm.js
// JavaScript functions for the All Country DBs search form.
// Warren Blatt, May 2005.  Updated July 2007.


// OnChange event handler for DataType SELECT box:
function SelProc(choice, target1, target2)
{
  var datatype = choice.value;
  var target_opt = document.getElementById(target1);
  var target_txt = document.getElementById(target2);

  if (datatype == "X")
    { 
      target_opt.disabled=1; 
      target_opt.options[5] = new Option ("contains", "", false, true);
      target_txt.disabled=0;
    }
  else if (datatype == "")
    { 
      target_opt.disabled=1;
      target_opt.selectedIndex = 0;
      target_txt.disabled=1;
    }
  else
    { 
      if (datatype == "S")
        { target_opt.selectedIndex = 1; }
      else if (datatype == "G")
        { target_opt.selectedIndex = 1; }
      else if (datatype == "T")
        { target_opt.selectedIndex = 1; }
        
      target_opt.disabled=0; 
      if ( target_opt.options.length == 6 )
         { target_opt.options.length = 5; }
      target_txt.disabled=0; 
    }
  return true;
}



function Sensitize(val)
{
  document.f.Months.disabled=val;
  document.f.Years.disabled=val;
}


// Disables all form controls.
function DisableForm (fo)
{
  var count = fo.elements.length;
  for (i=0; i<count; i++) 
    {
      fo.elements[i].disabled = true; 
    } 
}

// Enables all form controls.
function EnableForm (fo)
{
  var count = fo.elements.length;
  for (i=0; i<count; i++) 
    {
      fo.elements[i].disabled = false; 
    } 
}


// Submits form, then disables (to prevent repeat clicks).
function doSubmit(fo)
  {
    fo.submit();
    DisableForm(fo);
    document.getElementById("SearchButton").value = "Request has been submitted";
  }


// To be used as a BODY onload() event handler
function InitSearchForm()
{
  document.f.dates[0].checked=1;
  Sensitize(1);
  var sel2 = document.getElementById('SEL2'); SelProc(sel2, 'OPT2', 'SRCH2');
  var sel3 = document.getElementById('SEL3'); SelProc(sel3, 'OPT3', 'SRCH3');
  var sel4 = document.getElementById('SEL4'); SelProc(sel4, 'OPT4', 'SRCH4');
  document.f.srch1.focus();
}
