﻿function PositionContentShadow() {
    var $upperRight = $('#content-upper-right');
    var $upperLeft = $('#content-upper-left');
    var $lowerLeft = $('#content-lower-left');
    var $lowerRight = $('#content-lower-right');
    var $top = $('#content-top');
    var $bottom = $('#content-bottom');
    var $left = $('#content-left');
    var $right = $('#content-right');
    var $content = $('#content');

    $upperRight.css({
        position: 'absolute',
        left: $content.offset().left + $content.innerWidth() - 17 + 'px',
        top: $content.offset().top - 5 + 'px',
        display: 'block'
    });

    $upperLeft.css({
        position: 'absolute',
        left: $content.offset().left - 12 + 'px',
        top: $content.offset().top - 5 + 'px',
        display: 'block'
    });

    $lowerLeft.css({
        position: 'absolute',
        left: $content.offset().left - 12 + 'px',
        top: $content.offset().top + $content.innerHeight() - 17 + 'px',
        display: 'block'
    });

    $lowerRight.css({
        position: 'absolute',
        left: $content.offset().left + $content.innerWidth() - 17 + 'px',
        top: $content.offset().top + $content.innerHeight() - 17 + 'px',
        display: 'block'
    });

    $top.css({
        position: 'absolute',
        left: $content.offset().left + $upperRight.innerWidth() - 14 + 'px',
        top: $content.offset().top - 5 + 'px',
        width: $content.innerWidth() - 34 + 'px',
        display: 'block'
    });

    $bottom.css({
        position: 'absolute',
        left: $content.offset().left + $upperRight.innerWidth() - 14 + 'px',
        top: $content.offset().top + $content.innerHeight() + 'px',
        width: $content.innerWidth() - 34 + 'px',
        display: 'block'
    });

    $left.css({
        position: 'absolute',
        left: $content.offset().left - 12 + 'px',
        top: $content.offset().top + 15 + 'px',
        height: $content.innerHeight() - 32 + 'px',
        display: 'block'
    });

    $right.css({
        position: 'absolute',
        left: $content.offset().left + $content.innerWidth() + 'px',
        top: $content.offset().top + 18 + 'px',
        height: $content.innerHeight() - 35 + 'px',
        display: 'block'
    });
}

function HideShadows() {
    $('#content-upper-right').css('display', 'none');
    $('#content-upper-left').css('display', 'none');
    $('#content-lower-left').css('display', 'none');
    $('#content-lower-right').css('display', 'none');
    $('#content-top').css('display', 'none');
    $('#content-bottom').css('display', 'none');
    $('#content-left').css('display', 'none');
    $('#content-right').css('display', 'none');
}

function CenterMiddleAbsoluteElement(elemID) {
    $elem = $('#' + elemID);

    $elem.css({
        'position' : 'absolute',
        'left' : '50%',
        'top' : '50%',
        'margin-left' : '-' + ($elem.innerWidth() / 2) + 'px',
        'margin-top' : '-' + ($elem.innerHeight() / 2) + 'px'
    });
}

function CenterElement(elemID)
{
    $('#' + elemID).css({
        'position' : 'relative',
        'left' : '50%',
        'margin-left' : '-' + ($('#' + elemID).width() / 2) + 'px'
    });
}

function EnableElement(call)
{
    $(call).removeAttr('disabled');
}

function DisableElement(call)
{
    $(call).attr('disabled', 'disabled');
}

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 files
    $(myPops).parent().find('.req-file').each(function () {
        if ($(this).val().trim().length == 0) {
            $(this).parent().find(".required").css("display", "block");
            $(this).parent().addClass("failed");
            returnValue = false;
        }   //if
        else {
            $(this).parent().find(".required").css("display", "none");
            $(this).parent().removeClass("failed");
        }   //else
    });

    // 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;
}

function checkRequiredField(elem) {
    var returnValue = true;

    if (elem.val().trim() == "") {
        returnValue = false;
    }

    return returnValue;
}

function checkRequiredEmail(elem) {
    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(elem.val());
    var returnValue = true;

    if (!isEmail || elem.val() == "") {
        returnValue = false;
    }

    return returnValue;
}

function checkRequiredPhone(elem) {
    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(elem.val());
    var returnValue = true;

    if (!isPhone || elem.val() == "") {
        returnValue = false;
    }

    return returnValue;
}

function checkRequiredSelection(elem) {
    if (elem.val().trim() == "" || elem.val() == "-Select-") {
        return false;
    }

    return true;
}

function resetAllInputs(elem) {
    $(elem).parent().find("input").each(function () {
        var type = $(this).attr('type');

        if (type == 'text') {
            $(this).val('');
        }
        else if (type == 'checkbox') {
            $(this).attr('checked', false);
        }
    });

    $(elem).parent().find("textarea").each(function () {
        $(this).val('');
    });

    $(elem).parent().find("select").each(function () {
        var index = 0;
        $(this).find('option').each(function () {
            if (index == 0) {
                $(this).attr('selected', 'selected');
            } else {
                $(this).removeAttr('selected');
            }

            index++;
        });
    });
}

String.prototype.trim = function()
{
    return jQuery.trim(this);
}

function ShowMemberProfile(id)
{
    var currDate = new Date();
    $.showModal({
        url: '/Presents/Overlays/MemberProfile.aspx',
        width: 400,
        parameters: { MEMID: id, SE: true, TS: currDate.getTime() }
    })
}

function doPostBackAsync(eventName, eventArgs) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
        prm._asyncPostBackControlIDs.push(eventName);
    }

    if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
        prm._asyncPostBackControlClientIDs.push(eventName);
    }

    __doPostBack(eventName, eventArgs);
}
