
// JS functions

//
// open new window
//   winURL:    after open the new window, set it's location to the given URL
//   winWidth:  open new window with the given width, if given
//   winHeight: open new window with the given height, if given
//
// open new window with:
//   toolbar=1
//   location=1
//   menubar=1
//   status=1
//   directories=1
//   scrollbars=1
//   resizable=1
//
var w;
function openNewWin(winURL, winWidth, winHeight) {
  winConfig = "toolbar=1,location=1,menubar=1,status=1,directories=1,scrollbars=1,resizable=1,";
  if(winWidth != '')  winConfig += "width=" + winWidth + ",";
  if(winHeight != '') winConfig += "height=" + winHeight + ",";

  w = window.open(winURL, "", winConfig);
  w.focus();
}


//
// open new window
//   winURL:    after open the new window, set it's location to the given URL
//   winName:   new window name
//   winWidth:  open new window with the given width
//   winHeight: open new window with the given height
//
// open new window with:
//   location=0
//   toolbar=0
//   menubar=0
//   status=0
//   scrollbars=yes
//
function openWin0(winURL, winName, winWidth, winHeight) {
  winConfig = "toolbar=0,location=0,menubar=0,status=0,scrollbars=yes,";
  winConfig += "width=" + winWidth + ",";
  winConfig += "height=" + winHeight + ",";

  w = window.open(winURL, winName, winConfig);
  w.focus();
}


//
// open new window
//   winURL:    after open the new window, set it's location to the given URL
//   winName:   new window name
//   winWidth:  open new window with the given width
//   winHeight: open new window with the given height
//
// open new window with:
//   location=1
//   toolbar=1
//   menubar=1
//   status=1
//   scrollbars=yes
//
function openWin1(winURL, winName, winWidth, winHeight) {
  winConfig = "toolbar=1,location=1,menubar=1,status=1,scrollbars=yes,";
  winConfig += "width=" + winWidth + ",";
  winConfig += "height=" + winHeight + ",";

  w = window.open(winURL, winName, winConfig);
  w.focus();
}



//
// function for Tab selection to change image
//
function changeImage(imgid, imgobject) {
  document.images[imgid].src = imgobject.src;
  return;
}



// display or not-display items
function displayItem(itemId) {
  if(document.getElementById(itemId).style.display == "none") {
    document.getElementById(itemId).style.display = "block";
  }
  else {
    document.getElementById(itemId).style.display = "none";
  }
}



//
// change the color of given index item to clr1 (default to red)
// others to color clr2 (default to blue)
// 
//
function changeColor(itemNm, idx, clr1, clr2) {
  if(!clr1) clr1 = 'red';    // if undefined, default to 'red'
  if(!clr2) clr2 = 'blue';   // if undefined, default to 'blue'

  itms = document.getElementsByName(itemNm);
  for(var i = 0; i < itms.length; i++) {
    if(i == idx) {
      itms[i].style.color = clr1;
    }
    else {
      itms[i].style.color = clr2;
    }
  }
}



//
// update item value of given item name
//
function uptItemVal(itmNam, itmVal) {
  itm1 = document.getElementsByName(itmNam);
  itm1[0].value = itmVal;

//alert("itmNam: " + itmNam);
//alert("itmVal: " + itmVal);

//alert("itm1 length: " + itm1.length);
//for(i = 0; i < itm1.length; i++) {
//  alert("item " + i + ": " + itm1[i].name);
//}
}



//
// form submit
function submitform() {
  document.frm1.submit();
}

//
// used in header.js
// for instrument Tab selection changes
function submit_form0(insidx) {
  document.frm1.insidx.value = insidx;
  document.frm1.mnuidx.value = 0;

  submitform();
}

// used in lmenu.php
// for left menu item selection changes
function submit_form1(insidx, mnuidx) {
  document.frm1.insidx.value = insidx;
  document.frm1.mnuidx.value = mnuidx;

  submitform();
}

