<!-- BEGIN

// check to make sure have tested browser:
//  Netscape 4.7 or later
//  MSIE 4 or later
browser_name = navigator.appName;
browser_version = parseFloat(navigator.appVersion); 
okVer = false;
msg = "Version 4";
if (browser_name == "Netscape") { 
  if (browser_version >= 4.7) { okVer = true; } 
  msg = "Netscape 4.7";
}
else if (browser_name == "Microsoft Internet Explorer") { 
  if (browser_version >= 4.0) { okVer = true; }
  msg = "MS Internet Explorer 4";
}
else if (browser_version >= 4.0) { okVer = true; }
 
okVer = true;

if (!okVer) { 
  alert("Please upgrade your browser to " + msg + " or later.");
}

function check_it(which,flag) { 
  // written by Dan Jacobs
  // which - values from form
  // flag - number of the next step for perl script ('step'flag)
  // defined in the web page javascript:
  // check_email - value of email entry to check
  // field_name - name of the email field
  // figures - number of figures (optional)

  var ok = true; // true/false flag if pass check(s)

  // check required fields
  if (required.length > 0) { ok = required_fields(which); }

  // have all required fields, now check email
  if (ok && email_var.length > 0) { ok = check_email(which); } 

  // have all required fields & email, now double check checkboxes 
  if (ok && checkboxes.length > 0) { ok = check_checkboxes(which); }
 
  // have all required fields & email, now double check patterns 
  if (ok && pattern_var.length > 0) { ok = check_patterns(which); }

  // now check if have entries for all members of each defined group 
  if (ok && groups.length > 0) { ok = check_groups(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 = "";
  var num = 0; 
 
  if (document.images) {
    for (var j = 0; j < required.length; j++) {
      var this_pass = this_radio = false;
      for (var i=0; i<which.length; i++) {
        var tempobj=which.elements[i];
        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";
          }
          else if (tempobj.type=="radio") {
            this_radio = true;
            if (tempobj.checked) { this_pass = true; }
          } 
        }
      }
      if (this_radio && !this_pass) {
        pass=false;
        num += 1;
        msg += "  * " + required_text[j] + "\n";
      }
    }
  }  
 
  if (!pass) {
    if (num > 1) { 
      msg = "Please make sure that entries for \n" + msg;
      msg += "are entered correctly.\n"; 
    }
    else { 
      msg = "Please make sure that the entry for \n" + msg;
      msg += "is entered correctly.\n"; 
    }
    alert(msg);
    return false;
  }
  else { return true; }
}

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 check_patterns(which) { // double check the patterns
  // written by Dan Jacobs
  var pass = true;
  var num = 0;
  var msg = ""; 

  for (var j = 0; j < pattern_var.length; j++) {
    pass = 
      pattern_check(which,pattern_var[j],pattern_text[j],pattern_name[j],'1'); 
    if (!pass) { 
      num += 1;
      msg += "  * " + pattern_text[j] + "\n";
    }
  }
 
  if (num > 0) {
    tt = "appear to be valid or match the pattern";
    if (num > 1) { 
      alert("The following do not " + tt + ":\n" + msg);
    }
    else { 
      alert("The following does not " + tt + ":\n" + msg);
    }
    return false;
  }
  else { return true; }
}

