/* verify_fields.js - verifies form input fields contain the correct info.

** JBM 06 Jan 98


- looks for value of <form>.<element>.optional

    if the value is true, then no verification necessary
    if the value is false, then field needs verification


- looks for value of <form>.<element>.strLenMax or
                     <form>.<element>.strLenMin 

    if the value exists in the case of strMinLen and strMaxLen,
       then string length verification is performed


- looks for value of <form>.<element>.numeric or
                     <form>.<element>.max or
                     <form>.<element>.min 

    if the value is true, (or exists in the case of min and max,
       then numeric verification is performed

*/



function isWhiteSpace(s) 
{
  for ( var i = 0; i < s.length; i++ ) {
    var c = s.charAt(i);
    if (( c != ' ') && ( c != '\n' ) && ( c != '\t' )) {
      return false;
    }
  }
  return true;
}

function getEvilChars(s)
{
  if ( s == "password" ) {
    evilChars = new Array("@", "&");
  }
  if ( s == "userid" ) {
    //evilChars = new Array("`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "\", "|", "[", "{", "]", "}", "\"", "\'", ":", ";", "?", "/", ">", ".", "<", ",");
  }
  return evilChars;
}

function translate(s)
{

  // populate the values of the hash

  translateHash = new Array();

  translateHash["firstname"] = "first name";
  translateHash["lastname"] = "last name";
  translateHash["middlename"] = "initial";
  translateHash["oldpassword"] = "current password";
  translateHash["newpassword"] = "new password";
  translateHash["confirmpassword"] = "confirm password";
  translateHash["adminid"] = "administrator id";
  translateHash["middleinitial"] = "middle initial";
  translateHash["aliastarget"] = "alias target";
  translateHash["street1"] = "address";
  translateHash["hometel"] = "home phone";
  translateHash["postalcode"] = "postal code";
  translateHash["worktel"] = "work phone";
  translateHash["fax"] = "fax phone";
  translateHash["fullname"] = "full name";
  translateHash["maxIdlePeriod"] = "Max Idle Period";
  translateHash["csrlogonuserid"] = "admin id";
  translateHash["csrlogonpassword"] = "password";
  translateHash["csraccountnum"] = "account number";
  translateHash["street2"] = "Suite #";
  translateHash["userdef17"] = "starting date";
  translateHash["userdef18"] = "ending date";

  // massage the input string to a standard format
  var _index = s.indexOf('_');
  var subWord = s.substring( _index + 1, s.length);
  var massagedWord = subWord.toLowerCase();

  // return the translated value of the massaged version of the string passed to us
  translatedWord = translateHash[massagedWord];

/*
alert('s             : ' + s + '\n' +
      '_index        : ' + _index + '\n' +
      'subWord       : ' + subWord + '\n' +
      'massagedWord  : ' + massagedWord + '\n' +
      'translatedWord: ' + translatedWord + '\n' );
*/
  
  if ( translatedWord == null ) {
    return massagedWord;
  } else {
    return translatedWord;
  }
}
  


function verify( f )
{
  var msg;
  var empty_fields = "";
  var errors = "";

  for ( var i = 0; i < f.length; i++ ) {
    var e = f.elements[i];
    if ((( e.type == "text" ) || ( e.type == "textarea" ) || ( e.type == "password" )) && !e.optional ) {
      // check if field is empty
      if (( e.value == null) || ( e.value == "" ) || isWhiteSpace( e.value )) {
        empty_fields += "\n        " + translate( e.name );
        continue;
      }

      // check for numeric fields
      if ( e.numeric || ( e.min != null ) || ( e.max != null ) ) {
        var v = parseFloat(e.value);

        if ( isNaN(v) || (( e.min != null ) && ( v < e.min )) 
                          || (( e.max != null ) && ( v < e.max )) ) {
          errors += "- The field " + translate( e.name ) + " must be a number" ;
          
          if ( e.min != null ) errors += " that is greater than " + e.min;
          if ( e.max != null && e.min != null ) errors += " and less than " + e.max;
          else if ( e.max != null ) errors += " that is less than " + e.max;
          errors += ".\n";

        }
      }

      // check for length of string
      if (( e.strLenMin != null ) || ( e.strLenMax != null )) {

        if (( e.value.length < e.strLenMin ) || ( e.value.length > e.strLenMax )) {
          errors += "- The field " + translate( e.name ) + " must be" ;

          if ( e.strLenMin != null ) errors += " at least " + e.strLenMin;
          if ( e.strLenMin != null && e.strLenMax != null ) errors += ",\n  and no more than " + e.strLenMax;
          else if ( e.strLenMax != null ) errors += " no more than " + e.strLenMax;
          errors += " characters in length.\n";

        }
      }
/*
      // check for evil characters
      if ( e.noEvilChars ) {
        errors += "- The " + translate( e.name ) + " cannot contain the following characters:\n\n";
        for ( evil in getEvilChars(translate(e.name)) ) {
          errors += "\n        " + evil;
        }
        errors += "\n";
      }
*/
    }
  }

  // if errors exist, display the message and 
  // return false - otherwise return true


  if ( !empty_fields && !errors ) return true;

  msg  = "___________________________________________\n\n"
  msg += "Please correct the following error(s) and try again:\n";
  msg += "___________________________________________\n\n";

  if ( empty_fields ) {
    msg += "- The following required fields are empty:"
            + empty_fields + "\n";
    if ( errors ) msg += "\n";
  }
  msg += errors;
  alert(msg);
  return false;
}

