// This file simply adds more form validators to the mootools Form.Validator class
//
// See the mootools documentation for usage of the Form.Validator class and how
// to specify the type of form validation for a form field
//
// Usage:
/*
    <script type="text/javascript" src="gtLib/mootools-1.2.5-core.js"></script>
    <script type="text/javascript" src="gtLib/mootools-1.2.5.1-more.js"></script>

    <script type="text/javascript" src="gtLib/GtFormValidators/GtFormValidators-en-US.js"></script>
    <script type="text/javascript" src="gtLib/GtFormValidators/GtFormValidators.js"></script>
    <link href="gtLib/GtFormValidators/GtFormValidators.css" rel="stylesheet" type="text/css">

    <!-- if an hourglass icon is desirable due to an emailing delay -->
    <script type="text/javascript" src="gtLib/GtHourglass/GtHourglass.js"></script>
    <link href="gtLib/GtHourglass/GtHourglass.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
      // if an hourglass icon is desirable due to an emailing delay
      var hourglass;

      // initialize form validation
      window.addEvent("domready",function() {

        // if an hourglass icon is desirable due to an emailing delay
        hourglass = new GtHourglass({
          imageName : "bars",
          message : "Sending email ..."
        });

        new Form.Validator.Inline($("myForm"), {
          serial : false,

          // if an hourglass icon is desirable due to an emailing delay
          onFormValidate : function(valid,element,event) {
            // only show this if valid, it will be erased by the page reload
            if ( valid ) {
              hourglass.show();
            } // endif
          }
        });
      });
    </script>

    <!-- standard mootools form validation type -->
    <td><input type="text" name="email" class="validate-email required"></td>

    <!-- my custom validation types -->
    <td><input type="text" name="first" class="lvalidate-name required"></td>
    ...
    <td><input type="text" name="phone" class="lvalidate-internationalPhone required"></td>
    ...
    <td><input type="text" name="postalCode" class="lvalidate-postalCode required"></td>

*/
// NOTE: it is essential that GtFormValidatos-en-US.js is loaded before this
//       file
//
// NOTE: When creating new form validators, it is essential that an empty field
//       is treated as valid so that it can only fail when empty if "required" is included
//
// $Log: GtFormValidators.js $
// Revision 1.8  2011-11-23 19:03:59-04  Battersby
// - added lvalidate-alphaNum
//
// Revision 1.7  2011-11-03 18:21:36-04  Battersby
// - international phone verification was causing non-required fields
//   to act as required field and therefore fail verification
//
// Revision 1.4  2011-05-07 15:40:37-04  Battersby
// - added usage documentation
//
// Revision 1.3  2011-02-24 14:37:44-04  Battersby
// - added postal code
//
// Revision 1.2  2010-10-26 11:33:29-04  Battersby
// - corrected usage documentation
//
// Revision 1.1  2010-10-25 11:40:31-04  Battersby
// Initial revision
//
//

//************************************************************************
// Name   : GTFORMVALIDATORS_countDigits
//  This counts the number of digits in the given string excluding characters,
//  spaces etc.
//
//  "number" - a number whose digits are to be counted
//
// Returns : (int) number of digits in "number"
//*************************************************************************/
function GTFORMVALIDATORS_countDigits(number) {
  var matchesList = new Array();

  /* count the digits */
  matchesList = number.match(/\d/g);

  /* if there are no digits at all */
  if ( matchesList === null ) {
    return 0;

  /* return the length of the array returned by the match() method */
  /* which corresponds to the number of digits in the number */
  } else {
    return matchesList.length;
  } /* endif */

}  /* end of GTFORMVALIDATORS_countDigits */


Form.Validator.addAllThese([
  // characters that are valid for a name including " " and "-"
  ['lvalidate-name', {
    errorMsg: MooTools.lang.get("GtFormValidators","lvalidate-name"),
    test: function(field) {
      if (Form.Validator.getValidator('IsEmpty').test(field) ||
        field.value.match(/^[a-z \-]+$/i) ) {
        return true;
      }
      return false;
    }
  }],

  // alpha numeric characters including " " and "-"
  ['lvalidate-alphaNum', {
    errorMsg: MooTools.lang.get("GtFormValidators","lvalidate-alphaNum"),
    test: function(field) {
      if (Form.Validator.getValidator('IsEmpty').test(field) ||
        field.value.match(/^[0-9a-z \\#,\/\.\-]+$/i) ) {
        return true;
      }
      return false;
    }
  }],

  ['lvalidate-phone', {
    errorMsg: MooTools.lang.get("GtFormValidators","lvalidate-phone"),
    test: function(field) {
      // if the phone number is not in this format
      // 1-111-222-3333
      // or 1 111 222 3333
      // or 111-222-3333
      // or 111 222 3333
      // or 111 222 3333 x123
      // or 111 222 3333 ext 123 etc
      if (Form.Validator.getValidator('IsEmpty').test(field) ||
        field.value.match(/^(1[- \.])?\(?\d{3}\)?[- \.]\d{3}[- \.]\d{4}(\s+([xX ]|(ext)|(EXT)|(ex)|(EX))?\s*\d+)?$/) ) {
        return true;
      }
      return false;
    }
  }],

  ['lvalidate-internationalPhone', {
    errorMsg: MooTools.lang.get("GtFormValidators","lvalidate-internationalPhone"),
    test: function(field) {
      /* if the phone number contains invalid characters */
      /* (may contain an extension) */
      if (Form.Validator.getValidator('IsEmpty').test(field) ||
        field.value.match(/^\+?[0-9 ()-\~]+[0-9](\s*([xX ]|(ext)|(EXT)|(ex)|(EX))\s*\d+)?$/) ) {

        // if there are enough digits or the field is empty and not a required field
        if ((field.value == "") || GTFORMVALIDATORS_countDigits(field.value) >= 10) {
          return true;

        // if there are not enough digits
        } else {
          return false;
        } // endif
      }
      return false; // incorrect phone format
    }
  }],

  // characters that are valid for a postal code
  ['lvalidate-postalCode', {
    errorMsg: MooTools.lang.get("GtFormValidators","lvalidate-postalCode"),
    test: function(field) {
      if (Form.Validator.getValidator('IsEmpty').test(field) ||
        field.value.match(/^[a-zA-Z]\d[a-zA-Z]\s*\d[a-zA-z]\d$/) ) {
        return true;
      }
      return false;
    }
  }]

]);


