<!-- start

// used by all (general functions)

function dhtml_check() {
  W3C = (document.getElementById) ? 1 : 0; 
  IE4 = (document.all) ? 1 : 0; 
  NS4 = (document.layers) ? 1 : 0; 
  // set browser specific items for javascript using layers
  if (document.getElementById) { 
    // netscape 6 (gecko) or greater - DOM specific object variables
    layerRef = "document.getElementById('";
    layerEnd = "')";
    layerStyle = "').style";
    layerText = "').innerHTML";
    visible = '.visibility = "visible"';
    hidden = '.visibility = "hidden"';
    zlevel = '.zindex';
    pxunits = 'px';
    okMenu = okMouse = true;
  }
  else if (document.layers) { 
    // netscape 4 - DOM specific object variables
    window.onResize = reloadIt;// patch for netscape window resizing problem
    layerRef = "document.layers['";
    layerEnd = "']";
    layerStyle = "']";
    visible = '.visibility = "show"';
    hidden = '.visibility = "hide"';
    zlevel = '.zIndex';
    pxunits = 'px';
    okMenu = okMouse = true;
    
  }
  else if (document.all) { // msie 4 or greater - DOM specific object variables
    layerRef = "document.all['";
    layerEnd = "']";
    layerStyle = "'].style";
    layerText = "'].innerHTML";
    visible = '.visibility = "visible"';
    hidden = '.visibility = "hidden"';
    zlevel = '.zindex';
    pxunits = 'px';
    okMenu = okMouse = true;
  }
  else { okMenu = okMouse = false; }
}
function reloadIt() { document.location = document.location; }
dhtml_check(); 

function preload_img(image_array,flag) { // preload images
  // image file names stored in image_array (defined above)
  // called below (or can use with BODY onLoad
  if (okMenu || okMouse) {
    for (var loop in image_array) {
      var t = eval('preload_' + flag);
      t[loop] = new Image();
      t[loop].src = image_array[loop];
    }
  }
}

function msover(img,ref) { // mouse rollover function
  // img - name of the IMG statement
  // ref - new_imgs array name 
  if (!ref) { ref = img; } 
  if (okMenu || okMouse) {
    document.images[img].src = preload_on[ref].src;
    lastimg = img;  
    lastref = base_imgs[lastimg]; 
  }
}

function msout(img,ref) { // mouse rollout function
  // img - name of the IMG statement
  // ref - new_imgs array name 
  if (!ref) { ref = img; }
  if (okMenu || okMouse) { document.images[img].src = preload_off[ref].src;}
}

function on_msover(string,img,ref) {
  if (okMouse) {
    if (!ref) { ref = img; }
    if (img) { msover(img,ref); }
    if (leader) { window.status = leader + string; }
    else { window.status = string; }
  }
}

function on_msout(img,string) {
  if (okMouse) {
    if (img) { msout(img); }
    if (string) { window.status = string; }
    else { window.status = 'Maryland Sea Grant'; }
  }
}

function promo(date,msg,height,width,movetox,movetoy) {
  // date - last day (month day, year) to display promo
  // msg - html file with promo
  // height = height of pop-window
  // width = width of pop-window
  // movetox - move this number of pixels to the right (x axis)
  // movetoy - move this number of pixels down (y axis)
  var winopt, today, end_date;
  if (!msg) { return; }
  if (!date) { remaining = 1; }
  else { 
    today = new Date();
    end_date = new Date(date); // month day, year
    milliseconds = (24 * 60 * 60 * 1000);
    remaining = ((end_date.getTime() - today.getTime()) / milliseconds); 
  }
  if (!movetox) { movetox = 0; }
  if (!movetoy) { movetoy = 0; }
  if (remaining >= 0) {  // pop-up window
    if (!height) { height = 400; }
    if (!width) { width = 400; }  
    // note: toolbar = yes to allow printing of pop-up window
    winopt = "toolbar=no,location=no,directories=no,";
    winopt += "menubar=no,resizable=no,";
    winopt += "scrollbars=yes,width=" + width + ",height=" + height;
    newWin=window.open(msg,"msg",winopt);
    newWin.moveTo(movetox,movetoy);
    return;
  }
  else { return; }
}

function status_line(string) { // status line message with mouseover
  if (okMouse) {
     if (leader) { window.status = leader + string; }
     else { window.status = string; }
  }
}

function CloseAndExectute(myform) {  
  // send form results back to opener window 
  // (named using window.name in its head)
  myform.target = window.opener.name;
  window.close(); 
} 

function CloseAndReturn(goto_htm) {  
  // open link (goto_htm) in opener window 
  window.opener.location = goto_htm;  
  window.close(); 
} 

