﻿$(document).ready(function () {

    $("#dialog-reset-password").hide();
    $("#dialog-popup").hide();

    $('#txtPayGatePasswordClear').show();
    $('#txtPayGatePassword').hide();

    $('.breadCrumb-links').children().addClass('lnsLNK');

    var topMessageText = $("#top-message-text").text();

    if (topMessageText.length == 0)
        $("#top-message").hide();

    $('#btnPayGateSubscribeNow').click(function () {
        location.replace("CartSignIn.aspx?ProductID=10386");
        return false;
    });

    $('#btnPayGateLeanMore').click(function () {
        location.replace("Subscribe.aspx");
        return false;
    });

    $('#ChangeActivationEmail').click(function () {

        $('#dialog-popup-message').hide();

        $.fn.OpenBlockUI();

        //var formSubmitted = false;

        var iframe = $('#iFramePopUp');
        iframe.attr('src', 'ChangeNonVerifiedEmail.aspx');
        iframe.show();

        iframe.load(function () {
            setTimeout(function () {
                $.unblockUI({
                    onUnblock: function () {
                        $("#dialog-popup").dialog({
                            autoOpen: false,
                            resizable: false,
                            height: 400,
                            width: 600,
                            closeOnEscape: true,
                            modal: true,
                            buttons: {
                                "button1": {
                                    text: "Change",
                                    id: "jquery-ui-button1",
                                    click: function () {
                                        var src = $('#iFramePopUp').attr('src');
                                        var btnSubmit;

                                        if (src == 'ChangeNonVerifiedEmail.aspx') {
                                            btnSubmit = $("#iFramePopUp").contents().find('input[name="btnSubmit"]');
                                            btnSubmit.trigger("click");
                                        }
                                        else {
                                            $(this).dialog("close");
                                        }
                                    }
                                }
                            },
                            close: function (event, ui) { $.unblockUI(); }
                        });

                        $("#dialog-popup").dialog("open");
                        $(".ui-dialog").attr('id', 'profile-db');
                        $.unblockUI();
                    }
                });
            }, 1000);
        });
        return false;
    });


    $('#ResendActivationEmail').click(function () {

        $('#iFramePopUp').hide();

        var message = $.fn.GetActivationEmailMesssage();

        $('#dialog-popup-message').html(message);
        $('#dialog-popup-message').show();

        $("#dialog-popup").dialog({
            autoOpen: false,
            resizable: false,
            height: 200,
            width: 600,
            modal: true,
            buttons: {
                "button1": {
                    text: "Resend Email",
                    id: "jquery-ui-button1",
                    click: function () {
                        $.fn.OpenBlockUIElement("#ui-dialog-popup");
                        var ret_val = $.fn.ResendActivationEmail();
                        if (ret_val.indexOf("Email was sent.") != -1) {
                            $('#dialog-popup-message').html(ret_val);
                            $('#jquery-ui-button1').hide();
                            $("#ui-dialog-popup").unblock();
                        }
                        else {
                            $('#dialog-popup-message').html(ret_val);
                            $("#ui-dialog-popup").unblock();
                        }
                    }
                },
                "button2": {
                    text: "Close",
                    id: "jquery-ui-button2",
                    click: function () {
                        $(this).dialog("close");
                    }
                }
            }
        });

        $("#dialog-popup").dialog("open");
        $(".ui-dialog").attr('id', 'ui-dialog-popup');

        return false;
    });

    $('.RegisterPopUpIframe').click(function () {

        $(".ui-dialog-content").dialog("close");
    });

    $('.RegisterPopUp').click(function () {

        var isSignInOpen = $("#signin-popup").dialog("isOpen");

        if (isSignInOpen) {
            $('#signin-popup').dialog("close");
        }

        $('#dialog-popup-message').hide();

        $.fn.OpenBlockUI();

        var iframe = $('#iFramePopUp');
        iframe.attr('src', 'Register.aspx');
        iframe.show();

        iframe.load(function () {
            setTimeout(function () {
                $.unblockUI({
                    onUnblock: function () {
                        $("#dialog-popup").dialog({
                            autoOpen: false,
                            resizable: false,
                            height: 750,
                            width: 600,
                            closeOnEscape: true,
                            modal: true,
                            buttons: {
                                "button1": {
                                    text: $.fn.GetButtonText(),
                                    id: "jquery-ui-button1",
                                    click: function () {
                                        $.fn.OpenBlockUIElement("#profile-db");

                                        var src = $('#iFramePopUp').attr('src');
                                        var btnSubmit;

                                        if (src == 'Register.aspx') {
                                            var btnSubmit = $("#iFramePopUp").contents().find('input[name="btnSubmit"]');
                                            btnSubmit.trigger("click");
                                        }
                                        else if (src == 'RegisterStep2.aspx') {
                                            var btnSubmit = $("#iFramePopUp").contents().find('input[name="btnSubmit"]');
                                            btnSubmit.trigger("click");
                                        }
                                        else {
                                            $(this).dialog("close");
                                        }
                                    }
                                }
                            },
                            close: function (event, ui) { $.unblockUI(); }
                        });

                        $.fn.ShowHideRegisterDialog();
                        $.unblockUI();
                    }
                });
            }, 1000);
        });
        $.unblockUI();
        return false;
    });

    $.fn.ShowHideRegisterDialog = function () {

        var src = $('#iFramePopUp').attr('src');

        if (src == '') {
            $("#dialog-popup").dialog("close");
        }
        else {

            if (src == 'Register.aspx') {
                $("#dialog-popup").dialog("option", "height", 750);
            }
            else {
                $(".ui-dialog-buttonpane").prepend("<label id='TermsAndConditions' for='terms'>By clicking Register, you confirm that you accept our <a target='_blank' href='/terms-of-service.aspx'>Terms of Service</a> and <a target='_blank' href='/privacy.aspx'>Privacy Policy</a>. Don't worry, we'll never sell your email address to anyone.</label>");
                $("#dialog-popup").dialog("option", "height", 720);
            }

            if (!$("#dialog-popup").dialog("isOpen")) {
                $("#dialog-popup").dialog("open");
                $(".ui-dialog").attr('id', 'profile-db');
            }
        }
    }

    $.fn.GetButtonText = function () {

        var src = $('#iFramePopUp').attr('src');

        if (src == 'Register.aspx') {
            return "Next";
        }
        else if (src == 'RegisterStep2.aspx') {
            return "Register";
        }
        else {
            return "Close";
        }
    }

    $('.forgot-password').click(function () {

        var isSignInOpen = $("#signin-popup").dialog("isOpen");

        if (isSignInOpen) {
            $('#signin-popup').dialog("close");
        }

        $.fn.OpenBlockUI();

        var iframe = $('#frame-reset-password');
        iframe.attr('src', 'PasswordReset.aspx');

        iframe.load(function () {

            $("#dialog-reset-password").dialog({
                autoOpen: false,
                resizable: false,
                height: 400,
                width: 400,
                modal: true,
                buttons: {
                    "button1": {
                        text: "Continue",
                        id: "jquery-ui-button1",
                        click: function () {
                            $.unblockUI();
                            var btnPost = $("#frame-reset-password").contents().find('input[id="__ekResetPassSubmitButton"]');
                            btnPost.trigger("click");
                        }
                    },
                    "button2": {
                        text: "Close",
                        id: "jquery-ui-button2",
                        click: function () {
                            $.unblockUI();
                            $(this).dialog("close");
                        }
                    }
                },
                close: function (event, ui) { $.unblockUI(); }
            });

            $("#dialog-reset-password").dialog("open");

        });

        return false;
    });

    $('#ctl00_topCPH_ts_txtUserNameEmail').keydown(function (e) {
        if (e.keyCode == 13) {
            $('#btnSignIn').trigger('click');
        }
    });

    $('#ctl00_topCPH_ts_txtPassword').keydown(function (e) {
        if (e.keyCode == 13) {
            $('#btnSignIn').trigger('click');
        }
    });

    $('.SignInPopUp').click(function () {

        $('#ctl00_topCPH_ts_txtUserNameEmail').val('');
        $('#ctl00_topCPH_ts_txtPassword').val('');
        $('#signin-warning').hide();
        $('#lblUserNameEmail').hide();
        $('#lblPassword').hide();

        $("#signin-popup").dialog({
            autoOpen: false,
            resizable: false,
            height: 460,
            width: 400,
            title: 'Sign In',
            modal: true
        });

        $("#signin-popup").dialog("open");
        $(".ui-dialog").attr('id', 'signin-popup-container');
        return false;
    });

    $('#btnSignIn').click(function () {

        $('#signin-warning').hide();
        $('#lblUserNameEmail').hide();
        $('#lblPassword').hide();

        var form_height = 460;
        var un_lenght = $('#ctl00_topCPH_ts_txtUserNameEmail').val().length;

        if (un_lenght == 0) {
            $('#lblUserNameEmail').show();
            form_height = form_height + 20;
        }

        var pw_lenght = $('#ctl00_topCPH_ts_txtPassword').val().length;

        if (pw_lenght == 0) {
            $('#lblPassword').show();
            form_height = form_height + 20;
        }

        if (un_lenght > 0 && pw_lenght > 0) {

            var un = $('#ctl00_topCPH_ts_txtUserNameEmail').val();
            var pw = $('#ctl00_topCPH_ts_txtPassword').val();
            var rm = "off";

            if ($('#ctl00_topCPH_ts_cbRememberMe:checked').val() !== undefined) {
                rm = "on";
            }

            $.fn.OpenBlockUIElement("#signin-popup-container");

            var return_val = $.fn.login(un, pw, rm);

            if (return_val.indexOf("Success") != -1) {

                if (return_val.indexOf("Administrators") != -1 || return_val.indexOf("Everyone") != -1) {
                    $('#ctl00_topCPH_ts_hpLogIn').hide();
                    $('#ctl00_topCPH_ts_hpLogOut').show();
                    $('#liRegister').hide();
                    $('#liWorkarea').show();
                    $('#liMyAccount').show();
                    $('#liMyProfile').show();
                }
                else if (return_val.indexOf("Subscribers") != -1 || return_val.indexOf("Complimentary") != -1) {
                    $('#ctl00_topCPH_ts_hpLogIn').hide();
                    $('#ctl00_topCPH_ts_hpLogOut').show();
                    $('#liRegister').hide();
                    $('#liWorkarea').hide();
                    $('#liMyAccount').show();
                    $('#liMyProfile').show();
                }
                else {
                    // OA Connect Users
                    $('#ctl00_topCPH_ts_hpLogIn').hide();
                    $('#ctl00_topCPH_ts_hpLogOut').show();
                    $('#liRegister').hide();
                    $('#liWorkarea').hide();
                    $('#liMyAccount').hide();
                    $('#liMyProfile').show();
                }

                $("#signin-popup-container").unblock();

                var Pathname = window.location.pathname;
                var Pagename = Pathname.substring(Pathname.lastIndexOf('/') + 1);

                $("#signin-popup").dialog("close");
                location.reload();
            }
            else {
                form_height = form_height + 50;
                $("#signin-popup-container").height(form_height);
                $('#signin-warning').html(return_val);
                $('#signin-warning').show();
                $("#signin-popup-container").unblock();
            }
        }
        else {
            $("#signin-popup-container").height(form_height);
        }

        return false;
    });


    $('#ctl00_topCPH_ts_hpLogOut').click(function () {

        $.fn.OpenBlockUI();

        var return_val = $.fn.logout();

        $.unblockUI();

        if (return_val.indexOf("Success") != -1) {

            $('#ctl00_topCPH_ts_hpLogIn').show();
            $('#ctl00_topCPH_ts_hpLogOut').hide();
            $('#liRegister').show();
            $('#liWorkarea').hide();
            $('#liMyAccount').hide();
            $('#liMyProfile').hide();

            window.location = "Home.aspx";
        }

        return false;
    });

    $('#txtPayGatePasswordClear').focus(function () {
        if (this.value == 'PASSWORD') {
            this.value = '';
        }
        $('#txtPayGatePasswordClear').hide();
        $('#txtPayGatePassword').show();
        $('#txtPayGatePassword').focus();
    });

    $('#txtPayGatePassword').blur(function () {
        if ($('#txtPayGatePassword').val() == '') {
            $('#txtPayGatePasswordClear').show();
            $('#txtPayGatePasswordClear').val('PASSWORD');
            $('#txtPayGatePassword').hide();
        }
    });

    $('#ctl00_topCPH_ts_hpWorkarea').click(function () {

        var Pathname = window.location.pathname;
        var Pagename = Pathname.substring(Pathname.lastIndexOf('/') + 1);
        var WorkAreaPath = "";

        if (Pagename != '') {
            WorkAreaPath = Pathname.replace(Pagename, "WorkArea/workarea.aspx?LangType=1033")
        }
        else {
            WorkAreaPath = "WorkArea/workarea.aspx?LangType=1033";
        }

        window.open(WorkAreaPath, 'Admin400', 'menubar=no, toolbar=no');

        return false;
    });

    $("form input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('.defaultButton').click();
            return false;
        } else {
            return true;
        }
    });
});

