function selectDateHandler()
{
  //alert('selectDateHandler');
  // Make sure the request is loaded (readyState = 4)
  if (req.readyState == 4)
  {
    //alert('selectDateHandler(): 4');
    // Make sure the status is "OK"
    if (req.status == 200)
    {
      //alert('selectDateHandler(): 200 - req.responseXML.xml = ' + req.responseXML.xml);
      var notes = req.responseXML.getElementsByTagName('note');
      //alert('selectDateHandler(): 200 - notes.length = ' + notes.length);
      for (i=0; i<notes.length; i++)
      {
        //alert('selectDateHandler(): i = ' + i);
        var noteNode = notes.item(i);
        if (noteNode != null && noteNode.hasChildNodes())
        {
          name = noteNode.getAttribute('name');
          //alert('name = ' + name);
          if (name == "description")
          {
            doDescription(noteNode);
          }
          else if (name == "content")
          {
            doContent(noteNode);
          }
          else if (name == "calendar")
          {
            doCalendar(noteNode);
          }
        }
      }
    } //200
    else
    {
      //alert("There was a problem retrieving the XML data:\n" + req.statusText);
      alert("There was a problem retrieving the XML data:\n");
    }
  }
}

function doCalendar(noteNode)
{
  var trs = noteNode.getElementsByTagName('tr');
  //alert('trs.length = ' + trs.length);
  tablehtml = "<table border=1>";
  tablehtml += "<tr>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">Su</th>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">M</th>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">T</th>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">W</th>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">Th</th>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">F</th>";
  tablehtml += "<th align=center WIDTH=\"28\" class=\"heading\">S</th>";
  tablehtml += "</tr>";
  for(j=0; j<trs.length; j++)
  {
    tablehtml += "<tr>";
    var tr = trs.item(j);
    var tds = tr.getElementsByTagName('td');
    //alert('tds.length = ' + tds.length);
    for(k=0; k<tds.length; k++)
    {   
      var td = tds.item(k);
      value = td.firstChild.nodeValue;
      if (value == "nbsp")
        value = "&nbsp;";
      //alert('j=' + j + '; value = [' + value + ']');
      tablehtml += "<td align=center WIDTH=\"28\" class=\"bold12\"";
      bgcolor = td.getAttribute('bgcolor');
      if (bgcolor != null)
        tablehtml += " bgcolor=\"" + bgcolor +"\"";
      link = td.getAttribute('link');
      if (link != null)
        tablehtml += "><a id=\"" + value + "\" href=\"#\" onClick=\"return selectDay('" + value + "', false, true)\">" + value + "</a></td>";
      else
        tablehtml += ">" + value + "</td>";
    } //k
    tablehtml += "</tr>";
  } //j
  tablehtml += "<tr>";
  tablehtml += "<td align=\"center\" colspan=7>";
  tablehtml += "<INPUT TYPE=BUTTON NAME=\"previousYear\"  VALUE=\" <<  \" onClick=\"setPreviousYear()\">&nbsp";
  tablehtml += "<INPUT TYPE=BUTTON NAME=\"previousMonth\" VALUE=\"  <  \" onClick=\"setPreviousMonth()\">&nbsp";
  tablehtml += "<INPUT TYPE=BUTTON NAME=\"nextMonth\"     VALUE=\"  >  \" onClick=\"setNextMonth()\">&nbsp";
  tablehtml += "<INPUT TYPE=BUTTON NAME=\"nextYear\"      VALUE=\"  >> \" onClick=\"setNextYear()\">";
  tablehtml += "</td>";
  tablehtml += "</tr>";
  tablehtml += "</table>";
  //alert(tablehtml);
  setInnerHTML('jcalendar', tablehtml);
  doInit();
}   

