$(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"));
        $('#password').val(get_cookie("login_password"));
    }
    $("#btn_login").click(function () {
        login();
    })
    function login() {
        var ajaxhtml = $("#btn_login").html();
        $("#btn_login").html("正在登录..");
        $("#btn_login").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);
                }
                $("#btn_login").html(ajaxhtml);
                $("#btn_login").removeAttr("disabled");
            }
        });

    }
    function login_action() {
        $("#btn_login").html("正在登录..");
        $.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);
                    set_cookie("login_password", $('#password').val(), 60);
                    window.location = "Home/Portal.aspx";
                } else {
                    alert(r);
                }
                $("#btn_login").html("登录");
            }
        });
    }
    document.body.onkeydown = function (evt) {
        evt = evt ? evt : (window.event ? window.event : null);
        if (evt.keyCode == "13") {
            login();
        }
    }
});
//添加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;
}