function ChangeText(txt_tag,this_txt,id) {
  // txt_tag - id of div and ilayer (layer name is txt_tag + _a) 
  // this_txt - what is to be displayed or array name
  // id - array id if this_txt is array name
  if (id) { this_txt =  eval(this_txt)[id]; }
  if (W3C) { eval(layerRef + txt_tag + "')").innerHTML  = this_txt; }
  else if (IE4) { eval(layerRef + txt_tag + "']").innerText  = this_txt; }
  else if (NS4) { 
    var txt_tag2 = txt_tag + '_a';  
    document.eval(txt_tag).document.eval(txt_tag2).document.open();
    document.eval(txt_tag).document.eval(txt_tag2).document.write(this_txt); 
    document.eval(txt_tag).document.eval(txt_tag2).document.close();
  }
  else { alert("Don't recognize this browser"); }
}

function check_required(which,req_fields,req_fields_txt) { 
  // which - values from form
  if (req_fields) { required = req_fields; required_text = req_fields_txt; }
  var ok;
  ok = required_fields(which);
  return ok;
}  

function required_fields(which) {
  // checks if all required fields are filled in
  // do not change the rest of this function
  // adapted from script by wsabstract.com posted at 
  // The JavaScript Source!! http://javascript.internet.com

  var pass = true;
  var msg = "Please make sure that entries for \n";
  var num = 0; 
 
  if (document.images) {
    for (i="0"; i<which.length; i++) {
      var tempobj=which.elements[i];
      for (j = 0; j < required.length; j++) {
        if (tempobj.name==required[j]) {
          if (((tempobj.type=="password" ||
            tempobj.type=="text" ||
            tempobj.type=="file" ||
            tempobj.type=="textarea") && 
            tempobj.value=='') ||
            (tempobj.type.toString().charAt(0)=="s" && 
             tempobj.selectedIndex=="0")) {
            pass=false;
            num += 1;
            msg += "  * " + required_text[j] + "\n";
          }
        } 
      }
    }
  }  
 
  if (!pass) {
    if (num > 1) { msg += "are entered correctly.\n"; }
    else { msg += "is entered correctly.\n"; }
    alert(msg);
    return false;
  }
  else { return true; }
}

function emailCheck(emailStr) {
  // V1.1.3: Sandeep V. Tamhankar (stamhankar@hotmail.com) 
  // The JavaScript Source!! http://javascript.internet.com

  // Modified by Dan Jacobs 
  // comment out alerts - use return true/false only

  /* The following variable tells the rest of the function whether or not to
   verify that the address ends in a two-letter country   or well-known TLD. 
   1 means check it, 0 means don't. */

  var checkTLD=1;

  /* List of known TLDs that an e-mail address must end with. */

  var knownDomsPat = 
    /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

  /* The following pattern is used to check if the entered e-mail address fits the 
   user@domain format. Also used to separate the username from the domain. */

  var emailPat=/^(.+)@(.+)$/;

  /* This string represents the pattern for matching all special characters.  
   We don't want to allow special characters in the address, such as
   ( ) < > @ , ; : \ " . [ ] */

  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

  /* The following string represents the range of characters allowed in a
   user name or domain name. It really states which chars aren't allowed.*/

  var validChars="\[^\\s" + specialChars + "\]";

  /* The following pattern applies if the "user" is a quoted string (in which 
   case, there are no rules about which  characters are allowed and which aren't; 
   anything goes).  E.g. "jiminy cricket"@disney.com is a legal e-mail address. */

  var quotedUser="(\"[^\"]*\")";

  /* The following pattern applies for domains that are IP addresses, rather than 
   symbolic names - e.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The 
   square brackets are required. */

  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

  /* The following string represents an atom
   (basically a series of non-special characters.) */

  var atom=validChars + '+';

  /* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */

  var word="(" + atom + "|" + quotedUser + ")";

  // The following pattern describes the structure of the user

  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

  /* The following pattern describes the structure of a normal symbolic domain, 
    as opposed to ipDomainPat, shown above. */

  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

  /* Finally, start to figure out if the supplied address is valid. */

  /* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */

  var matchArray=emailStr.match(emailPat);

  if (matchArray==null) {
    /* Too many/few @'s or something; basically, this address doesn't even fit the
     general mold of a valid e-mail address. */
    //alert("Email address seems incorrect (check @ and .'s)");
    return false;
  }

  var user=matchArray[1];
  var domain=matchArray[2];

  // Start by checking if only basic ASCII characters are in the strings (0-127).

  for (i="0"; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
      //alert("The username contains invalid characters.");  
      return false;
    }
  }
  for (i="0"; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
      //alert("The domain name contains invalid characters.");
      return false;
    }
  }

  // See if "user" is valid 

  if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.");
    return false;
  }

  /* if the e-mail address is at an IP address (as opposed to a symbolic host
   name) make sure the IP address is valid. */

  var IPArray=domain.match(ipDomainPat);
  if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        //alert("Destination IP address is invalid!");
        return false;
      }
    }
    return true;
  }

  // Domain is symbolic name.  Check if it's valid.
 
  var atomPat=new RegExp("^" + atom + "$");
  var domArr=domain.split(".");
  var len=domArr.length;
  for (i="0";i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
      //alert("The domain name does not seem to be valid.");
      return false;
    }
  }

  /* domain name seems valid, but now make sure that it ends in a known
   top-level domain (like com, edu, gov) or a two-letter word, representing 
   country (uk, nl), and a hostname precedes the domain or country. */

  if (checkTLD && domArr[domArr.length-1].length!=2 && 
    domArr[domArr.length-1].search(knownDomsPat)==-1) {
    //alert("The address must end in a well-known domain or two letter country.");
    return false;
  }

  // Make sure there's a host name preceding the domain.

  if (len<2) { 
    //alert("This address is missing a hostname!");
    return false;
  }

  // If we've gotten this far, everything's valid!
  return true;
}

