<!-- Hide from old browsers
//
// FILE:   EWBepicselect.js
// Author: Willa Zhu (PMEL/NOAA), willa@pmel.noaa.gov
// Date:   Feb 15, 2000
//
// These functions are used by EWB (Epic Web Browser) HTML files:
// ewb_selprof.htm and ewb_seltime.htm.
//
//------------------------------------------------------------------

// These constants define the dimensions of the small map used on 
// the HTML page: ewb_selprof.htm and ewb_seltime.htm
// constants
  USR_X1 = 20;   //usr_nn   map corners in  geogr. coordinates
  USR_Y1 = 90;
  USR_X2 = 380;
  USR_Y2 = -90;
  PXL_X1 = 0;   //pxl_nn corrsponding actual pixels in gif to above
  PXL_Y1 = -2;       
  PXL_X2 = 300;
  PXL_Y2 = 202;  //197  

//Global variables
  var numDataset;
  var mouseClicked = 0;
  var theMinDate, theMaxDate;   //string
  var theMinLonStr, theMaxLonStr;  // string
  var theMinLatStr, theMaxLatStr;  // string
  var totalNumFiles;         // number 

  var datasetStr;
  var newWin = 0;


function init() {  // initialization when the page is loaded
  args = init.arguments;
  dbinfo();
  //when page is loaded from the cache, we re-draw the rectangles 
  document.DrawRect.reset();  // reset the map first
  var dataset;
  var datasetname;
  numDataset = args[0];
  for(i=1; i<= numDataset; i++) {
    eval("dataset = document.selectForm.dataset" + i);
     if(dataset.checked == true) {
       EWB_drawRect(dataset.value, dataset.checked);
     }
  }
  EWB_updateInfoFields();
}

function EWB_drawRect(name,checked) { //v2.0
 //draw or un-draw the Rectangle
  var minlon, maxlon, minlat, maxlat;
  var pxlx1,pxly1,pxlx2,pxly2;
  var info, infostr;

  eval("infostr = document." + name + "_info");  
  info = infostr.split("|");

  // Now prepare to draw the Rectangle.
  minlon = info[2];
  maxlon = info[4];
  minlat = info[6];
  maxlat = info[8];

  // geo definitation of maxLat > minLat while
  // pixel definitation of y1 < y2
  pxlx1=EWB_getPixelX(minlon);
  pxly1=EWB_getPixelY(maxlat);
  pxlx2=EWB_getPixelX(maxlon)+1;
  pxly2=EWB_getPixelY(minlat)+1;
  document.DrawRect.setRect(pxlx1,pxly1,pxlx2,pxly2,checked);
}

function EWB_getMinMaxTime_totalNumFiles() { //v2.0
  // update dataset info fields first
  var theMinJday, theMaxJday;
  var mindate, maxdatem, minjday, maxjday;
  var dataset;

  totalNumFiles = 0;
  theMinDate = "";
  theMaxDate = "";
  var j = 0;
  for(var i=1; i<= numDataset; i++) {
    eval("dataset = document.selectForm.dataset" + i);
    if(dataset.checked == true) {
      j++;
      eval("infostr = document." + dataset.value + "_info");
      info = infostr.split("|");
      totalNumFiles += parseInt(info[18]);
      minjday = parseInt(info[12]);
      maxjday = parseInt(info[15]);
      mindate = info[14];
      maxdate = info[17];
      if(j == 1) {
	theMinJday = minjday;
	theMaxJday = maxjday;
	theMinDate = new String(mindate);
	theMaxDate = new String(maxdate);
      }
      else {
        if(minjday < theMinJday) {
   	  theMinJday = minjday;
	  theMinDate = new String(mindate);
	}
	if(maxjday > theMaxJday) {
	  theMaxjday = maxjday;
	  theMaxDate = new String(maxdate);
   	}
      }	    
    }
  }
}

