/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  checkSubmit.js                                                                              
 * ----------------                                                                            
 * AUTHOR: Tom Peters                                                                         
 * EMAIL: tom@weters.com                                                                     
 * LAST MODIFIED: 01/14/2005                                                                
 * VERSION: 1.2.4                                                                          
 *                                                                                        
 * DOCUMENTATION                                                                         
 *                                                                                      
 *    Please see the documentation.html file that came bundled in this package.        
 *                                                                                    
 * END-USER LICENSE AGREEMENT                                                        
 *                                                                                  
 *    Please see the EULA.html file that came bundled in this package.             
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 

function CheckSubmit()
{
   this.field          = new Array();     // Array will contain all the fields to be checked
   this.customFields   = new Array();     // Array will contain list of user defined functions
   this.errors         = "";
   this.checkForErrors = checkForErrors;  // Add checkForErrors as a method
   this.resetMsg       = "Are you sure you want to reset your data?";
   this.errorMsgFmt    = "X: Y";
   this.errorHeader    = "There are one or more errors:";
   this.errorMsg       = new Array();     // Default error messages

      this.errorMsg['email']            = "invalid e-mail address";
      this.errorMsg['int']              = "whole numbers only";
      this.errorMsg['dec']              = "integers and decimals only";
      this.errorMsg['alpha']            = "letters only";
      this.errorMsg['alphanum']         = "alphanumeric only";
      this.errorMsg['alphanumextended'] = "alphanumeric, underscore, hyphens and spaces only";
      this.errorMsg['radio']            = "must select a choice";
      this.errorMsg['checkbox']         = "must select to continue";
      this.errorMsg['select']           = "must select a value";
      this.errorMsg['req']              = "field left blank";

   this.changeErrorMsg = function (fieldName, msg)
   {
      this.errorMsg[fieldName] = msg;
   }

   this.addField = function (fieldName, fieldNameToDisplay, type, customMsg)
   {
      this.field.push(new Array(fieldName, fieldNameToDisplay, type, customMsg));
   }

   //
   // Set the form
   //

   this.setForm = function (formName)
   {
      this.form     = formName;
      var formTmp   = "document." + this.form;
      var form      = eval(formTmp);
      var obj       = this;

      form.onsubmit = function ()
      {
         return checkSubmit(obj);
      }

   }

   this.setReset = function (str)
   {
      var formTmp   = "document." + this.form;
      var form      = eval(formTmp);

      var resetMsg = this.resetMsg;

      if (form)
      {
         form.onreset = function ()
         {
            str = (str) ? str : resetMsg;
            var c = confirm (str);
            return c;
         }
      }
   }

   this.setConfirm = function (msg)
   {
      if (msg)
         this.confirm = msg;
      else
         this.confirm = "Are you sure you are ready to submit this information?";
   }

   this.addCustomFunction = function (functionName)
   {
      this.customFields.push(functionName);
      var func = "this." + functionName + " = " + functionName; // make user defined function
      eval(func);                                               // method of CheckSubmit() 
   }

   this.addError = function (errorCategory,error)
   {
      var str = this.errorMsgFmt;

      str = str.replace('X', errorCategory);
      str = str.replace('Y', error);

      this.errors += str + "\n";
   }

   this.clearErrors = function ()
   {
      this.errors = "";
   }

   this.getErrors = function ()
   {
      return this.errors;
   }

   this.setErrorFormat = function (str)
   {
      this.errorMsgFmt = str;
   }

   this.setErrorHeader = function (str)
   {
      this.errorHeader = str;
   }
}

