function loginLoading(action) {
    if (action)
    { block("#logincontrol"); }
    else
    { unblock("#logincontrol"); }
}

function disableControls() {
    $(".loginControl").attr("disabled", "disabled");
}

function enableControls() {
    $(".loginControl").removeAttr("disabled");
}

$(document).ready(function() {

    $("#userInfo").hide();
    $("#loginForm").hide();
    $("#btnUserLogin").hide();
    $("#imgLoading").hide();

    $("#btnUserLogin").click(function() {
        $("#loginForm").show();
        $("#txtEmail").focus();
        $("#btnUserLogin").hide();
        $("#loginlinks").hide();
        return false;
    });

    $("#txtPassword").keydown(function(event) {
        if (event.keyCode == 13) {
            $("#btnSubmit").click();
            return false;
        }
    });

    $("#btnSubmit").click(function() {

        growlCloseAll();
        $(".hideOnLogin").hide();
        disableControls();
        loginLoading(true);

        //$.jGrowl.shutdown();
        var search = location.search;
        search = search.replace(/\?/, '');
        if (search !== null && search !== '')
        { search = "&" + search; }
        $.ajax({
            type: "POST",
            url: loginUserUrl,
            actiondescription: "Your login request",
            allowRetry: true,
            data: "email=" + encodeURIComponent($("#txtEmail").val()) + "&password=" + encodeURIComponent($("#txtPassword").val()) + "&rememberme=" + $("#chkRememberMe").is(":checked") + search,
            dataType: "json",
            reenableUI: true,
            success: function(loginData) {
                this.reenableUI = false;
                if (loginData.Success) {
                    document.cookie = 'PrevAuth=Yes; path=/'; // SESSION_EXPIRATION_DETECTION_COOKIE
                    if (loginData.RedirectUrl !== null && loginData.RedirectUrl.length > 0)
                    { window.location = loginData.RedirectUrl; }
                    else
                    { window.location.reload(); }
                } else {
                    if (window.loginFailedUrl) {
                        window.location = window.loginFailedUrl + '?loginfailed=' + escape($("#txtEmail").val());
                    } else {
                        growlAjaxResult(loginData);

                        $("#lostpasswordlink").attr('href', '');
                        $("#lostpasswordlink").click(function() {
                            loginLoading(true);
                            growlCloseAll();
                            disableControls();
                            $.ajax({
                                url: userServiceUrl,
                                actiondescription: "Send password request",
                                allowRetry: true,
                                dataType: 'json',
                                data: { m: 'sendlostpassword', email: $("#txtEmail").val() },
                                success: function(json) {
                                    growlCloseAll();
                                    growlAjaxResult(json, "Send Forgotten Password");
                                },
                                complete: function(XMLHttpRequest, textStatus) {
                                    loginLoading(false);
                                    enableControls();
                                }
                            });
                            return false;
                        });
                        this.reenableUI = true;
                    }
                }
            },
            complete: function(XMLHttpRequest, textStatus) {
                if (this.reenableUI) {
                    loginLoading(false);
                    enableControls();
                }
            }

        });
        return false;
    });
});