// used for ToolBar "HOME" item links
// or non-menu use
// here:
//   insidx1 = -(insidx + 1)
//   ftye: html or image
function submit_form2(fnmurl, insidx, ftype) {
  var insidx1 = -(insidx + 1);
  var fname = ftype.toLowerCase() + ":" + fnmurl;

  document.frm1.insidx.value = insidx1;
  document.frm1.mnuidx.value = 0;
  document.frm1.fnmurl.value = fname;

  submitform();
}

// update given item to the given value
// and submit form
// called when:
//   year/month/week/wholeperiod radio box selection changes
//   ...
function submit_form3(itmNam, itmVal) {
  uptItemVal(itmNam, itmVal);
  submitform();
}

// called when (non-menu use case):
//   year/month/week/wholeperiod radio box selection changes
// Note:
//   this one need keep the original url
function submit_form4(itmNam, itmVal) {
  uptItemVal(itmNam, itmVal);

  // get current insidx & fnmurl
  itm1 = document.getElementsByName('insidx');
  var insidx = parseInt(itm1[0].value);
  itm1 = document.getElementsByName('fnmurl');
  var fnmurl = itm1[0].value;

//alert("insidx: " + insidx);
//alert("fnmurl: " + fnmurl);

  // change the insidx & fnmurl to needed value
  // and submit form
  submit_form2(fnmurl, insidx, 'html');
}

// set given item to given value
// and set uptimg to zero (0)
function submit_form5(itmNam, itmVal) {
  uptItemVal(itmNam, itmVal);
  document.frm1.uptimg.value = 0;
  submitform();
}

// set given item to given value
// and set uptimg to one (1)
function submit_form6(itmNam, itmVal) {
  uptItemVal(itmNam, itmVal);
  document.frm1.uptimg.value = 1;
  submitform();
}



//
// get last day of the month
// here: yyyy -- 4 digit year
//       mm   -- month, 1 - 12
//
//   lday:   31, 28/29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
//   month:  1,  2,     3,  4,  5,  6,  7,  8,  9,  10, 11, 12
//
function getlday(yyyy, mm) {
  var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  var lday = days[mm-1];
  if(mm == 2){
    if(((yyyy % 4 == 0) && (yyyy % 100 != 0)) || (yyyy % 400 == 0)) {
      lday += 1;
    }
  }

  return lday;
}


//
// update day selection length according to year & month
//
function upt_day() {
  // get year & month
  var yr = document.frm1.iyr.options[document.frm1.iyr.selectedIndex].value;
  var mn = document.frm1.imn.options[document.frm1.imn.selectedIndex].value;

  // get last day of the month
  lday = getlday(yr, mn);

  var Dlen = document.frm1.idy.length;

  // if the original day selection is more than lday
  // delete the extra option items
  // because we just delete the end items, so the
  // following two ways all work fine.
  // use the first way
  //
  // way 1: jast set the length to lday (last day)
  if(Dlen > lday) {
    document.frm1.idy.length = lday;
  }
  //
  /*
  // way 2: delete extra items one by one
  while(Dlen > lday) {
    Dlen--;
    document.frm1.idy.options[Dlen] = null;
  }
  */

  // if the original day selection is less than lday
  // add needed days
  var i1;
  while(Dlen < lday) {
    i1 = Dlen + 1;
    document.frm1.idy.options[Dlen] = new Option(i1, i1);
    Dlen++;
  }

/*
  Dlen = document.frm1.idy.length;
  for(i1 = 0; i1 < Dlen; i1++) {
    alert("Option Text: " + document.frm1.idy.options[i1].text + "\t" +
          "Option Value: " + document.frm1.idy.options[i1].value);
  }
*/
}



//
// check channel number
// to make sure the value is in the range
// called from L1B Browser
//
function ck_chnbr() {
  var ptxt = document.frm1.ptype.options[document.frm1.ptype.selectedIndex].text;
  var chnb = parseInt(document.frm1.chnbr.value);

  var fmt1 = /^.*\(ch(\d+)-(\d+)\)$/;
  if(fmt1.test(ptxt)) {
    var dt1 = fmt1.exec(ptxt);
    var ch1 = parseInt(dt1[1], 10);
    var ch2 = parseInt(dt1[2], 10);
    if((chnb < ch1) || (chnb > ch2)) {
      document.frm1.chnbr.value = ch1;
    }
  }
}


