// function syncRadioMenu(m,f,r) should be triggered by onFocus for any of the pull-down menus for
// selecting a place to jump to. It should be passsed the value of the corresponding radio button (as a string),
// the name of the enclosing form (as a string), and the name of the corresponding radio button group 
// (as a string). What does it do? It changes which radio button is checked so that it corresponds to the 
// active menu.

function syncRadioMenu(m,f,r) {
  var radioGroup = eval("document.forms['" + f  + "']." + r); 
  for (i=0;i<radioGroup.length;i++) {
    if (radioGroup[i].value == m)
      radioGroup[i].checked = true;
    else
      radioGroup[i].checked = false;
  } 
}

function rank() {
  var url = document.ranker.variable.options[document.ranker.variable.selectedIndex].value;
  if(url == "") {
    alert('Please use the "Choose Variable" pull-down menu to select the variable you want ranked.');
  } else {
    for (i=0;i<document.ranker.geogLevel.length;i++) {
      if (document.ranker.geogLevel[i].checked) {
        switch(document.ranker.geogLevel[i].value) {
          case "states":
            jumpURL("us/" + url);
            break;
          case "counties":
            if(document.ranker.counties.value != "") {
              jumpURL("us/s" + document.ranker.counties.options[document.ranker.counties.selectedIndex].value + "/" + url);
            } else {
              alert('You need to specify which state\'s counties you want ranked. Please use the "Choose State" pull-down menu to select a state.');
            }
            break;
          case "metros":
            jumpURL("us/metro_" + url);
            break;
        }
      }
    } 
  }
}

// function zoomIn() should be triggered by onClick on the "zoom in" button. It loads
// the page specified by the pull-down menus. It assumes that for every radio button 
// geogLevel[i], there is a select box with geogLevel[i]'s value as its name.
function zoomIn() {
  for (i=0;i<document.zoomer.geogLevel.length;i++) {
    if (document.zoomer.geogLevel[i].checked) {
      //var url = eval("document.zoomer." + document.zoomer.geogLevel[i].value + ".value") + "/" + filename;
      var lvl = document.forms['zoomer'].geogLevel[i].value;
      var url = eval("document.zoomer." + lvl + ".options[document.zoomer." + lvl + ".selectedIndex].value") + "/" + filename;      
      if(url == "/" + filename) {
        alert("Please select a place to zoom in to from one of the pull-down menus.");
        return;
      }
      switch(document.zoomer.geogLevel[i].value) {
        case "state":
          url = "us/s" + url;
          break;
        case "metro":
          if(fips['state']) 
            url = "us/s" + fips['state'] + "/m" + url;
          else
            url = "us/m" + url;
          break;
        case "county":
          url = "us/s" + fips['state'] + "/c" + url;
          break;
      }
      if(url != "")
        jumpURL(url);
      else 
        alert("Please select a place to zoom in to from one of the pull-down menus.");
    }
  } 
}

function zoomOut() {
  switch(geogLevel) {
    case "state":
      jumpURL("us/" + filename);
      break;
    case "metro":
      if(fips['state'])
        jumpURL("us/s" + fips['state'] + "/" + filename);
      else
        jumpURL("us/" + filename);
      break;
    case "county":
      jumpURL("us/s" + fips['state'] + "/" + filename);
      break;
  }
}

function jumpURL(urlString) {
  window.location.href = baseURL + urlString;
}

// Koch's popup code (slightly modified)
// see http://www.xs4all.nl/~ppk/js/popup.html

function popitup(url)
{
  newwindow=window.open(baseURL + url,'name','height=350,width=350,resizable=yes,scrollbars=yes');
  if (window.focus) {newwindow.focus()}
}

// keeping up the tab metaphor

function recallTab(tab) {
  var c = readCookie(tab);
  if(c && (tab != 'about')) {
    window.location.href = c;
  } else {
    switch(tab) {
      case 'chart':
        window.location.href = baseURL + "us/chart_popl.html";
        break;
      case 'ranking':
        window.location.href = baseURL + "us/rank_popl_growth.html";
        break;
      case 'map':
        window.location.href = baseURL + "us/map_nhwhite.html";
        break;
      case 'segregation':
        window.location.href = baseURL + "segregation.html";
        break;
      case 'about':
        window.location.href = baseURL + "index.html";
        break;
    }
  }
}

function memorizeTab(tab,url) {
  createCookie(tab,url);
}

// The cookie fns are slimmed-down versions of Peter-Paul Koch's
// see http://www.xs4all.nl/~ppk/js/cookies.html

function createCookie(name,value)
{
  document.cookie = name+"="+value+"; path=/";
}

function readCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i<ca.length;i++)
  {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