function EWB_updateInfoFields() {
  //update text fields 
  EWB_getMinMaxTime_totalNumFiles();
  document.selectForm.begin_date.value = theMinDate;  
  document.selectForm.end_date.value = theMaxDate;  
  document.selectForm.tot_num_file.value = totalNumFiles;
}

function EWB_nextSelect(theURL) {
//When "next" button is clicked, it will either open next
//page in the same window if Cookie is allowed or open
//a new window is Cookie is not allowed.

  self.window.name="mainWin";
  var j = 0;
  for(var i=1; i<= numDataset; i++) {
    eval("dataset = document.selectForm.dataset" + i);
    if(dataset.checked == true) {
      j++;
      if(j==1) datasetStr = dataset.value;
      else datasetStr += '|' + dataset.value;
    }
  }
  if(j == 0) alert("You must select at least one dataset.");
  else {
    SetCookie("selectedDataset",datasetStr, null, "/");
    if(GetCookie("selectedDataset")) {  
//if cookie allowed use self window
      window.location = theURL;
    }
    else {  
//cookie not allowed, open a new window and use status to store the info
      if(newWin == 0) {
        alert("Opening a new browser window.");
      }
      self.window.status = datasetStr;
      newWin = window.open(theURL,"newSelectWin");
      newWin.focus();
    }
  }
}

function EWB_unSelectAll() {
  var dataset;
  for(i=1; i<= numDataset; i++) {
    eval("dataset = document.selectForm.dataset" + i);
    dataset.checked=false;
  }
  document.selectForm.begin_date.value = "";  
  document.selectForm.end_date.value = "";  
  document.selectForm.tot_num_file.value = 0;
}

function EWB_updateInfo() { 
  
  EWB_drawRect(EWB_updateInfo.arguments[0].value,EWB_updateInfo.arguments[0].checked);

  // update the Info Fields
  EWB_updateInfoFields();
}

function EWB_showInfo() { 
  var i, args, dataset, lon_rng, lat_rng, dep_rng, time_rng, numfile;
  var infostr;
  if(mouseClicked == 0) {
    mouseClicked = 1;
    args = EWB_showInfo.arguments;
    eval("infostr = document." + args[0] + "_info");
    info = infostr.split("|");
    // i=0: datastname
    // i=1: dataset category, datasubtype
    dataset = new String(info[1]);
  
    // i=2,3,4,5: minlong, maxlong
    lon_rng = new String(info[3] + " to " + info[5]);
  
    // i=6,7,8,9: minlat, maxlat
    lat_rng = new String(info[7] + " to " + info[9]);
  
    // i=10,11: mindepth, maxdepth
    dep_rng = new String(info[10] + " to " + info[11]);
  
    // i=12 - 17: mintime, maxtime
    time_rng = new String(info[14] + " to " + info[17]);
  
    // i=18: number of Files
    numfile = new String(info[18]);
  
    document.DrawRect.setInfo(dataset,numfile,lon_rng,lat_rng,dep_rng,time_rng);
  }
}

function EWB_redraw() { 
  document.DrawRect.redraw();
  mouseClicked = 0;
}

function EWB_mouseout() { 
  if(mouseClicked) {
    EWB_redraw();
  }
}

function EWB_reset() { 
  document.DrawRect.reset();
  EWB_unSelectAll();
}

function EWB_getPixelX() {
  var args, geo_x, pixel_x;
  args = EWB_getPixelX.arguments;
  geo_x = args[0];
  pixel_x = PXL_X1 + (geo_x-USR_X1)*(PXL_X2-PXL_X1)/(USR_X2-USR_X1);
  return(pixel_x);
}

function EWB_getPixelY() {
  var args, geo_y, pixel_y;
  args = EWB_getPixelY.arguments;
  geo_y = args[0];
  pixel_y = PXL_Y1 + (geo_y-USR_Y1)*(PXL_Y2-PXL_Y1)/(USR_Y2-USR_Y1);
  return(pixel_y);
}

function dummy() {
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