//
// update channel selection length according to ptype
// all start channel is 1, no need check start channel
// called from Level 1 Time Series
//
function upt_ch() {
  var ptxt = document.frm1.ptype.options[document.frm1.ptype.selectedIndex].text;

  var fmt1 = /^.*\(ch(\d+)-(\d+)\)$/;
  if(fmt1.test(ptxt)) {
    var dt1 = fmt1.exec(ptxt);
    var ch1 = parseInt(dt1[2], 10);

    var ch0 = document.frm1.chnbr.length;
    var chn = parseInt(document.frm1.chnbr.options[document.frm1.chnbr.selectedIndex].value);

    // if ch0 > ch1, delete extra channels
    if(ch0 > ch1) {
      document.frm1.chnbr.length = ch1;
    }

    // if ch0 < ch1, add needed channels
    var ch2;
    while(ch0 < ch1) {
      ch2 = ch0 + 1
      document.frm1.chnbr.options[ch0] = new Option(ch2, ch2);
      ch0++;
    }

    // set channel 1 as selected channel if(chn > ch1)
    if(chn > ch1) {
      document.frm1.chnbr.selectedIndex = 0;
    }
  }
}


//
// function to print out channel selection options
//   chnb1: start channel number
//   chnb2: end channel number
//   selch: selected channel, default to 1
//
function SelectCh(chnb1, chnb2, selch){
  // set default selected channel, if need
  if(selch == '') selch = 1;

  var ctr;
  for(ctr = chnb1; ctr <= chnb2; ctr++) {
    if(ctr == selch) {
      document.write("<option value=" + ctr +" selected>" + ctr);
    }
    else {
      document.write("<option value=" + ctr +">" + ctr);
    }
  }
}



//
// print out options
//   aval: option value array
//   atxt: option text array
//   asel: selected option value
//
function prt_opts(aval, atxt, asel) {
  var alen = aval.length;

  var i;
  for(i = 0; i < alen; i++) {
    if(aval[i] == asel) {
      document.write("<option value='" + aval[i] + "' selected>" + atxt[i]);
    }
    else {
      document.write("<option value='" + aval[i] + "'>" + atxt[i]);
    }
  }
}



//
// update chsel selection display & check channel number, if need
// called from AIRS L1B Browse
//
function upt_chsel() {
  var ptxt = document.frm1.ptype.options[document.frm1.ptype.selectedIndex].text;

  var fmt1 = /^.*\(ch(\d+)-(\d+)\)$/;
  if(fmt1.test(ptxt)) {
    // display "Ch" selection
    document.getElementById("chsel").style.display = "block";

    // get channel range
    var dt1 = fmt1.exec(ptxt);
    var ch1 = parseInt(dt1[1], 10);
    var ch2 = parseInt(dt1[2], 10);

    // get selected channel number
    var chn = parseInt(document.frm1.chnbr.options[document.frm1.chnbr.selectedIndex].value);

    // if chn is not in the ch1 & ch2 range, set to ch1
    if((chn < ch1) || (chn > ch2)) {
      chn = ch1;
    }

    // set Ch selection to range [ch1 ch2]
    // first set the selection length to 0 ==> delete all options
    // then add new options according to ch1 & ch2 range
    document.frm1.chnbr.length = 0;
    var i, i1;
    for(i = ch1; i <= ch2; i++) {
      i1 = i - 1;
      if(i == chn) {
        document.frm1.chnbr.options[i1] = new Option(i, i, true);
      }
      else {
        document.frm1.chnbr.options[i1] = new Option(i, i);
      }
    }
  }
  else {
    // hidden "Ch" selection
    document.getElementById("chsel").style.display = "none";
  }
}



