/***************************************************
*
*              q u a d _ o r _ c o u n t i e s
*
*  If the user clicks on Select counties or
*  Select quadrangles, control passes here to 
*  handle the request
***************************************************/

leftoff = window.screenX;
topoff = window.screenY;
// alert(leftoff + ", " + topoff);

function quad_or_counties()
{
 ngmdir = "/ngmdb";
 quaddir = "/100k_quads/";
 states = new Array("AL", "Alabama",
                    "AK", "Alaska",
                    "AS", "American Samoa",
                    "AZ", "Arizona",
                    "AR", "Arkansas",
                    "CA", "California",
                    "CO", "Colorado",
                    "CT", "Connecticut",
                    "DE", "Delaware",
                    "DC", "District of Columbia",
                    "FM", "Federated States of Micronesia",
                    "FL", "Florida",
                    "GA", "Georgia",
                    "GU", "Guam",
                    "HI", "Hawaii",
                    "ID", "Idaho",
                    "IL", "Illinois",
                    "IN", "Indiana",
                    "IA", "Iowa",
                    "KS", "Kansas",
                    "KY", "Kentucky",
                    "LA", "Louisiana",
                    "ME", "Maine",
                    "MD", "Maryland",
                    "MH", "Marshall Islands",
                    "MA", "Massachusetts",
                    "MI", "Michigan",
                    "MN", "Minnesota",
                    "MS", "Mississippi",
                    "MO", "Missouri",
                    "MT", "Montana",
                    "NE", "Nebraska",
                    "NV", "Nevada",
                    "NH", "New Hampshire",
                    "NJ", "New Jersey",
                    "NM", "New Mexico",
                    "NY", "New York",
                    "NC", "North Carolina",
                    "ND", "North Dakota",
                    "MP", "Northern Mariana Islands",
                    "OH", "Ohio",
                    "OK", "Oklahoma",
                    "OR", "Oregon",
                    "PA", "Pennsylvania",
                    "PR", "Puerto Rico",
                    "PW", "Republic of Palau",
                    "RI", "Rhode Island",
                    "SC", "South Carolina",
                    "SD", "South Dakota",
                    "TN", "Tennessee",
                    "TX", "Texas",
                    "UM", "U.S. Minor Outlying Islands",
                    "UT", "Utah",
                    "VT", "Vermont",
                    "VI", "Virgin Islands",
                    "VA", "Virginia",
                    "WA", "Washington",
                    "WV", "West Virginia",
                    "WI", "Wisconsin",
                    "WY", "Wyoming");
 
/*********
*    if option not Select counties or quadrangles, ignor.
*********/
   chkmode = document.ngmform.Counties.selectedIndex;
   if(chkmode >= 0 && document.ngmform.Counties.options[chkmode].value.indexOf("Select") != 0) {
     return;
/*
   } else {
     if (chkmode == 0) {
       document.ngmform.Counties.options[1].selected = false;
     } else {
       document.ngmform.Counties.options[0].selected = false;
     }
*/
   }

/*********
*    if no state(s) is selected, inform the user and return
*********/
   onesel = false;
   k = document.ngmform.State.options.length;
   for (j = 0; j < k; j++)
   {
     if(document.ngmform.State.options[j].selected)
     {
       onesel = true;
       break;
     }
   }
   if(!onesel)
   {
     alert("**Error**select a state or states before selecting counties or quadrangles");
     reset_q_and_c(false);
     return;
   }

/*
*     alert("**made it into quad_or_counties OK"); 
*/
              
/*********
*    go through the list of states to see which were selected
*********/
  for (j = 0; j < k; j++)
  {
 
/*********
*    if a state is selected, look for its abbreviation,
*    construct the HTML file name for its counties using
*    the abbreviation, and open a new window using the
*    constructed counties HTML name
*********/
     if(document.ngmform.State.options[j].selected)
     {
        m = document.ngmform.State.options[j].text;
        for(i = 1; i <= states.length; i = i+2)
        {
          if (m == states[i])
          {
             
             if(document.ngmform.Counties.options[0].selected)
             {
               document.ngmform.cqtype.value = "Counties";
               if(states[i-1] == "AK") {
                 htmlname = quaddir + states[i-1] + "_250k_quads_new.html";
                 winwid = 700;
                 winhgt = 650;
                 alert ("NGMDB uses quadrangle\nmap names "+
                        "instead\nof buroughs (counties)\n"+
                        "for Alaska");
               } else {
                 htmlname = ngmdir+"/Counties/" +
                            states[i-1] + "_counties_new.html";
                 winwid = 250;
                 winhgt = 300;
               }

               newwin = window.open(htmlname,states[i-1],
                          "status=yes, toolbar=yes,resizable=yes,"+
                          "scrollbars=yes,width="+winwid+",height="+winhgt);
             }
             
             else if(document.ngmform.Counties.options[1].selected)
             {
               document.ngmform.cqtype.value = "Quads";
               if(states[i-1] == "AS") break;
               if(states[i-1] == "DC") break;
               if(states[i-1] == "FM") break;
               if(states[i-1] == "GU") break;
               if(states[i-1] == "MH") break;
               if(states[i-1] == "MP") break;
               if(states[i-1] == "PW") break;
               if(states[i-1] == "UM") break;
               if(states[i-1] == "AK") {
                 htmlname = quaddir + states[i-1] + "_250k_quads.html";
               } else if(states[i-1] == "PR") {
                 htmlname = quaddir + states[i-1] + "_24k_quads.html";
               } else if(states[i-1] == "VI") {
                 htmlname = quaddir + states[i-1] + "_24k_quads.html";
               } else {
                 htmlname = quaddir + states[i-1] + "_100k_quads.html";
               }

               newwin = window.open(htmlname,states[i-1],
                          "status=yes,menubar=yes,resizable=yes,"+
                          "scrollbars=yes,width=700,height=650");
             }

             break;
          }
        }
      }
    }
/*
*  alert("search type: "+document.ngmform.cqtype.value);
*/
}
 