function pattern_check(which,this_var,this_string,pattern,this_flag) {   
  // written by Dan Jacobs
  var min_year = 1960; max_year = 2030;
  var this_match, this_msg, this_indx, this_num; 
  var this_var = eval('document' + '.' + form_name + '.' + this_var + '.value');
  if (!this_var) { return true; }
  if (!pattern) { return true; }
  if (pattern == "phone") { // xxx-xxx-xxxx
    this_match = this_var.match(/^(\d{3})\-(\d{3})\-(\d{4})$/);
    this_msg = "The pattern is xxx-xxx-xxxx, where each x is a number ";
    this_msg += "between 0-9.";  
  }
  else if (pattern == "mmyyyy") { 
    this_match = this_var.match(/^(\d{1,2})\/(\d{4})$/);
    if (this_match) { // check limits
      this_indx = this_var.indexOf('/');
      this_num = this_var.substring(0,this_indx) * 1;
      if (this_num > 12) { this_match = ""; }
      this_indx += 1;
      this_num = this_var.substring(this_indx,this_var.length) * 1;
      if (this_num < min_year || this_num > max_year) { this_match = ""; }
    }
    this_msg = "The pattern is mm/yyyy, where mm is the month (01-12) ";
    this_msg += "and yyyy is the year (e.g, 2003).";
  }
  else if (pattern == "ssn") {
    this_match = this_var.match(/^(\d{3})\-(\d{2})\-(\d{4})$/);
    this_msg = "The pattern is xxx-xx-xxxx, where each x is a number ";
    this_msg += "between 0-9.";
  }
  else { return true; }
 
  if (!this_match) { 
    if (this_flag) { // call from double check on submit
      return false;
    }
    else {
      alert('Please correct your ' + this_string + '.\n' + this_msg + '\n');
    }
  } 
  else { return true; }
 
}


// Check if all items in a group are provided before submitting the form
// Some of the code from Universal Related Select Menus - cascading popdown menus
// by Andrew King. v1.34 19990720

// Adapted by Dan Jacobs for this use
// a - indexed by groupIndex and itemsIndex based on calls in html 
// groups - in html and is the name of each group
//          indexed in same order as group creation in array a


a=new Array(22); // max number for groups
o=new Array(22); // max number for groups, used if select (not other input)
groupIndex = -1;

function newgroup() { // create new group
  groupIndex++;
  a[groupIndex] = new Array();
  o[groupIndex] = new Array();
  itemsIndex = 0;
}

function group(item,flag) { // create new entry in a group
  // flag - 1 = select, 2 = radio
  var myflag = 0;
  a[groupIndex][itemsIndex] = item; 
  if (flag) { myflag = flag; }
  o[groupIndex][itemsIndex] = myflag;
  itemsIndex++;
}

function check_groups(which) {
  // only if number created (groupIndex) = number defined (groups.length) 
  // end text of a group name with QQ to inidicate that the group is required 
  var defined_groups = groups.length - 1; // account for array index start at 0 
  if (groupIndex != defined_groups) { return true; }
  var pass = true;
  var num = 0;
  var msg = ""; 
  var this_value, this_index, members, max, flag;
  for (i=0; i <= groupIndex; i++) {  // group loop
    members = 0; max = a[i].length;
    for(j=0; j < max; j++) { // item loop 
      var thisObj = eval('document' + '.' + form_name + '.' + a[i][j]);
      this_value = thisObj.value; 
      if (o[i][j] == 1) { // select
        this_value = thisObj.selectedIndex; 
      }
      else if (o[i][j] == 2) { // radio
        this_value = ""; 
        for (var r = 0; r < thisObj.length; r++) {
          if (thisObj[r].checked) {
            this_value = thisObj[r].value;
          }
        } 
      }
      if (this_value) { members++; }
    }
    // members must = 0 (nothing for this group) or = max 
    flag = groups[i].match("QQ"); 
    if (flag) { // group required 
      var txt = groups[i].replace("QQ", ""); // remove flag
      if (members < max && members >= 0) { // not all in a group have entries 
        num++;
        pass=false;
        msg += "  * " + txt + "\n";
      }
    }
    else {  
     if (members < max && members > 0) { // not all in a group have entries 
        num++;
        pass=false;
        msg += "  * " + groups[i] + "\n";
      }
    }  
  }  
 
  if (!pass) {
    msg = "Please provide all the required information for: \n" + msg; 
    alert(msg);
    return false;
  }
  else { return true; }
}

// for checkboxes