function show_layer(layerid,num_layer,pxleft_r,pxtop_r) {
  if (num_layer) { n_layer = num_layer; }
  for (i=1; i<=n_layer; i++) {
    var this_layer = "m" + i;
    if (i==layerid) {
      eval(layerRef + this_layer + layerStyle + visible);
    }
    else {
      eval(layerRef + this_layer + layerStyle + hidden);
    }
  }
}

function hide_layer(layerid,num_layer) {
  if (num_layer) { n_layer = num_layer; }
  for (i=1; i<=n_layer; i++) {
    var this_layer = "m" + i;
    if (layerid == 'all') { 
      eval(layerRef + this_layer + layerStyle + hidden);
    }
    else if (i==layerid) { 
      eval(layerRef + this_layer + layerStyle + visible);
    }
    else { 
      eval(layerRef + this_layer + layerStyle + hidden);
    }
  }
}

function set_layer_location(n_layer,pxleft,pxtop,offset) {  
  if (offset != "n") {
    if (document.all) { 
      if ((navigator.userAgent).indexOf("Windows")!=-1) {
        pxtop += 110; pxleft += 12; 
      }
    }
    else if (document.layers) { pxtop += 95; pxleft += 8; }
    else { pxtop += 85; pxleft += 8; }
    if ((navigator.userAgent).indexOf("Opera")!=-1) {
      pxtop += 110; pxleft += 8;
    }
  }
  var this_layer = "m" + n_layer; 
  var thisObj = layerRef + this_layer + layerStyle;  
  eval(thisObj).left = pxleft + pxunits; 
  eval(thisObj).top  = pxtop + pxunits; 
}

function clear_radio_item(form,item) {
  // reset (turn off) radio buttons named item in form
  for (j = 0; j < eval('document.' + form + '.' + item).length; j++) {
    eval('document.' + form + '.' + item + '[' + j + '].checked = false'); 
  }
}

function save_text(form,new_text,item) {
  var ii, str;
  ii = eval('document.' + form + '.' + new_text).selectedIndex;
  if (ii == 0) { str = ''; }
  else { 
    str = eval('document.' + form + '.' + new_text + '.options['+ ii +']').text;   
  }
  eval('document.' + form + '.' + item).value =  str;
} 

function set_value(item,value) {
  var this_var = eval('document' + '.' + form_name + '.' + item);
  this_var.value =  value;
} 

function change_field(form,item,val) {
  if (form && item) { eval('document.' + form + '.' + item).value = val; }
}

function submit_form(form,item,val,flag) {
  // item - form field name
  // val - value for item
  // flag - set if target is _blank (not _self)
  if (item && val) { change_field(form,item,val); }
  if (flag) { eval('document.' + form).target = '_blank'; }
  else { eval('document.' + form).target = '_self'; }
  eval('document.' + form).submit();
}

function open_window(file,width,height,toolbar,movetox,movetoy) {
  // note: toolbar = yes to allow printing of pop-up window
  // movetox - move this number of pixels to the right (x axis)
  // movetoy - move this number of pixels down (y axis)
  if (!toolbar) { toolbar = "no"; }  
  if (!movetox) { movetox = 0; }
  if (!movetoy) { movetoy = 0; }
  winopt = "toolbar=" + toolbar + ",location=no,directories=no,menubar=yes,";
  winopt += "resizable=no,scrollbars=yes";
  if (width && height) { winopt += ",width=" + width + ",height=" + height; }
  newWin=window.open(file,"dummy",winopt);
  newWin.moveTo(movetox,movetoy);
}

