﻿/// <reference path="BxJsLib.js" />
/// <reference path="Plugins/XmlApi.js" />
/// <reference path="web.js" />
Bx.User = window.bxUser = function ()
{
    var config = { apiurl: "/api/apiuser.aspx" };
    var features = { appid: "BxUserAPI" };
    function pageAPI(method, params, funReceive)
    {
        bxPageAPI(config.apiurl, method, params, funReceive, features);
    }
    return {
        isLogined: function (receiveFunction)
        {
            /// <summary>查询当前是否有用户登录</summary>
            /// <param name="receiveFunction">回调函数，传入参数为布尔值</param>

            bxPageAPI(config.apiurl, "islogined", [], function (code, mes, response)
            {
                if (code == 0) { if (response.get("status") == "1") { receiveFunction(true, response) } else { receiveFunction(false, response); } } else { alert(mes); }
            }, features);
        },
        getUserInfo: function (receiveFunction)
        {
            /// <summary>查询当前用户登录状态信息</summary>
            bxPageAPI(config.apiurl, "getuserinfo", [], receiveFunction, features);
        },
        checkUsername: function (username, receiveFunction)
        {
            /// <summary>检测用户名</summary>
            /// <param name="username">要检测的用户名</param>
            /// <param name="receiveFunction">回调函数</param>
            bxPageAPI(config.apiurl, "checkusername", ["username", username], receiveFunction, features);
        },
        checkEmail: function (email, receiveFunction)
        {
            /// <summary>检测用户名</summary>
            /// <param name="username">要检测的用户名</param>
            /// <param name="receiveFunction">回调函数</param>
            bxPageAPI(config.apiurl, "checkemail", ["email", email], receiveFunction, features);
        },
        userLogin: function (username, useremail, userpassword, isrememberusername, isrememberpassword, otherinfo, receiveFunction)
        {
            /// <summary>用户登录</summary>  
            bxPageAPI(config.apiurl, "userlogin", ["username", username, "useremail", useremail, "userpassword", userpassword, "isrememberusername", isrememberusername, "isrememberpassword", isrememberpassword.toString(), "otherinfo", otherinfo], receiveFunction, features);
        },
        userRegist: function (username, useremail, userpassword, otherinfo, receiveFunction)
        {
            /// <summary>用户注册</summary>   
            bxPageAPI(config.apiurl, "userregist", ["username", username, "useremail", useremail, "userpassword", userpassword, "otherinfo", otherinfo], receiveFunction, features);
        },
        getPassword: function (username, useremail, vcode, otherinfo, funCall)
        {
            /// <summary>用户找回密码</summary>            
            pageAPI("getpassword", ["username", username, "useremail", useremail, "vcode", vcode], funCall);
        },
        resetPassword: function (username, email, password, guid, otherinfo, funCall)
        {
            /// <summary>重新设置密码</summary>

            pageAPI("resetpassword", ["username", username, "email", email, "password", password, "guid", guid, "otherinfo", otherinfo], funCall);
        },
        webToRegister: function ()
        {
            /// <summary>从登录页面跳转到注册页面</summary>
            // var ru = bxGetUrlParam("ReturnUrl");
            var s = window.location.search;
            if (window == window.top)
            {
                webShowRegist(null, function ()
                {
                    bxUser.isLogined(function (isLogined)
                    {
                        var url = bxGetUrlParam("ReturnUrl");
                        if (url != null || url == "")
                        {
                            url = "/";
                        }
                        window.location.href = url;
                    })
                });
            } else
            {
                window.location = "regist.html" + s;
            }
            return false;
        },
        webToLogin: function ()
        {
            /// <summary>从注册页跳转到登录页面</summary>
            var s = window.location.search;
            window.location = "login.html" + s;
            return false;
        },
        webUserLogin: function (txtUsername, txtPassword, chkIsrememberUsername, chkIsrememberPassword, txtVcode, imgVcode, btnSrc, funEnd)
        {
            /// <summary>用户登录</summary>
            ///#region         
            var txtUsername = $(txtUsername), txtPassword = $(txtPassword), chkIsrememberUsername = chkIsrememberUsername ? $(chkIsrememberUsername) : null, chkIsrememberPassword = chkIsrememberPassword ? $(chkIsrememberPassword) : null, txtVcode = txtVcode ? $(txtVcode) : null, btnScr = $(btnScr), imgVcode = imgVcode ? $(imgVcode) : null;
            txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = true;
            var otherInfo = txtVcode ? "valcode=" + encodeURI(txtVcode.value.trim()) : "hasvalcode=false";
            bxUser.userLogin(txtUsername.value.trim(), "", txtPassword.value.trim(), chkIsrememberUsername ? chkIsrememberUsername.checked : false, chkIsrememberPassword ? chkIsrememberPassword.checked : false, otherInfo, function (code, message, response)
            {
                if (code == 0)
                {
                    var cookiename = response.get("usercookiename"), cookievalue = response.get("usercookievalue");
                    /*if (chkIsrememberPassword.checked && cookiename && cookiename.length > 0)
                    {
                    Bx.Cookie.Add(cookiename, cookievalue, 20160);
                    }*/
                    if (funEnd) { funEnd(); return false; }
                    if (bxGetUrlParam("cmd") == "close")
                    {
                        bxCloseOpenWindow();
                    }
                    else if (Bx.Pub.GetUrlParam("ReturnUrl").length > 0)
                    {
                        window.location = decodeURIComponent(Bx.Pub.GetUrlParam("ReturnUrl"));
                    }
                    else
                    {
                        window.top.location = "/" + webHelper.getLang() + "/user/";
                    }
                }
                else
                {
                    alert("登录错误,请重试\n" + message);
                    txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = false;
                    if (txtVcode)
                    {
                        txtVcode.disabled = false;
                    }
                    if (imgVcode)
                    {
                        imgVcode.click();
                    }
                }
            });
            ///#endregion 
        },
        webGetPassword: function (txtUsername, txtEmail, txtVcode, imgVcode, btnSrc)
        {
            /// <summary>找回密码</summary>
            var txtUsername = $(txtUsername), txtEmail = $(txtEmail), txtVcode = $(txtVcode), imgVcode = $(imgVcode), btnSrc = $(btnSrc);
            btnSrc.disabled = true;
            if (txtUsername.value.length == 0 && txtEmail.value.length == 0)
            {
                alert("用户名或用户至少输入一项");
                btnSrc.disabled = false;
            }
            else
            {
                bxUser.getPassword(txtUsername.value.trim(), txtEmail.value.trim(), txtVcode ? txtVcode.value.trim() : "", null, function (code, mes, reponse)
                {
                    if (code == 0)
                    {
                        alert("密码邮件已经送到您的邮箱，请及时查收");
                    }
                    else
                    {
                        alert(mes);
                    }
                    btnSrc.disabled = false;
                    if (imgVcode)
                    {
                        imgVcode.click();
                    }
                });
            }

        },
        webResetPassword: function (txtPsw, btnSrc)
        {
            /// <summary>重新设置密码</summary>

            btnSrc.disabled = true;
            var email = decodeURIComponent(bxPub.GetUrlParam("email"));
            var guid = bxPub.GetUrlParam("guid");

            bxUser.resetPassword("", email, $(txtPsw).value.trim(), guid, "", function (code, mes, response)
            {
                if (code == 0) { alert("新密码设置成功！"); }
                else { alert(mes); }
                btnSrc.disabled = false;
            });

        },
        webAutoReload: function ()
        {
            var url = decodeURIComponent(bxGetUrlParam("ReturnUrl"));
            bxUser.isLogined(function (isLogined)
            {
                if (isLogined) { window.location.href = url; }
            });


        },
        runLogined: function (callBack, useOpenMode)
        {
            /// <summary>检测用户是否登录，并自动执行绑定方法</summary>
            /// <param name="callBack">用户登录时要执行的函数</param>
            /// <param name="useOpenMode">true:未登录时使用弹窗登录模式，false:使用页面跳转登录模式，无(null)：不做自动登录处理</param>

            bxUser.isLogined(function (isLogind)
            {
                if (isLogind)
                {
                    callBack();
                }
                else
                {
                    if (useOpenMode == true)
                    {
                        webShowLogin(null, function ()
                        {
                            bxUser.isLogined(function (isLogind)
                            {
                                if (isLogind)
                                {
                                    callBack();
                                }
                            });
                        });
                    }
                    else if (useOpenMode == false)
                    {
                        var url = "/" + webHelper.getLang() + "/user/login2.html?ReturnUrl=" + encodeURIComponent(window.top.location.href);
                        window.top.location.href = url;
                    }
                }
            });
        }
    };
} ();

