//===============================================================================
// Name:     xwaterwatch.js
// Purpose:  collection of javascript routines for waterwatch.
// Version:  2.3.8
// Author:   X. Jian
// Usage:
//      <script language='JavaScript' src='scripts/xwaterwatch.js'></script>
//-------------------------------------------------------------------------------
// Requirements:
//   xdoc.js -- IsNN, IsIE, DocX, DocY
//   xwin.js -- openWin(), openInfoWin(), closeWin()
//-------------------------------------------------------------------------------
// History
//  00/00/02 - Version 1.0: Initial coding.
//===============================================================================
var OpenNewWin = 0;   
var DailyMainW =  "http://water.usgs.gov/cgi-bin/waterwatch";
var DailySelectW= "http://water.usgs.gov/cgi-bin/dailySelectW";
var strSelIDs = new Array("map_type", "state", "map", "xi", "hu");
var intSelIdxs = new Array(5);
function linking(type){
   parent.frmLink.document.clear();
   parent.frmLink.document.location = "http://water.usgs.gov/waterwatch/dwc_tc_"+type+".htm";
}
function ImgOver(strImgName)
{
  var strIconName
  strIconName = strImgName + "_over.jpg"
  document.images[strImgName].src = "images/icons/" + strIconName
}
function ImgOver2(strImgName, strName)
{
  var strIconName
  strIconName = strImgName + "_" + strName + ".jpg"
  document.images[strImgName].src = "images/icons/" + strIconName
}
function ImgOut(strImgName)
{
  var strIconName
  strIconName = strImgName + "_out.jpg"
  document.images[strImgName].src = "images/icons/" + strIconName
}
function clickUS(strSt) {
   strFormName = "frmUS";
   var frmUS = document.forms[strFormName];
   if (frmUS == null) {
      alert("Invalid use of javacript function clickUS()");
      return;
   }
   var radSelectType = frmUS.select_type;
   var select_type = getRadioValue(radSelectType)  
   var map_type = frmUS.map_type.value;
   var scale = frmUS.scale.value
   var state = 'us'
   var img = document.imgUS;            
   var imgdiv = IsIE?document.all.us:'';
   if (select_type == null) {select_type = frmUS.select_type.value}
   var x0 = (IsNN)?img.x:(imgdiv?imgdiv.offsetLeft+img.offsetLeft:img.offsetLeft);
   var y0 = (IsNN)?img.y:(imgdiv?imgdiv.offsetTop+img.offsetTop:img.offsetTop);
   x = DocX - x0;
   y = DocY - y0;
   if (strSt != null && select_type != "point") {
      if (select_type == "statemap") {
         var type = (map_type == "real" || map_type == "flood")? 
                     "?type=flow" : "?type=dailystagedischarge&group_key=NONE";
         url = "http://waterdata.usgs.gov/" + strSt + "/nwis/current" + type;
      }
      else {
         url = DailyMainW + "?map_type=" + map_type + "&state=" + strSt
      }
   }
   else {
     url = DailySelectW + "?select_type="+select_type+"&scale=" + scale 
           + "&map_type=" + map_type + "&point.x=" + x + "&point.y=" + y
   }
   if (OpenNewWin && select_type == "statemap") {
      openWin(url);
   }
   else {
      window.document.location = url;
   }
}
function clickStation(url_default, x, y){
   var frmSelectType = document.forms['frmSelectType'];
   var map_type = frmSelectType.map_type.value;
   var state = frmSelectType.state.value;
   var scale = frmSelectType.scale.value;
   var radSelectType = frmSelectType.select_type;    
   var select_type = getRadioValue(radSelectType);
   
   var url = url_default;
   switch (select_type) {
      case 'point':
         url = DailySelectW + "?select_type=point&scale=" + scale 
                 + "&map_type=" + map_type + "&state=" + state + "&point.x=" + x + "&point.y="+y;
         window.document.location = url;
         return;
         break;
      case 'statelist':
         url = "http://water.usgs.gov/" + state + "/nwis/current?";
         if (map_type == 'real' || map_type == 'flood') {
            url += "type=flow"
         }
         else {
            url += "type=dailystagedischarge&group_key=NONE"
         }
         break;
      default:
         url = url_default;
   }
   if (OpenNewWin) {
      openWin(url)
   }
   else {
      window.document.location = url
   }
}
function clickHU2(zone) {
   var frmHU2 = document.forms['frmHU2'];
   var map_type = frmHU2.map_type.value;
   var web_type = frmHU2.web_type.value;

   var url = DailyMainW + "?state="+zone+"&map_type="+map_type+"&web_type="+web_type;
   window.document.location = url;

}
function whichRadioSet(radButton) {
   for (var i = 0; i < radButton.length; i++) {
      if (radButton[i].checked) 
         return i;
   }
   return -1
}
function getRadioValue(radButton) {
   var i = whichRadioSet(radButton)
   if (i >= 0) return radButton[i].value;
   return null
}

