/**
* 静态html页面方式接入passport的支持函数库，同时也是为以前522版passport接入代码提供兼容性支持而保留
*/

var _blend = {};

_blend.helpers = {};

_blend.helpers.trim = function(s){
    if(typeof(s) == 'string'){
        return s.replace(/(^\s*)|(\s*$)/g, '');
    }    
    return s;
}

_blend.helpers.gettextvalue = function(id, defaultValue) {

    var value;

    try {
        var obj = document.getElementById(id);
        value = obj.value;
    }
    catch (e) {
        value = defaultValue;
    }

    return value;
}

_blend.helpers.getcheckvalue = function(id, defaultValue) {

    var value;

    try {
        var obj = document.getElementById(id);
        value = obj.checked;
    }
    catch (e) {
        value = defaultValue;
    }

    return value;
}

_blend.helpers.setcookie = function(name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
            ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
            ((path == null) ? "" : ("; path=" + path)) +
            ((domain == null) ? "" : ("; domain=" + domain)) +
            ((secure == true) ? "; secure" : "");
};

_blend.helpers.getcookie = function(name) {

    var value = document.cookie.match('(?:^|;)\\s*' + name + '=([^;]*)');
    return value ? unescape(value[1]) : '';
};

_blend.helpers.clearcookie = function(name) {

    if (_blend.helpers.getcookie(name)) {
        var expdate = new Date();
        expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
        _blend.helpers.setcookie(name, "", expdate);
    }
};

//用户名
_blend._nickname = "";

_blend.login = function() {

    var logintype, username, mobile, email, password, verifycode, auto_login;
    with(_blend.helpers){
        logintype = gettextvalue('logintype',_blend.context.login_type); // 登录类型(数字校园 = 1, 网站 = 2)
        setcookie("logintype", logintype);
        username = trim(gettextvalue('username', null)); // 用户名
        mobile = trim(gettextvalue('mobile', null)); // mobile
        email = trim(gettextvalue('email', null)); // email 
        password = trim(gettextvalue('password', ''));// 密码
        verifycode = trim(gettextvalue('verifyCode', ''));// 验证码
        auto_login = getcheckvalue('auto_login', false);// 自动登录
    }
      
    if(logintype == "1") {
        with(_blend.passport){
            var type = email == null ? (mobile == null ? (username == null ? null : 'u') : 'm') : 'e';    
            switch(type){
                case 'u':
                    if(username == '' || password == ''){
                        processError('请输入用户名和密码!');                    
                    }
                    else{
                        login(username, password, verifycode, auto_login);
                    }
                    break;
                
                case 'm':
                    if(mobile == '' || password == ''){
                        processError('请输入手机号码和密码!');
                    }
                    else{
                        if(username != ''){
                            login(username, password, verifycode, auto_login);
                        }
                        else{
                            login(mobile, password, verifycode, auto_login);
                        }
                    }
                    break;
                
                case 'e':
                    if(email == '' || password == ''){
                        processError('请输入邮件地址和密码!');
                    }
                    else{
                        if(username != ''){
                            login(username, password, verifycode, auto_login);
                        }
                        else{
                            login(email, password, verifycode, auto_login);
                        }
                    }
                    break;
                
                default:
                    processError('发生未知错误!');
                    break;
            }
        }
    }
    else{
        _blend.website.login(username, password, verifycode);
    }    
}

_blend.check = function() {

    window.isLogined = false;

    var logintype = _blend.helpers.getcookie("logintype");

    if (!logintype || logintype == '') {
        logintype = _blend.context.login_type;
        _blend.helpers.setcookie("logintype", logintype);
    }

    if (logintype == "1") {
        _blend.passport.check();
    }
    else if (logintype == "2") {
        _blend.website.check();
    }
    else {
        return;
    }
}

_blend.init = function() {

    try {
        document.getElementById("username").type = 'text';
    } catch (e) { }

    var logintype = _blend.helpers.getcookie("logintype");
    if (logintype == "") {
        return;
    }

    if (isLogined) {
        if (logintype == "1") {
            _blend.passport.init();
        }
        else {
            //_blend.website.init();
        }
    }
    else {
        // 自动登录
        //
        if (logintype == '1') {
            var auto_login = _blend.helpers.getcookie('passport_auto_login');
            if (auto_login) {

                var auto_login_time = parseInt(_blend.context.auto_login_time, 10);
                if (!auto_login_time) {
                    auto_login_time = 2000;
                }

                setTimeout(function() {

                    var userName = _blend.helpers.getcookie('__userName');
                    var password = _blend.helpers.getcookie('__password');
                    _blend.passport.login(userName, password, '', true);

                }, auto_login_time);
            }
        }
        else {
            // 绑定对键盘事件的响应
            var username = document.getElementById("username");
            var password = document.getElementById("password");
            var verifycode = document.getElementById("verifycode");

            username.attachEvent('onkeypress', function() {
                if (event.keyCode == 13) {
                    password.focus();
                }
            });

            password.attachEvent('onkeypress', function() {
                if (event.keyCode == 13) {
                    _blend.login();
                }
            });
        }
    }
}



