/***********************************************************************************
 * 
 *     Title : viewfilelist.js
 *    Author : Zaihua Ji,  zji@ucar.edu
 *      Date : 05/03/2006
 *   Purpose : javascript program to validate the form inputs for request of 
 *             file list request from publish_filelist.inc.
 * Test File : /huron/http/js/viewfilelist_test.js
 * Work File : /huron/http/js/viewfilelist.js
 *  CVS File : $Header: /huron/dss/cvsroot/javascript/viewfilelist.js,v 1.84 2009/10/20 22:17:11 zji Exp $
 *
 **********************************************************************************/

/**
 * open a window to get an answer of yes, no or cancel
 */

var ynSaves;
var ynInternal;
var ynwin;

function yn_window(items, idx1, idx2, offset)
{ 
   var args = "width=500,height=110,left=325,top=300,scrollbars=0,resizable=0";
   var bttns = '<input type="button" name="yes" value="Yes" onClick="CloseForm(1)">&nbsp;&nbsp;' +
               '<input type="button" value="No" onClick="CloseForm(0)">&nbsp;&nbsp;' +
               '<input type="button" value="Unselect Last" onClick="CloseForm(2)">&nbsp;&nbsp;' +
               '<input type="button" value="Unselect Both" +" onClick="CloseForm(3)">';

   var scripts = "<script language=JavaScript>function CloseForm(val) { " +
                 "window.opener.ynSaves[0] = val; window.close(); }</script>";    
   
   ynSaves = new Array(0, items, idx1, idx2, offset);
   
   ynwin = window.open("", "Confirmation", args);
   ynwin.document.write("<html><head><title>Confirmation</title></head>" + scripts);
   ynwin.document.write("<body onblur=\"window.focus();\">");
   ynwin.document.write('<table border=0 width="95%" align=center cellspacing=0 cellpadding=2>');
   ynwin.document.write('<tr><td align=center>Include Data File(s) Between Indices ' + idx1 + ' And ' + idx2 + '?</td></tr>');
   ynwin.document.write('<tr><td><br></td></tr>');
   ynwin.document.write('<tr><td align=center>' + bttns + '</td></tr></body></html>');
   ynwin.document.close();
   ynwin.focus();
   ynInterval = window.setInterval("check_ynwin()", 5);
}

function check_ynwin()
{
   try {
      if(ynwin.closed) {
         window.clearInterval(ynInterval);
         yn_return();
      } else {
         ynwin.focus();
      }
   } catch(everything) {   }
}

function yn_return() {

   var j;
   var items;
 
   if(ynSaves[0] > 0) {
      items = ynSaves[1];
      if(ynSaves[0] == 1) {
         for(j = ynSaves[2] + 1; j < ynSaves[3]; j++) {
            if(! items[j].disabled) {
               items[j].checked = true;
            }
         }
      } else if(ynSaves[0] == 2) {
         items[ynSaves[2 + ynSaves[4]]].checked = false;
      } else {
         items[ynSaves[2]].checked = false;
         items[ynSaves[3]].checked = false;
      }
   }
}

/**
 * to initialize the restriction for MSS files that can be accessed online
 */
function init_maccess() {
   
   var atype = document.form.atype ? document.form.atype.value : null;
   var btns;
   if(atype == null) return; // no further action
   
   // todo process mss access info and modify value of butten idetified as 'FILELIST'
   btns = document.form.elements("FILELIST");
}

/**
 * function to reset the checkbox selctions of a group
 */
function resetFileSelection(fidx, gidx, gcnt, initcall) {
   var items = document.form.elements["GRP" + gidx];
   var check = items[fidx].checked ? true : false;
   var idbox, idx1, i;

   if(fidx == 0) { // INDEX check box clicked
      for(i = 1; i < items.length; i++) {
         if(items[i].checked != check && ! items[i].disabled) {
            items[i].checked = check;
         }
      }
      if(initcall) {
         eval("idbox = document.form.GID" + gidx);
         if(idbox && idbox.checked != check) {
            idbox.checked = check;
            resetGroupSelection(gidx, gcnt, 0);
         }
      }
   } else {
      if(check) {
         // checking multiple selections
         idx1 = 0;
         for(i = 1; i < items.length; i++) {
            if(i != fidx && items[i].checked) {
               if(idx1 == 0) {
                  idx1 = i;
               } else {
                  idx1 = 0;
                  break;
               }
            }
         }
         if(idx1 > 0) {
            if(idx1 - fidx > 1) {
               yn_window(items, fidx, idx1, 0);
            } else if(fidx - idx1 > 1) {
               yn_window(items, idx1, fidx, 1);
            }
         }

         // checking if any unselected
         for(i = 1; i < items.length; i++) {
            if(i != fidx && !items[i].checked) {
               check = false;
               break;
            }
         }
      }
      if(items[0].checked != check) {
         items[0].checked = check;
         if(initcall) {
            eval("idbox = document.form.GID" + gidx);
            if(idbox && idbox.checked != check) {
               idbox.checked = check;
               resetGroupSelection(gidx, gcnt, 0);
            }
         }
      }
   }
}

