<!-- start

// used by all (general functions)
W3C = (document.getElementById) ? 1 : 0; 
IE4 = (document.all) ? 1 : 0; 
NS4 = (document.layers) ? 1 : 0; 

n_layer = 5;  // default number of layers

function dhtml_check() {
  // set browser specific items for javascript using layers
  if (W3C) { // 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 = true;
  }
  else if (NS4) { // 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 = true;
  }
  else if (IE4) { // msie 4 or greater - DOM specific object variables
    layerRef = "document.all['";
    layerEnd = "']";
    layerStyle = "'].style";
    layerText = "'].innerHTML";
    layerText = "').innerText";
    visible = '.visibility = "visible"';
    hidden = '.visibility = "hidden"';
    zlevel = '.zindex';
    pxunits = 'px';
    okMenu = true;
  }
  else { okMenu = false; } 
}

function reloadIt() { document.location = document.location; }

dhtml_check();

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) {
  // note: toolbar = yes to allow printing of pop-up window
  if (!toolbar) { toolbar = "no"; }  
  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);
}

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" ';
  html += 'HREF="http://www.mdsg.umd.edu/Research/RFP/rfp.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; 
}

minyr =  2004;
maxyr = 2030;

function check_date(form,item,pattern,txt) { // check if date follows pattern
  var this_var = eval('document' + '.' + form + '.' + item);
  var this_value = this_var.value;
  var this_match = "";
  if (!txt) { txt = item; }
  if (!this_value) { return false; }
  if (pattern == "yyyy") { // must be 4 digit year
    this_match = this_value.match(/^(\d{4})$/);
    if (this_value < minyr || this_value > maxyr || !this_match) { 
      alert("The value for " + txt + " (" + this_value + ") is not valid.");
      this_var.value = ""; 
      return false;
    }
  }
  else if (pattern == "mdy") { // must be mm/dd/yyyy
    this_date  = isValidDate(form,item,'this',1);
    if (!this_date) { 
      return false;
    }
  }
}

function splitDate(this_value) {
  var this_indx = this_value.indexOf('/');
  month = this_value.substring(0,this_indx) * 1;
  if (month > 12 || month < 1) { this_match = ""; }
  this_indx += 1;
  this_value = this_value.substring(this_indx,this_value.length);
  this_indx = this_value.indexOf('/');
  day = this_value.substring(0,this_indx) * 1;
  this_indx += 1;
  year = this_value.substring(this_indx,this_value.length) * 1;
}

function isValidDate(form,date_var,date_txt,flag) {
  // form - name of the form
  // date_var - name of the date variable in the form
  // flag - if return requested
  var retval = true;
  var thisObj = eval('document.' + form + '.' + date_var);
  var this_value = thisObj.value;
  var this_date, this_indx, this_msg;
  var this_match = this_value.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
  if (this_match) { // check limits
    splitDate(this_value);
    if (year < minyr || year > maxyr) { this_match = ""; }
  }
  if (!this_match) {
    this_msg = "The pattern of " + date_txt + " date is mm/dd/yyyy\nwhere mm is the month (1-12), ";
    this_msg += "dd is the day (1-28, 29, 30 or 31, depending on the month and year) ";
    this_msg += "and yyyy is the year (e.g, 2008).";
    thisObj.value = "";
    alert(this_msg);
    retval = false;
  } 
  else { 
    month--; // Javascript goes 0-11 (not 1-12)
    this_msg = thisObj.value;
    this_date = new Date(year,month,day); // convert to legit date
    if ((day == this_date.getDate()) && (month == this_date.getMonth()) && 
        (year == this_date.getFullYear())) { return true; }
    month = this_date.getMonth() + 1; // reset to legit month (1-12)
    day = this_date.getDate(); // reset to legit day for this month (including leap years) 
    year = this_date.getFullYear(); // reset to legit year
    thisObj.value = month + '/' + day + '/' + year; // reset to legit date
    this_msg = "The date you entered " + this_msg +  " is invalid ";
    this_msg += "and has been changed to " + thisObj.value; 
    alert(this_msg);
    retval = false;
  }
  if (flag) { return retval; }
}

//end -->
