$(document).ready(function () {

    // Init functions

    loginPortalInit();

    if (returnToPassword) { // focus on the correct tab when throwing error on password reset fields
        changePage('password-tab');
    }

    if (clientCodeNotSet) { // check for client code, if not present use page var to init client portal page
        clientPortalInit();
    }


    // Event watchers    
    $("input[name='txtUserID']").blur(function (e) {
        var client = $.cookie("CLECID");
        var username = $("input[name='txtUserID']").attr("value");

        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/Ajax/VIP.aspx",
            data: { "c": client, "u": username },
            success: function (data) {

                var h = 207;
                if (data.Required) {
                    h = 257;
                    $("li[id='labelPass']").show();
                    $("li[id='textPass']").show();
                } else {
                    $("li[id='labelPass']").hide();
                    $("li[id='textPass']").hide();
                }
                if (data.Required == true && data.Activated == false) {
                    h = 307;
                    $("li[id='labelCredential']").show();
                    $("li[id='textCredential']").show();
                } else {
                    $("li[id='labelCredential']").hide();
                    $("li[id='textCredential']").hide();
                }
                $("div[name='container']").height(h);

            }
        });

    });


    $("div.container input[type='password']").keypress(function (e) {
        var charCode = e.charCode || e.which;
        if (e.keyCode == 13) {
            return false;
        }
        if (!e.shiftKey && charCode >= 65 && charCode <= 90) {
            $("li.cap-warning").html('Password: Caps lock is on');
        } else {
            $("li.cap-warning").html('Password:');
        }
    });

    $('div.container input').keydown(function (e) { // keypress watch to submit correct sub forms
        if (e.keyCode == 13) {
            $("form").submit(function () { return false });
            $('div.container:visible').find('input:submit').click();
        }
    });

    $('ul.navigation a').click(function () { // navigation
        if (!($(this).hasClass('selected'))) {
            changePage($(this).attr('href').replace('#', ''));
        } return false;
    });

    // General Functions

    function clientPortalInit() {
        $('ul.navigation a').parent().hide();
        $('ul.navigation a.tab-selection').parent().show();
        changePage('selection-tab');
    }

    function loginPortalInit() {
        $('#cap-warning').hide();
        $('div.container').hide();
        $('div.container:first').show();
        $('div.container:first input[type="submit"]').removeAttr('disabled');
        $('div.container input[type="text"]').attr('autocomplete', 'off');
        $('div.container:first input:first').focus(); // focus on first input field
        $('ul.navigation a.tab-selection').parent().hide();
        $('body .hidden').removeClass('hidden'); // fix for flash of content that is supposed to be hidden    
    }

    function changePage(target) {
        $('ul.navigation a').removeClass('selected');
        $('ul.navigation').find('a[href="#' + target + '"]').addClass('selected');
        $('div.container').each(function () {
            if ($(this).hasClass(target)) {
                $('div.container').hide();
                $(this).show();
                $('div.container:visible input:first').focus();
            }
        })
    }
});