/**
 * function to reset the checkbox selctions of group ids
 */
function resetGroupSelection(gidx, gcnt, initcall) {
   var idbox;
   var items;
   var check;
   var i;

   eval("idbox = document.form.GID" + gidx);
   check = idbox.checked ? true : false;

   if(gidx == 0) { // GROUP ID check box clicked
      for(i = 1; i <= gcnt; i++) {
         eval("idbox = document.form.GID" + i);
         if(idbox && idbox.checked != check) {
            idbox.checked = check;
            if(initcall) {
               items = document.form.elements['GRP' + i];
               if(items && items[0].checked != check) {
                  items[0].checked = check;
                  resetFileSelection(0, i, gcnt, 0);
               }
            }
         }
      }
   } else {
      if(initcall) {
         items = document.form.elements['GRP' + gidx];
         if(items && items[0].checked != check) {
            items[0].checked = check;
            resetFileSelection(0, gidx, gcnt, 0);
         }
      }
      if(check) {
         for(i = 1; i <= gcnt; i++) {
            if(i != gidx) {
               eval("idbox = document.form.GID" + i);
               if(!idbox.checked) {
                  check = false;
                  break;
               }
            }
         }
      }
      if(document.form.GID0.checked != check) {
         document.form.GID0.checked = check;
      }
   }
}

/**
 * function to redisplay full (act == 1) or short (act == 0) notes
 * do action on all notes if nidx is 0
 */
function displayNotes(nidx, ncnt, gidx, act) {
   var lnote, snote;
   var i, ldsp, lpos, sdsp, spos;

   if(act == 1) {
      ldsp = "visible";
      lpos = "relative";
      sdsp = "hidden";
      spos = "absolute";
   } else {
      ldsp = "hidden";
      lpos = "absolute";
      sdsp = "visible";
      spos = "relative";
   }
   for(i = nidx; i <= ncnt; i++) {
      lnote = document.getElementById("LN" + gidx + "_" + i);
      if(nidx != ncnt && lnote == null) continue;
      snote = document.getElementById("SN" + gidx + "_" + i);
      lnote.style.visibility=ldsp;
      lnote.style.position=lpos;
      snote.style.visibility=sdsp;
      snote.style.position=spos;
   }
}

/**
 * function to show (act == 1) or hide (act == 0) sub groups
 */
function displayGroupNodes(hcnt, ocnt, act) {
   var gopen, ghide;
   var i, odsp, opos, hdsp, hpos;

   if(act == 1) {
      odsp = "hidden";
      opos = "absolute";
      hdsp = "visible";
      hpos = "relative";
   } else {
      odsp = "visible";
      opos = "relative";
      hdsp = "hidden";
      hpos = "absolute";
   }
   for(i = 0; i <= ocnt; i++) {
      gopen = document.getElementById("GOPEN" + i);
      gopen.style.visibility=odsp;
      gopen.style.position=opos;
   }
   for(i = 0; i <= hcnt; i++) {
      ghide = document.getElementById("GHIDE" + i);
      ghide.style.visibility=hdsp;
      ghide.style.position=hpos;
   }
}

/**
 * function to valid form inputs
 *
 */
function checkFileSelection(grpcnt)
{
   var i, j;
   var files;
   var sizes;
   var size = 0;
   var count = 0;
   var gidx, idx1, idx2;
   
   for(i = 1; i <= grpcnt; i++) {
      files = document.form.elements['GRP' + i];
      sizes = document.form.elements['SIZ' + i];
      if(files == null) continue;
      for(j = 1; j < files.length; j++) {
         if(files[j].checked) {
            count++;
            size = size + parseInt(sizes[j].value);
         }
      }
   }
   if(count == 0) {
      alert("Select at least one file to continue!");
   }
   document.form.total.value = size;
   return count;
}

/**
 * set value for hidden input: action
 */

function setActionType(act)
{
   document.form.action.value = act;
}

/**
 * open a note window for long description
 */