function webUserCheckUserName(txtUsername, valControl, messageOk, messageError)
{
    /// <summary>检测用户名</summary>
    /// <param name="txtUsername"></param>
    /// <param name="valControl"></param>
    /// <param name="messageOk"></param>
    /// <param name="messageError"></param>

    var txtUsername = $(txtUsername);
    var valControl = $(valControl);
    bxUser.checkUsername(txtUsername.value.trim(), function (code, message, response)
    {
        if (code == 0)
        {
            bxValSetMessage(valControl, true, messageOk);
        }
        else
        {
            bxValSetMessage(valControl, false, messageError);
        }
    });
}
function webUserCheckEmail(txtEmail, valControl, messageOk, messageError)
{
    /// <summary>检测用户名</summary>
    /// <param name="txtEmail"></param>
    /// <param name="valControl"></param>
    /// <param name="messageOk"></param>
    /// <param name="messageError"></param>

    var txt = $(txtEmail);
    var valControl = $(valControl);
    bxUser.checkEmail(txt.value.trim(), function (code, message, response)
    {
        if (code == 0)
        {
            bxValSetMessage(valControl, true, messageOk);
        }
        else
        {
            bxValSetMessage(valControl, false, messageError);
        }
    });
}
function webUserRegist(txtUsername, txtEmail, txtPassword, txtVcode, radUserType, txtMobile, imgVcode, btnSrc,otherinfo)
{
    var txtUsername = $(txtUsername), txtEmail = $(txtEmail), txtPassword = $(txtPassword), txtType = $(txtType), txtMobile = $(txtMobile);
    var txtVcode = txtVcode ? $(txtVcode) : null, imgVcode = imgVcode ? $(imgVcode) : null;
    txtEmail.disabled = txtMobile.disabled = txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = true;
    if (txtVcode) { txtVcode.disabled = true; }
    var others = new bxQueryString("", false);
    others.setValues("mobile", txtMobile.value.trim(),
    "usertype", bxRadioGetValue(radUserType, 0),
    "valcode", (txtVcode ? txtVcode.value.trim() : ""));
    if (otherinfo)
    {
        for (var fo in otherinfo)
        {
            if (otherinfo.hasOwnProperty(fo))
            {
                others.setValue(fo, otherinfo[fo].toString());
            }
        }
    }
    bxUser.userRegist(txtUsername.value.trim(), txtEmail.value.trim(), txtPassword.value.trim(), others.getQueryString(), function (code, message, response)
    {
        if (code == 0)
        {
            var status = response.get("status");
            if (status == "logined") //自动登录逻辑
            {
                alert("恭喜您注册成功!");
                window.top.location.href = "/html/user/#target=myinfo.aspx"; return;
                if (window == window.top)
                {
                    window.top.location.href = "/html/user/";
                }
                else
                {
                    bxCloseOpenWindow();
                }
            }
            else if (status == "toactived") //等待激活逻辑
            {
                window.top.location.href = "/" + webHelper.getLang() + "/user/active.aspx?email=" + txtEmail.value.trim();
            }
            else
            {
                alert("恭喜您注册成功！将转到登录页面");
                window.top.location = "/" + webHelper.getLang() + "/user/login.html";
            }
        }
        else
        {
            alert("注册失败\\n" + message);
            txtEmail.disabled = txtMobile.disabled = txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = false;
            if (txtVcode)
            {
                txtVcode.disabled = false;
            }
            if (imgVcode)
            {
                imgVcode.click();
            }
        }
    });
}
function webUserGetInfo(obj, callBack)
{
    /// <summary>查询用户综合信息</summary>
    /// <param name="obj">信息容器对象</param>
    /// <param name="callBack">回调函数,function callBack(code, mes, response){}</param>
    var divUser = obj ? $(obj) : $("divTopUserInfo");
    if (divUser == null) { return; }
    bxUser.getUserInfo(function (code, mes, response)
    {
        if (code == 0)
        {
            divUser.innerHTML = response.get("htmltext");
            if (callBack) { callBack(code, mes, response); }
        }
    });
}
function webShowLogin(type, funCall)
{
    /// <summary>显示用户登录框</summary>
    /// <param name="type">窗体类型</param>
    /// <param name="funCall">回调函数</param>
    var fEnd = function ()
    {
        webUserGetInfo();
        if (funCall) { funCall(); }
    }
    return bxOpenWindow("", "/" + webHelper.getWords("html", "en", "jp") + "/user/login.html?cmd=close", "width:700px;height:400px", "right:10px;", "scrolling:auto;", fEnd);
}
function webShowRegist(type, funCall)
{
    /// <summary>显示用户登录框</summary>
    /// <param name="type">窗体类型</param>
    /// <param name="funCall">回调函数</param>
    var fEnd = function ()
    {
        webUserGetInfo("divTopUserInfoBar");
        if (funCall) { funCall(); }
    }
    return bxOpenWindow("", "/" + webHelper.getWords("html", "en", "jp") + "/user/regist.html?cmd=close" + (type != null ? "&type=" + type : ""), "width:700px;height:400px", "right:10px", "scrolling:auto;", fEnd);
}
function webShowGetPassword(type, funCall)
{
    /// <summary>显示用户登录框</summary>
    /// <param name="type">窗体类型</param>
    /// <param name="funCall">回调函数</param>
    var fEnd = function ()
    {
        webUserGetInfo("divTopUserInfoBar");
        if (funCall) { funCall(); }
    }
    return bxOpenWindow("", "/" + webHelper.getWords("html", "en", "jp") + "/user/getpassword.html?cmd=close", "width:700px;height:500px", "right:10px", "scrolling:auto;", fEnd);
}