function doDescription(noteNode)
{
  var trs = noteNode.getElementsByTagName('tr');
  //alert('trs.length = ' + trs.length);
  tablehtml = "<table>";
  for(j=0; j<trs.length; j++)
  {
    tablehtml += "<tr>";
    var tr = trs.item(j);
    var tds = tr.getElementsByTagName('td');
    //alert('tds.length = ' + tds.length);
    for(k=0; k<tds.length; k++)
    {
      var td = tds.item(k);
      value = td.firstChild.nodeValue;
      //alert('j=' + j + '; value = [' + value + ']');
      if (j==0) //short desc
      {
        if (value != "N/A")
          tablehtml += "<td class=\"bluebold14\">" + value + "</td>";
        else
          tablehtml += "<td class=\"bluebold14\">&nbsp;</td>";
      }
      else if (j==1) //desc
      {
        if (value.length > 120)
        {
          desc = value.substring(0, 140);
          blkindx = desc.lastIndexOf(" ");
          desc = desc.substring(0, blkindx) + ' ... ';
          tablehtml += "<td class=\"comment\">" + desc;
          tablehtml += "<input class=normal8 type=button value=\"more\" onClick=\"doInfo()\"></td>";
        }
        else
        { 
          tablehtml += "<td class=\"comment\"><p style='text-align:justify'>" + value + "</p></td>";
        } 
      }
      else //LCS link
      {
        tablehtml += "<td class=\"normal10\" align=right>" + value + "</td>";
      } 
    } //k
    tablehtml += "</tr>";
  } //j
  tablehtml += "</table>";
  //alert(tablehtml);
  var project = document.scbform.selectedProject.value; 
  if (project != "SCBForecast")
    setInnerHTML('description', tablehtml);
}     

