////引用prototype.js
//if(typeof Prototype=='undefined')
//    document.write('<script type="text/javascript" src="/js/prototype.js"></scr' + 'ipt>');

/*********************************************************************************************
 * 2007.06.01 created by Janpoem
 * 目的：用户基础类 - 用户行为总结
 *********************************************************************************************/
function tgUser() {
    // 初始化属性格式
    this.location = ''; // 当前location 字符类型
    this.from = '';     // 当前来源 字符类型,默认空,表示没有来源,或者直接输入网址
    this.history = [];  // 当前对象实例的浏览器历史记录
    this.errors = [];   // 当前对象实例的错误统计
    this.lodingbar = null;
    // 初始化赋值
    this.init();
}
tgUser.prototype = {
    init : function() {
        this.from = document.referrer;
        this.location = window.location;
        this.history = history; // 针对IE和FF要进行兼容性优化
        this.lodingbar = null;
    },
    showLodingBar : function() {
        if($('lodingbars')) $('lodingbars').style["display"] = "";
    },
    hideLodingBar : function() {
        if($('lodingbars')) $('lodingbars').style["display"] = "none";
    },
    doLogin : function(tbNo, tbPwd,btnId, chkRemember, sUrl) {
        if (!tbNo || !tbPwd || !chkRemember || !$(tbNo) || !$(tbPwd) || !$(chkRemember)) return;
        if ($(tbNo).value.length == 0 || $(tbPwd).value.length == 0) {
            var oFocusTb = $(tbNo).value.length == 0 ? $(tbNo) : $(tbPwd);
            oFocusTb.focus(); //oFocusTb = null;
            return alert("请输入完整的用户名和密码！");
        }        
        var _self = this;
        if($(btnId)) $(btnId).disabled = "disabled";
        _self.showLodingBar();
        var param = 'op=login&no='+encodeURIComponent($(tbNo).value)+'&pwd='+$(tbPwd).value;
        if ($(chkRemember).checked)
            param = param + '&iremember=1';
        else
            param = param + '&iremember=0';
        var myajax = new Ajax.Request("/System/AjaxService.aspx", {
            method : 'post',
            parameters : param,
            onComplete : function(oriRequest) {
                var sResult = oriRequest.responseText.substring(0,1);
                switch (sResult) { // 该处仍需要根据最新的AjaxService修改
                    case "1" : _self.locationTo(sUrl); break;
                    case "0" : alert("不存在该用户！"); $(tbPwd).value = '';break;
                    case "2" : window.open("http://user.tgnet.cn/activation.html"); break;
                    default : alert("不存在该用户或密码输入有误！"); $(tbPwd).value = '';break;
                }
                sResult = null;
                if($(btnId)) $(btnId).disabled = "";
                _self.hideLodingBar();
            }
        })
        param = null; myajax = null;
    },
    doLoginout : function() {
        var _self = this;
        var param = 'op=logout';
        var myajax = new Ajax.Request("/System/AjaxService.aspx", {
            method : 'get',
            parameters : param,
            onComplete : function(oriRequest) {
                var sResult = oriRequest.responseText.substring(0,1);
                if (sResult == "0") return alert("操作有误，请重新退出！");
                else _self.locationTo(0);
                sResult = null;
            }
        });
        param = null; myajax = null;
    },
    bindLoginEvent : function(tbNo, tbPwd, chkRemember, btnId, sUrl, aClsName) {
        if (!$(tbNo) || !$(tbPwd) || !$(chkRemember) || !$(btnId)) return;
        var _self = this;
        $(btnId).onclick = function() { _self.doLogin(tbNo, tbPwd,btnId, chkRemember, 0); return false; }
        this.bindLoginTbEvent(tbNo, btnId, aClsName);
        this.bindLoginTbEvent(tbPwd, btnId, aClsName);
        this.bindLoginTbEvent(chkRemember, btnId, aClsName);
    },
    bindLoginTbEvent : function(tbId, btnId, aClsName) {
        if (aClsName && isArray(aClsName) && aClsName.length == 2) {
            $(tbId).onfocus = function() { this.className = aClsName[1]; }
            $(tbId).onblur = function() { this.className = aClsName[0]; }
        }
        $(tbId).onkeydown = function(e) {
            e = e || window.event;
            if (e.keyCode == 13) {
                $(btnId).click();
                return false;
            }
        }
    },
    locationTo : function(sUrl) {
        if (!sUrl) window.location.reload(true);
        else window.location.href = sUrl;
    }
}

var __User = new tgUser();