var options = { path: '/', expires: 365 };

function GetUS_PassDetails() {

    var system_login_u = decrypt_str($.cookie('system_login_u'));
    var system_login_p = decrypt_str($.cookie('system_login_p'));

    if (system_login_p.length > 0) {
        $("#INUsername").val(system_login_u);
        $("#INPassword").val(system_login_p);
        $("#chkSavePassword").attr("checked", "checked");
    }

}

function ecrypt_str(to_enc) {
    var xor_key = '6';

    var the_res = '';

    for (i = 0; i < to_enc.length; ++i) {
        the_res += String.fromCharCode(xor_key ^ to_enc.charCodeAt(i));
    }
    return the_res;
}

function decrypt_str(to_dec) {

    var dec = '';

    if (to_dec != null) {
        var xor_key = '6';
        for (i = 0; i < to_dec.length; i++) {
            dec += String.fromCharCode(xor_key ^ to_dec.charCodeAt(i));
        }
    }
    return dec
}

$(document).ready(function () {

    //check for page=bp
    // which means bad password...
    //Username or Password are incorrect


    var query = window.location.search.substring(1);
    // alert(query.split("&")[0]);
    var queryx = query.split("&");

    for (var i = 0; i < queryx.length; i++) {
        if (queryx[i] == "page=fp") {
            $.get("/ForgotPassword.aspx", null, function (data) {
                jQuery.facebox(data);
                var Locale = $.cookie("NRLocale");
                //FilterNO();
                Try("*:not(.NRtranslated,.NR_NOtranslation,translatable)");

                Translate(Locale);

            });
        } else if (queryx[i] == "page=bp") {
            $("#conClose").addClass("tipErrorClose");
            $("#conClose").removeClass("tipWelcomeClose");
            $("#conClose").removeClass("tipNoticeClose");
            $("#conOpen").addClass("tipErrorOpen");
            $("#conOpen").removeClass("tipWelcomeOpen");
            $("#conOpen").removeClass("tipNoticeOpen");
            $("#conClose").html("<p>Username or Password are incorrect</p>");
            $(".loginPreloader").addClass("hideTag");
        } else if (queryx[i] == "page=bt") {
            //brand trasfer... redirection correction
            $("#conClose").html("<p>You have been transfered from a different Brand Login, you may now login again correctly...</p>");
        }
    }





    GetUS_PassDetails();

    $(".submitBtn").click(function () {
        if ($("#chkSavePassword").is(":checked")) {
            SavePassword();
        }
        LoginFunction();
    });

    $("#ForgotPassword").click(function (e) {
        $.get("/ForgotPassword.aspx", null, function (data) {
            jQuery.facebox(data);
        });
    });

    $("#chkSavePassword").change(function () {

        if ($(this).is(":checked")) {
            SavePassword();
        }
        else {
            $.cookie('system_login_u', null, options);
            $.cookie('system_login_p', null, options);
        }
    });

    var tabindex = 1;
    $('input,select').each(function () {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);
            tabindex++;
        }
    });

    $("input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            if ($("#chkSavePassword").is(":checked")) {
                SavePassword();
            }
            LoginFunction();
        }
        else {
            return true;
        }
    });

});


function SavePassword() {
    $.cookie('system_login_u', ecrypt_str($("#INUsername").val()), options);
    $.cookie('system_login_p', ecrypt_str($("#INPassword").val()), options);
}

function LoginFunction() {

    if ($("#INUsername").val() != "") {
        if ($("#INPassword").val() != "") {
            $(".loginPreloader").removeClass("hideTag");
            $.post("/Login.aspx", { us: $("#INUsername").val(), pa: $("#INPassword").val() },
             function (data) {
                 if ((data.indexOf(".aspx") > 1) & (data.length < 200)) {
                     window.location.href = data;
                 }
                 else if (data.length > 200) {
                     $("#conClose").addClass("tipErrorClose");
                     $("#conClose").removeClass("tipWelcomeClose");
                     $("#conClose").removeClass("tipNoticeClose");
                     $("#conOpen").addClass("tipErrorOpen");
                     $("#conOpen").removeClass("tipWelcomeOpen");
                     $("#conOpen").removeClass("tipNoticeOpen");
                     $("#conClose").html("<p>A System Error Has Occured!</p>");
                     $(".loginPreloader").addClass("hideTag");
                 }
                 else {
                     $("#conClose").addClass("tipErrorClose");
                     $("#conClose").removeClass("tipWelcomeClose");
                     $("#conClose").removeClass("tipNoticeClose");
                     $("#conOpen").addClass("tipErrorOpen");
                     $("#conOpen").removeClass("tipWelcomeOpen");
                     $("#conOpen").removeClass("tipNoticeOpen");
                     $("#conClose").html("<p>" + data + "</p>");
                     $(".loginPreloader").addClass("hideTag");
                 }
             });
        }
        else {
            $("#conClose").addClass("tipNoticeClose");
            $("#conClose").removeClass("tipWelcomeClose");
            $("#conOpen").addClass("tipNoticeOpen");
            $("#conOpen").removeClass("tipWelcomeOpen");
            $("#conClose").html("<p>Both fields are required</p>");
        }
    }
    else {
        $("#conClose").addClass("tipNoticeClose");
        $("#conClose").removeClass("tipWelcomeClose");
        $("#conOpen").addClass("tipNoticeOpen");
        $("#conOpen").removeClass("tipWelcomeOpen");
        $("#conClose").html("<p>Both fields are required</p>");
    }
}