function openNoteWindow(gindex, title)
{
   eval("note = document.form.GNOTE" + gindex + ".value");

   notewin = window.open("", "DescWin", "width=700,height=400,scrollbars=yes,resizable=yes");

   notewin.document.write("<html><head><title>" + title + "</title></head><body>\n" +
           "<p><h3>Data Description for '" + title + "':</h3>\n" + note + "</p>\n" +
           "<form><center><input type=\"button\" value=\"Close This Window\" " + 
           "onClick=\"self.close()\"></center></form>\n</body></html>\n");
   notewin.document.close();
   notewin.focus();
}

/**
 * open a help window for given key
 */

function helpWindow(hkey, title)
{
   eval("note = document.form.HELP" + hkey + ".value");

   notewin = window.open("", "DescWin", "width=700,height=400,scrollbars=yes,resizable=yes");
   notewin.document.write("<html><head><title>" + title + "</title></head><body>\n" +
           "<p><h3>Help Information for " + title + ":</h3>\n" + note + "</p>\n" +
           "<form><center><input type=\"button\" value=\"Close This Window\" " + 
           "onClick=\"self.close()\"></center></form>\n</body></html>\n");
   notewin.document.close();
   notewin.focus();
}

/**
 * open a help window
 */

function openHelpWindow(helpkey, grpcnt)
{
   notewin = window.open("", "DescWin", "width=500,height=400,scrollbars=yes,resizable=yes");

   notewin.document.write("<html><head><title>Help Document</title></head><body>\n");
   if(helpkey == "ff") { // file archive format help
      notewin.document.write("<p><h3>Archive Format - post-proccessed file archive format:</h3>\n" +
              "<ul><li>BI - binary COS blocked *\n" +
              "<li>C1/CH - ASCII/character COS blocked *\n<li>TAR - multiple files archived into one per 'tar'\n" +
              "<li>Z - compressed per UNIX 'compress'\n" +
              "<li>ZIP - compressed per 'zip'\n" +
              "<li>GZ - compressed per 'gzip'\n" +
              "<li>BZ2 - compressed per 'bzip2'</ul>\n" +
              "Example: <b>C1.TAR.GZ</b> means ASCII COS blocked,\n" +
              "then tarred and then gzipped.<br>" +
              "* COS blocks can be checked and removed by Unix " +
              "utility programs, 'cosfile', 'cossplit' and 'cosconvert'. These " +
              "utilities are available on most CISL computers; otherwise click " +
              "<a href=\"javascript:window.open('http://dss.ucar.edu/libraries/io/cos_blocking/utils/');self.close()\">" +
              "HERE</a> to download them.</p>\n");
   } else if(helpkey == 'fc') { // index checkbox help
      var tmp = grpcnt > 1 ? "group" : "file list";
      notewin.document.write("<p><h3>Usage of INDEX CheckBox:</h3>" +
                "Click the checkbox at the top of the INDEX column to select/unselect " +
                "all data files in the " + tmp + ". If checkboxes of two individual files " +
                "are selected with unselected ones between, a question pops up offering " +
                "option to include them.</p>\n");
   } else if(helpkey == 'gc') { // group checkbox help
      notewin.document.write("<p><h3>Usage of Group ID CheckBox:</h3>" +
                "Click the checkbox at the top of the GROUP ID column to select/unselect " +
                "all data files shown on this webpage in different groups. " +
                "Click a checkbox of individual group to select/unselect " +
                "all data files in the group.</p>\n");
   } else if(helpkey == 'wb') { // web button help
      notewin.document.write("<p><h3>Usage of Buttons:</h3>" +
                "Click button of 'View Selected Files/Get As a Tar File' " +
                "to view the list of selected file names and optionally to " +
                "download them together as a single tar file if the total " + 
                "size of the selected data files does not exceed 2GB limit. " +
                "Perl/Csh scripts for downloading the selected files " +
                "in backgroup are available via the buttons of " +
                "'Perl Download Script'/'Csh Download Script'. To run any " +
                "of the download scripts, Internet download utility program " +
                "'wget' must exist on your system.</p>\n");
   } else if(helpkey == 'mb') { // mss button help
      notewin.document.write("<p><h3>Usage of Buttons:</h3>" +
                "Click button of 'View Selected Files' to view the list of selected " +
                "MSS file names. Perl/Csh scripts for downloading the selected files " +
                "in backgroup are available via the buttons of 'Perl Download Script'/" +
                "'Csh Download Script'. To run any of the download scripts, MSS utility " +
                "program 'msrcp' must exist on your system.</p>\n");
   } else if(helpkey == 'rb') { // mss online request button help
      notewin.document.write("<p><h3>Usage of Buttons:</h3>" +
                "Click button of 'View Selected Files/Request Online Download' " +
                "to view the list of selected MSS file names and optionally to " +
                "send request to make them available for online download.</p>\n");
   }
   notewin.document.write("<form><center><input type=\"button\" value=\"Close This Window\" " +
           "onClick=\"self.close()\"></center></form>\n</body></html>\n");
   notewin.document.close();
   notewin.focus();
}

