$(document).ready(function () {
    var login_db_id = get_cookie("login_db_id");
    if (login_db_id != "" && login_db_id != null) {
        $('#db_id').val(get_cookie("login_db_id"));
        $('#username').val(get_cookie("login_username"));
    }
    $(".logininput").blur(function () {
        if ($(this).val() == "") {
            $(this).css("border-color", "red");
        }
        else
            $(this).css("border-color", "#D9D6C4");
    })
    $("#loginbtn").click(function () {
        login();
    })

    document.body.onkeydown = function (evt) {
        evt = evt ? evt : (window.event ? window.event : null);
        if (evt.keyCode == "13") {
            login();
        }
        
    }

    function login() {
        var k = 0;
        $(".logininput").each(function (i, obj) {
            if ($(obj).val() == "") {
                k++;
                $(this).css("border-color", "red");
                $(this).focus();
                return false;
            }
        });
        if (k != 0) return;
        var ajaxhtml = $("#loginbtn").html();
        $("#loginbtn").html("正在登录....  <img src='Images/Login/loading.gif' alt='Announcement' /> ");
        $("#loginbtn").attr("disabled", "disabled");

        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "System/Login/Login_Service.asmx/CheckHasLogin",
            data: "{db_id:'" + $('#db_id').val() + "',username:'" + $('#username').val() + "'}",
            dataType: 'json',
            success: function (result) {
                var r = result.d;
                if (r == "0") {
                    login_action();
                } else if (r == "1") {
                    if (confirm("用户已登录，是否踢出对方？")) {
                        login_action();
                    }
                } else {
                    alert(r);
                }
                $("#loginbtn").html(ajaxhtml);
                $("#loginbtn").removeAttr("disabled");
            }
        });

    }
    function login_action() {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "System/Login/Login_Service.asmx/Login",
            data: "{db_id:'" + $('#db_id').val() + "',username:'" + $('#username').val() + "',password:'" + $('#password').val() + "',login_date:'" + $('#login_date').val() + "'}",
            dataType: 'json',
            success: function (result) {
                var r = result.d;
                if (r == "success") {
                    set_cookie("login_db_id", $('#db_id').val(), 60);
                    set_cookie("login_username", $('#username').val(), 60);
                    window.location = "System/SystemFrame/Index.aspx";
                } else {
                    alert(r);
                    $("#loginbtn").html(ajaxhtml);
                    $("#loginbtn").removeAttr("disabled");
                }
            }
        });
    }

});

//添加Cookie
function set_cookie(name, value, day) {
    var exp = new Date();
    exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//获取Cookie
function get_cookie(name) {
    var arr, reg = new RegExp("(^|)" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
//显示二维码
var is_show_qr_code = false;
function ShowQRCode() {
    var btn_show_qr_code = document.getElementById("btn_show_qr_code");
    if (!is_show_qr_code) {
        $("#content").css("display", "none");
        $("#content2").css("display", "block");
        btn_show_qr_code.title = "显示登录窗口";
        $("#btn_show_qr_code").removeClass("codeentry").addClass("codeentry2");
    } else {
        $("#content").css("display", "");
        $("#content2").css("display", "none");
        btn_show_qr_code.title = "显示二维码";
        $("#btn_show_qr_code").removeClass("codeentry2").addClass("codeentry");
    }

    is_show_qr_code = !is_show_qr_code;
}

window.onload = function () {
    $('#password').focus();
}