function doContent(noteNode)
{ 
  var subproject = document.scbform.selectedSubProject.value;
  var project = document.scbform.selectedProject.value; 
  //alert('doContent():: project = ' + project + '; subproject = ' + subproject);
  var year = document.scbform.selectedYear.value;
  var month = document.scbform.selectedMonth.value;
  var day = document.scbform.selectedDay.value;
  var firstname = ''; //red check mark
  var datadir = "/myocean/SCB/data/las";
  var imagedir = "/myocean/SCB/data/images/nowcast/" + year;
  //alert('imagedir = ' + imagedir);
  if (project == "G1SST")
  {
    doG1SSTCheck();
    return;
  }

  if (project == "SCBForecast")
  {
    //setInnerHTML('description',fcstForecast);
    var type = document.scbform.selectedImageType.value;
    imageTypeChange(type);
/*
    var iname = noteNode.firstChild.nodeValue;
    alert('iname = ' + iname);
    if (iname != "NA")
    {
      setInnerHTML('description',fcstForecast);
      document.scbform.filename.value = iname;
      var myImage = new Image();
      myImage.onerror = loadImage;    //bad
      myImage.onload  = assignImage;  //good
      myImage.src = fname;
    }
    else
    {
      var datestr = month.pad(2, "0", 0) + "/" + day.pad(2,"0", 2) + "/" + year;
      var tablehtml = "<table>";
      tablehtml += "<tr>";
      tablehtml += "<td class=\"bluebold14\" align='center'>Sorry, no data found for the selected date.</td>";
      tablehtml += "</tr>";
      tablehtml += "<tr>";
      tablehtml += "<td class=\"bluebold14\" align='center'>" + datestr + "</td>";
      tablehtml += "</tr>";
      //alert('tablehtml = [' + tablehtml + ']');
      tablehtml += "</table>";
      setInnerHTML('content', tablehtml);
    }
*/
    return;
  }

  if (project == "SCBHFRadar")
  {
    datadir = "/myocean/SCB/data/analysis_ms";
    imagedir = "/myocean/SCB/data/images/hfradar/" + year + "/" + month.pad(2, "0", 0);
  }
  else if (project == "SCBMM5")
  {
    datadir = "/myocean/data/MM5/" + year + month.pad(2, "0", 0);
    imagedir = "/repository/HB06/" + year + "/MM5/" + month.pad(2, "0", 0) + day.pad(2,"0", 2);
  } 
  else if (project == "SCBROMSData")
  {
    imagedir = "/myocean/SCB/data/images/tide/" + year + "/" + month.pad(2, "0", 0);
  } 
  else if (project == "SCBGlider")
  {
    datadir = "/myocean/SCB/data/downloads/" + year + month.pad(2, "0", 0) + day.pad(2,"0", 2) + "/glider_sio";
    imagedir = "/myocean/SCB/data/downloads/" + year + month.pad(2, "0", 0) + day.pad(2,"0", 2) + "/glider_sio";
  }
  else if (project == "SioProfile")
  {
    datadir  = "/myocean/SCB/data/images/romsvsglider/" + year + month.pad(2, "0", 0) + day.pad(2,"0", 2);
    imagedir = "/myocean/SCB/data/images/romsvsglider/" + year + month.pad(2, "0", 0) + day.pad(2,"0", 2);
  }
  else if (project == "USCProfile")
  {
    datadir  = "/myocean/SCB/data/images/romsvsuscglider/" + year + month.pad(2, "0", 0) + day.pad(2,"0", 2);
    imagedir = "/myocean/SCB/data/images/romsvsuscglider/" + year + month.pad(2, "0", 0) + day.pad(2,"0", 2);
    //alert('imagedir = ' + imagedir);
  }

  var trs = noteNode.getElementsByTagName('tr');
  //alert('trs.length = ' + trs.length);
  var tablehtml = "<table>";
  var tr = trs.item(0);
  var tds = tr.getElementsByTagName('td');
  //alert('tds.length = ' + tds.length);
  if (tds.length > 1)
  {
    tablehtml += "<tr>";
    if (project != "SCBMM5") //no check mark for MM5
      tablehtml += "<td width=10 nowrap>&nbsp;</td>";

    if (project == "SCBHFRadar")
    {
      tablehtml += "<td class=\"dataHead\" width=100 align=center>&nbsp;Time Step</td>";
      tablehtml += "<td class=\"dataHead\" align=center colspan=3>View</td>";

    }
    else if (project == "SioProfile" || project == "USCProfile")
    {
      tablehtml += "<td class=\"dataHead\" width=100 align=center>&nbsp;File Name</td>";
      tablehtml += "<td class=\"dataHead\" align=center colspan=3>View</td>";
    }
    else
    {
      tablehtml += "<td class=\"dataHead\" width=190 nowrap>&nbsp;File Name</td>";
      tablehtml += "<td class=\"dataHead\" nowrap>&nbsp;File Size&nbsp;</td>";
      tablehtml += "<td class=\"dataHead\" align=center nowrap>Download</td>";
      tablehtml += "<td class=\"dataHead\" align=center nowrap>View</td>";
    }
    tablehtml += "</tr>"; 
  
    for(j=0; j<trs.length; j++)
    {   
      if (j%2 == 0)
        tablehtml += "<tr class='dataEven'>";
      else 
        tablehtml += "<tr class='dataOdd'>";
      tr = trs.item(j); 
      var tds = tr.getElementsByTagName('td');
      //alert('tds.length = ' + tds.length);
    
      if (project != "SCBMM5")
        tablehtml += "<td bgcolor=\"FFFFFF\"><img id=\"icheck" + j + "\" name=\"icheck" + j + "\" src=\"images/checkmark2.gif\" border=\"0\" width=16 height=16 style=\"visibility:hidden\"></td>";
    
      for(k=0; k<tds.length; k++)
      {
        var td = tds.item(k);
        value = td.firstChild.nodeValue;
        //alert('value = [' + value + ']');
        if (k!=1)
        {
          if (project == "SCBGlider")
            value += ".SCI";
          else if (project == "SioProfile" || project == "USCProfile")
            value += ".jpg";
          else
            value += ".nc";
        }
        if (k==0) //File Name
        {
          if (project == "SCBHFRadar")
          {
            val = value;
            idx = value.indexOf("_");
            if (idx != -1)
              val = value.substring(idx-2, idx);
            tablehtml += "<td class=\"dataSmall\" align=center>&nbsp;";
            tablehtml += val + "</td>";
          }
          else
          {
            tablehtml += "<td class=\"dataSmall\">&nbsp;";
            tablehtml += value + "</td>";
          }
        }
        if (k==1) //size
        {
          if (project != "SCBHFRadar" && project != "SioProfile" && project != "USCProfile")
          {
            tablehtml += "<td class=\"dataSmall\">&nbsp;";
            tablehtml += value + "</td>";
          }
        }
        else if (k==2) //download
        {   
          type = td.getAttribute('type');
          var fname = datadir + "/" + value;
          if (project != "SioProfile" && project != "USCProfile")
          { 
            tablehtml += "<td class=\"dataSmall\" align='center'>";
            //alert('type = ' + type + '; fname = ' + fname);
            tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"hdl" + j + "\" value=\"http\" onClick=\"httpDownload('" + fname + "');\">";
            tablehtml += "</td>";
          } 
        }   
        else if (k==3) //Header, LAS, Image
        {     
          tablehtml += "<td class=\"dataSmall\" align='center'>&nbsp;";
          image = td.getAttribute('image');
          las = td.getAttribute('las');
          var fname = datadir + "/" + value;
          //alert('type = ' + type + '; fname = ' + fname);
          if (type == "ASCII")
          { 
            tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"eql" + j + "\" value=\"File\" onClick=\"edit('" + fname + "');\">";
          }
          else if (type == "NetCDF")
          {
            tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"eql" + j + "\" value=\"Header\" onClick=\"ncdump('" + fname + "');\">";
          }

          if (las != null)
          {
            if (project == "SCBNowcast")
              tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"eql" + j + "\" value=\"LAS\" onClick=\"lasEdit('" + las + "');\">";
            else
              tablehtml += "&nbsp;&nbsp;";
          }
          else
          {
            if (project == "SCBNowcast")
              tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"eql" + j + "\" value=\"LAS\" disabled>";
            else
              tablehtml += "&nbsp;&nbsp;";
          }

          if (image != null)
          {
            if (project == "SCBNowcast")
            {
              if (subproject == "NowcastCurrent")
                imagename = imagedir + "/scb_curr" + image + '.jpg';
              else if (subproject == "NowcastTemp")
                imagename = imagedir + "/scb_temp" + image + '.jpg';
              else if (subproject == "NowcastSalinity")
                imagename = imagedir + "/scb_salinity" + image + '.jpg';
              else
                imagename = imagedir + "/scb_ssh" + image + '.jpg';               if (firstname == '')
                firstname = imagename;
              tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"image" + j + "\" value=\"Image\" onClick=\"showImage('" + imagename + "', 'visible', " + j + ");\">";
            }
            else if (project == "SCBHFRadar")
            {               var iyy = year-0;  //for new convert
              if (iyy < 2009)
              {
                imagename = imagedir + '/' + image + '_hfrd.jpg';
              }
              else
              {
                var imm = month-0;                 var idd = day-0;
                if (imm == 1 && idd < 28)
                {
                  imagename = imagedir + '/' + image + '_hfrd.jpg';
                }
                else
                {
                  imagename = imagedir + '/' + image + '_hfrd.gif';  // '_hfrd.jpg';
                }
              }
              if (firstname == '')
                firstname = imagename;
              tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"image" + j + "\" value=\"Image\" onClick=\"showImage('" + imagename + "', 'visible', " + j + ");\">";
            }   
            else if (project == "SCBROMSData")
            {
              //imagename = 'images/tide_station_scb.jpg';
              imagename = imagedir + '/plot_ssh_2d' + '_' + year + month.pad(2, "0", 2) + day.pad(2, "0", 2) + '.jpeg';
              if (firstname == '')
                firstname = imagename;
              tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"image" + j + "\" value=\"Image\" onClick=\"showImage('" + imagename + "', 'visible', " + j + ");\" disabled>";
            }
            else if (project == "SCBGlider" || project == "SioProfile" || project == "USCProfile")
            {
              imagename = imagedir + '/' + image;
              //alert('iamgename = ' + imagename);
              if (firstname == '')
                firstname = imagename;
              tablehtml += "<input class=\"normal8\" type=\"button\"  name=\"image" + j + "\" value=\"Image\" onClick=\"showImage('" + imagename + "', 'visible', " + j + ");\">";
            }
            else //MM5
            {
              tablehtml += "<SELECT NAME=\"image\"" + j + "\" onChange=\"showImage(this[this.selectedIndex].value, 'visible', -1)\">";
              dd = 1;
              time = 7;
              stime = ''+time;
              for (ii=2; ii<=49; ii++)
              {
                name = imagedir + '/' + image + '_' + year + month.pad(2, "0", 2) + day.pad(2, "0", 2) + "_" + ii + '.jpg';
                if (ii == 2) //save the first name to display
                  firstname = name;
                tablehtml += "<option value=\"" + name + "\">" + stime.pad(1, "0", 2) + "GMT (Day " + dd + ")</option>";          
                if (time != 0 && time%23 == 0)                {
                  time = 0; 
                  dd++;
                }
                else
                {
                  time++;
                }
                stime = ''+time;
              }
              tablehtml += "</SELECT>";
            }   
          }   
          tablehtml += "</td>";
        }   
      } //k 
      tablehtml += "</tr>";
    } //j         
    if (project == "SCBMM5")
    {         
      var fname = value;
      tablehtml += "<tr>";
      tablehtml += "<td colspan=4 width=\"100%\" align=right>";
      tablehtml += "<input type=\"button\" name=\"animate\" style=\"background-color:#C0C0C0\" value=\"Animation\" onclick=\"showAnimation()\">";      var yyyy = document.scbform.selectedYear.value-0;
      var mm = document.scbform.selectedMonth.value-0;
      var dd = document.scbform.selectedDay.value-0;
      var ndate = new Date(yyyy, mm-1, dd);
      year  = ndate.getYear();
      month = ndate.getMonth()+1;
      day   = ndate.getDate();
      //alert('year = ' + year + '; month = ' + month + '; day = ' + day);
      var date = new Date(2007, 9, 20);
      if (ndate > date)
      {
        tablehtml += "&nbsp;&nbsp;<input type=\"button\" name=\"kmz\" style=\"background-color:#C0C0C0\" value=\"Download Google Earth Animation\" onClick=\"KMZDownload('" + fname + "');\">";
      }
      tablehtml += "</td>";
      tablehtml += "</tr>";
    }
    else if (project == "SCBROMSData") //overwrite the setup above
    {
      tablehtml = "";
    }
  }
  else //empty
  {
    for(j=0; j<trs.length; j++)
    {
      tablehtml += "<tr>";
      tr = trs.item(j);
      var tds = tr.getElementsByTagName('td');
      var td = tds.item(0);
      value = td.firstChild.nodeValue;
      //alert('value = [' + value + ']');
      tablehtml += "<td class=\"bluebold14\" align='center'>";
      tablehtml += value + "</td>";
      tablehtml += "</tr>";
    } //j
  }   
  tablehtml += "</table>";
  //alert(tablehtml);
  setInnerHTML('mycontent', tablehtml);
  //alert('tds.length = ' + tds.length);
  if (tds.length > 1)
  {   
    //alert('project = ' + project);
    if (project == "SCBMM5" || project == "SCBROMSData")
      showImage(firstname, 'visible', -1);
    else
      showImage(firstname, 'visible', 0);
  } 
}   