/**
 * open a window for filelist, perl script or csh script
 * action: 0 - 'Filelist', 1 - 'Perl Script', 2 - 'Csh Script'
 */

function openFileWindow(grpcnt, act)
{
   var filewin;
   var ftype;
   var dsid;
   var action;
   var fname;
   var count = checkFileSelection(grpcnt);

   if(count == 0) return;
   
   ftype = document.form.ftype.value;
   dsid = document.form.dsid.value;
   fname = dsid;
   if(act == 0) {
      action = 'Filelist';
      fname += '.list.txt'; 
   } else if(act == 1) {
      action = 'Perl Script';
      fname += '.pl';
   } else {
      action = 'Csh Script';
      fname += '.csh';
   }

   filewin = window.open("", action, "width=750,height=600,scrollbars=yes,resizable=yes");
   filewin.document.write("<html><head><title>" + fname + "</title></head><body>\n");

   if(act == 0) {
      showFilelist(filewin, dsid, fname, grpcnt, count, ftype);
   } else if(ftype == 'MSS') {
      showMSSScript(filewin, action, dsid, fname, grpcnt, count);
   } else {
      showWebScript(filewin, action, dsid, fname, grpcnt, count);
   }
   filewin.document.write("<form><center><input type=\"button\" value=\"Close This Window\" " +
           "onClick=\"self.close()\"></center></form>\n</body></html>\n");
   filewin.document.close();
   filewin.focus();   
}

