/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  checkSubmit.js                                                                               *
 * ----------------                                                                              *
 * AUTHOR: Tom Peters                                                                            *
 * EMAIL: tom@weters.com                                                                         *
 * LAST MODIFIED: 01/07/2005                                                                     *
 * VERSION: 1.2.1                                                                                *
 * DESCRIPTION: This script is designed to easily validate forms                                 *
 *                                                                                               *
 * UPDATES                                                                                       *
 *                                                                                               *
 *   1.2.1 - Changed the script so the user does not have to specify an onSubmit in the form     *
 *           field.                                                                              *
 *         - Added a way to specify the error message per form field.                            *
 *         - Cleaned up the script.                                                              *
 *         - Moved examples and documentation to their own pages.  Created EULA.                 *
 *   1.1.2 - Added the function changeErrorMsg to allow custom error messages to display for     *
 *           the various types.                                                                  *
 *   1.1.1 - Changed name of class from validateValues to CheckSubmit to reflect name of file.   *
 *         - Changed custom fields to better incorporate objects.                                *
 *         - Changed the way errors are called.                                                  *
 *         - Added a new function 'checkForErrors' to enable error checking with custom fields.  *
 *   1.0.5 - Added custom fields.                                                                *
 *   1.0.4 - Added 'select' type.                                                                *
 *   1.0.3 - Added 'alphanumextend' type.                                                        *
 *   1.0.2 - Added 'radio' and 'checkbox' types.                                                 *
 *   1.0.1 - Initial version.                                                                    *
 *                                                                                               *
 * END-USER LICENSE AGREEMENT                                                                    *
 *                                                                                               *
 *    Please see the EULA.html file that came bundled in this package.                           *
 *                                                                                               *
 * HOW TO USE                                                                                    *
 *                                                                                               *
 *    Please see the documentation.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.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));
   }

   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.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)
   {
      this.errors += "- " + errorCategory + ": " + error + "\n";
   }

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

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

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 "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 ("There are errors with the following fields:\n\n" + errors);
      return false;
   }

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

   return true;
}
