function getCounties() {
  var f = document.forms['gnis'];
  var qs = 'searchtype=counties&state=' + f.state.value;
  var xhr = new xHttpRequest();
  if (!xhr.send('POST', '/lib/gnis.php', qs, xhrTo, false, false, null, onCountyResponse)) gotoError();
  xhr = null;
}
function onCountyResponse(req, status, data) {
  if (status == 0) {
    var json = JSON.parse(req.responseText);
    if (json.ok)  {
      var f = document.forms['gnis'];
      var sel = f.county;
      sel.options.length = 0;
      var opt = xCreateElement('option');
      opt.value = '0';
      opt.text = 'All Counties';
      sel.add(opt, null);
      for (var n=0; n<json.data.length; n++) {
        opt = xCreateElement('option');
        opt.value = json.data[n][0];
        opt.text = json.data[n][1];
        sel.add(opt, null);
      }
    } else alert(json.err);
  } else gotoError();
}
function searchGnis() {
  if (validateGnis()) {
    var f = document.forms['gnis'];
    var qs = 'searchtype=places&type=' + f.feature.value + '&fips=' + f.county.value + '&name=' + f.fname.value;
    var xhr = new xHttpRequest();
    if (!xhr.send('POST', '/lib/gnis.php', qs, xhrTo, false, false, null, onGnisResponse)) gotoError();
    xhr = null;
  }
  return false;
}
function onGnisResponse(req, status, data) {
  if (status == 0) {
    var json = JSON.parse(req.responseText);
    if (json.ok) {
      var i, j, c;
      var html = '<h3>Select Location:</h3><table border="0" cellpadding="5" cellspacing="0">';
      html += '<tr><th>Name</th><th>State</th><th>County</th><th>Feature Type</th></tr>';
      for (i in json.data) {
        row = json.data[i];
        html += '<tr class="' + (((i % 2) == 0) ? 'dark' : 'light') + '">';
        html += "<td><a href='javascript:selectGnisRow(\"" + row.join(':') + "\")'>" + row[0] + '</td>';
        for (j=1; j<4; j++) html += '<td>' + row[j] + '</td>';
        html += '<td>Select</td></tr>';
      }
      html += '</table>';
      if (gnisPop) {
        if (checkFenster('xfgnis')) setFensterHtml('xfgnis', 'xfgnis_content', html);
        else makeFenster('xfgnis', null, null, html, null, 'Select Location', gnisLeft, gnisTop, gnisWidth, gnisHeight);
      } else {
        xInnerHtml(gnisDiv, html);
        showDiv(gnisDiv);
      }
    } else alert(json.err);
  } else gotoError();
}
function validateGnis() {
  if (document.forms['gnis'].fname.value.match(/^\s*$/)) {
    alert('You must enter a place name.');
    return false;
  } else return true;
}
