﻿function $LoginGetId(id) { return document.getElementById(id); };
function $LoginGetValue(id) { return $LoginGetId(id).value; };
function $LoginBindEvent(id, eventName, funName) {
    var o = $LoginGetId(id);
    if (!o["evtAdd"]) {
        o.attachEvent ? o.attachEvent("on" + eventName, funName) : o.addEventListener(eventName, funName, false);
        o["evtAdd"] = true;
    }
}
function $LoginGetText(id, msg) {
    var o = $LoginGetId(id);
    o.innerHTML = msg;
}
String.prototype.trim = function() {
    return this.replace(/^[\s　\t]+|^[　　\t]+|[[\s　\t]+$|[　　\t]+$|[\s\t]+$]/g, "");
}
function messageData(httpResponseText) {
    this.responseText = httpResponseText;
    var code = 0;
    var desc = "";
    this.msgCode = function() {
        var tmp = "";
        try {
            tmp = this.responseText.substring(0, 2);
        }
        catch (e) {

        }

        switch (tmp) {
            case "0:":
                code = 0;
                break;
            case "1:":
                code = 1;
                break;
            case "2:":
                code = 1;
                break;
            case "3:":
                code = 1;
                break;
            case "4:":
                code = 1;
                break;
            case "5:":
                code = 5;
                break;
            case "7:":
                code = 7;
                break;
            default:
                code = -1;
                break;
        }
        return code;
    }
    this.msgDesc = function() {
        try {
            desc = this.responseText.substring(2);
        }
        catch (e) {
        }
        return desc;

    }
}
function ajaxTimeOutCallback(xmlHttp, timeCallback) {
    this.timeId = 0;
    this.cb = timeCallback;
    this.xmlHttp = xmlHttp;
    var cth = this;

    this.onTimeOut = function() {
        try {
            clearTimeout(cth.timeId);
            cth.xmlHttp.abort();
        }
        catch (e) { };
        try { cth.cb(); } catch (e) { };
    }

    this.run = function() {
        cth.timeId = setTimeout(cth.onTimeOut, 8000);
    }

    this.clear = function() {
        clearTimeout(cth.timeId);
    }

}
function ajaxLogin(lgoinViewId, loginCtrlId, lgUserName, lgUserPwd, lgCmd, loginStatusId, loginStatusName, loginOutId, loginRemberMe, loginFailedId) {

    this.ajaxCallback = new ajaxTimeOutCallback(null, null);
    this.loginViewId = lgoinViewId;

    this.loginControlId = loginCtrlId;
    this.loginUserName = lgUserName;
    this.loginPassword = lgUserPwd;
    this.loginButton = lgCmd;

    this.loginStatusId = loginStatusId;

    this.loginStatusName = loginStatusName;
    this.loginOutButton = loginOutId;

    this.loginRemberMeId = loginRemberMe;
    this.loginFailedId = loginFailedId;

    this.timerId = 0;
    var cTh = this;

    if (typeof ajaxLogin.__RegisteredView == "undefined") {
        ajaxLogin.__RegisteredView = new Array();
    }
    ajaxLogin.__RegisteredView.push(this);

    if (typeof ajaxLogin.__IsChecked == "undefined") {
        ajaxLogin.__IsChecked = 0;
    }

    if (typeof ajaxLogin.__TimerId == "undefined") {
        ajaxLogin.__TimerId = 0;
    }

    this.init = function() {
        var http = false;
        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            var arrXmlHttpTypes = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'Microsoft.XMLHTTP'];
            for (var i = 0; i < arrXmlHttpTypes.length; i++) {
                try {
                    http = new ActiveXObject(arrXmlHttpTypes[i]);
                } catch (ex) { } //不支持MSXML.XMLHttp对象的IE
            }
        }
        if (http) {
            var regView = ajaxLogin.__RegisteredView;
            for (var i = 0; i < regView.length; i++) {
                $LoginBindEvent(regView[i].loginButton, "click", regView[i].loginClick);
                $LoginBindEvent(regView[i].loginOutButton, "click", regView[i].loginOutClick);
                $LoginBindEvent(regView[i].loginUserName, "keydown", regView[i].nameKeyDown);
                $LoginBindEvent(regView[i].loginPassword, "keydown", regView[i].pwdKeyDown);
            }
        }
        return http;
    }
    this.showIn = function() {
        $LoginGetId(cTh.loginControlId).style.display = "none";
        $LoginGetId(cTh.loginStatusId).style.display = "";
        $LoginGetText(cTh.loginStatusName, "");
        $LoginGetText(cTh.loginFailedId, "");
    }
    this.showOut = function() {

        $LoginGetId(cTh.loginStatusId).style.display = "none";
        $LoginGetId(cTh.loginControlId).style.display = "";
        $LoginGetText(cTh.loginStatusName, "");
        $LoginGetText(cTh.loginFailedId, "");
    }
    this.clear = function() {
        $LoginGetId(cTh.loginUserName).value = "";
        $LoginGetId(cTh.loginPassword).value = "";
        try {
            $LoginGetId(cTh.loginRemberMeId).checked = false;
        }
        catch (e) {
        }
    }

    this.loginOut = function(onCompleted, onRunning, onError) {
        var http = cTh.init();
        if (!http)
            return;

        http.open("GET", "http://" + parent.location.hostname + "/ajaxLoginOut.aspx", true);
        http.onreadystatechange = function() {
            if (http.readyState == 4) {
                if (http.status == 200) {
                    onCompleted();
                } else {
                    onError();
                }
            } else {
                onRunning();
            }
        }
        http.send(null);

    }

    this.loginOutComplected = function() {
        var regView = ajaxLogin.__RegisteredView;
        for (var i = 0; i < regView.length; i++) {
            regView[i].showOut();
        }
        if (ajaxLogin.__TimerId != 0) {
            window.clearInterval(ajaxLogin.__TimerId);
        }
        if (cTh.afterLoginOut) {
            cTh.afterLoginOut();
        }
    }

    this.loginOutRunning = function() {
    }
    this.loginOutErr = function() {
    }
    //单击事件的处理
    this.loginClick = function(evt) {

        var name = $LoginGetValue(cTh.loginUserName);
        var pwd = $LoginGetValue(cTh.loginPassword);

        name = name.trim();
        if (name == "") {
            $LoginGetText(cTh.loginFailedId, "用户名不能为空！");
            $LoginGetId(cTh.loginUserName).focus();
            if (window.event) {
                window.event.returnValue = false;
            } else {
                evt.preventDefault();
            }
            return;
        }
        pwd = pwd.trim();
        if (pwd == "") {
            $LoginGetText(cTh.loginFailedId, "密码不能为空！");
            $LoginGetId(cTh.loginPassword).focus();
            if (window.event) {
                window.event.returnValue = false;
            } else {
                evt.preventDefault();
            }
            return;
        }

        var rm = "0"
        try {
            rm = $LoginGetId(cTh.loginRemberMeId).checked ? "1" : "0";
        }
        catch (e) {
        }
        cTh.login(name, pwd, rm, cTh.onLoginCompleted, cTh.onHttpStatusError, cTh.onLoginRunning);
        if (window.event) {
            window.event.returnValue = false;
        } else {
            evt.preventDefault();
        }
    }
    this.loginOutClick = function(evt) {
        if (window.confirm("您要确定退出吗?")) {
            cTh.loginOut(cTh.loginOutComplected, cTh.loginOutRunning, cTh.loginOutErr);
            if (window.event) {
                window.event.returnValue = false;
            } else {
                evt.preventDefault();
            }
        }
        window.event.returnValue = false;
    }
    //keydown事件的处理
    this.nameKeyDown = function(evt) {
        if (window.event) {

            if (event.keyCode == 13) {
                $LoginGetId(cTh.loginPassword).focus();
                event.returnValue = false;
            }
        } else {

            if (evt.keyCode == 13) {
                $LoginGetId(cTh.loginPassword).focus();
                evt.preventDefault();
            }
        }

    }
    this.pwdKeyDown = function(evt) {
        var srcObject;
        if (window.event) {
            srcObject = event.srcElement;
            if (event.keyCode == 13) {
                $LoginGetId(cTh.loginButton).click();
                event.returnValue = false;
            }
        } else {
            srcObject = evt.target;
            if (evt.keyCode == 13) {
                $LoginGetId(cTh.loginButton).click();
                evt.preventDefault();
            }
        }
    }

    this.onTimeOut = function() {
        var httpOut = cTh.init();
        if (!httpOut)
            return;
        httpOut.open("GET", "http://" + parent.location.hostname + "/ajaxLoginOut.aspx", true);
        httpOut.send(null);
        alert("系统繁忙，登陆超时，请稍后重试！");
    }

    this.login = function(userName, passwrod, remberMe, onCompletedCallback, onErrorCallback, onRunningCallback) {
        var http = cTh.init();
        if (!http)
            return

        http.open("POST", "http://" + parent.location.hostname + "/ajaxLogin.aspx", true);

        cTh.ajaxCallback.cb = cTh.onTimeOut;
        cTh.ajaxCallback.xmlHttp = http;


        var ajtcb = new ajaxTimeOutCallback(http, cTh.onTimeOut);
        http.onreadystatechange = function() {
            var state = http.readyState;
            if (http.readyState == 4) {
                if (http.status == 200) {
                    cTh.ajaxCallback.clear();
                    onCompletedCallback(http.responseText);
                } else {
                    onErrorCallback(http.status);
                }
            } else {
                if (onRunningCallback) {
                    onRunningCallback(state);
                }
            }
        }
        var params = "UserName=" + escape(userName) + "&Password=" + escape(passwrod) + "&RememberMe=" + remberMe;

        http.send(params);
        cTh.ajaxCallback.run();
    }

    this.onLoginRunning = function(state) {

    }
    this.onLoginCompleted = function(data) {
        var msg = new messageData(data);
        var code = msg.msgCode();
        var desc = msg.msgDesc();
        switch (code) {
            case 0:
                $LoginGetText(cTh.loginFailedId, desc);
                var regView = ajaxLogin.__RegisteredView;
                for (var i = 0; i < regView.length; i++) {
                    regView[i].showIn();
                    $LoginGetText(regView[i].loginStatusName, desc);
                    $LoginGetText(regView[i].loginFailedId, "");
                    regView[i].clear();
                }
                if (ajaxLogin.__TimerId == 0) {
                    ajaxLogin.__TimerId = window.setInterval(cTh.notifyServer, 2 * 60 * 1000);
                }
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                $LoginGetText(cTh.loginFailedId, desc);
                break;
            case 7:
                $LoginGetText(cTh.loginFailedId, desc);
                break;
            default:
                alert("网络超时，请重试！");
                break;

        }
        if (cTh.afterLogin) {
            cTh.afterLogin(code, desc);
        }
    }

    this.notifyServer = function() {
        var http = cTh.init();
        if (!http)
            return
        if (http) {
            http.open("POST", "http://" + parent.location.hostname + "/chkUserOnLine.aspx", false);
            http.send(null);
        }
    }
    this.isOnline = function(onCompletedCallback, onErrorCallback) {
        var http = cTh.init();
        if (!http)
            return;
        if (ajaxLogin.__IsChecked == 1)
            return;
        ajaxLogin.__IsChecked = 1;
        http.open("GET", "http://" + parent.location.hostname + "/ajaxChk.aspx", true);
        http.onreadystatechange = function() {
            var state = http.readyState;
            if (http.readyState == 4) {
                if (http.status == 200) {
                    onCompletedCallback(http.responseText);
                } else {
                    onErrorCallback(http.status);
                }
            }
        }
        http.send(null);
    }
    this.onCheckUserCompleted = function(data) {
        var msg = new messageData(data);
        var code = msg.msgCode();
        var regView = ajaxLogin.__RegisteredView;
        var tip = msg.msgDesc();
        switch (code) {
            case 0:

                for (var i = 0; i < regView.length; i++) {
                    regView[i].showIn();
                    $LoginGetText(regView[i].loginStatusName, tip);
                }
                if (ajaxLogin.__TimerId == 0) {
                    ajaxLogin.__TimerId = window.setInterval(cTh.notifyServer, 2 * 60 * 1000);
                }
                break;
            case 1:
                for (var i = 0; i < regView.length; i++) {
                    regView[i].showOut();
                    $LoginGetText(regView[i].loginStatusName, "");
                }
                break;
            default:
                break;
        }
        if (cTh.afterCheckUser) {
            cTh.afterCheckUser(code, tip);
        }
    }
    this.onHttpStatusError = function(status) {
        //  alert("服务器错误:"+status );
    }

}