// Sets the Top Nav Controls
$.fn.SetTopNavControls = function (UserGroup) {
    if (UserGroup == 'Administrators') {
        $('#ctl00_topCPH_ts_hpLogIn').hide();
        $('#ctl00_topCPH_ts_hpLogOut').show();
        $('#liRegister').hide();
        $('#liWorkarea').show();
        $('#liMyAccount').show();
        $('#liMyProfile').show();
        $('.signup-go').remove();
        $('.oa-connect-wrapper').remove();
        $('#sb-Newsletter').remove();        
    }
    else if (UserGroup == 'Subscribers') {
        $('#ctl00_topCPH_ts_hpLogIn').hide();
        $('#ctl00_topCPH_ts_hpLogOut').show();
        $('#liRegister').hide();
        $('#liWorkarea').hide();
        $('#liMyAccount').show();
        $('#liMyProfile').show();
        $('.signup-go').remove();
        $('.oa-connect-wrapper').remove();
        $('#sb-Newsletter').remove();
    } else if (UserGroup == 'OAConnect') {
        $('#ctl00_topCPH_ts_hpLogIn').hide();
        $('#ctl00_topCPH_ts_hpLogOut').show();
        $('#liRegister').hide();
        $('#liWorkarea').hide();
        $('#liMyAccount').show();
        $('#liMyProfile').show();
        $('.signup-go').remove();
        $('.oa-connect-wrapper').remove();
        $('#sb-Newsletter').remove();
    }
    else //Anonymous
    {
        $('#ctl00_topCPH_ts_hpLogIn').show();
        $('#ctl00_topCPH_ts_hpLogOut').hide();
        $('#liRegister').show();
        $('#liWorkarea').hide();
        $('#liMyAccount').hide();
        $('#liMyProfile').hide();
    }
}