//
// update s2 selection Label & Options according to s1 selection value
// called from Daily AMSU Data Browser
// called from Archive AMSU Data Browser
//
// sp = 1, add space in "Level" label
//
function upt_s2(itemId, itemTxt, sp, defsel) {
  var s1val = document.frm1.s1.options[document.frm1.s1.selectedIndex].value;
  if((s1val == "tpw") || (s1val == "clw") || (s1val == "hr")) {
    document.getElementById(itemId).style.display = "none";

    // here set length = 1 & value = ""
    // to make sure updatesrc() function works fine
    // for: Total Precipitable Water, Cloud Liquid Water,
    //      & Observation Time selection
    // by setting so I don't need do Parameter check there
    document.frm1.s2.length = 1;
    document.frm1.s2.options[0].value = "";
  }
  else {
    document.getElementById(itemId).style.display = "block";

//  var txt=document.getElementById(itemTxt).innerText;
//  var txt=document.getElementById(itemTxt).innerHTML;
//  alert("txt: " + txt);

    // Note:
    //   innerText property only works for IE
    //   innerHTML property works for both IE & Mozilla
    //   so use innerHTML to set Label
    if(s1val == "rt") {
      if((sp == null) || (sp == '')) sp = 0;

      if(sp == 1) {
        document.getElementById(itemTxt).innerHTML = "Level:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      }
      else {
        document.getElementById(itemTxt).innerHTML = "Level:";
      }

      // Level array:
      var opts = ["0.1 MB",
                  "0.2 MB",
                  "0.5 MB",
                  "1 MB",
                  "1.5 MB",
                  "2 MB",
                  "3 MB",
                  "4 MB",
                  "5 MB",
                  "7 MB",
                  "10 MB",
                  "15 MB",
                  "20 MB",
                  "25 MB",
                  "30 MB",
                  "50 MB",
                  "60 MB",
                  "70 MB",
                  "85 MB",
                  "100 MB",
                  "115 MB",
                  "135 MB",
                  "150 MB",
                  "200 MB",
                  "250 MB",
                  "300 MB",
                  "350 MB",
                  "400 MB",
                  "430 MB",
                  "475 MB",
                  "500 MB",
                  "570 MB",
                  "620 MB",
                  "670 MB",
                  "700 MB",
                  "780 MB",
                  "850 MB",
                  "920 MB",
                  "950 MB",
                  "1000 MB"
                 ];
      if((defsel == null) || (defsel == '')) defsel = 20;

    }
    else {
      document.getElementById(itemTxt).innerHTML = "Channel:";

      // Channel array:
      var opts = ["1", "2", "3", "4", "5", "6", "7", "8",
                  "9", "10", "11", "12", "13", "14", "15"];
      if((defsel == null) || (defsel == '')) defsel = 1;
    }

    // delete all options of original selection
    document.frm1.s2.length = 0;

    // add new options according to opts array
    var alen = opts.length;
    var i, i1;
    for(i = 1; i <= alen; i++) {
      i1 = i - 1;
      if(i == defsel) {
        document.frm1.s2.options[i1] = new Option(opts[i1], i, true);
      }
      else {
        document.frm1.s2.options[i1] = new Option(opts[i1], i);
      }
    }
  }
}



//
// get year & month selection range according to satellte selection
//
// called from upt_mn & upt_ym
//
function get_ym() {
  var s0txt = document.frm1.s0.options[document.frm1.s0.selectedIndex].text;
  var yrval = document.frm1.iyr.options[document.frm1.iyr.selectedIndex].value;
  var mnval = document.frm1.imn.options[document.frm1.imn.selectedIndex].value;

  var retst = "";

  var fmt1 = /^.*\((\d+)-(\w+)\)$/;
  if(fmt1.test(s0txt)) {
    var dt1 = fmt1.exec(s0txt);
    var v1 = dt1[1];
    var v2 = dt1[2];

    // get start year & month
    var s_yr = parseInt(v1.substr(0, 2), 10);
    var s_mn = parseInt(v1.substr(2, 2), 10);
    if(s_yr > 70) {
      s_yr += 1900;
    }
    else {
      s_yr += 2000;
    }

    // get end year & month
    if(dt1[2] == "current") {
      var date = new Date();
      var e_yr = date.getFullYear();
      var e_mn = date.getMonth() + 1;
    }
    else {
      var e_yr = parseInt(v2.substr(0, 2), 10);
      var e_mn = parseInt(v2.substr(2, 2), 10);
      if(e_yr > 70) {
        e_yr += 1900;
      }
      else {
        e_yr += 2000;
      }
    }

    yrval = parseInt(yrval, 10);
    if((yrval < s_yr) || (yrval > e_yr)) {
      yrval = e_yr;
    }

//  alert("s_yr: " + s_yr + ", s_mn: " + s_mn + "\n"
//      + "e_yr: " + e_yr + ", e_mn: " + e_mn);

    retst = s_yr + " " + e_yr + " " + yrval + " "
          + s_mn + " " + e_mn + " " + mnval;
  }

  return retst;
}


//
// reset month selection (imn) to the given range
//   s_yr: start year
//   e_yr: end year
//   selyr: selected year
//
//   s_mn: start month, 1 -- 12
//   e_mn: end month, 1 -- 12
//   selmn: selected month, 1 -- 12
//
// called from upt_mn & upt_ym
//
function rst_mn(s_yr, s_mn, e_yr, e_mn, selyr, selmn) {
  selyr = parseInt(selyr, 10);
  selmn = parseInt(selmn, 10);

  var months = ["",
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"
               ];

  // determine start & end month for the given selected year
  // if selyr out of range, default to e_yr
  var mn1, mn2;
  if(s_yr == e_yr) {
    mn1 = s_mn;
    mn2 = e_mn;
  }
  else {
    if((selyr > s_yr) && (selyr < e_yr)) {
      mn1 = 1;
      mn2 = 12;
    }
    else {
      if(selyr == s_yr) {
        mn1 = s_mn;
        mn2 = 12;
      }
      else {
        mn1 = 1;
        mn2 = e_mn;
      }
    }
  }

  // delete current options
  document.frm1.imn.length = 0;

  // if selsected month out of range, set to end month
  if((selmn < mn1) || (selmn > mn2)) {
    selmn = mn2;
  }

  // add new options according to given range
  var i1 = 0;
  for(var i = mn1; i <= mn2; i++) {
    if(i == selmn) {
      document.frm1.imn.options[i1] = new Option(months[i], i, true);
    }
    else {
      document.frm1.imn.options[i1] = new Option(months[i], i);
    }
    i1++;
  }
}


//
// reset month selection range according to satellte selection and year selection
// called from AMSU archive data browser
//
function upt_mn() {
  // get Y/M range & selected value
  var retstr = get_ym();
  if(retstr != "") {
    var retval = retstr.split(" ");
    var s_yr  = retval[0];
    var e_yr  = retval[1];
    var yrval = retval[2];
    var s_mn  = retval[3];
    var e_mn  = retval[4];
    var mnval = retval[5];

    rst_mn(s_yr, s_mn, e_yr, e_mn, yrval, mnval);
  }
}



//
// reset year & month selection range according to satellte selection
// called from AMSU archive data browser
//
function upt_ym() {
  // get Y/M range & selected value
  var retstr = get_ym();
  if(retstr != "") {
    var retval = retstr.split(" ");
    var s_yr  = retval[0];
    var e_yr  = retval[1];
    var yrval = retval[2];
    var s_mn  = retval[3];
    var e_mn  = retval[4];
    var mnval = retval[5];

    // reset year selection
    //   delete all options of original year selection
    //   add new options according to new year range
    // keep the original year selection value, if the value is in the range
    // otherwise, set to the end year (e_yr)
    // Note: did yrval checking in get_ym function
    document.frm1.iyr.length = 0;
    var i1 = 0;
    for(var i = s_yr; i <= e_yr; i++) {
      if(i == yrval) {
        document.frm1.iyr.options[i1] = new Option(i, i, true);
      }
      else {
        document.frm1.iyr.options[i1] = new Option(i, i);
      }
      i1++;
    }

    rst_mn(s_yr, s_mn, e_yr, e_mn, yrval, mnval);
  }
}



