function setInnerHTML(what, txt)
{     
  //alert('setInnerHTML():: what = ' + what);
  document.getElementById(what).innerHTML = txt;
}

function setDisable(what, dis)
{     
  //alert('setImage():: what = ' + what);
  document.getElementById(what).disabled = dis;
}

function setVisible(what, vis)
{     
  //alert('setVisible():: what = ' + what);
  document.getElementById(what).style.visibility = vis;
}

function setImage(what, vis, iname)
{ 
  //alert('setImage():: what = ' + what);
  document.getElementById(what).style.visibility = vis;  //'visible';
  document.getElementById(what).src = iname;
}

function clearAll()
{
  setInnerHTML('description','');
  setInnerHTML('abutton','');
  setInnerHTML('clicknote','');
  setInnerHTML('content','');
  setInnerHTML('pwsmap','');
  setVisible('content','hidden');
  setImage('scbimage','hidden','images/spacer.gif');
}

function roundVal(val, dec)
{
  var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
  return result;
}

function pad(number,length)
{
  var str = '' + number;
  while (str.length < length)
    str = '0' +  str;
  return str;
}

String.prototype.pad = function(l, s, t)
{
  return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
                + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
                + this + s.substr(0, l - t) : this;
}

function wstm(station)
{
  var txt = '<table><tr><td class="normal8" align="center" nowrap>';
  if (station == "46081")
  {
    txt += 'Station: Western PWS';
    txt += '</td></tr>';
    txt += '<tr><td class="normal8" align="center" nowrap>';
    txt += 'Location: (60.80&ordm;N, 148.28&ordm;W)';
  }
  else if (station == "BLIA2")
  {
    txt += 'Station: Bligh Reeh Light';
    txt += '</td></tr>';
    txt += '<tr><td class="normal8" align="center" nowrap>';
    txt += 'Location: (60.84&ordm;N, 146.88&ordm;W)';
  }
  else if (station == "MRKA2")
  {
    txt += 'Station: Middle Rock Light';
    txt += '</td></tr>';
    txt += '<tr><td class="normal8" align="center" nowrap>';
    txt += 'Location: (61.08&ordm;N, 146.66&ordm;W)';
  }
  else if (station == "46060")
  {
    txt += 'Station: West Orca Bay';
    txt += '</td></tr>';
    txt += '<tr><td class="normal8" align="center" nowrap>';
    txt += 'Location: (60.59&ordm;N, 146.83&ordm;W)';
  }
  else if (station == "46061")
  {
    txt += 'Station: Seal Rock';
    txt += '</td></tr>';
    txt += '<tr><td class="normal8" align="center" nowrap>';
    txt += 'Location: (60.22&ordm;N, 146.83&ordm;W)';
  }
  else if (station == "POTA2")
  {
    txt += 'Station: Potato Point';
    txt += '</td></tr>';
    txt += '<tr><td class="normal8" align="center" nowrap>';
    txt += 'Location: (61.06&ordm;N, 146.70&ordm;W)';
  }
  txt += '</td></tr></table>';
  Text[0][1] = txt;
  stm(Text[0],Style[0]);
}

function tstm()
{
  Text[0][1] = 'Click to view plot comparing tide gauge observations with the ROMS forecast';
  stm(Text[0],Style[0]);
}

function tstm2()
{
  document.body.style.cursor = 'pointer';
  Text[0][1] = 'Click to view ROMS forecast plot';
  stm(Text[0],Style[0]);
}

function qstm(region)
{
  Text[0][1] = 'Click to view ' + region + ' sub-region';
  stm(Text[0],Style[0]);
}

function setWait(b)
{
  //alert('setWait()');
  var vis = "hidden";
  if (b)
  {
    vis = "visible";
  }
  if (document.layers) //NS
  {
    document.layers["qwait"].visibility = vis;
    //document.layers["qwait"].focus();
  }
  else if (document.all) //IE
  {
    document.all.qwait.style.visibility = vis;
    //document.all.qwait.focus();
  }
  else //other browsers
  {
    //alert('Other');
    document.getElementById("qwait").style.visibility = vis;
    //document.getElementById("qwait").focus();
  }
  waitflag = b;
}

function ncdump(fname)
{
  url = '/PWS/edit.jsp?filename=' + fname;
  popWindow=open(url,'popWindow','resizable=yes,width=600,height=500,scrollbars=yes');
  if (popWindow.opener == null)
    popWindow.opener = self;
  else
    popWindow.focus();
}

function httpDownload(fname)
{
  //alert(fname);
  ipopWindow=open(fname,'ipopWindow','resizable=yes,scrollbars=yes,width=300,height=300');
  if (ipopWindow.opener == null)
    ipopWindow.opener = self;
  else
    ipopWindow.focus();
}

function doInfo()
{
  var subproject = document.pwsform.selectedSubProject.value;
  //alert('subproject = ' + subproject);
  //url = subproject + '.pdf';
  url = subproject + '.html';
  subplot_popup(url, 500, 370);
}