function gotoURL(objSelect) {
   var strURL=objSelect.options[objSelect.selectedIndex].value;
   objSelect.selectedIndex = 0;
   window.location = strURL;
}
function showState() {
   var frmMain = document.forms['frmMain'];
   var selState= frmMain.state;
   var st = selState.options[selState.selectedIndex].value;
   selState.selectedIndex = intSelIdxs[1];
   showWaterWatch(null, st);
}
function showHU(sel) {
   var frmMain = document.forms['frmMain'];
   var map_type = frmMain.map.options[frmMain.map.selectedIndex].value;
   var selHU = frmMain.hu;
   var st = selHU.options[selHU.selectedIndex].value;
   var web_type = 'sumplot';
   var tmps = map_type.split(",");
   if (tmps.length > 1) {
      map_type = tmps[0];
      web_type = tmps[1];
   }
   selHU.selectedIndex = intSelIdxs[4];
   showWaterWatch(map_type, st, web_type);
}
function showAnimationYM(sel) {
   var frmMain = document.forms['frmMain'];
   var map_type = frmMain.map.options[frmMain.map.selectedIndex].value;
   var selHU = frmMain.hu;
   var ym = selHU.options[selHU.selectedIndex].value;
   var web_type = 'animation';
   var tmps = map_type.split(",");
   if (tmps.length > 1) {
      map_type = tmps[0];
      web_type = tmps[1];
   }
   selHU.selectedIndex = intSelIdxs[4];
   showWaterWatch(map_type, 'us', web_type, ym);
}
function showGroup() {
   var frmMain = document.forms['frmMain'];
   var selMapType = frmMain.map_type;
   var tmp = selMapType.options[selMapType.selectedIndex].value;
   if (tmp.search(/http\:/) >= 0) {
      window.document.location = tmp
      return
   }
   var map_types = tmp.split(",")
   var map_type = map_types[0];
   var selState = frmMain.state;
   var st = (selState == null)? 'us' : frmMain.state.options[frmMain.state.selectedIndex].value;
   tmp = frmMain.map.options[frmMain.map.selectedIndex].value;
   var tmps = tmp.split(",");

   web_type = (map_types.length>1)? map_types[1] : ((tmps.length > 1)?tmps[1]:'map');
   if (tmps[0].search(/a$/) >=0 && map_type.search(/dvd|weekd|dryd/) >=0) {
      map_type = map_type.replace(/d$/, 'a');
   }
   if (map_type == 'dryw') {web_type = 'map'}
   if (tmps.length > 3 && map_type != 'dryw') {
      point_x = tmps[1];
      point_y = tmps[2];
      fscale = tmps[3];
      var url = DailySelectW + "?state="+st+"&map_type="+map_type
                +"&select_type=point&scale="+fscale
                +"&point.x="+point_x+"&point.y="+point_y;
      window.document.location = url;  
      return true;    
   }
   if (web_type.search(/sum(plot|table)/) >= 0 && frmMain.hu != null) {
      st = frmMain.hu.options[frmMain.hu.selectedIndex].value;
   }
   selMapType.selectedIndex = intSelIdxs[0];
   showWaterWatch(map_type, st, web_type);
}
function showMap() {
   var frmMain = document.forms['frmMain'];
   var selMap = frmMain.map;
   var map_type = selMap.options[selMap.selectedIndex].value;
   if (map_type.search(/^http:/) >= 0) {
      window.location= map_type
      return true;
   }
   var tmps = map_type.split(",");

   if (tmps.length > 3) {
      var map_type = tmps[0];
      var point_x = tmps[1];
      var point_y = tmps[2];
      var fscale = tmps[3];
      var st = frmMain.state.options[frmMain.state.selectedIndex].value;
      var url = DailySelectW + "?state="+st+"&map_type="+map_type
                +"&select_type=point&scale="+fscale
                +"&point.x="+point_x+"&point.y="+point_y;
      window.document.location = url;  
      return true;    
   }

   selMap.selectedIndex = intSelIdxs[2];
   showWaterWatch(map_type);
}
function showWaterWatch(map_type, st, web_type, ym) {
   var frmMain = document.forms['frmMain'];
   if (map_type == null) {
      map_type = frmMain.map.options[frmMain.map.selectedIndex].value;
   }
   if (map_type.search(/^http:/) >= 0) {
      window.location= map_type
      return true;
   }
   var tmps = map_type.split(",");
   if (tmps.length > 1) {
      map_type = tmps[0];
      web_type = tmps[1];
      if (tmps.length > 2) {
         if (map_type == "nwc") {ym = tmps[2]}
      }
   }
   if (web_type == null) {
      web_type = 'map';
   }
   if (map_type == 'nwc') {st = 'us'}
   if (typeof st == 'undefined') {
      st = frmMain.state.options[frmMain.state.selectedIndex].value;
      if (web_type.search(/sum(plot|table)/) >= 0 && frmMain.hu != null) {
         st = frmMain.hu.options[frmMain.hu.selectedIndex].value;
      }
   }
   var url = DailyMainW + "?state="+st+"&map_type="+map_type+"&web_type="+web_type;
   if (ym) { url += "&ym="+ym};
   window.document.location = url;
}
function getCurrentSelectedIndex() {
   var frmMain = document.forms['frmMain'];
   for (var i = 0; i < strSelIDs.length; i++) {
      var strSelID = strSelIDs[i];
      var sel = eval("frmMain."+strSelID);
      intSelIdxs[i] = (sel != null)? sel.selectedIndex : -1;
   }
}
