﻿(function($) {
    $.validators = new Object();
    $.extend($.validators, {
        name: /^[\w\s,.\-]{2,}$/gi,
        address: /^[\w\d\b\s,\.\-]{5,}$/g,
        phone1: /^\d{3}$/,
        phone2: /^\d{3}$/,
        phone3: /^\d{4}$/,
        email: /^[\w._%+-]{2,}@[\w.-]{2,}\.[a-z]{2,5}$/gi,
        guardianname: /^[\w\s,.\-]{2,}$/i,
        guardianphone1: /^\d{3}$/,
        guardianphone2: /^\d{3}$/,
        guardianphone3: /^\d{4}$/,
        dobmonth: /^(0?[1-9]|1[0-2])$/,
        dobday: /^(0?[1-9]|1[0-9]|2[0-9]|3[0-1])$/,
        dobyear: /^(19[3-9][0-9]|200[0-9])$/,
        recentphoto: /\.(jpg|gif|png)$/gi
    });

    $.extend(Array.prototype, {
        makeUnique: function() {
            var array = [];

            for (var i = 0; i < this.length; i++) {
                if ($.inArray(this[i], array) < 0)
                    array.push(this[i]);
            }

            return array;
        },
        showErrors: function() {
            var errorMessage = "Please correct the following:\n\n";
            errorMessage += this.makeUnique().join('\n');
            alert(errorMessage);
        }
    });

    var isValid = function(elmId) {
        var regex = $.validators[elmId];
        //alert(regex);
        if ($('#' + elmId).val().match(regex))
            return true;

        return false;
    }

    $(function() {
        //just to be nice (remove non-numerics)
        $("#phone1, #phone2, #phone3, #guardianphone1, #guardianphone2, #guardianphone3, #dobmonth, #dobday, #dobyear").blur(function(e) {
            $(this).val($(this).val().replace(/[^\d]/g, ''));
        });

        //main validation
        $("#submit-form").click(function(e) {
            e.preventDefault();

            var errors = [];

            //remove previous errors
            $('.validation-error').removeClass('validation-error');

            //basic validation
            if (!isValid('name')) errors.push({ id: "#name", error: "Please enter your name" });
            if (!isValid('address')) errors.push({ id: "#address", error: "Please enter your address" });
            if (!isValid('phone1')) errors.push({ id: "#phone1", error: "Please enter your phone number" });
            if (!isValid('phone2')) errors.push({ id: "#phone2", error: "Please enter your phone number" });
            if (!isValid('phone3')) errors.push({ id: "#phone3", error: "Please enter your phone number" });
            if (!isValid('email')) errors.push({ id: "#email", error: "Please enter your email address" });
            if (!isValid('guardianname')) errors.push({ id: "#guardianname", error: "Please enter your parent/guardian's name" });
            if (!isValid('guardianphone1')) errors.push({ id: "#guardianphone1", error: "Please enter your parent/guardian's phone number" });
            if (!isValid('guardianphone2')) errors.push({ id: "#guardianphone2", error: "Please enter your parent/guardian's phone number" });
            if (!isValid('guardianphone3')) errors.push({ id: "#guardianphone3", error: "Please enter your parent/guardian's phone number" });
            if (!isValid('dobmonth')) errors.push({ id: "#dobmonth", error: "Please enter your birthday" });
            if (!isValid('dobday')) errors.push({ id: "#dobday", error: "Please enter your birthday" });
            if (!isValid('dobyear')) errors.push({ id: "#dobyear", error: "Please enter your birthday" });
            if (!isValid('recentphoto')) errors.push({ id: "#recentphoto", error: "Please upload a recent photo of your self (jpg, gif or png)" });

            if ($("#recordingconsent:checked").length <= 0) errors.push({ id: "#recordingconsent", error: "You must consent to the filming and recording of your participation in the Program" });
            if ($("#guardianconsent:checked").length <= 0) errors.push({ id: "#guardianconsent", error: "Your parent/guardian must agree to the terms and conditions" });
            
            if (errors.length > 0) {
                var messages = [];
                for (var i = 0; i < errors.length; i++) {
                    $(errors[i].id).addClass('validation-error');
                    messages.push(" - " + errors[i].error);
                }

                $(errors[0].id).focus();

                messages.showErrors();
                return;
            }

            //submit the form
            $('#name').parents('form').submit();
        });
    });
})(jQuery);