// Login user to ektron CMS
$.fn.login = function (un, pw, rm) {

    var return_val;

    $.ajax({
        type: "POST",
        url: "handlers/Login.ashx",
        cache: false,        
        data: "un=" + un + "&pw=" + pw + "&rm=" + rm,
        async: false,
        timeout: 30000,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "text",        
        error: function (jqXHR, textStatus, errorThrown) {            
            return_val = jqXHR;
        },
        success: function (data) {
            return_val = data;
        }
    });

    return return_val;
}

// Logout user from ektron CMS
$.fn.logout = function () {

    var return_val;

    $.ajax({
        type: "POST",
        url: "handlers/Logout.ashx",
        cache: false,
        data: "{}",
        async: false,
        timeout: 30000,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "text",
        error: function () {
            return_val = "0";
        },
        success: function (msg) {
            return_val = msg;
        }
    });

    return return_val;
}


$.fn.OpenBlockUI = function () {

    $.blockUI({
        message: '<img src="images/ajax-loader.gif" />',
        fadeIn: 2000,
        fadeOut: 2000,
        showOverlay: true,
        centerY: true,
        css: {
            padding: '10px',
            margin: 0,
            width: '5%',
            top: '45%',
            left: '47%',
            textAlign: 'center',
            border: '2px solid #aaa',
            backgroundColor: '#FFF',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: 100,
            color: '#000'
        },
        overlayCSS: { backgroundColor: '#000' }
    });
}

