// ReqFields.js
// Copyright (C)2000 NetTek, LLC
// Tim McAdoo - 6/9/2000
// This is support for field validation that can change on the fly
// 07/13/2000 - McAdoo - Added support for required fields and new field types
// 07/14/2000 - McAdoo - Added example code & other notes
// 10/06/2000 - McAdoo - updated code to use a switch->case statement; added "number" type
// 03/13/2001 - McAdoo - default date length

// Sample use within a global JavaScript allocation space :
//
//  var currentList = new Array (5);
//  currentList [0] = new rFields ("Name", "Please provide a valid name", "text", 5);
//  currentList [1] = new rFields ("Phone", "Please provide a valid phone number", "Phone");
//  currentList [2] = new rFields ("Email", "Please provide a valid e-mail address", "email");
//  currentList [3] = new rFields ("City", "Please provide a valid city");
//  currentList [4] = new rFields ("Date", "Please provide a valid date", "date", 7, false);
//
//
// This creates 5 validation fields using various optional parameters:
//   0 - Name - plain text, min length of 5
//   1 - Phone - phone formatting (note case insensitive)
//   2 - Email - Email address formatting
//   3 - City - plain text
//   4 - Date - date formatting, min length (not really used in this case), not manditory
//
// Field Types
//   * phone
//   * email
//   * date
//   * number
//   * text


function rFields (fieldname, message, fieldtype, minlen, req)
  // "Constructor" for a rFields
  // rFields =
  //   {
  //     fname     - field name (required)
  //     msg       - error message (required)
  //     ftype     - field type (date, email, phone, text - default) (optional)
  //     minlength - minumum length (default 1) (optional)
  //     req       - required field (default true) (optional)
  //   }
  // 10/06/2000 - McAdoo - updated code to use a switch->case statement; added "number" type
  {
    // save the fieldname
    this.fname = fieldname;
    
    // save the message
    this.msg   = message;  

    // check that it's not null
    if (fieldtype != null)
      {
        // figure out the field type
        switch ( fieldtype.toLowerCase () )
          {
            // set the phone
            case "phone" :
              this.ftype = "phone";
            break;
            
            // set to e-mail
            case "email" :
              this.ftype = "email";
            break;
            
            // set to date
            case "date" :
              this.ftype = "date";
            break;
            
            // set to number
            case "number" :
              this.ftype = "number";
            break;
            
            // default to text
            default:
              this.ftype = "text";
            break;
          }
      }
    else 
      this.ftype = "text";
    

    // figure out the field type for lengths
    switch ( this.ftype )
      {
        // dates must be at least #/#/####
        // McAdoo - 03/13/2001 - revised #/#/## = 6 chars
        case "date" :
          this.minlength = 6;
        break;
        
        // email must be at least X@X.X
        case "email" :
          this.minlength = 5;
        break;
     
        // phones must be at least ########## (area code required)
        case "phone" :
          this.minlength = 10;
        break;
     
        // handle the other types  
        default :
          if ( minlen == null )
            {
              this.minlength = 1;
            }
          else
            {
              this.minlength = minlen;
            }
        break;
     }
      
    // save the "required" flag (no passed value, default to true)
    if (req == null)
      this.req = true;
    else
      this.req = req;
  }