<!------- find browser type & mouseover 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";
    visible = '.visibility = "visible"';
    hidden = '.visibility = "hidden"';
    zlevel = '.zindex';
    pxunits = 'px';
    okMenu = 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 = 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 = 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 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 check_required(which,req_fields,req_fields_txt) { 
  // which - values from form
  // req_eamil - field name for email
  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 change_field(form,item,val) {
  if (item && val) { 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 check_email(which) { //make sure emails are valid
  // written by Dan Jacobs
  var pass = true;
  var msg = "";
  var num = 0; 

  for (var j = 0; j < email_var.length; j++) {
    var this_email = eval('document' + '.' + form_name + '.' + 
      email_var[j] + '.value');
    var email_name = email_label[j];
    pass = emailCheck(this_email.toLowerCase());
    if (!pass) { 
      num += 1;
      //msg += "  * " + email_name + "'s email = " + this_email + "\n";
      msg += "  * " + email_name;
      if (this_email) {  msg += "  = '" + this_email + "'\n"; }
      else { msg += " was not entered\n"; }
    }
  }
 
  if (!pass) {
    if (num > 1) { 
      alert("The following do not appear to be valid:\n" + msg);
    }
    else { 
      alert("The following does not appear to be valid:\n" + 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 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);
}

function image_window(image,h,w,txt,blur_flag) {
  // image - file name
  // h - height of image 
  // w - width of image
  // txt - caption (optional)
  // blur_flag - y if close window on blur (default is y, turn off with n)
  if (!image || !h || !w) { return; }
  var winopt, html;
  var height = h * 1; 
  var width = w * 1; 
  var bgcolor = "#333399";
  var fc = "#FFFFFF";
  var web_dir = "http://www.mdsg.umd.edu/programs/research/reu/";
  var img_dir = "http://www.mdsg.umd.edu/images/uploads/siteimages/reu/";

  // option - close window when user moves off it (default is to do this)
  var blur_it = " onblur=window.close()"
  if (blur_flag == "n") { blur_it = ""; }

  html = "<!DOCTYPE HTML PUBLIC\ \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n   ";
  html += "  \"http://www.w3.org/TR/html4/loose.dtd\">\n";
  html += "<HTML LANG=\"EN-US\">\n";
  html += "<HEAD>\n";
  html += "<TITLE>REU Photograph<\/TITLE>\n";
  html += "<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" ";
  html += "HREF=\"" + web_dir + "reu.css\">\n";

  html += "<\/HEAD>\n";
  html += "<BODY BGCOLOR=\"" + bgcolor + "\"" + blur_it +">\n";
  html += "<DIV ALIGN=\"CENTER\">\n";
 
  html += "<IMG SRC=\"" + img_dir + image + "\" HEIGHT=\"" + height;
  html += "\" WIDTH=\"" + width + "\ ALT=\"REU Photograph";
  if (txt) { html+= " - " + txt; }
  html += "\">\n"; 

  if (txt) { 
    html+= "<DIV STYLE=\"color:" + fc + "\">\n"; 
    html += txt + "\n";
    html += "<\/DIV>\n";
  }
  html += "<\/DIV>\n";
  html += "<\/BODY>\n";
  html += "<\/HTML>\n";
  // adjust height & width so window does not have scrollbars
  height = height + 50;
  width = width + 100;
  // note: toolbar = yes to allow printing of pop-up window
  winopt = "toolbar=yes,location=yes,directories=yes,menubar=yes,resizable=no,";
  winopt = "toolbar=no,location=no,directories=no,menubar=no,resizable=no,";
  winopt += "scrollbars=yes,width=" + width + ",height=" + height;
  newWin=window.open("","dummy",winopt);
  newWin.document.write(html);
  newWin.document.close();
  return
}

function html_window(myhtml,h,w,txt,blur_flag) {
  // myhtml - file name
  // h - height of window 
  // w - width of window
  // txt - caption (optional)
  // blur_flag - y if close window on blur (default is y, turn off with n)
  if (!myhtml || !h || !w) { return; }
  var winopt, html;
  var height = h * 1; 
  var width = w * 1; 
  var bgcolor = "#333399";
  var fc = "#FFFFFF";
  var web_dir = "http://www.mdsg.umd.edu/programs/research/reu/";
  var img_dir = "http://www.mdsg.umd.edu/images/uploads/siteimages/reu/";

  // option - close window when user moves off it (default is to do this)
  var blur_it = " onblur=window.close()"
  if (blur_flag == "n") { blur_it = ""; }

  // adjust height & width so window does not have scrollbars
  height = height + 60;
  width = width + 50;
  // note: toolbar = yes to allow printing of pop-up window
  winopt = "toolbar=yes,location=yes,directories=yes,menubar=yes,resizable=no,";
  winopt = "toolbar=no,location=no,directories=no,menubar=no,resizable=no,";
  winopt += "scrollbars=yes,width=" + width + ",height=" + height;
  newWin=window.open(myhtml,"dummy",winopt);
  //newWin.document.write(html);
  //newWin.document.close();
  return
}

function myID(myImg,myName,myLink) {
  if (!myLink) {
    myLink = 'javascript:void(0);';
  }
  if (myName == 'array' || !myName) { 
    // saved in header array to avoid problems with web editor
    myName = myNames[myImg];
  }
  msover('class',myImg);
  ChangeText('txt',myName);
  if (myLink) {
    document.getElementById(myImg).href=myLink;
  }
}

function displayApplyForm(myid,request) {
  // used by reu_apply_manage.php
  document.applicants.REU_ID.value = myid;
  document.applicants.myform.value = request;
  document.applicants.target = "_blank";
  document.applicants.manage.value = 'showForm'; 
  var msg = "The letter of recommendation "; 
  if (request == "application") { msg = "The application form "; }
  msg += "for the applicant will be displayed in a new window or tab (depending on how your browser is configured).\n\nClose this window (tab) to return to the list of applicants.\n";
  alert(msg);
  document.applicants.submit();
  document.applicants.target = "_self";
  document.applicants.manage.value = 'committee';
}

function displayApplyForms(myid,requests) {
  // used by reu_apply_manage.php
  document.applicants.REU_ID.value = myid;
  document.applicants.myform.value = requests;
  document.applicants.target = "_blank";
  document.applicants.manage.value = 'showForms'; 
  var msg = "The html form submissions ";
  msg += "for the applicant will be displayed in a new window or tab (depending on how your browser is configured).\n\nClose this window (tab) to return to the list of applicants.\n";
  alert(msg);
  document.applicants.submit();
  document.applicants.target = "_self";
  document.applicants.manage.value = 'committee';
}

function displayApplicantForm(myname,myemail,mypwd) {
  // used by reu_apply_manage.php
  document.applicants.myemail.value = myemail;
  document.applicants.mypwd.value = mypwd;
  document.applicants.myname.value = myname;
  document.applicants.target = "_blank";
  document.applicants.manage.value = 'modifyApplicant'; 
  var msg = "Are you sure you want to modify the application material for " + myname + "?\n\n"; 
  msg += "OK - Yes, Cancel - No\n\n";
  msg += "If yes, you will be logged in as the applicant. The forms will be displayed in a new window or tab (depending on how your browser is configured).  Remember to close this window (tab) to return to the list of applicants.\n";
  ok = confirm(msg); 
  if (ok) { 
    document.applicants.submit();
  }
  document.applicants.myname.value = '';
  document.applicants.myemail.value = '';
  document.applicants.mypwd.value = '';
  document.applicants.target = "_self";
  document.applicants.manage.value = 'committee';
}

function displayRecommenderForm(myname,myemail,mypwd) {
  // used by reu_apply_manage.php
  document.applicants.myemail.value = myemail;
  document.applicants.mypwd.value = mypwd;
  document.applicants.myname.value = myname;
  document.applicants.target = "_blank";
  document.applicants.manage.value = 'modifyRecommend'; 
  var msg = "Are you sure you want to modify a recommendation by " + myname + "?\n\n"; 
  msg += "OK - Yes, Cancel - No\n\n";
  msg += "If yes, you will be logged in as the recommender. The forms will be displayed in a new window or tab (depending on how your browser is configured).  Remember to close this window (tab) to return to the list of recommenders.\n";
  ok = confirm(msg); 
  if (ok) { 
    document.applicants.submit();
  }
  document.applicants.myname.value = '';
  document.applicants.myemail.value = '';
  document.applicants.mypwd.value = '';
  document.applicants.target = "_self";
  document.applicants.manage.value = 'update_recommenders';
}

function forceDownload(file,combine_cmd) {  
  // force download file instead of displaying in the browser
  // input file is submitted to php script (reu_apply_manage.php) used to force download the file
  document.applicants.combine_pdfs.value = combine_cmd; 
  document.applicants.download.value = file; 
  document.applicants.submit();  
  document.applicants.download.value = '';  
  document.applicants.combine_pdfs.value = ''; 
}

function applyMgmtGo(form,item,val,flag) { 
  // used by reu_apply_manage.php 
  change_field(form,item,val);  
  if (flag) { eval('document.' + form).target = "_blank"; }
  else { eval('document.' + form).target = "_self"; }
  eval('document.' + form).submit(); 
}

function applyMgmtDates(form) { 
  // used by reu_apply_manage.php 
  var reuyr = eval('document.' + form).REU_Year.value;
  var group = eval('document.' + form).group.value;
  var manage = "select_dates"; 
  var myhtml = '/programs/research/reu/apply/form/reu_apply_manage.php?REU_Year=' + reuyr + '&group=' + group + '&manage=' + manage;
  html_window(myhtml,'250','300','','yes');
  //dummy.opener = self;
}

function closeFormWindow(form,myfield,mytype,myform) {
  // used by all the application forms 
  // close window if group = admin 
  if (!myform) { myform = "apply"; }
  myObj = eval('document.' + form + '.' + myfield);
  var todo = "";
  if (mytype == 'radio') {
    for (var i in myObj) {
      if (myObj[i].checked == true) {
        todo = myObj[i].value;
      }
    }
  }
  else {
    todo = myObj.value;
  }
  var group = eval('document.' + form + '.group').value; 
  if (group == 'admin' && (todo == 'exit' || todo == 'quit')) {
    var reuyr = eval('document.' + form).REU_Year.value;
    adminReload(group,reuyr,myform);
    return false;
  }
  else {
    return true;
  }
}

function adminReload(group,reuyr,myform) {  
  // close window and reload default admin window if group = admin 
  var myhtml = '/programs/research/reu/apply/form/reu_apply_manage.php?REU_Year=' + reuyr + '&group=' + group; 
  if (myform == "recommend") {
    myhtml += "&manage=recommenders";
  }
  window.close();
  window.opener.top.location.href = "http://www.mdsg.umd.edu" + myhtml;
}

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;
} 

//close off comment to hide this script from old browsers----------->
