﻿    
    $(window).ready(function(){ WireEvents() });
    
    function WireEvents() 
    {
        PageEvents();
    }
    
    //  ====================================================================================================
    //  Preload Images
    //  ====================================================================================================

        jQuery.preloadImages = function(arr) {
            for (var i = 0; i < arr.length; i++) {
                jQuery("<img>").attr("src", arr[i]);
            }
        }
        
    //  ====================================================================================================
    //  Form Validation Functions
    //  ====================================================================================================
        function checkRequiredFields(myPops)
    {
        var returnValue = true;
        
        // check required fields
        $(myPops).parent().find(".req-field").each(function() {
            if ($(this).val().trim() == "") {
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
            }
        });

        // check for a drop down selection
        $(myPops).parent().find(".req-selection").each(function() {
            if ($(this).val().trim() == "" || $(this).val() == "0") {
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
            }
        });


        // check required fields
        $(myPops).parent().find(".req-month").each(function() {
            if (($(this).val()) == "" || ($(myPops).parent().find(".req-year").val() == "")) {
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
            }
        });

        $(myPops).parent().find(".req-email").each(function() {
            
            var isEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(this).val());

            if (!isEmail || $(this).val() == "") {
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
            }
        });
        
        // check zipcode
        $(myPops).parent().find(".req-zipcode").each( function() {
            if ( $(this).val() == "" ) {
                $(this).parent().find(".required").text("please enter zip/postal code.");
                $(this).parent().find(".required").css("display", "block"); 
                $(this).parent().addClass("failed");
                returnValue = false; 
            }
            else {
                    $(this).parent().find(".required").css("display","none"); 
                    $(this).parent().removeClass("failed"); 
            }

        });

        // check cvv
        $(myPops).parent().find(".req-cvv").each( function() {

            var isNumber = /^\d+$/.test($(this).val());

            if (!isNumber || $(this).val().length < 3 || $(this).val().length > 4) {
                $(this).parent().find(".required").css("display","block"); 
                $(this).parent().addClass("failed");
                returnValue = false; 
            }
            else { 
                $(this).parent().find(".required").css("display","none"); 
                $(this).parent().removeClass("failed"); 
            }
        });

        // check credit card
        $(myPops).parent().find(".req-creditcard").each(function() {

            var passLuhn = true;
            var value = $(this).val();

            // accept only digits and dashes
            if (/[^0-9-]+/.test(value)) {
                passLuhn = false;
            }
            else {
                var nCheck = 0,
			                nDigit = 0,
			                bEven = false;

                value = value.replace(/\D/g, "");

                for (n = value.length - 1; n >= 0; n--) {
                    var cDigit = value.charAt(n);
                    var nDigit = parseInt(cDigit, 10);
                    if (bEven) {
                        if ((nDigit *= 2) > 9)
                            nDigit -= 9;
                    }
                    nCheck += nDigit;
                    bEven = !bEven;
                }

                passLuhn = ((nCheck % 10) == 0) ? true : false;
            }

            if (!passLuhn || value.length < 1) {
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
            }
        });

        // check passwords
        $(myPops).parent().find(".req-password1").each(function() {
            if ($(this).val().length < 8) {
                $(this).parent().find(".required").css("display", "block").text("your password must be at least 8 characters.");
                $(this).parent().addClass("failed");
                $(myPops).parent().find(".req-password2").parent().find(".required").css("display", "none");
                $(myPops).parent().find(".req-password2").parent().removeClass("failed");
                returnValue = false;
            }
            else if ($(this).val().length >= 8 && $(myPops).parent().find(".req-password2").val().length == 0)
            {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
                $(myPops).parent().find(".req-password2").parent().find(".required").css("display", "block");
                $(myPops).parent().find(".req-password2").parent().addClass("failed")
                returnValue = false;
            }
            else if ($(this).val() != $(myPops).parent().find(".req-password2").val())
            {
                $(this).parent().find(".required").css("display", "block").text("your passwords don't match.");
                $(this).parent().addClass("failed");
                $(myPops).parent().find(".req-password2").parent().find(".required").css("display", "block").text("your passwords don't match.");
                $(myPops).parent().find(".req-password2").parent().addClass("failed")
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
                $(myPops).parent().find(".req-password2").parent().find(".required").css("display", "none");
                $(myPops).parent().find(".req-password2").parent().removeClass("failed");
            }
        });

        //  =======================================================
        //  === VAL TYPE:  Number field Greater than 5
        //  === REQUIRMENTS: not null, valid number, greater than 5
        //  === SUPPORTED CLASSES: "req-number-5"
        //  =======================================================

        $(myPops).parent().find(".req-number-5").each(function () {

			var isNumber = !isNaN(parseInt($(this).val()));

			var isWholeNumber = /^\d+$/.test($(this).val());

			if (!isWholeNumber || !isNumber || $(this).val() < 5) {
				$(this).parent().find(".required").css("display", "block");
				$(this).parent().addClass("failed");
				returnValue = false;
			}
			else {
				$(this).parent().find(".required").css("display", "none");
				$(this).parent().removeClass("failed");
			}
		});

        // check for spaces
        $(myPops).parent().find(".no-spaces").each(function() {
            if ($(this).val().indexOf(" ") != -1) {
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else {
                $(this).parent().find(".required").css("display", "none");
                $(this).parent().removeClass("failed");
            }
        });

        // fix to show/hie resiter button on registration page
        if (($(".newadd-button").length > 0) && returnValue)
        {
            $(".newadd-button").css("display", "none");
            $(".submit-label").css("display", "inline");
        }

        // We have failed validation: scroll to the first field that failed and give it focus
        if ( !returnValue ) {

            // Scroll to the first failed field 
			$.scrollTo( $(".failed:first"), 1000 );
			// Give that field focus
			$(".failed:first input").focus();
			// Hide the .net error response label
			$(".output-error").css("display","none");
        }
        
        return returnValue;
       
    }
    
    
    //  ====================================================================================================
    //  Button Functions
    //  ====================================================================================================

    //  ========= Login Button ============
        function preloadLoginButton() {
            $.preloadImages(['/Images/Buttons/button-login_f2.jpg']);
        }
        function bindLoginButton() {
            $(".login-button").hover(
                    function() { $(this).attr("src", "/Images/Buttons/button-login_f2.jpg"); },
                    function() { $(this).attr("src", "/Images/Buttons/button-login.jpg"); }
                );
        }
        function triggerLoginClick() {
            $(".login-button").click(function() { $("#boundPageEvents_UpdatePanels").attr("title", "NO"); });
        }
        
    //  ============ Register Button ============
        function preloadRegisterButton() {
            $.preloadImages(['/Images/Buttons/button-register_f2.jpg']);
        }
        function bindRegisterButton() {
            $(".register-button").hover(
                    function() { $(this).attr("src", "/Images/Buttons/button-register_f2.jpg"); },
                    function() { $(this).attr("src", "/Images/Buttons/button-register.jpg"); }
                );
        }
        function triggerRegisterClick() {
            $(".register-button").click(function() { $("#boundPageEvents_UpdatePanels").attr("title", "NO"); });
        }
        
        //  ============ Back To Dashboard Button ============
        function preloadDashboardButton() {
            $.preloadImages(['/Images/Buttons/button-backToDashboard-Acct_f2.jpg']);
        }
        function bindDashboardButton() {
            $(".dashboard-button img").hover(
                    function() { $(this).attr("src", "/Images/Buttons/button-backToDashboard-Acct_f2.jpg"); },
                    function() { $(this).attr("src", "/Images/Buttons/button-backToDashboard-Acct.jpg"); }
                );
        }
        function triggerDashboardClick() {
            $(".dashboard-button img").click(function() { $("#boundPageEvents_UpdatePanels").attr("title", "NO"); });
        }