$.fn.OpenBlockUIElement = function (element) {

    $(element).block({
        message: '<img src="images/ajax-loader.gif" />',
        fadeIn: 2000,
        fadeOut: 2000,
        showOverlay: true,
        centerY: true,
        css: {
            padding: '10px',
            margin: 0,
            width: '100px',
            top: '45%',
            left: '47%',
            textAlign: 'center',
            border: '2px solid #aaa',
            backgroundColor: '#FFF',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: 100,
            color: '#000'
        },
        overlayCSS: { backgroundColor: '#000' }
    });
}

$.fn.OpenLogInDialog = function () {

    $("#login-panel-mid-content").dialog({
        autoOpen: false,
        resizable: false,
        height: 200,
        width: 400,
        modal: true
    });

    $("#login-panel-mid-content").dialog("open");
}

// Login user to ektron CMS
$.fn.ResendActivationEmail = function () {

    var return_val;

    $.ajax({
        type: "POST",
        url: "handlers/ResendActivationEmail.ashx",
        cache: false,        
        async: false,
        timeout: 30000,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "text",
        error: function () {
            return_val = msg;
        },
        success: function (msg) {
            return_val = msg;
        }
    });

    return return_val;
}



// Delete message board comment ajax function
$.fn.GetActivationEmailMesssage = function () {

    var return_val;

    $.ajax({
        type: "POST",
        url: "handlers/GetActivationEmailMessage.ashx",
        cache: false,        
        async: false,
        timeout: 30000,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "text",
        error: function () {
            return_val = msg;
        },
        success: function (msg) {
            return_val = msg;
        }
    });

    return return_val;
}

$.fn.ShowDialogError = function () {

    $("#dialog-post-error").dialog({
        height: 100,
        modal: true,
        buttons: {
            Close: function () {
                $(this).dialog("close");
            }
        }
    });
}

$.fn.SetTopMessage = function (msg) {
    $("#top-message").show();
    $("#top-message-text").text(msg);      
    return true;
}

$.fn.SetDialogPopUpHtml = function (msg) {
    $('#dialog-popup-message').html(msg);  
    return true;
}

$.fn.ChangeNonVerifiedUserName = function (newemail) {

    var return_val;

    $.ajax({
        type: "POST",
        url: "handlers/ChangeNonVerifiedUserName.ashx",
        cache: false,
        data: "newemail=" + newemail,
        async: false,
        timeout: 30000,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "text",
        error: function () {
            return_val = msg;
        },
        success: function (msg) {
            return_val = msg;
        }
    });

    return return_val;
}

function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);

    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}