function showNowcastSubImage()
{
  var subproject = document.pwsform.selectedSubProject.value;
  var filename = document.pwsform.filename.value;
  //alert(filename);
  var key = "temp";
  if (subproject == "NowcastSalinity")
    key = "salinity";
  else if (subproject == "NowcastCurrent")
    key = "curr";
  else if (subproject == "NowcastSSH")
    key = "ssh";
  var iname = filename.replace(key,key+"HF"); 
  //alert(iname);
  spopWindow=open(iname,'spopWindow','resizable=yes,scrollbars=yes,width=630,height=620');
  if (spopWindow.opener == null)
    spopWindow.opener = self;
  else
    spopWindow.focus();
}

function showSubImageTide(area)
{
  var year = document.pwsform.selectedYear.value;
  var month = document.pwsform.selectedMonth.value;
  var day = document.pwsform.selectedDay.value;
  var imagedir = "/myocean/PWS/data/images/tide/" + year + month.pad(2,"0", 2) + day.pad(2,"0", 2);
  var image = imagedir + '/' + 'ssh_' + area + '_' + year + month.pad(2,"0", 2) + day.pad(2,"0", 2) + '.jpg';
  showSubImage(image, 'visible');
}

function showSubImage(iname, vis)
{
  //alert('showSubImage() - iname=[' + iname + ']');
  subsetPopup=open(iname,'subsetPopup','resizable=yes,scrollbars=yes,width=630,height=480');
  if (subsetPopup.opener == null)
    subsetPopup.opener = self;
  else
    subsetPopup.focus();
}

function tshtm()
{
  document.body.style.cursor = 'default';
  htm();
}

function detectBrowser()
{
  if (document.layers) //NS
  {
    //alert('Netscape');
    xoffset = 77;
    yoffset = 66;
  }
  else if (document.all) //IE
  {
    //alert('IE');
    xoffset = 77;
    yoffset = 66;
  }
  else if (document.getElementById)
  {
    //alert('Firefox');
    xoffset = 77;
    yoffset = 66;
  }
  else
  {
    alert('What Browser ????');
  }

/*
  var browser=navigator.appName;
  var b_version=navigator.appVersion;
  var version=parseFloat(b_version);
  alert('Your browser is: ' + browser + ' ' + version);
  if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))
  {
    alert("Your browser is good enough!");
  }
  else
  {
    alert("It's time to upgrade your browser!");
  }
*/
}

/*
function doDisplayPlot(what)
{
  if (what.checked)
  {
    var pname = document.mangenForm.outname.value;
    var ind = pname.lastIndexOf("/");
    pname = pname.substr(ind+1);
    //http://ourocean.jpl.nasa.gov/myocean/mangen/PWS09/output/mangen.1246480951724.png
    pname = "http://ourocean.jpl.nasa.gov/myocean/mangen/PWS09/output/" + pname.replace("kml", "png");
    //alert('doDisplayPlot():: pname = ' + pname);
    document.getElementById("rplot").style.visibility = "visible";
    document.getElementById("rplot_img").src = pname;
    document.getElementById("pnote").style.visibility = "visible";
  }
  else
  {
    document.getElementById("rplot").style.visibility = "hidden";
    document.getElementById("rplot_img").src = "http://ourocean.jpl.nasa.gov/PWS/images/spacer.gif";
  }
}

function doDisplayText(what)
{
  if (what.checked)
  {
    var pname = document.mangenForm.outname.value;
    var ind = pname.lastIndexOf("/");
    pname = pname.substr(ind+1);
    //http://ourocean.jpl.nasa.gov/myocean/mangen/PWS09/output/mangen.1246480951724.png
    pname = "http://ourocean.jpl.nasa.gov/myocean/mangen/PWS09/output/" + pname.replace("kml", "txt");
    //alert('doDisplayText():: pname = ' + pname);
    var txt = clientSideInclude(pname);
    //alert('doDisplayText():: txt = ' + txt);
    document.getElementById("rtext").style.visibility = "visible";
    document.getElementById("rtext_box").value = txt;
  }
  else
  {
    document.getElementById("rtext").style.visibility = "hidden";
    document.getElementById("rtext_box").value = "";
  }
}
*/

function clientSideInclude(url) 
{
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) 
  {
    try 
	{
      req = new XMLHttpRequest();
    } 
	catch (e) 
	{
      req = false;
    }
  } 
  else if (window.ActiveXObject) 
  {
    // For Internet Explorer on Windows
    try 
    {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } 
	catch (e) 
	{
      try 
	  {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } 
	  catch (e) 
	  {
        req = false;
      }
	}
  }
  if (req) 
  {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    return(req.responseText);
  } 
  else 
  {
    return('Undefined ' + url);
  }
}

function subplot_popup(url, w, h)
{
  //alert('subplot_popup()');
  infopopWindow=open(url,'infopopWindow','resizable=yes,width='+w+',height='+h);
  if (infopopWindow.opener == null)
    infopopWindow.opener = self;
  //else
  //  infopopWindow.opener.focus();
}