function showFilelist(win, dsid, fname, grpcnt, count, ftype)
{
   var i, j, k;
   var srcids, files;
   var limit, grpid;
   var size = document.form.total.value;
   var total = totalSize(size);
   var s = count > 1 ? "s" : "";
   var are = count > 1 ? " are" : " is";
   var sizes, locfiles, notes;
   var specialist, name;
   var gname;
   var stat = 0;
   var ogidx = 1;
   var showgroup = false;
   var showlocal = false;
   var shownote = false;
   var adesc, wpath;
   var rstat, rtype;
   var atype = document.form.atype ? document.form.atype.value : null;

   if(ftype == "Web") {
      win.document.write("<form name=\"form\" action=\"/cgi-bin/mget.tar\" method=\"post\">\n");
   } else {
      win.document.write("<form name=\"form\" action=\"/php/dsrqst.php\" method=\"post\">\n");
   }
   win.document.write("<p><h2>" + ftype + " File" + s + " Selected For '" + dsid +
                      "'</h2></p>\n<p>" + count + " file" + s + ", total " +
                      total + ", " + are + " selected.\n");
   size /= 2000000000;
   if(ftype == "Web") {
      if(size >= 2) {
         win.document.write("Total data size surpasses limit of 2.0GB. The selected files " +
                            "can not be downloaded directly as a single tar file.</p>\n");
      } else {
         wpath = document.form.wpath.value;
         if(wpath.substring(1, 9) == "datasets") {
            wpath = "/huron/ftp" + wpath;
         }
         win.document.write("<input type=\"hidden\" name=\"directory\" value=\"" + wpath + "/\">\n");
         win.document.write("Click the <b>'Download Selected'</b> button to " +
                            "download directly the selected files as a single tar file.</p>\n");
         win.document.write("<p><input type=\"submit\" value=" +
                            "\"Download Selected Files As A Tar File\"></p>\n");
         stat = 2;
      }
   } else if(atype) { // MSS files
      limit = document.form.alimit ? document.form.alimit.value : 20;
      if(size >= limit) {
         win.document.write("Total data size surpasses limit of " + limit + "GB. " +
                            "The selected files can not be requested for online downloaded.</p>\n");
      } else {
         stat = check_access_type(atype);
         if(stat == 1) {
            win.document.write("<input type=\"hidden\" name=\"dsid\" value=\"" + dsid + "\">\n");
            if(document.form.rstat) {
               win.document.write("<input type=\"hidden\" name=\"rstat\" value=\"" + 
                                  document.form.rstat.value + "\">\n");
            }
            if(document.form.rtype) {
               win.document.write("<input type=\"hidden\" name=\"rtype\" value=\"" + 
                                  document.form.rtype.value + "\">\n");
            }
            win.document.write("Click the following button to send a " +
                               "request for the selected MSS data being staged online " +
                               "for download.</p>\n");
            win.document.write("<p><input type=\"submit\" value=" +
                               "\"Request Online Download for Selected Files\"></p>\n");
         } else {
            adesc = document.form.adesc.value;
             if(stat == 0) {
                win.document.write("You have to login to our Web Server with permission to " +
                                   "access " + adesc + " before being able to request " +
                                   "online download of the selected MSS data!</p>\n");
            } else {
                win.document.write("You do not have the permission to access " + adesc +
                                   " and therefore not being able to request " +
                                   "online download of the selected MSS data!</p>\n");
            }
         }
      }
   }

   if(document.form.specialist) {
      specialist = document.form.specialist.value;
      name = document.form.fstname.value + " " + document.form.lstname.value;
   } else {
      specialist = "zji";
      name = "Zaihua Ji";
   }
   win.document.write("<p>Right click this window and select 'Save Page As ...',\n" +
                      "a 'Save As' window will popup; then choose the 'Text File'\n" +
                      "as Format/File Type and change file name to " + fname + ";\n" +
                      "click 'Save' button to save this filelist in a simple text file.</p>\n" +
                      "<p>Contact " + specialist + "@ucar.edu (" + name + ") for further assistance.</p>\n");
   // check if show local file names / group ids
   for(i = 1; i <= grpcnt; i++) {
      files = document.form.elements["GRP" + i];
      if(files == null) continue; // should not happen
      if(ftype == 'MSS') locfiles = document.form.elements["LOC" + i];
      gname = eval("document.form.GNAME" + i);
      notes = document.form.elements["NOTE" + i];
      if(locfiles || gname || notes) {
         for(j = 1; j < files.length; j++) {
            if(files[j].checked) {
               if(locfiles && !showlocal && locfiles[j].value) showlocal = true;
               if(notes && !shownote && notes[j].value) shownote = true;
               if(gname && !showgroup && i != ogidx) showgroup = true;
               ogidx = i;
            }
         }
      }
      if(showgroup && (showlocal || shownote)) break;
   }
   if(showlocal) {
      shownote = false;
   }
   win.document.write("<p>File" + s + " selected" + are + " listed bellow:\n");
   win.document.write("<p><table border=2 cellspacing=0 cellpadding=2 bgcolor=\"#dfcfb3\">\n");
   win.document.write("<tr><th>File Name</th>\n");
   if(showlocal) win.document.write("<th>Original File Name</th>\n");
   win.document.write("<th>Size</th>\n");
   if(showgroup) win.document.write("<th>GROUP ID</th>\n");
   win.document.write("<th>INDEX</th>\n");
   if(shownote) win.document.write("<th>Description</th>\n");
   win.document.write("</tr>\n");
   k = 1;
   for(i = 1; i <= grpcnt; i++) {
      files = document.form.elements["GRP" + i];
      if(files == null) continue; // should not happen
      if(stat == 1) srcids = document.form.elements["SID" + i];
      gname = eval("document.form.GNAME" + i);
      sizes = document.form.elements["SIZ" + i];
      if(showlocal) {
         locfiles = document.form.elements["LOC" + i];
      } else if(shownote) {
         notes = document.form.elements["NOTE" + i];
      }
      for(j = 1; j < files.length; j++) {
         if(!files[j].checked) continue;
         win.document.write("<tr><td>" + files[j].value + "</td>\n");
         if(showlocal) {
            win.document.write("<td>" + str_value(locfiles[j]) + "</td>\n");
         }
         win.document.write("<td align=\"right\">" + totalSize(sizes[j].value) + "</td>\n");
         if(showgroup) win.document.write("<td>" + str_value(gname) + "</td>\n");
         win.document.write("<td align=\"right\">" + k++ + "</td>\n");
         if(shownote) {
            win.document.write("<td>" + str_value(notes[j]) + "</td>\n");
         }
         win.document.write("</tr>\n");
         if(stat == 2) {
            win.document.write("<input type=\"hidden\" name=\"file\" value=\"" + 
                               files[j].value + "\">\n");
         } else if(stat == 1) {
            win.document.write("<input type=\"hidden\" name=\"srcids[]\" value=\"" + 
                               srcids[j].value + "\">\n");
         }
      }
   }
   win.document.write("</table></p>\n");
   
   win.document.write("</form>\n");
}