function showImage(iname, vis, idx)
{
  //alert('showImage() - iname=[' + iname + '] & idx = ' + idx);
  var icheck = 'icheck'+idx;
  if (iname == "")
    vis = 'hidden';
  //alert('showImage() - vis = ' + vis);
  var maptable = SCBNowcastMap;
  var ind = iname.indexOf('nowcast');
  //alert('iname = ' + iname);
  if (ind != -1)
  {
    maptable = SCBNowcastMap;
  }
  else
  {
    ind = iname.indexOf('hfradar');
    if (ind != -1)
    {
      maptable = SCBFRadarMap;
    }
    else
    {
      ind = iname.indexOf('MM5');
      if (ind != -1)
      {
        maptable = NoMap;
      }
      else
      {
        ind = iname.indexOf('glider');
        if (ind != -1)
        {
          maptable = NoMap;
        }
        else
        {
          maptable = SCBROMSDataMap;
        }
      }
    }
  }

  if (document.layers) //NS
  {
    //alert('NS');
    document.scbform['scbimage'].visibility = vis;  //'visible';
    document.scbform['scbimage'].src = iname;
    if (idx != -1)
    {
      document.scbform[icheck].visibility = vis;  //'visible';
      if (saveidx != "" && saveidx != icheck)
        document.scbform[saveidx].visibility = 'hidden';
    }
    //document.scbform['scbimage'].useMap = newIamgeMap;
    document.scbform['scbmap'].innerHTML = maptable;
  }
  else if (document.all) //IE
  {
    //alert('IE');
    document.all['scbimage'].style.visibility = vis;  //'visible';
    document.all['scbimage'].src = iname;
    if (idx != -1)
    {
      document.all[icheck].style.visibility = vis;  //'visible';
      if (saveidx != "" && saveidx != icheck)
        document.all[saveidx].style.visibility = 'hidden';
    }
    //alert('maptable = [' + maptable + "]");
    document.all("scbmap").innerHTML = maptable;
  }
  else
  {
    //alert('Other browsers');
    document.getElementById("scbimage").style.visibility = vis;  //'visible';
    document.getElementById("scbimage").src = iname;
    if (idx != -1)
    { 
      document.getElementById(icheck).style.visibility = vis;  //'visible';
      if (saveidx != "" && saveidx != icheck)
        document.getElementById(saveidx).style.visibility = 'hidden';
    }
    //document.getElementById('scbimage').useMap = newImageMap;
    document.getElementById('scbmap').innerHTML = maptable;
  } 
  //alert('hold');
  if (idx != -1)
    saveidx = icheck;
  xx = xPageX('PlaceHolderDiv');
  yy = xPageY('PlaceHolderDiv');
  //alert('xx = ' + xx + '; yy = ' + yy);
  myimage = xGetElementById("scbimage");
  w = xWidth(myimage);
  h = xHeight(myimage);
  //alert('w = ' + w + '; h = ' + h);
  var anote = '';
  ind_ = iname.indexOf('nowcast');
  if (ind_ != -1)
  { 
    anote = nowcastnote;
  } 
  else
  {   
    ind_ = iname.indexOf('hfradar');
    if (ind_ != -1)
    {
      anote = hfradarnote; 
    }
    else
    {
      ind_ = iname.indexOf('tide');
      if (ind_ != -1)
      {
        anote = tidenote;
      }
    }
  }
  setInnerHTML('clicknote', anote);
  doMap(iname);
}


