﻿/*
加载必需的js、css
*/
var javastr = "";
javastr += "<script src=\"/GlobalResource/Global-thickbox.js\" type=\"text/javascript\">";
javastr += "</script>";
javastr += "";
javastr += "<link href=\"/GlobalResource/Global-thickbox.css\" rel=\"stylesheet\" type=\"text/css\" />";
document.write(javastr);

/*
主处理流程
*/

//默认配置项
var cboxconfig = {
    /*
    以下为thickbox方式的配置项
    */
    height: 300         //弹出窗体的高度
    , width: 400        //弹出窗体的宽度
    , cboxTitle: ""     //弹出窗体的标题
    , needlogin: false
    , cboxClkObjId: ""  //单击能弹出checkbox窗体的元素的Id
    , loginoutClkObjId: ""//退出按钮id
}

$().ready(function() {
    //验证是否登陆
    localServerReg();

    $("body").keypress(function(event) {
        if (event.keyCode == 13)
            $("#global-submit").click();
    });

    //为退出按钮绑定id
    if (cboxconfig.loginoutClkObjId != "") {
        $("#" + cboxconfig.loginoutClkObjId).click(function() {
            loginOut(function() {
                callback_LoginOuted();
            })
            return false;
        });
    }
});

function bindLoginButton() {
    /*
    登陆事件触发处理流程
    */
    $("#global-submit").click(function() {
        var rv = checkLoginStr();
        if (rv != false) {
            $(this).attr("disabled", "true");
            var username = unescape(stringtrim($("#username").val()));
            var user = { "username": username, "password": $("#pw").val(), "checkcode": $('#checkcode').val(), singleLogin: true };
            user.callback = function(user) {

                if (!user.isLogin) {
                    //登陆失败
                    alert("账号不存在，或密码错误");
                    //刷新验证码
                    refreshcode();
                    $("#checkcode").val("");
                } else {
                    if (cboxconfig.cboxUrl != "" && cboxconfig.cboxClkObjId != "") {
                        //接除原来的按钮登陆事件绑定
                        $("#" + cboxconfig.cboxClkObjId).unbind("click");
                    }

                    //执行登陆回调事件
                    if (typeof (callback_Logined) != "undefined") {
                        callback_Logined(user);
                    }
                }
                $("#global-submit").attr("disabled", "");
            }
            PassportLogin(user);
        }
    });
}


//调用本地注册信息到服务器
function localServerReg() {
    if (PassportUser.userid > 0) {
        //执行登陆回调事件
        if (typeof (callback_Logined) != "undefined") {
            tb_remove();
            callback_Logined(PassportUser);
        }

    } else {
        if (typeof (callback_NoLogin) != "undefined") {
            callback_NoLogin();
        }
        //未登陆或者登陆过期
        if (cboxconfig.needlogin) {
            showLogin();
        }
        //else {
            if (cboxconfig.cboxUrl != "" && cboxconfig.cboxClkObjId != "") {
                $("#" + cboxconfig.cboxClkObjId).click(function() {
                    showLogin();
                    return false;
                })
            }
        //}
    }
}

//显示登陆框
function showLogin() {
    tb_show(cboxconfig.cboxTitle, globalconfig.loginBoxUrl + "?height=" + cboxconfig.height + "&width=" +
                    cboxconfig.width + "&modal=true", false, bindLoginButton);
}

//退出登陆
function loginOut(callbackloginout) {
    $.ajax({
        type: "GET",
        url: globalconfig.localLoginCheckUrl + "?loginStatus=0",
        dataType: "script",
        cache: false,
        global: true,
        success: function(data) {
            if (typeof (callbackloginout) != "undefined") {
                callbackloginout();
            }
        }
    });

}

//直接显示对话框