_blend.logout = function(logintype) {

    if (logintype == 1) {

        _blend.passport.logout();
    }
    else {

        _blend.website.logout();
    }
}

_blend.register = function(obj) {

    var logintype = _blend.helpers.gettextvalue('logintype', 1);

    if (logintype == "1") {

        obj.href = _blend.context.passport + "/register?server=10" +
                       "&url=" + encodeURIComponent(_blend.context.website) +
                       "&root=1&auth=1f4ccf0c897966c0cdc291a586d62edb";
    }
    else {
        obj.href = "javascript:openreg();";
    }
}

_blend.forgot_password = function(obj) {

    var logintype = _blend.helpers.gettextvalue('logintype', 1);

    if (logintype == "1") {
        obj.href = _blend.context.passport + "/forgotPassword?server=10" +
                       "&url=" + encodeURIComponent(_blend.context.website) +
                       "&root=1&auth=1f4ccf0c897966c0cdc291a586d62edb";
    }
    else {
        obj.href = "javascript:forget();";
    }
}

_blend.passport = {};
_blend.passport.login = function(username/*用户名*/, password/*密码*/, verifycode/*验证码*/, auto_login/*是否自动登录*/) {

    var src = _blend.context.passport + "/scriptLogin?action=login" +
                    "&username=" + encodeURIComponent(username) +
                    "&server=10" +
                    "&root=1" +
                    "&multiAccount=1" +
                    "&cookieSaveType=" + (auto_login ? "3" : "2") +
                    "&password=" + hex_md5(password) + hex_sha1(password) +
                    "&verifyCode=" + verifycode +
                    "&charset=gb2312" +
                    "&dynamicVerifyURL=0" +
                    "&url=" + encodeURIComponent(location.href) +
                    "&v=" + new Date().getTime();

    var script = document.createElement("script");
    script.type = "text/javascript";
    script.language = "javascript";
    
    script.src = src;

    _blend.helpers.setcookie("__redirect", "true");

    //如果选择了自动登录,则保存相应值和用户名密码
    if (auto_login) {

        _blend.helpers.setcookie('__userName', username, new Date("June 3, 2020"));
        _blend.helpers.setcookie('__password', password, new Date("June 3, 2020"));
    }

    document.body.appendChild(script);
}

_blend.passport.check = function() {

    document.write("<script language=\"javascript\" type=\"text/javascript\" src=\"" + _blend.context.passport + "/scriptLogin?action=check&server=99&checkServerType=5&charset=gb2312&v=" + new Date().getTime() + "\"></scr" + "ipt>");
}

_blend.passport.init = function() {

    _blend._nickname = account.nickname;
    _blend.context.pp_init();
}

_blend.passport.logout = function() {

    var auto_login = _blend.helpers.getcookie('passport_auto_login');

    if (auto_login) {
        _blend.helpers.clearcookie('passport_auto_login');
        _blend.helpers.clearcookie('__userName');
        _blend.helpers.clearcookie('__password');
    }

    location.href = _blend.context.member + "/user/logout.htm?url=" + encodeURIComponent(location.href);
}

_blend.website = {};
_blend.website.login = function(username, password, verifycode) {

    Web.Service.LoginService.CheckUserLogin(username, password, _blend.website.login_response, null, null);
}

_blend.website.login_response = function(result) {

    if (result == 1)
        window.location.href = window.location.href;
}

_blend.website.check = function() {

    Web.Service.LoginService.CheckUserIsLogin(_blend.website.check_response, null, null);
}

_blend.website.check_response = function(result) {

    if (result[0] == "1") {

        _blend._nickname = result[1];
        window.isLogined = true;
        _blend.website.init();
    }
    else
        window.isLogined = false;
}

_blend.website.init = function() {

    _blend.context.ws_init();
}

_blend.website.logout = function() {

    Web.Service.LoginService.LogOut();
    location.href = location.href;
}