//==============================================================================
// Name:    rtqwPlotUtils
// Purpose: Process the request for RTQWWeb.
// Version: 3.0
// Date:    2/2008
// Authour: X. Jian, B. Garner
//-------------------------------------------------------------------------------
// Usage:
//    In the <head> area, add 
//    <script language = "JavaScript" src = "scripts/rtqwPlotUtils.js"> </script>
//-------------------------------------------------------------------------------
// Notes:
//   This version is for the MVC/PHP version of the RTQW website.
//-------------------------------------------------------------------------------
// History
//  01/14/02 - Initial coding.
//  11/01/02 - 1.0.1
//  07/11/07 - Garner, major revisions for dynamic frontend
//  03/02/09 - Garner, fixed small runtime error
//===============================================================================

//
//----------Define a list of user's varialbes
//
var gFormSelects = new Array('station', 'parameter', 'valueType', 'timePeriod', 
			     'season', 'month', 'availableData');
var _station, _parameter, _valueType, _timePeriod, _season, _month, _availableData;

//===============================================================================   
function showNext(frmObj) {
   var idx = frmObj.parameter.selectedIndex
   idx++;
   if (idx >= frmObj.parameter.length) {
      idx = 0
   }

   frmObj.parameter.selectedIndex = idx
   showCurrent(frmObj)
}

//===============================================================================
function showPrevious(frmObj) {
   var idx = frmObj.parameter.selectedIndex
   idx--;
   if (idx < 0) {
      idx = frmObj.parameter.length - 1
   }

   frmObj.parameter.selectedIndex = idx
   showCurrent(frmObj)
}

//==============================================================================
// Name:    getFormComponents (frmObj)
// Purpose: Get the values of form components
// Argument: 
//   frmObj - The object of html form.
//=============================================================================
function getFormComponents(frmObj){
   if (frmObj == null) {
      frmObj = document.frmSwap;
   }
   if (frmObj == null) {return 0}

   for (i = 0; i < gFormSelects.length; i++) {
       var formControl; 
       selectName = gFormSelects[i];
       eval ("_" + selectName + " = ''");
       eval ("formControl = frmObj." + selectName);
       if (formControl) {
	   var idx;
	   idx = formControl.selectedIndex;
	   eval ('_' + selectName + " = formControl.options[idx].value");
       }
   }

   return 1;
}

//===============================================================================
// Name:    ShowCurrent
// Purpose: Update the rtqw url and time-series plot.
// Argument: 
//   frmObj - The object of html form.
//-----------------------------------------------------------------------------
// Algorithm:
//   1. Get the form component values
//   2. Determine the page in terms of the component values.
//   3. Open the new page.
//-----------------------------------------------------------------------------
// History
//   Date      Description
// ----------- ----------------------------------------------------------------
// 04/23/2002  When the time period is '7D' or '31'D, set the available data to 'uv'.
// 02/2008     Build new url more flexibly, to always go to same URI as current page.
//===============================================================================
function showCurrent(frmObj) {
   getFormComponents(frmObj);

   if (_timePeriod == '31d' || _timePeriod == '7d') {
       _season = "all";
   } 
   if (_season == "mm") {
       _season = _month;
       if (_season == "") {
	   _season = "01"; // default to January, as control may not always be populated
       }
   } 
   if (_season == "") {
       _season = "all";
   }

   // built GET parameter list
   var parms = "site_no=" + _station + "&pcode=" + _parameter + "&period=" + _timePeriod + "_" + _season + "&timestep=" + _availableData;
   if (_valueType != 'concentration') {
       parms = parms + "&units=" + _valueType;
   }

   parms = parms.toLowerCase();

   // paste together new url using current one,
   var newurl = document.URL.split('?')[0] + '?' + parms;

   window.location = newurl; // load new page
}
