﻿$(document).ready(function () {
    initMenu();
});

function CenterElementInParent(elem) {
    elem.css('margin-left', (elem.parent().width() / 2) - (elem.width() / 2));
}

(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

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 {
            if ($(this).parent().find(".zip-validator").css("visibility") == "visible")
            {
                $(this).parent().find(".required").text("Please enter zip/postal code in the correct format.");
                $(this).parent().find(".required").css("display", "block");
                $(this).parent().find(".zip-validator").css("display", "none")
                $(this).parent().addClass("failed");
                returnValue = false;
            }
            else { 
                $(this).parent().find(".required").css("display","none"); 
                $(this).parent().removeClass("failed"); 
            }
        }

    });
    
    // check or statements
    var orValid = false;
    $(myPops).parent().find('.req-or').each(function() {
        if ($(this).val().trim() != '')
        {
            orValid = true;
        }   //if
    });
    
    if (!orValid)
    {
        $(myPops).parent().find('.req-or').each(function() {
            $(this).parent().find('.required').css('display', 'block');
            $(this).parent().addClass('failed');
            returnValue = false;
        });
    }   //if
    else
    {
        $(myPops).parent().find('.req-or').each(function() {
            $(this).parent().find('.required').css('display', 'none');
            $(this).parent().removeClass('failed');
            returnValue = true;
        });
    }   //else

    // check phone number
//        $(myPops).parent().find(".req-phone").each(function() {
//            var isPhone = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/i.test($(this).val());

//            if (!isPhone || $(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 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");
        }
    });

    // 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");
    }
   
    return returnValue;
}