/***************************************************
*
*              r e s e t _ q _ a n d _ c
*
*  If the user clicks on Clear, this function is
*  called to set the counties/quads options back 
*  "Select counties" and "Select quadrangles". It 
*   also resets some default parameters.
***************************************************/
function reset_q_and_c()
{
  document.ngmform.cqlist.value = "";
  document.ngmform.Counties.options.length = 0;
 
  newcounty = new Option("Select counties","Select counties");
  document.ngmform.Counties.options[0] = newcounty;
  document.ngmform.Counties.options[0].text = "By counties";
  document.ngmform.Counties.options[0].value = "Select counties";
  document.ngmform.Counties.options[0].selected = false;
 
  newcounty = new Option("Select quadrangles","Select quadrangles");
  document.ngmform.Counties.options[1] = newcounty;
  document.ngmform.Counties.options[1].text = "By 100K quads";
  document.ngmform.Counties.options[1].value = "Select quadrangles";
  document.ngmform.Counties.options[1].selected = false;
  document.ngmform.Counties.options.length = 2;
}
 
/***************************************************
*
*              r e s t o r e _ q _ a n d _ c
*
*  On return from the display results page (Back or
*  Refine) this routine checks to see if the "cqlist"
*  input field is empty.  If not the routine restores
*  the Counties select field with the Options as
*  found in the "cqlist" field.
***************************************************/
function restore_q_and_c()
{
  if (document.getElementById("cqlist").value == "") {
    reset_q_and_c();
  } else {
    var cqarray = document.getElementById("cqlist").value.split(";");
    document.getElementById("Counties").options.length = 0;
    for (j=0; j < (cqarray.length-1); j++) {
      document.getElementById("Counties").options.add(document.createElement("OPTION"),j);
      document.getElementById("Counties").options[j].text = cqarray[j];
      document.getElementById("Counties").options[j].value = cqarray[j];
      document.getElementById("Counties").options[j].selected = true;
    }
  }
}