function showMSSScript(win, action, dsid, fname, grpcnt, count)
{
   var i, j, k, l;
   var filelist = new Array();
   var loclist = new Array();
   var files;
   var locfiles;
   var script;
   var lfile;
   
   // built up MSS and local file lists
   for(k = 0, i = 1; i <= grpcnt; i++) {
      files = document.form.elements["GRP" + i];
      locfiles = document.form.elements["LOC" + i];
	  if(files == null) continue; // should not happen
      for(j = 1; j < files.length; j++) {
         if(files[j].checked) {
            filelist[k] = files[j].value;
            if(locfiles == null) {
               loclist[k] = null;
            } else {
               lfile = locfiles[j].value;
               if(lfile.indexOf('/') == 0) {  // remove absolute path
                  l = lfile.lastIndexOf('/');
                  loclist[k] = lfile.substring(l + 1, lfile.length);
               } else {
                  loclist[k] = lfile;
               }
            }
            k++;
         }
      }
   }
   for(i = 0; i < count; i++) {
      if(loclist[i] != null) {
         for(j = 0; j < i; j++) {
            if(loclist[j] == loclist[i]) {
               loclist[i] = null;  // same local file names
               break;
            }
         }
      }
      if(loclist[i] == '' || loclist[i] == null) {  // use MSS file name if missed
         lfile = filelist[i];
         l = lfile.lastIndexOf('/');
         loclist[i] = lfile.substring(l + 1, lfile.length);
      }
   }

   // write out script
   win.document.write("<pre>" + mssScriptHeader(action, dsid, fname, count));
   if(action == 'Perl Script') {
      script = "use strict;\nuse File::Basename;\nmy $dir;\nmy $syscmd;\nmy @filelist = (\n";
      for(i = 0; i < count; i++) {
          script += "  [\"" + filelist[i] + "\", \"" + loclist[i] + "\"],\n";
      }
      script += ");\n"
             + "for(my $i = 0; $i < @filelist; $i++) {\n"
             + "  $dir = dirname($filelist[$i][1]);\n"
             + "  if($dir && ! -e $dir) {\n"
             + "    $syscmd = \"mkdir -p $dir\";\n"
             + "    print \"$syscmd...\\n\";\n"
             + "    system($syscmd);\n  }\n"
             + "  $syscmd = \"msrcp -n mss:$filelist[$i][0] $filelist[$i][1]\";\n"
             + "  print \"$syscmd...\\n\";\n"
             + "  system($syscmd);\n"
             + "}\nexit 0;\n";
      win.document.write(script);
   } else {
      for(j = 0; j < count; j += 300) {
         script = "set filelist = ( \\\n";
         k = (j + 300) < count ? (j + 300) : count;
         for(i = j; i < k; i++) {
            script += "  " + filelist[i] + " " + loclist[i] + " \\\n";
         }
         script += ")\n\n"
                +  "while($#filelist > 0)\n"
                + "  set dir = `dirname $filelist[2]`\n"
                + "  if ( x$dir != 'x' && ! -e $dir ) then\n"
                + "    echo \"mkdir -p $dir...\"\n"
                + "    mkdir -p $dir\n  endif\n"
                +  "  echo \"msrcp -n mss:$filelist[1] $filelist[2]...\"\n"
                +  "  msrcp -n mss:$filelist[1] $filelist[2]\n\n"
                +  "  shift filelist\n  shift filelist\n"
                +  "end\n\n";
         win.document.write(script);
      }
      win.document.write("exit 0\n</pre>");
   }
}


