/**
 * This file contains generic validation components for use with the jQuery Validation plugin.
 *
 * Any of our sites should be able to use these components.
 */
$(document).ready(function(){

    /**
     * Simple validator that checks if the value is a valid United States state abbreviation.
     *
     * For now this only checks that the value is two letter characters. A better implementation might
     * check a table of state abbreviations.
     */
    $.validator.addMethod('usStateCode', function(value, element) {
        return this.optional(element) || /^[A-Za-z]{2}$/.test(value);
    }, "Invalid state abbreviation");

    /**
     * This is a very simplistic phone validator. It's basically just a whitelist of characters allowed
     * in a phone number with no thought to formatting. I made it simple because I was lazy. Any place that
     * uses it should also be able to use a more robust version of it as well.
     */
    $.validator.addMethod('phone', function(value, element) {
        return this.optional(element) || /^[\(\)\-\s\d]+$/.test(value);
    }, "Invalid phone number");

    /**
     * Very simple zip code validator. Only works with US Zip Codes. Allows 5 digits w/ an optional zip4.
     */
    $.validator.addMethod('zip', function(value, element) {
        return this.optional(element) || /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(value);
    }, "Invalid zip copde");
    
});


