// Open a new window containing only the high-res map
function openBigMap(mode, state, stateName) {
   // If the user navigated directly to sabre.php, mode and state won't be
   // set. Use 'alaska' and 'production' as defaults
   if (state == "") {
      state = "alabama";
   }
   if (mode == "") {
      mode = "prod"
   }
   url = "big_map.php?state=" + state + "&mode=" + mode + "&stateName=" + stateName;
   window.open(url, 'large_map');
}
  
// Validate user input for percent utilization of FFVs before submitting
function calculateFFVUse() {
   var error = false;
   var pctE85 = document.prodUseForm.percent_e85.value;
   if (isNaN(parseInt(pctE85))) {
      error = true;
   } else {
      var value = parseInt(pctE85);
      if (value < 0 || value > 100) {
         error = true;
      } else {
         document.prodUseForm.percent_e85.value = value;
      }
   }
   if (error) {
      alert ("Please enter the frequency of E85 use in the state's FFV fleet as a percentage, 0 - 100");
      document.prodUseForm.percent_e85.selectionStart = 0;
      return false;
   } else {
      saveScrollXY();
      return true;
   }
}
// Save the current scroll position so the user doesn't have scroll back
// to calculation results
function saveScrollXY() {
   var scrollX, scrollY;
      
   if (document.all) { // For IE
      if (!document.documentElement.scrollLeft)
         scrollX = document.body.scrollLeft;
      else
         scrollX = document.documentElement.scrollLeft;
            
      if (!document.documentElement.scrollTop)
         scrollY = document.body.scrollTop;
      else
         scrollY = document.documentElement.scrollTop;
   }   
   else {  // For other browsers
      scrollX = window.pageXOffset;
      scrollY = window.pageYOffset;
   }
    document.prodUseForm.ScrollX.value = scrollX;
    document.prodUseForm.ScrollY.value = scrollY;
}
function resetScrollPosition() {
    var hidx, hidy;
    if (document.prodUseForm) {
      hidx = document.prodUseForm.ScrollX;
      hidy = document.prodUseForm.ScrollY;
      if (typeof hidx != 'undefined' && typeof hidy != 'undefined') {
         window.scrollTo(hidx.value, hidy.value);
      }
    }
}

// Check the value of a select box. If it's null, don't do anything
//(ignore the prompt)
function handleSelectNewState(select) {
   if (select.options[select.selectedIndex].value == "") {
      return false;
   } else {
      return true;
   }
}

// Handle 'potential production' form submission
function handleProdSubmit(max_grain) {
   var warning = false;
   var pctCropUse = document.prodUseForm.grain_limit.value;
      if (isNaN(parseInt(pctCropUse)) || parseInt(pctCropUse) > 100) {
         alert ("Please enter the percentage of grain harvest that is to be used " +
             "in biofuels production (0 to 100).");
         return false;
   } else {
      var value = parseInt(pctCropUse);
      if (value < 0 || value > max_grain) {
         warning = true;
      } 
   }
   if (warning) {
      alert ("You have entered a value greater than " + max_grain + "%.  Several " +
             "studies estimate the upper limit of ethanol that can be sustainably " +
             "produced in the U.S. from corn at around 15 billion gallons per " +
             "year based on projected corn yields and food/feed uses in 2015. " +
             "This represents around 30%-40% of the projected corn production  " +
             "rates in 2015.");
   } 
   saveScrollXY();
   return true;
}