// all this return true, return false nonsense has to do with testing; cut it from the
// real thing. But *do* keep the returns (should still return rather than finishing loop).
function segChart() {
  for (i=0;i<document.charter.measure.length;i++) {
    if (document.charter.measure[i].checked) {
      var url = document.charter.measure[i].value;
    }
  }
  for (i=0;i<document.charter.geogLevel.length;i++) {
    if (document.charter.geogLevel[i].checked) {
      switch(document.charter.geogLevel[i].value) {
        case "metro":
          if(document.charter.metro.options[document.charter.metro.selectedIndex].value != "") {
            jumpURL("us/m" + document.charter.metro.options[document.charter.metro.selectedIndex].value + "/" + url);
          } else {
            alert('Please use the "Choose Metro" pull-down menu to select the metropolitan area for which you want to view a chart.');
            return false;
          }
          break;
        case "state":
          if(document.charter.state.options[document.charter.state.selectedIndex].value != "") {
            if(document.charter.city.options[document.charter.city.selectedIndex].value != "") { 
              jumpURL("us/s" + document.charter.state.options[document.charter.state.selectedIndex].value 
                      + "/p" + document.charter.city.options[document.charter.city.selectedIndex].value + "/" + url);
            } else {
              alert('Please use the "Choose City" pull-down menu to select the city for which you want to view a chart.');
              return false;
            }
          } else {
            alert('Please use the "Choose State" pull-down menu to select a state. Then use the "Choose City" pull-down menu to select a city within that state.');
            return false;
          }
          break;
      }
    }
  }
  return true;
}

function segRank() {
  var url = document.ranker.variable.options[document.ranker.variable.selectedIndex].value;
  if(url == "") {
    alert("You need to select a variable to rank.");
    return false;
  } else {
    for (i=0;i<document.ranker.geogLevel.length;i++) {
      if (document.ranker.geogLevel[i].checked) {
        switch(document.ranker.geogLevel[i].value) {
          case "metros":
            jumpURL("us/" + url);
            break;
          case "state":
            if(document.ranker.state.value != "") {
              jumpURL("us/s" + document.ranker.state.options[document.ranker.state.selectedIndex].value + "/" + url);
            } else {
              alert('You need to specify which state\'s cities you want ranked. Please use the "Choose State" pull-down menu to select a state.');
              return false;
            }
            break;
        }
      }
    } 
  }
  return true;
}

// The gist of these scripts is comes from Peter-Paul Koch 
// So if you need to work with them, see http://www.xs4all.nl/~ppk/js/options.html

// Note to self: If this causes problems on some browsers, it may be because I
// ignored Koch's admonition to make "size=5" (or some number larger than 1) in
// the select that has new items inserted.

function segPopulate() { 
  if (!segOptionTest) return; 
  var box = document.charter.state; 
  var box2 = document.charter.city;
  var number = box.options[box.selectedIndex].value;
  if (!number) {
    box2.disabled = true;
    return; 
  }
  var list = segStore[number]; 
  while (box2.options.length) box2.options[0] = null; 
  for(i=0;i<list.length;i+=2) { 
    box2.options[i/2] = new Option(list[i+1],list[i]);
  }
  box2.disabled = false;
}

function segOptionTestIt() {
  segOptionTest = true; 
  lgth = document.charter.city.options.length - 1;
  document.charter.city.options[lgth] = null; 
  if (document.charter.city.options[lgth]) segOptionTest = false;
 
  // This is unrelated code and should perhaps be moved: it makes sure that the city box is populated and
  // has the correct option selected if the person had selected a city to get to the current page. (Most
  // of this state-preservation happens in scheme, but this bit has to be client-side.

  if(geogLevel=="place" && segOptionTest) {
    segPopulate();
    for(i=0;i<document.charter.city.length;i++) {
	// alert(i + ' ' + document.charter.city.options[i] + ' ' + fips['place']);
      if(document.charter.city.options[i].value == fips['place']) {
        document.charter.city.selectedIndex = i;
      }     
    }
  }
} 

// These arrays are to allow the  select menu to be dynamically 
// swapped after a state is selected. While these arrays make the js 
// file attrociously large, things aren't quite as bad as they seem:
// after the user has viewed any segregation pages, the js will be
// cached and viewing other segregation pages won't incur the same
// (massive) download penalty. Beats putting it in the HTML, anyway.

// This file is auto-generated by cs-build-segstore-array.scm

