﻿
var Msgs = {
    GameIDEmpty: "游戏ID号码不能为空！",
    AccountsEmpty: "登录用户名不能为空！",
    PasswordEmpty: "登录密码不能为空！",
    VerifyEmpty: "验证码不能为空！",
    VerifyError: "验证码输入错误！",

    PasswordErrorCount: 1
};

var UserForm = new Class({
    initialize: function() {
        this.txtAccounts = $("txtAccounts");
        this.txtPassword = $("txtPassword");
        this.txtVerifyCode = $("txtVerifyCode");

        this.imgVerifyCode = $("imgVerifyCode");
        this.btnUserLogon = $("btnUserLogon");
        this.userLogon_b = $("userLogon_b");
        this.userLogon_a = $("userLogon_a");
        this.usernameDesc = $("usernameDesc");
        this.userDetail = $("userDetail");

        this.btnUserLogout = $("btnUserLogout");       
    },
    cleanForm: function() {
        this.txtAccounts.value = "";
        this.txtPassword.value = "";     
    }
});

var Accounts = new Class({
    Implements:UserForm,
    Focus: function() {
        this.txtAccounts.select();
    },
    Keydown: function(event) {
        if (event.key=="enter") {
            this.txtPassword.focus();         
        }
    }
});

var Password = new Class({
    Implements:UserForm,
    Focus: function() {
        this.txtPassword.select();
    },
    Keydown: function(event) {
        if (event.key == "enter") {
            this.txtVerifyCode.focus();
        }
    }
});

var VerifyCode = new Class({
    Implements: UserForm,
    Focus: function() {
        this.txtVerifyCode.select();
    },
    Keydown: function(event) {
        if (event.key == "enter") {
            this.btnUserLogon.fireEvent("click");
        }
    }
});

var ImgVerifyCode = new Class({
    Implements: UserForm,
    Click: function() {
        this.imgVerifyCode.set("src", "/ajax/verifyimage.ashx?t=" + Math.random());
    }
});

//验证规则
var UsersRules = new Class({
    Implements: UserForm,
    ValidLogon: function() {
    if (this.txtAccounts.value.trim().length == 0) {
        this.txtAccounts.focus();
            alert(Msgs.AccountsEmpty);
            return false;
        }
        if (this.txtPassword.value.trim().length == 0) {
            this.txtPassword.focus();
            alert(Msgs.PasswordEmpty);
            return false;
        }        

        return true;
    }
});

//登录错误
function SetLogonErrorCount(errorCount) {
    if (errorCount <= 0) {
        Cookie.write("errorCount", 0);
    }
    else {
        errorCount = GetLogonErrorCount();
        Cookie.write("errorCount", (++errorCount));
    }
}

//获取错误次数
function GetLogonErrorCount() {
    var errorCount = Cookie.read("errorCount");
    if (errorCount == undefined || errorCount == null || errorCount == "") {
        return 0;
    }

    return errorCount.toInt();
}

//登录
var ULogon = new Class({
    Implements: UserForm,

    a: new Accounts(),
    b: new Password(),
    c: new VerifyCode(),
    d: new ImgVerifyCode(),

    logon: function() {
        var r = new UsersRules();
        if (!r.ValidLogon()) {
            return;
        }

        this.accounts = this.txtAccounts.value.trim();
        this.password = this.txtPassword.value.trim();
        this.verifyCode = "";

        this.password = hex_md5(this.password);

        var errorCount = GetLogonErrorCount();
        if (errorCount >= Msgs.PasswordErrorCount) {
            SetLogonErrorCount(0);
            goURL("logon.aspx?mode=verifycode");
        }

        this.btnUserLogon.disabled = true;

        Users.Logon(this.accounts, this.password, this.verifyCode,
            function(res) {
                if (!res || res.error) {
                    $("btnUserLogon").disabled = false;
                    alert("抱歉！登录请求发送错误，请稍后再试。");
                }
                else {
                    $("btnUserLogon").disabled = false;
                    if (!res.value.Success) {
                        SetLogonErrorCount(1);
                        alert(unescape(res.value.Content));
                    }
                    else {
                        SetLogonErrorCount(0);

                        var userStatus = res.value.EntityList[0];

                        var userDesc = new Array();
                        userDesc.push("<li>欢迎 <span id=\"usernameDesc\" class=\"stress\">");
                        userDesc.push(unescape(userStatus.Accounts));
                        userDesc.push("</span><img src='images/gameIcons/icons/userArrow.gif' />");
                        userDesc.push(" <a href=\"##\" style=\"margin-left:30px;\">管理面板</a> | <a id=\"btnUserLogout\" href=\"javascript:void(0);\" onclick=\"logout();\">退出</a>");

                        userDesc.push("<div id=\"userDetail\" style=\"display: none; position: absolute; z-index: 50;\">");
                        userDesc.push("<p>游戏ID：<span class=\"stress\">");
                        userDesc.push(unescape(userStatus.GameIDDescribe));
                        userDesc.push("</span><br />");
                        userDesc.push("<div class=\"exper\">游戏经验：</div>");
                        userDesc.push("<div class=\"my_uclevel_icons\" id=\"level\" ");
                        userDesc.push("title=\"");
                        userDesc.push("经验级别：");
                        userDesc.push(GetExperienceLevel(userStatus.Experience));
                        userDesc.push(" 级\">");
                        userDesc.push(GetExperienceIcons(userStatus.Experience));
                        userDesc.push("</div>");
                        userDesc.push("</p></div></li>");

                        var uform = new UserForm();
                        uform.userLogon_b.setStyle("display", "none");
                        uform.userLogon_a.setStyle("display", "block");

                        uform.userLogon_a.empty();
                        uform.userLogon_a.set("html", userDesc.join(""));

                        //清空表单                        
                        uform.cleanForm();

                        //状态事件
                        new popup("userDetail", "usernameDesc", true, "", "center");

                        //refresh();
                    }
                }
            }
        );
    }
});

//用户注销
function logout() {
    Users.Logout();
    var uform = new UserForm();
    uform.userLogon_b.setStyle("display", "block");
    uform.userLogon_a.setStyle("display", "none");
    uform.userLogon_a.empty();
    SetLogonErrorCount(0);

    goURL("/index.aspx");
}

//页面加载
window.addEvent('domready', function() {
    var u = new ULogon();

    //事件绑定
    u.txtAccounts.addEvent("focus", function(event) { u.a.Focus(); });
    u.txtPassword.addEvent("focus", function(event) { u.b.Focus(); });

    u.txtAccounts.addEvent("keydown", function(event) { 
        var event = new Event(event); 
        u.a.Keydown(event)
    });
    u.txtPassword.addEvent("keydown", function(event) {
        var event = new Event(event);
        u.c.Keydown(event);
    });

    u.btnUserLogon.addEvent("click", function(event) { u.logon(); });
    u.btnUserLogon.addEvent({
        "mouseenter": function(e) { u.btnUserLogon.set("class", "btn_indexLogin_hover"); },
        "mouseleave": function(e) { u.btnUserLogon.set("class", "btn_indexLogin"); }
    });    
});

