//form.js version 1.35
//useful boolean functions for form validations
//isBlank(field, msg)
//isNotEmail(field)
//fieldValueIs(field, value)
//accumulateChecks(form, field, check_for_other)
//   combine all checkboxes into one string in <field>_all form field
//   if check_for_other=true, field_other_# will be checked and values replaced in corresponding list slots
//hideDiv(divId)
//showDiv(divId)
//flipDiv(divId)

  function isBlank(field_var, msg) {
    //generic function that returns true if field is blank
    //regardless of the field type (text, radio, check, select)
    var i,focusVar;
    var blankIs = true;
    if (field_var == undefined) {
      alert('ERROR@isBlank: Form variable is not defined.\nCheck validation function.\n\nDEBUG: msg='+msg);
      return true;
    }
    var ftype = fieldType(field_var);
    switch (ftype) {
      case 'text':
      case 'textarea':
        blankIs = (field_var.value == '');
        focusVar = field_var;
        break;
      case 'radio':
      case 'checkbox':
        if (field_var.length == undefined) {
          if (field_var.checked) { blankIs = false; }
        } else {
          for (i=0; i<field_var.length; i++) {
            if (field_var[i].checked) { blankIs = false; }
          }
        }
        focusVar = field_var[0];
        break;
      case 'select':
        blankIs = (field_var.selectedIndex == 0);
        focusVar = field_var;
        break;
      default:
        alert('ERROR@isBlank: Form element type is unknown.\nCheck validation function.\n\nDEBUG: type='+ftype);
        return true;
    }
    if (blankIs) {
      alert(msg);
      focusVar.focus();
      self.scrollBy(0,-60);
      return true;
    } else {
      return false;
    }
  }

  function fieldType(field_var) {
    //returns type of field
    var ft = (field_var.length == undefined)? field_var.type:field_var[0].type;
    if ((ft == undefined) && (field_var.options.length != undefined)) { ft = 'select'; }
    return(ft);
  }
  
  function fieldValueIs(field_var, comp_to) {
    //generic function that compares field value to the second argument
    //regardless of the field type (text, radio, check, select)
    if (field_var == undefined) {
      alert('ERROR@fieldValueIs: Form variable is not defined.\nCheck validation function.\n\nDEBUG: compare value='+comp_to);
      return true;
    }
    var ftype = fieldType(field_var);
    switch (ftype) {
      case 'text':
      case 'textarea':
        return (field_var.value == comp_to);
        break;
      case 'radio':
      case 'checkbox':
        if (field_var.length == undefined) {
          if ((field_var.checked) && (field_var.value == comp_to)) { return true; }
        } else {
          for (var i=0; i<field_var.length; i++) {
            if ((field_var[i].checked) && (field_var[i].value == comp_to)) { return true; }
          }
        }
        break;
      case 'select':
        if (field_var.options[field_var.selectedIndex].value == comp_to) { return true; }
        break;
    }
    return false;
  }
  
  function accumulateChecks(form, var_name, check_for_other) {
    var accumList = '';
    for (i=0; i<eval('form.'+var_name+'.length'); i++) {
      if (eval('form.'+var_name+'['+i+'].checked')) {
        if ((check_for_other) && (eval('form.'+var_name+'_other_'+i+' != undefined'))) {
          eval('accumList += form.'+var_name+'_other_'+i+'.value');
        } else {
          eval('accumList += form.'+var_name+'['+i+'].value');
        }
        accumList += "\n";
      }
    }
    eval('form.'+var_name+'_all.value = accumList');
  }

  function isNotEmail(field_var) {
    if (emailCheck(field_var.value)) {
      return false;
    } else {
      field_var.focus();
      return true;
    }
  }
  
  function emailCheck(emailStr) {
    var emailPat=/^(.+)@(.+)$/
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    
    //See if emailStr is not empty
    if (emailStr == '') {
      alert("Email address is missing.");
      return false;
    }
    
    //Begin with the coarse pattern to simply break up user@domain into workable pieces
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
    	alert("Email address seems incorrect (check @ and .'s)");
    	return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    
    // See if "user" is valid 
    if (user.match(userPat)==null) {
        alert("The email user name doesn't seem to be valid.");
        return false;
    }
    
    // if domain is an IP address
    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("Email's IP address is invalid!");
  		      return false;
    	    }
        }
        return true;
    }
    
    // if domain is symbolic name
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
    	alert("The domain name doesn't seem to be valid.");
      return false;
    }
    
    // make sure domain ends in a 2 to 4 letter word, and is preceded by hostname
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
       alert("The email must end in a three or four letter domain, or two letter country.");
       return false;
    }
    
    // Make sure there's a host name preceding the domain.
    if (len<2) {
       alert("This email address is missing a hostname!");
       return false;
    }
    return true;
  }

  function hideDiv(theDiv) {
    var the_style = getStyleObject(theDiv);
    if (the_style != false) { the_style.display = 'none'; }
  }
  
  function showDiv(theDiv) {
    var the_style = getStyleObject(theDiv);
    if (the_style != false) { the_style.display = 'block'; }
  }

  function flipDiv(theDiv) {
    var the_style = getStyleObject(theDiv);
    if (the_style != false) {
      if ((the_style.display == 'block') || (the_style.display == '')) {
        the_style.display = 'none';
      } else {
        the_style.display = 'block';
      }
    }
  }
 
  function getStyleObject(objectId) {
    if (document.getElementById && document.getElementById(objectId)) {
      return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
      return document.all(objectId).style;
    } else {
      return false;
    }
  }
