﻿var ac_input;
var ac_div;
var ac_list;
var ac_timeout;
var auto_interval;
var lockdispose = 0;
function acInit() {
    ac_input = document.getElementById("acinput");
    if (typeof (acInitEvent) != 'undefined' && typeof (acInitDiv) != 'undefined') {
        acInitEvent();
        acInitDiv();
    }
    autoDetection();
    if (typeof (auto_interval) != 'undefined') {
        clearInterval(auto_interval);
    }
    auto_interval = setInterval(autoDetection, 180000);
}
function onLine() {
    XMLHttp.sendReq("POST", "/AutoDetection.aspx?action=online", null, lineResult);
}
function offLine() {
    XMLHttp.sendReq("POST", "/AutoDetection.aspx?action=offline", null, lineResult);
}
function lineResult(obj) {
    if (obj.readyState == 4) {
        if (obj.status == 200 || obj.status == 304) {
            if (obj.responseText == "notenter") {
                alert('用户登录失效！');
                window.location.href = 'http://swg.6621.cn/enter.html';
            } else if (obj.responseText == "on") {
                document.getElementById("ishidden").innerHTML = "[<a href=\"javascript:void(0);\" onclick=\"offLine();return false;\">在线</a>]";
            } else if (obj.responseText == "off") {
                document.getElementById("ishidden").innerHTML = "[<a href=\"javascript:void(0);\" onclick=\"onLine();return false;\">隐身</a>]";
            }
        }
    }
}
function autoDetection() {
    try {
        if (GetCookie('MZUser') != null || GetCookie('MZUser') != '') {
            XMLHttp.sendReq("POST", "/AutoDetection.aspx", null, autoResult);
        }
    }
    catch (err) { }
}
function autoResult(obj) { }
function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function acInitEvent() {
    ac_input.onkeyup = function() {
        if (getEvent().keyCode != 38 && getEvent().keyCode != 40 && getEvent().keyCode != 13) {
            acDispose();
            if (ac_input.value != '') {
                if (typeof (ac_timeout) != 'undefined') {
                    clearTimeout(ac_timeout);
                }
                ac_timeout = setTimeout("acAjax();", 500);
            }
        }
    };
    ac_input.onblur = acDispose;
}
function acInitDiv() {
    var cacdiv = document.createElement("div");
    cacdiv.id = 'acdiv';
    var caclist = document.createElement('div');
    caclist.id = 'aclist';
    var cacclose = document.createElement('div');
    cacclose.id = 'acclose';
    cacclose.innerHTML = '<div class="ac_close"><a href="javascript:void(0);" onclick="lockdispose = 0;acDispose();return false;">关闭</a></div>';
    document.body.appendChild(cacdiv);
    cacdiv.appendChild(caclist);
    cacdiv.appendChild(cacclose);
    ac_div = document.getElementById('acdiv');
    ac_div.onmouseover = function() { lockdispose = 1; }
    ac_div.onmouseout = function() { lockdispose = 0; ac_input.focus(); }
    ac_list = document.getElementById('aclist');
    ac_div.style.top = (ac_input.offsetTop + ac_input.offsetHeight) + "px";
    ac_div.style.left = ac_input.offsetLeft + "px";
    ac_div.style.width = ac_input.offsetWidth + "px";
    ac_div.style.visibility = "hidden";
}
function acDispose() {
    if (!lockdispose) {
        ac_input.onkeydown = function() { };
        if (ac_div) {
            if (typeof (ac_timeout) != 'undefined') {
                clearTimeout(ac_timeout);
            }
            ac_div.style.visibility = "hidden";
            ac_list.innerHTML = '';
        }
    }
}
function acAjax() {
    if (ac_input.value != '') {
        XMLHttp.sendReq("POST", "/AutoComplateSearch.aspx?key=" + escape(ac_input.value), null, getData);
        ac_div.style.top = (ac_input.offsetTop + ac_input.offsetHeight) + "px";
        ac_div.style.left = ac_input.offsetLeft + "px";
        ac_div.style.width = ac_input.offsetWidth + "px";
    }
}
function getData(obj) {
    if (obj.readyState == 4) {
        if (obj.status == 200 || obj.status == 304) {
            if (obj.responseText != '') {
                var jsonarray = eval(obj.responseText);
                if (jsonarray.length > 0) {
                    ac_list.innerHTML = '';
                    for (var index = 0; index < jsonarray.length; index++) {
                        ac_list.innerHTML += '<div class="acdiv-b">' + jsonarray[index] + '</div>';
                    }
                    setRowEvent();
                    acDivShow();
                }
            }
        }
    }
}
function acDivShow() {
    if (ac_div) {
        ac_div.style.visibility = "visible";
    }
}
function rowOnMouseOver() {
    this.className = "aclist_a";
}
function rowOnMouseOut() {
    this.className = "aclist_b";
}
function rowOnClick() {
    lockdispose = 0;
    ac_input.value = this.innerHTML;
    acDispose();
    document.getElementById("acform").submit();
}
function setRowEvent() {
    for (var i = 0; i < ac_list.childNodes.length; i++) {
        ac_list.childNodes[i].onmouseover = rowOnMouseOver;
        ac_list.childNodes[i].onmouseout = rowOnMouseOut;
        ac_list.childNodes[i].onclick = rowOnClick;
    }
    var justlist = -1;
    var maxlist = ac_list.childNodes.length - 1;
    if (maxlist > justlist) {
        ac_input.onkeydown = function() {
            if (getEvent().keyCode == 38) {
                justlist--;
                if (justlist == -2 || justlist == -1) {
                    justlist = maxlist;
                }
            } else if (getEvent().keyCode == 40) {
                justlist++;
                if (justlist == maxlist + 1) {
                    justlist = 0;
                }
            } else if (getEvent().keyCode == 13) {
                acDispose();
                document.getElementById("acform").submit();
            }
            for (var i = 0; i < ac_list.childNodes.length; i++) {
                if (i == justlist) {
                    ac_list.childNodes[i].className = "aclist_a";
                    ac_input.value = ac_list.childNodes[i].innerHTML;
                } else {
                    ac_list.childNodes[i].className = "aclist_b";
                }
            }
        };
    }
}
function getEvent() {
    return window.event || arguments.callee.caller.arguments[0];
}
if (window.attachEvent) {
    window.attachEvent("onload", acInit);
} else if (window.addEventListener) {
    window.addEventListener("load", acInit, false);
}