function CheckboxChoice(form_name, prefix, all_name, all_flag, any_flag) {
  // form_name - name of the form
  // prefix - array name
  // all_name - name of checkbox for all items
  // all_flag - true if checkbox for all item
  // any_flag - true if checkbox for all item is not the default
  var this_item, this_value; 
  var all_item = 'document.' + form_name + '.' + all_name;
  if (all_flag) { // clicked on 'all' item 
    this_value = eval('document.' + form_name + '.' + all_name).checked;
    if (this_value) { // selected, turn off rest of checkboxes
      for (j = 0; j < eval(prefix).length; j++) { 
        this_item = 'document.' + form_name + '.' + eval(prefix + '[' + j + ']');
        eval(this_item).checked = false;  
      } 
      eval(all_item).checked = true;
    }
  }
  else { // other checkbox selected - turn off 'all' checkbox, none - all on
    for (j = 0; j < eval(prefix).length; j++) { 
      this_item = 'document.' + form_name + '.' + eval(prefix + '[' + j + ']');
      if (eval(this_item).checked) { flag = true; }  
    } 
    // turn on 'all' checkbox if nothing selected and any_flag not set
    if (flag) { eval(all_item).checked = false; } 
    else if (!any_flag) { eval(all_item).checked = true; }
  }
  return;
}

// Check if any items in a set of checkboxes are provided before form is submitted  
// Some of the code from Universal Related Select Menus - cascading popdown menus
// by Andrew King. v1.34 19990720

// Adapted by Dan Jacobs for this use
// ca - indexed by checkboxIndex based on calls in html 
// checkboxes - in html and is the name of each set of checkboxes
//  indexed in same order as group creation in array ca

ca=new Array(22); // max number for checkbox sets
checkboxIndex = -1;

function newcheckbox() { // create new checkbox set
  checkboxIndex++;
  ca[checkboxIndex] = new Array(); 
}

function checkbox(item,num) { // create new entries for a set of checkboxes
  // item - root name of checkbox set
  // num  - number of items in set (1..num)
  for (var j=1; j <= num; j++) {  // checkbox set loop
    ca[checkboxIndex][j] = item + "_" + j; 
  }
}

function check_checkboxes(which) {
  // only if number created (checkboxIndex) = number defined (checkboxes.length)
  var defined_groups = checkboxes.length - 1; // since array index starts at 0 
  if (checkboxIndex != defined_groups) { return true; }
  var pass = true;
  var msg = ""; 
  var this_value, this_index, members, max;
  for (var i=0; i <= checkboxIndex; i++) {  // checkbox set loop
    members = 0; max = ca[i].length - 1; // since start at 1 (not 0) in checkbox 
    for(var j=1; j <= max; j++) { // item loop 
      this_value = 'document' + '.' + form_name + '.' + ca[i][j];
      if (eval(this_value).checked) { members++; }
    }
    // members must > 0 (something for this set of checkboxes) and <= max 
    if (members > max || members < 1) { 
      pass=false;
      msg += "  * " + checkboxes[i] + "\n";
    } 
  }  
 
  if (!pass) {
    msg = "Please select one or more checkboxes for: \n" + msg; 
    alert(msg);
    return false;
  }
  else { return true; }
}

function check_others(flag) {
  var ok = true;
  // flag - ending of other id (var name + flag)
  for (var j=0;j<other_var.length;j++) {  
    //var a = eval(layerRef + other_var[j] + flag + layerEnd);
    //var b = eval(layerRef + other_val[j] + layerEnd); 
    var a = eval('document.' + form_name + '.' +  other_var[j]); 
    var b =  eval('document.' + form_name + '.' +  other_val[j]);
    for(var i = 0; i < a.length; i++) {
      var str = a[i].value;
      if (str.match(/Other/i)) {
        if (a[i].checked && !b.value) { 
          ok = false;
          alert('You selected other for:\n' + other_label[j] + '\n\nPlease fill in its text field.');
          return ok;
        }    
        else if (!a[i].checked && b.value) { 
          ok = true; 
          b.value = "";
        }
        else {
          ok = true;
        }
      }
    }
  } 
  return ok;
}

//END -->