function checkForErrors(field,fieldDesc,type,customMsg)
{
   if (this.form)
   {              
      var fieldNameEval = "document." + this.form + "." + field;
      fieldName         = eval(fieldNameEval);
   }
   else
      fieldName = document.getElementById(field);

   if (type)
      equalPosition = type.indexOf("="); 

   if (equalPosition > 0)
   {
      command      = type.substring(0,equalPosition);
      commandValue = type.substring(equalPosition+1);
   }
   else
      command = type;

   switch (command)
   {
      case "email":
         var emailError = 0;

         var locationOfAt        = fieldName.value.indexOf("@");
         var emailStringBeforeAt = fieldName.value.substring(0,locationOfAt);
         var emailStringAfterAt  = fieldName.value.substring(locationOfAt+1);

         if (emailStringBeforeAt.length < 1) 
            emailError = 1;
         else if (emailStringAfterAt.length < 3)
            emailError = 1;
         else if (fieldName.value.indexOf("@") == -1)
            emailError = 1;
         else if (emailStringAfterAt.indexOf(".") == -1)
            emailError = 1;

         if (emailError == 1)
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['email']);
         }
      break;

      case "max":
         if (fieldName.value.length > commandValue)
         {
            var error = "must be no more than " + commandValue + " characters";
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,error);
         }
      break; 

      case "min":
         if (fieldName.value.length < commandValue)
         {
            var error = "must be at least " + commandValue + " characters";

            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,error);
         }
      break; 

      case "equals":
         if (fieldName.value.length != commandValue)
         {
            var error = "must be " + commandValue + " characters";

            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,error);
         }
      break;

      case "gt":
         if (fieldName.value < commandValue)
         {
            var error = "value must be greater than " + commandValue + "\n";
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,error);
         }
      break; 

      case "lt":
         if (fieldName.value > commandValue)
         {
            var error = "value must be less than " + commandValue + "\n";
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,error);
         }
      break; 

      case "int":
      case "num":
         if ((fieldName.value.search("[^0-9]") >= 0)||(fieldName.value == ""))
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['int']);
         }
      break;

      case "dec":
         if ((fieldName.value.search("[^0-9.]") >= 0)||(fieldName.value == ""))
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['dec']);
         }
      break;

      case "alpha":
         if ((fieldName.value.search("[^A-Za-z]") >= 0)||(fieldName.value == ""))
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['alpha']);
         }
      break;

      case "alphanum":
         if ((fieldName.value.search("[^A-Za-z0-9]") >= 0)||(fieldName.value == ""))
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['alphanum']);
         }
      break;

      case "alphanumextended":
         if ((fieldName.value.search("[^-A-Za-z0-9._\\s]") >= 0)||(fieldName.value == ""))
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['alphanumextended']);
         }
      break; 

      case "radio":
         var check = 0;
         for (j=0; j<fieldName.length; j++)
         {
            if (fieldName[j].checked)
               check = 1;
         }

         if (!check)
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['radio']);
         }
      break;

      case "checkbox":
         if (!fieldName.checked)
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg); 
            else
               this.addError(fieldDesc,this.errorMsg['checkbox']); 
         }
      break;

      case "select":
         if (fieldName.value == commandValue)
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['select']);
         }
      break;

      case "req":
      default:
         if (fieldName.value == "")
         {
            if (customMsg)
               this.addError(fieldDesc,customMsg);
            else
               this.addError(fieldDesc,this.errorMsg['req']);
         }
      break;
   }
}

function checkSubmit(fieldObj)
{
   fieldObj.clearErrors();

   for (i=0; i<fieldObj.field.length; i++)
   {
      var command       = "";
      var commandValue  = 0;
      var equalPosition = 0;
      var fieldName;

      fieldObj.checkForErrors(fieldObj.field[i][0],fieldObj.field[i][1],fieldObj.field[i][2],fieldObj.field[i][3]);
   } 

   for (var i=0; i<fieldObj.customFields.length; i++)
   {
      var customCall = "fieldObj." + fieldObj.customFields[i] + "()";
      eval(customCall);
   }

   var errors = fieldObj.getErrors();
   if (errors != "")
   {
      alert (fieldObj.errorHeader + "\n\n" + errors);
      return false;
   }

   if (fieldObj.confirm)
   {
      var c = confirm(fieldObj.confirm);
      if (!c)
         return false;
   }

   return true;
}
