// Validate.js
// Copyright (C)2000 NetTek, LLC
// This is code to manage form validation.  It requires use of ReqFields.js and Error.js
// McAdoo - 06/09/2000 - Initial setup
// McAdoo - 10/06/2000 - added e-mail support
// McAdoo - 10/09/2000 - revised numeric, date, phone, & email parsing; added emailParse; added phoneParse; added dateParse

// Special note(s):
//   * tform MUST be a form with a name
//   * currentList needs to be an array of rFields
//   * the rFields.fname MUST match a field name (case sensitive!) in tform

// Field Types
//   * phone  -
//   * email  - 10/09/2000 - works for X@X.X
//   * date   - 10/09/2000 - works for dates with 4 digit years
//   * number - 10/09/2000 - works ( strips out trailing characters )
//   * text   - 10/09/2000 - works for all text (duh!)

function Validate ( tform, currentList )
  // McAdoo - 05/30/2000 - validate form
  // McAdoo - 10/06/20000 - added e-mail address support
  {
    // set the error flag
    ErrorFound = false;
    
    // loop through the list of required elements
    for (i = 0; i < currentList.length; i++)
      // check for NULLed out elements (not needed later)
      if (currentList [i] != null)
        {
          // check for a form element with this name
          if ( eval ("document." + tform.name + "." + currentList [i].fname) )
            {            
              // check the length of the field and then provide an error message
              eval ("if (document." + tform.name + "." + currentList [i].fname + ".value.length < " + currentList [i].minlength + ") { Error (document." + tform.name + "." + currentList [i].fname + ", '" + currentList [i].msg + " that is at least " + currentList [i].minlength + " characters in length'); } /*else { window.alert ('Current length=' + document." + tform.name + "." + currentList [i].fname + ".value.length); } */ ");
              
              if (!ErrorFound)
                {
                  // check against various types
                  switch ( currentList [i].ftype )
                    {
                      case "number" :
                        // McAdoo - 10/09/2000 - Works! - check if it's a valid int/float, store the result
                        eval ("if ( isNaN ( parseFloat ( document." + tform.name + "." + currentList [i].fname + ".value ) ) ) { Error (document." + tform.name + "." + currentList [i].fname + ", '" + currentList [i].msg + " that is at least " + currentList [i].minlength + " digits in length'); } else { document." + tform.name + "." + currentList [i].fname + ".value = parseFloat ( document." + tform.name + "." + currentList [i].fname + ".value ); /*window.alert ('Valud number');*/ } ");
                      break;
                  
                      case "date" :
                        // McAdoo - 10/09/2000 - Works! - check if it's a valid date
                        eval ("if ( parseDate ( document." + tform.name + "." + currentList [i].fname + ".value ) ) { Error (document." + tform.name + "." + currentList [i].fname + ", '" + currentList [i].msg + " that has a four digit year'); } else { /*window.alert ('Valid date');*/ } ");
                      break;
                  
                      case "phone" :
                        // McAdoo - 10/09/2000 - Works! - check if it's a valid phone number
                        eval ("if (parsePhone ( document." + tform.name + "." + currentList [i].fname + ".value ) ) { Error (document." + tform.name + "." + currentList [i].fname + ", '" + currentList [i].msg + " that is formatted\\n\\n\\t(###)###-####'); } else { /*window.alert ('Valid phone');*/ } " );
                      break;
                  
                      case "email" :
                        // McAdoo - 10/09/2000 - Works! - check if it's a valid e-mail address
                        eval ("if (parseEmail ( document." + tform.name + "." + currentList [i].fname + ".value ) ) { Error (document." + tform.name + "." + currentList [i].fname + ", '" + currentList [i].msg + " that is at least " + currentList [i].minlength + " characters in length'); } else { /*window.alert ('Valid e-mail');*/ }" );
                      break;
                    }
                }
            }
        }    
        
    // return results
    return (!ErrorFound);
  }

function parseDate ( str )
  // McAdoo - 10/09/2000 - returns if this is a proper date field
  {
    // local vars
    var result = false;
    var tdate = new Date ( str );
    
    if ( !isNaN ( tdate )  )
      {
        //window.alert ( tdate.toString () + '==' + str );
        
        // set to true, since it's a match
        result = true;
      }
    else
      {
        // debug
        //window.alert ('tdate == NaN');
      }
    
    // return the result
    return ( !result );
  }

function parsePhone ( str )
  // McAdoo - 10/09/2000 - returns if this is a proper phone field
  {
    // local vars
    var result = false;
    // regular expressions
    var rexpr1 = /\s*\(\s*\d{3}\s*\)\s*\d{3}\s*-?\s*\d{4}\s*/;
    var rexpr2 = /\s*\d{3}\s*-\d{3}\s*-\s*\d{4}\s*/;
    var rexpr3 = /\s*\d{3}\s+\d{3}\s+\d{4}\s*/;
    
    // check the pattern (###) ###-####
    if ( !str.match ( rexpr1 ) )
      {
        // check the pattern ###-###-####
        if ( !str.match ( rexpr2 ) )
          {
            // check the pattern ### ### ####
            if ( !str.match ( rexpr3 ) )
              {
                // debug
                //window.alert ("Reject ### ### ####");
              }
            else
              {
                // debug
                //window.alert ("Accept ### ### ####");
                result = true;
              }
          }
        else
          {
            // debug
            //window.alert ("Accept ###-###-####")
            result = true;
          }
      }
    else
      {
        // debug
        //window.alert ("Accept (###)###-####");
        result = true;
      }
      
    // return the result
    return ( !result );
  }

function parseEmail ( str )
  // McAdoo - 10/09/2000 - returns if this is a proper e-mail address
  // Proper formatting: X@X.X
  {
    // local vars
    var result = false;
    var index = -1;
    
    // check for an '@' sign
    index = str.indexOf ('@');
    
    // check for any result
    if ( index != -1 )
      {
        // check for at least 1 character in front
        if ( index > 0 )
          {
            // check for at least three trailing characters
            if ( str.length >= (index + 3) )
              {
                // check for at least a "." following the @ sign
                if ( str.indexOf ('.', index) != -1 )
                  {
                    // it's a valid e-mail address
                    result = true;
                  }
                else
                  {
                    // debug
                    //window.alert ('"." is not found');
                  }
              }
            else
              {
                // debug
                //window.alert ('trailing after "@" is too short');              
              }
          }
        else
          {
            // debug
            //window.alert ('prefix to "@" is too short');
          }
      }
    else
      {
        // debug
        //window.alert ('no "@" present');
      }
    
    // use the return value
    return ( !result );
  }