// 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/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-phone required"></td>
    ...
    <td><input type="text" name="phone" 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.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
//
//
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;
    }
  }],

  ['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;
    }
  }],

  // 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;
    }
  }]

]);