function showWebScript(win, action, dsid, fname, grpcnt, count)
{
   var i, j, k;
   var filelist = new Array();
   var filename;
   var files;
   var script;
   var cookies;
   var wpath;
   var email = null;
   var dtype = document.form.dtype.value;
   var http = "http";
     
   if(dtype == 'O') {
      wpath = "/datasets/" + dsid + "/docs/";
   } else if(dtype == 'S') {
      wpath = "/datasets/" + dsid + "/software/";
   } else {
      wpath = document.form.wpath.value + "/";
      if(wpath.substring(1, 9) == "datazone") {
         http += "s";
      }
   }
   wpath = http + "://dss.ucar.edu" + wpath;
  
   // get duser cookie for email
   email = get_user_email(true);
   if(email == null) {
      win.document.write("Missing User Email Address!\n");
      return;
   }
   
   // built up Web file list
   for(k = 0, i = 1; i <= grpcnt; i++) {
      files = document.form.elements["GRP" + i];
	  if(files == null) continue; // should not happen
      for(j = 1; j < files.length; j++) {
         if(files[j].checked) {
            filelist[k++] = files[j].value;
         }
      }
   }
   
   // write out script
   win.document.write("<pre>" + webScriptHeader(action, dsid, fname, count));
   if(action == 'Perl Script') {
      k = email.indexOf('@');
      email = email.substring(0, k) + "\\" + email.substring(k, email.length);
      script = "use strict;\nmy ($syscmd, $vn, $opt, $i, @filelist);\n"
             + "my $pswd = (@ARGV ? $ARGV[0] : $ENV{RDAPSWD});\n"
             + "if(!$pswd) {\n print \"\\n Usage: $0 YourPassword\\n\\n\";\n exit 1;\n}\n"
             + "open VN, \"wget -V |\" or die 'cannot find wget';\n"
             + "$vn = (&lt;VN&gt; =~ /^GNU Wget (\\d+)\\.(\\d+)/) ? (100 * $1 + $2) : 109;\n"
             + "close(VN);\n"
             + "$syscmd = ($vn > 109 ? 'wget --no-check-certificate' : 'wget');\n"
             + "$syscmd .= ' -O /dev/null --save-cookies auth.dss_ucar_edu --post-data' .\n"
             + "\"='email=" + email + "&passwd=$pswd&action=login' \" .\n"
             + "'https://dss.ucar.edu/cgi-bin/login';\n"
             + "system($syscmd);\n"
             + "$opt = 'wget -N';\n"
             + "$opt .= ' --no-check-certificate' if($vn > 109);\n"
             + "$opt .= ' --load-cookies auth.dss_ucar_edu ' .\n"
             + "'" + wpath + "';\n"
             + "@filelist = (\n";
      for(i = 0; i < count; i++) {
         script += "  \"" + filelist[i] + "\",\n";
      }
      script += ");\n"
             +  "for($i = 0; $i < @filelist; $i++) {\n"
             +  "  $syscmd = $opt . $filelist[$i];\n"
             +  "  print \"$syscmd...\\n\";\n"
             +  "  system($syscmd);\n"
             +  "}\nsystem('rm -f auth.dss_ucar_edu');\nexit 0;\n\n";
      win.document.write(script);
   } else {
      script = "set pswd = $1\n"
             + "if(x$pswd == x && `env | grep RDAPSWD` != '') then\n"
             + " set pswd = $RDAPSWD\nendif\n"
             + "if(x$pswd == x) then\n echo\n"
             + " echo Usage: $0 YourPassword\n echo\n exit 1\nendif\n"
             + "set v = `wget -V |grep 'GNU Wget ' | cut -d ' ' -f 3`\n"
             + "set a = `echo $v | cut -d '.' -f 1`\n"
             + "set b = `echo $v | cut -d '.' -f 2`\n"
             + "if(100 * $a + $b > 109) then\n"
             + " set opt = 'wget --no-check-certificate'\nelse\n"
             + " set opt = 'wget'\nendif\n"
             + "set opt1 = '-O /dev/null --save-cookies auth.dss_ucar_edu --post-data'\n"
             + "set opt2 = \"email=" + email + "&passwd=$pswd&action=login\"\n"
             + "$opt $opt1=\"$opt2\" https://dss.ucar.edu/cgi-bin/login\n"
             + "set opt1 = \"-N --load-cookies auth.dss_ucar_edu\"\n"
             + "set opt2 = \"$opt $opt1 " + wpath + "\"\n"
      for(j = 0; j < count; j += 600) {
         script += "set filelist = ( \\\n";
         k = (j + 600) < count ? (j + 600) : count;
         for(i = j; i < k; i++) {
            script += "  " + filelist[i] + " \\\n";
         }
         script += ")\nwhile($#filelist > 0)\n"
                +  " set syscmd = \"$opt2$filelist[1]\"\n"
                +  " echo \"$syscmd ...\"\n $syscmd\n shift filelist\nend\n\n";
         win.document.write(script);
      }
      win.document.write("rm -f auth.dss_ucar_edu\nexit 0\n</pre>");
   }   
}

/**
 * get total size with unit
 */
function totalSize(size)
{
   var units = new Array("B", "KB", "MB", "GB", "TB", "PB");
   var total = '';
   var i = 0;
   var idx;
   
   while(i < 5 && size >= 1000) {
      size /= 1000;
      i++;
   }
   total += size;
   idx = total.indexOf('.');
   return total.substring(0, idx + 4) + " " + units[i];
}

