/*
#
# Kimus Web Solutions - Form Validation Functions
#
*/
var requiredFields = new Array();
var requiredFieldMessages = new Array();
var requiredMatchingDefaultOK = new Array();
var formatFields = new Array();
var formatSuccesses = new Array();
var formatMatchAllSuccesses = new Array();
var formatFailures = new Array();
var formatMatchAllFailures = new Array();
var formatMessages = new Array();

function evenOutArrays() {
   if (requiredFields.length < requiredFieldMessages.length) { requiredFieldMessages.length = requiredFields.length; }
   if (requiredFields.length < requiredMatchingDefaultOK.length) { requiredMatchingDefaultOK.length = requiredFields.length; }
   if (requiredFieldMessages.length < requiredFields.length) {
      var x = requiredFieldMessages.length;
      requiredFieldMessages.length = requiredFields.length;
      for (var i = x; i < requiredFields.length; i++) {
         requiredFieldMessages[i] = "Campo " + requiredFields[i] + " é de preenchimento obrigatório.";
      }
   }
   if (requiredMatchingDefaultOK.length < requiredFields.length) {
      var x = requiredMatchingDefaultOK.length;
      requiredMatchingDefaultOK.length = requiredFields.length;
      for (var i = x; i < requiredFields.length; i++) {
         requiredMatchingDefaultOK[i] = true;
      }
   }
   if (formatFields.length < formatSuccesses.length) { formatSuccesses.length = formatFields.length; }
   if (formatFields.length < formatMatchAllSuccesses.length) { formatMatchAllSuccesses.length = formatFields.length; }
   if (formatFields.length < formatFailures.length) { formatFailures.length = formatFields.length; }
   if (formatFields.length < formatMatchAllFailures.length) { formatMatchAllFailures.length = formatFields.length; }
   if (formatFields.length < formatMessages.length) { formatMessages.length = formatFields.length; }
   if (formatSuccesses.length < formatFields.length) {
      var x = formatSuccesses.length;
      formatSuccesses.length = formatFields.length;
      for (var i = x; i < requiredFields.length; i++) {
         formatSuccesses[i] = ".*";
      }
   }
   if (formatMatchAllSuccesses.length < formatFields.length) {
      var x = formatMatchAllSuccesses.length;
      formatMatchAllSuccesses.length = formatFields.length;
      for (var i = x; i < requiredFields.length; i++) {
         formatMatchAllSuccesses[i] = false;
      }
   }
   if (formatFailures.length < formatFields.length) {
      var x = formatFailures.length;
      formatFailures.length = formatFields.length;
      for (var i = x; i < requiredFields.length; i++) {
         formatFailures[i] = "^[\b]";
      }
   }
   if (formatMatchAllFailures.length < formatFields.length) {
      var x = formatMatchAllFailures.length;
      formatMatchAllFailures.length = formatFields.length;
      for (var i = x; i < requiredFields.length; i++) {
         formatMatchAllFailures[i] = false;
      }
   }
   if (formatMessages.length < formatFields.length) {
      var x = formatMessages.length;
      formatMessages.length = formatFields.length;
      for (var i = x; i < formatFields.length; i++) {
         formatMessages[i] = "Campo " + formatFields[i] + " não está formatado correctamente.";
      }
   }
} // Ends the "evenOutArrays" function

