﻿/*
   Dependencies:
   jquery-1.4.2.js
   jquery.maskedinput-1.2.2.js
   jquery.validate.js
*/
jQuery(function ($) {
    $(".date").mask("99/99/9999");
    $(".phone").mask("(999) 999-9999");

    $.validator.addMethod(
        "regex",
        function (value, element, regexp) {
            var check = false;
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        'Please correct the information provided.'

    );

    $("form").each(function () {
        var form = $(this);
        var validator = form.validate({
            errorClass: "invalid"
        });

        if (form.has(".multi_step_form").length > 0) {
            currStep = 1;

            form.find(".step .required")
                .removeClass("required")
                .addClass("requiredOFF");

            form.find(":submit, input:image").bind("click", function () {
                form.find(".step_" + currStep + " .requiredOFF")
                    .removeClass("requiredOFF")
                    .addClass("required");

                if (validator.form()) {
                    if ($(".step_" + (currStep + 1)).length > 0) {
                        $(".step_" + currStep).hide();
                        $(".step_" + (currStep + 1)).show();

                        currStep = currStep + 1;

                        return false;
                    }
                } else {
                    form.find('input.invalid:first').focus();
                    return false;
                }
            });
        }
    });

    $('[data-regex]').each(function () {
        $(this).rules('add', { regex: $(this).attr('data-regex') })
    });
});
