﻿function _(id) {
    return document.getElementById(id);
}

//下拉表当前选中项的索引 
var currentIndex = -1;

var requestObj = null;
//自动完成
function autoComplete(e) {
    //如果按下 向上, 向下 或 回车

    var evt = e || window.event;
    if (evt != null && (evt.keyCode == 38 || evt.keyCode == 40 || evt.keyCode == 13)) {
        //选择当前项 
        selItemByKey(evt);
    }
    else //向服务器发送请求
    {
        //恢复下拉选择项为 0 
        currentIndex = -1;

        //设置关键词类型
        var targetType = "courseClassRecode";
        var hidType = _("searchType").value;
        if (hidType == "1") {
            targetType = "courseClassRecode";
        }
        else if (hidType == "2") {
            targetType = "courseClassComputer";
        }
        else if (hidType == "3") {
            targetType = "courseClassLanguage";
        }
        else if (hidType == "4") {
            targetType = "courseClassJob";
        }

        //开始请求
        if (window.XMLHttpRequest) {
            requestObj = new XMLHttpRequest();
            if (requestObj.overrideMimeType) {
                requestObj.overrideMimeType("text/xml");
            }
        }
        else if (window.ActiveXObject) {
            try {
                requestObj = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                requestObj = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }

        requestObj.onreadystatechange = displayResult;
        requestObj.open("GET", "/AutoComplete.aspx?Text=" + encodeURIComponent(_("searchKeyword").value) + "&TargetType=" + targetType + "&tmp=" + Math.random(), true);
        requestObj.send(null);
    }
}

//显示结果
function displayResult() {
    if (requestObj.readyState == 4) {
        showData();
        _("divContent").style.display = "";
    }
}

//显示服务器返回的结果 ,并形成下拉表
function showData() {

    if (window.ActiveXObject) {
        //获取数据
        var doc = new ActiveXObject("MSXML2.DOMDocument.3.0");
        doc.loadXML(requestObj.responseText);

        //显示数据的xslt
        var docStyle = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
        docStyle.async = false;
        docStyle.load("/coustomAutoComplete.xslt");

        var docTemplate = new ActiveXObject("MSXML2.XSLTemplate");
        docTemplate.stylesheet = docStyle;

        //通过xslt转换xml数据 
        var processor = docTemplate.createProcessor();
        processor.input = doc;
        processor.transform();
        var res = processor.output;

        //显示转后后的结果
        _("divContent").innerHTML = res;
    }
    else if (document.implementation && document.implementation.createDocument) {
        try {
            var oParser = new DOMParser();
            var xmldoc = oParser.parseFromString(requestObj.responseText, "text/xml");
            xmldoc.async = false;

            var xmldocStyle = null;
            xmldocStyle = document.implementation.createDocument("", "", null);
            xmldocStyle.async = false;
            xmldocStyle.load("/coustomAutoComplete.xslt");

            var processor = new XSLTProcessor();
            processor.importStylesheet(xmldocStyle);
            var res = processor.transformToDocument(xmldoc); //返回一个XMLDocument对象

            //显示转后后的结果
            _("divContent").innerHTML = new XMLSerializer().serializeToString(res);
        }
        catch (e) {
            var xmldocStyle = new XMLHttpRequest();
            if (xmldocStyle.overrideMimeType) {
                xmldocStyle.overrideMimeType("text/xml");
            }
            xmldocStyle.onreadystatechange = function() {
                if (xmldocStyle.readyState == 4) {
                    if (xmldocStyle.status == 200) {

                        xsltProcessor = new XSLTProcessor();
                        xsltProcessor.importStylesheet(xmldocStyle.responseXML);
                        var res = (new XMLSerializer()).serializeToString(xsltProcessor.transformToFragment(requestObj.responseXML, document));

                        //显示转后后的结果
                        _("divContent").innerHTML = res;
                    }
                }
            };
            xmldocStyle.open("GET", "/coustomAutoComplete.xslt", true);
            xmldocStyle.send(null);
        }
    }
    else {
        alert('自动完成不支持此浏览器');
    }
}

//通过键盘选择下拉项 
function selItemByKey(evt) {
    //下拉表 
    var tbl = _("tblContent");
    if (!tbl) {
        return;
    }
    //下拉表的项数
    var maxRow = tbl.rows.length;
    //向上 
    if (evt.keyCode == 38 && currentIndex > 0) {
        tbl.rows[currentIndex].style.backgroundColor = "";
        currentIndex--;
        tbl.rows[currentIndex].style.backgroundColor = "#CCC";
    }
    //向下 
    else if (evt.keyCode == 40 && currentIndex < maxRow - 1) {
        if (currentIndex != -1) {
            tbl.rows[currentIndex].style.backgroundColor = "";
        }
        currentIndex++;
        tbl.rows[currentIndex].style.backgroundColor = "#CCC";
    }
    //回车
    else if (evt.keyCode == 13) {
        selSearchValue(tbl.rows[currentIndex].cells[0]);
        return;
    }
    _("searchKeyword").value = tbl.rows[currentIndex].cells[0].innerHTML;
}


//选择下拉表中当前项的值 ,用于按回车或鼠标单击选中当前项的值
function selSearchValue(tdtext) {
    _("searchKeyword").value = tdtext.innerHTML;
    initList();
}


//文本框失去焦点时 设置下拉表可见性
function setDisplay() {
    //获取当前活动td的表格
    if (document.activeElement.tagName == "TD") {
        var tbl = document.activeElement.parentElement.parentElement.parentElement;
        //如果不是下拉表,则隐藏 下拉表
        if (tbl.id != "tblContent") {
            initList();
        }
        else {
            return;
        }
    }
    else {
        window.setTimeout("initList()", 600);
    }
}

function initList() {
    _("divContent").style.display = 'none';
    _("divContent").innerHTML = "";
    currentIndex = -1;
}



function ChangeSearchType(type) 
{
    TabMove3(1, type, 6, '');
    _("searchType").value = type;
}

function SearchText(a, sText) {

    if (sText.length > 50) {
        alert("搜索内容太长");
        return false;
    }

    var hidType = _("searchType").value;
    if (hidType == "1") {
        a.href = "http://6318.cn/course/List.aspx?kw=" + encodeURIComponent(sText);
    }
    else if (hidType == "2") {
        a.href = "http://6318.cn/news6318/articleList.aspx?kw=" + encodeURIComponent(sText);
    }
    else if (hidType == "3") {
        a.href = "http://6318.cn/OrgList.aspx?kw=" + encodeURIComponent(sText);
    }
    else if (hidType == "4") {
        a.href = "http://6318.cn/UserArticleList.aspx?kw=" + encodeURIComponent(sText);
    }
    else if (hidType == "5") {
        a.href = "http://6318.cn/ask/QuestionList.aspx?kw=" + encodeURIComponent(sText);
    }
    else if (hidType == "6") {
        a.href = "http://6318.cn/zsdl/PositionList.aspx?tp=0&kw=" + encodeURIComponent(sText);
    }
}




function getParameter(name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(location.href))
        return decodeURIComponent(RegExp.$2.replace(/\+/g, " "));
    return "";
}

function InitParameterToSearchText() {
    _("searchKeyword").value = getParameter('kw');
    if (window.location.href.toLowerCase().indexOf("/course/list.aspx") > 0) {
        TabMove3(1, 1, 6, '');
        _("searchType").value = "1";
    }
    else if (window.location.href.toLowerCase().indexOf("/news6318/articlelist.aspx") > 0) {
        TabMove3(1, 2, 6, '');
        _("searchType").value = "2";
    }
    else if (window.location.href.toLowerCase().indexOf("/orglist.aspx") > 0) {
        TabMove3(1, 3, 6, '');
        _("searchType").value = "3";
    }
    else if (window.location.href.toLowerCase().indexOf("/userarticlelist.aspx") > 0) {
        TabMove3(1, 4, 6, '');
        _("searchType").value = "4";
    }
    else if (window.location.href.toLowerCase().indexOf("/ask/questionlist.aspx") > 0) {
        TabMove3(1, 5, 6, '');
        _("searchType").value = "5";
    }
    else if (window.location.href.toLowerCase().indexOf("/zsdl/positionlist.aspx") > 0) {
        TabMove3(1, 6, 6, '');
        _("searchType").value = "6";
    }
}