function areRequiredFieldsPresent(form) {
   for (var i=0; i < requiredFields.length; i++) {
      if (! requiredFields[i]) continue;
      eval("var field = form." + requiredFields[i]);
      if (field) {
         // NOTE: THE TRIM JAVASCRIPT FUNCTION MUST BE INCLUDED
         // NOTE: THE GETFIELDVALUE JAVASCRIPT FUNCTION MUST BE INCLUDED
         if (trim(getFieldValue(field)) == "") {
            alert(requiredFieldMessages[i]);
            field.focus();
            return false; // Return as soon as a failure is detected
         } // Ends the check to see if the field value is blank
         if (field.defaultValue) {
            if ( (getFieldValue(field) == field.defaultValue) && ( !requiredMatchingDefaultOK[i]) ) {
               alert(requiredFieldMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
         } // Ends the check to see if the field has a default value
      } // Ends the check to make sure the field exists
   } // Move on to the next field
   return true;
} // Ends the "areRequiredFieldsPresent" function

// ###

function areSuccessesPresent(form) {
   for (var i=0; i < formatFields.length; i++) {
      if (! formatFields[i]) continue;
      eval("var field = form." + formatFields[i]);
      if (field) { // Make sure the field is found
         // NOTE: THE TRIM JAVASCRIPT FUNCTION MUST BE INCLUDED
         // NOTE: THE GETFIELDVALUE JAVASCRIPT FUNCTION MUST BE INCLUDED
         var value = trim(getFieldValue(field));
         var successes = formatSuccesses[i];
         if (value.length > 0 && typeof(successes[0]) == "object") { // If it's an array
            // Go through all the expressions and see if they all match and see if any match
            var allMatched = true;
            var anyMatched = false;
            for (var x=0; x < successes.length; x++) {
               var exp = new RegExp(successes[x], "gi");
               var check = exp.test(value);
               if ( !check ) { allMatched = false; }
               if ( check ) { anyMatched = true; }
            }
            // If the developer said they all have to match and they didn't all match, return an error
            if (formatMatchAllSuccesses[i] && !allMatched) {
               alert(formatMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
            // If none of them matched, it doesn't matter what the developer said, return an error
            if ( !anyMatched ) {
               alert(formatMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
         } else if (value.length > 0) { // The formatSuccesses entry is a single value, not an array
            var exp = new RegExp(successes, "gi");
            var check = exp.test(value);
            if ( !check ) { // If that single expression didn't match the criteria (thus, a failure)
               alert(formatMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
         } // Ends the check to see if the format successes entry is an array or a single value
      } // Ends the check to see if the field exists
   } // Move on to the next field
   return true; // If we got this far, everything worked
} // Ends the "areSuccessesPresent" function

function areFailuresPresent(form) {
   for (var i=0; i < formatFields.length; i++) {
   	  if (! formatFields[i]) continue;
      eval("var field = form." + formatFields[i]);
      if (field) { // Make sure the field is found
         // NOTE: THE TRIM JAVASCRIPT FUNCTION MUST BE INCLUDED
         // NOTE: THE GETFIELDVALUE JAVASCRIPT FUNCTION MUST BE INCLUDED
         var value = trim(getFieldValue(field));
         var failures = formatFailures[i];
         if (typeof(failures[0]) == "object") { // If it's an array
            // Go through the expressions and see if any match and see if they all match
            var allMatched = true;
            var anyMatched = false;
            for (var x=0; x < failures.length; x++) {
               var exp = new RegExp(failures[x], "gi");
               var check = exp.test(value);
               if ( !check ) { allMatched = false; }
               if ( check ) { anyMatched = true; }
            }
            // If the developer says they all have to match to cause a failure, then cause a failure
            if (formatMatchAllFailures[i] && allMatched) {
               alert(formatMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
            // If the developer says that any match causes a failure, then cause a failure
            if (!formatMatchAllFailures[i] && anyMatched ) {
               alert(formatMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
         } else { // The formatSuccesses entry is a single value, not an array
            var exp = new RegExp(failures, "gi");
            var check = exp.test(value);
            if ( check ) { // If that single expression matched the criteria (thus, a failure)
               alert(formatMessages[i]);
               field.focus();
               return false; // Return as soon as a failure is detected
            }
         } // Ends the check to see if the format successes entry is an array or a single value
      } // Ends the check to see if the field exists
   } // Move on to the next field
   return true; // If we made it this far, everything passed
} // Ends the "areFailuresPresent" function

function isFormFormattedCorrectly(form) {
   evenOutArrays();
   if ( !(areRequiredFieldsPresent(form)) ) { return false; } // Return as soon as a failure is detected
   if ( !(areSuccessesPresent(form)) ) { return false; }
   if ( !(areFailuresPresent(form)) ) { return false; }
   return true;
} // Ends the "isFormFormattedCorrectly" function

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }
   
   return "";
}