/**
 * get string of MSS script header
 */
function mssScriptHeader(action, dsid, fname, count)
{
   var header, specialist, name;
   var total = totalSize(document.form.total.value);
   
   if(action == 'Perl Script') {
      header = "#!/bin/perl -w\n#\n";
   } else {
      header = "#!/bin/csh\n#\n";
   }
   
   if(document.form.specialist) {
      specialist = document.form.specialist.value;
      name = document.form.fstname.value + " " + document.form.lstname.value;
   } else {
      specialist = "zji";
      name = "Zaihua Ji";
   }
   header += "#################################################################\n"
          +  "# Auto-generated " + action + " to retrieve " + count + " MSS file(s) of\n"
          +  "# Dataset '" + dsid + "', total " + total + ", on any Unix machine\n"
          +  "# with MSS connection. This script uses MSS command 'msrcp' to\n"
          +  "# download the data. Make sure you have enough disk space to hold\n"
          +  "# the data. A MSS file name will be replaced by a local file name\n"
          +  "# if it is recorded in RDADB and also unique in the local file list.\n#\n"
          +  "# Right click this window and select 'Save Page As ...', a 'Save As'\n"
          +  "# window will popup; then choose the 'Text File' as Format/File Type\n"
          +  "# and change file name to " + fname + "; click 'Save' button to\n"
          +  "# save this script.\n#\n";
          +  "# Contact " + specialist + "@ucar.edu (" + name + ") for further assistance.\n"
          +  "#################################################################\n\n";

   return header;
}
/**
 * get string of Web script header
 */
function webScriptHeader(action, dsid, fname, count)
{
   var header, specialist, name;
   var total = totalSize(document.form.total.value);
   var dtype = document.form.dtype.value;
   var rinfo;
   var s = count > 1 ? "s" : "";
   if(dtype == 'O') {
      dtype = "Document";
   } else if(dtype == 'S') {
      dtype = "Software";
   } else { // default
      dtype = "Data";
   }
   if(action == 'Perl Script') {
      header = "#!/usr/bin/perl -w\n#\n";
   } else {
      header = "#!/bin/csh\n#\n";
   }
   if(document.form.rqstid) {
      rinfo = " from Request '" + document.form.rqstid.value + "'";
   } else {
      rinfo = "";
   }
   if(document.form.specialist) {
      specialist = document.form.specialist.value;
      name = document.form.fstname.value + " " + document.form.lstname.value;
   } else {
      specialist = "zji";
      name = "Zaihua Ji";
   }
   header += "#################################################################\n"
          +  "# " + action + " to retrieve " + count + " online " + dtype + " file" + s + rinfo + " of '" + dsid + "',\n"
          +  "# total " + total + ". This script uses 'wget' to download data.\n#\n"
          +  "# Right click this window and 'Save As' a 'Text File', named as\n"
          +  "# " + fname + ".\n#\n"
          +  "# You need to pass in your password as a parameter to execute\n"
          +  "# this script; or you can set an evnironment variable RDAPSWD\n"
          +  "# if your Operating System supports it.\n\#\n"
          +  "# Contact " + specialist + "@ucar.edu (" + name + ") for further assistance.\n"
          +  "#################################################################\n\n";
   return header;
}

// get a string value of an object
function str_value(obj) {

   var str = "&nbsp;";
   
   if(obj && obj.value) str= obj.value;
   
   return str;
}
 
 
/**
  * check access type for login users
  * return 1 - OK to select files; 0 - not logged in yet; -1 - logged in but no permission
  */
function check_access_type(atype) {

   var cookies, k, email;
   
   if(atype == 'g') return 1;

   email = get_user_email(false);
   if(email == null) return 0;

   k = email.indexOf("<" + atype + ">");
   if(k > 0) {
      return 1; // found match access type
   } else {
      return -1;
   }
}

/**
  * get user email, remove trailing '<' and '>' for remove == true
  * return null if not logged in
  */
function get_user_email(remove) {

   var cookies, files, k, email;

   // get duser cookie for email
   if(document.cookie != "") {
      cookies = document.cookie.split("; ");
      for(i = 0; i < cookies.length; i++) {
         files = cookies[i].split("=");
         if(files[0] == 'duser') {
            email = files[1];
            k = email.indexOf(";");
            if(k > 0) {
               email = email.substring(0, k);
            }
            if(remove) {
               k = email.indexOf(":");
               if(k > 0) {
                  email = email.substring(0, k);
               }
            }
            return email;
         }
      }
   }
   return null;
}