function noCR(thisEvent) {
  var key = window.event ? thisEvent.keyCode : thisEvent.which;
  var keychar = String.fromCharCode(key);
  var reg = /\r/; // key = 13 
  // reg.test is true if \r and false otherwise 
  // return the opposite of the reg.test value (true --> false, false --> true)
  return !reg.test(keychar);
}

// budgets, proposals

function help_window(item,width,height,toolbar) {
  if (!width) { width = 300; }
  if (!height) { height = 200; }
  var html = '<HTML>\n<HEAD>\n';
  html += '<TITLE>Budget Worksheet Help<\/TITLE>\n';
  html += '<LINK REL="STYLESHEET" TYPE="text\/css" HREF="' + url + '\/proposal.css">\n';
  html += '<\/HEAD>\n';
  html += '<BODY onBlur="window.close();">\n'; 
  html += '<P>\n'
  html += item;  
  html += '<\/P>\n'; 
  html += '<P ALIGN="CENTER" CLASS="FOOTER">\n';
  html += '<A HREF="javascript:window.close();"';
  html += 'onClick="window.close();">Close Window<\A>\n';
  html += '<\/P>\n';
  html += '<\/BODY>\n';
  html += '<\/HTML>\n';

  // note: toolbar = yes to allow printing of pop-up window
  if (!toolbar) { toolbar = "no"; }  
  winopt = "toolbar=" + toolbar + ",location=no,directories=no,menubar=no,";
  winopt += "resizable=no,scrollbars=yes";
  if (width && height) { winopt += ",width=" + width + ",height=" + height; }
  newWin=window.open("","dummy",winopt);
  newWin.document.write(html);
  newWin.document.close();
}

function listInstitutions() {
  document.write('<OPTION VALUE="" SELECTED>Current List\n');
  for (var ii in ICode) {
    if (ii != "") {
      document.write('<OPTION VALUE="' + ii + '">' + ii + '\n');
    }
   } 
 }

function setInstitution(form,select_field,text_field,icode_field,icode_div) {
  save_text(form,select_field,text_field);
  var thisPick = eval('document.' + form + '.' + select_field).value;
  eval('document.' + form + '.' + icode_field).value = ICode[thisPick];
  ChangeText(icode_div,ICode[thisPick]);
 }

function check_form(which,status) { 
  eval('document' + '.' + form_name + '.status').value = status; 
  if (status == 'submitted') { // finished with review
    ok = check_required(which); // check if all required fields are provided
  }
  else { ok = true; }
  return ok; 
}

function setICode(form,text_field,icode_field,icode_div) {
  var thisObj  = eval('document.' + form + '.' + text_field);
  if (!ICode[thisObj.value]) { var thisPick = ""; }
  else { var thisPick = thisObj.value; }
  eval('document.' + form + '.' + icode_field).value = ICode[thisPick];
  if (icode_div) { ChangeText(icode_div,ICode[thisPick]); } 
}

// reviews 
 
function check_value(field) { // create new group
   var low = scores_low[field]; 
   var high = scores_high[field]; 
   var item_name = scores[field];
   var this_var = eval('document' + '.' + form_name + '.' + field);
   if (score_scale == 'rank') { // rfp ordinal scale 1-5 allow 1 place to right of decimal
     var anum = /(^\d+$)|(^\d+\.\d+$)/;
     var msg = " ";
   }
   else { // 0-100 pct scale used by NSI, must be 1-2 digit integer
     var anum = /^(\d{1,2})$/;
     var msg = " an integer ";
   }
   var testresult = false;
   if (anum.test(this_var.value)) { testresult = true; } 
   if (this_var.value < low || this_var.value > high || !testresult) {
     alert("The value for " + item_name + " must be" + msg +  "between " + low + " and " + high); 
     this_var.value = "";
   }
 }

function check_total(which,status) { 
  // make sure sum of scores not more than 100
  var this_sum = 0;
  var this_total = eval('document' + '.' + form_name + '.sum');
  for (var item in scores) {
    var this_val = eval('document' + '.' + form_name + '.' + item).value; 
    this_sum += this_val * 1;
  }
  if (this_sum > 100) {  
    alert("Your total score is greater than 100."); 
    this_total.value = "";
    return false;
  }
  this_total.value = this_sum;
  eval('document' + '.' + form_name + '.status').value = status; 

  if (status == 'submitted') { // finished with review
    ok = check_required(which); // check if all required fields are provided
  }
  else { ok = true; }
  return ok; 
}

function trimAll(sString) { 
  if (sString == null) { return sString; }
  while (sString.substring(0,1) == ' ') { 
    sString = sString.substring(1, sString.length); 
  } 
  while (sString.substring(sString.length-1, sString.length) == ' ')  { 
    sString = sString.substring(0,sString.length-1); 
  } 
  return sString; 
} 

//end -->
