﻿$(document).ready(function () {
    /* init UI */
    $("#errorText").hide();
    $("#ProcessingMessage").hide();
    $("#logMeIn").click(function (e) { processLogin(); e.preventDefault(); });

    /*  STYLEIZE CONTROLS */
    $("#errorText").addClass("ui-state-error");
    $("#errorText p").addClass("ui-state-error-text")
    $("#logMeIn").addClass("btn");
    ($("#txtUserName").val() == "") ? $("#txtUserName").focus() : $("#txtPassword").focus();
    $("#txtUserName").attr({ tabindex: "1" });
    $("#txtPassword").attr({ tabindex: "2" });
    $("#logMeIn").attr({ tabindex: "3" });

    var u = getCookie("UserName");
    if (u != null) {
        $("#txtUserName").val(u);
        $("#chkRemeberMe").attr('checked', true);
    }

    checkCompatabilityMode();
});

function processLogin(u, p) {
    var u = $("#txtUserName").val();
    var p = $("#txtPassword").val();
    
    // remember username is requested
    ($('#chkRemeberMe').is(':checked')) ? setCookie("UserName", u) : setCookie("UserName", "");

    var qs = new Querystring();
    var t = qs.get("token", "");
    if (u == "" || p == "") {
        displayError("A User Name and Password are required.");
    } else {

        $("#ProcessingMessage").show();
        $("#logMeIn").hide();

        $.ajax({
            type: "POST",
            url: "ReportDataservice.asmx/ProcessLogin",
            dataType: "json",
            data: "{ username: '" + u + "',password: '" + p + "',token: '" + t + "'  }",
            contentType: "application/json; charset=utf-8",
            success: function(json) {
                $("#ProcessingMessage").hide();
                $("#logMeIn").show();
                var loginResponse = (typeof json.d) == 'string' ? eval('(' + json.d + ')') : json.d;
                //var myJSONText = JSON.stringify(loginResponse, null);
                if (loginResponse.Authenticated == false) {
                    // PROCESS ACCOUNTS THAT HAVE NOT BEEN MIGRATED
                    if (loginResponse.Migrated == false) {
                        if (loginResponse.LegacyUserToken > -1) {
                            location.href = "MigrateUser.aspx?token=" + loginResponse.LegacyUserToken;
                        } else {
                            displayError("There is no user in the database with the username " + loginResponse.UserName);
                        }
                    } else {
                        // PROCESS ALL OTHER AUTHENTICATION ERRORS
                        displayError(loginResponse.ErrorMessage);
                    }
                } else {
                    
                        location.href = "default.aspx";
                }
            },
            error: function(xhr, strError) {
                $("#ProcessingMessage").hide();
                $("#logMeIn").show();
                var err = eval("(" + xhr.responseText + ")");
                displayError(err.Message);
            }
        });
    }
}
function displayError(msg) {
    $("#errorText")
    .html(msg)
    .show();
}
