﻿var xmlHttp;
function createXmlHttpRequest() {
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
        if (xmlHttp.overrideMimeType) {
            xmlHttp.overrideMimeType("text/xml");
        }
    }
    else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (!xmlHttp) {
        window.alert("对不起，你的浏览器比较特殊，暂时不能使用本功能。");
    }
    return xmlHttp;
}
function CheckRetime() {

    createXmlHttpRequest();
    var url = "User/AjaxAction.aspx?op=re_time";
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                if (xmlHttp.responseText == "re_time_no") {
                    window.document.getElementById("TextErrror").innerHTML = "灌水机制已打开,请勿灌水!";
                    return false;
                } else {
                    var error = window.document.getElementById("TextErrror");
                    var text = window.document.getElementById("myAnswerContent");
                }
            } else {
                alert("发生系统故障，故障原因:\n" + xmlHttp.status);

            }
        }
    };
    xmlHttp.send(null);
}
function checkFM() {
    var error = window.document.getElementById("TextErrror");
    error.innerHTML = "";
    var text = window.document.getElementById("myAnswerContent");
     if (text.value == null || text.value == "") {
                        error.innerHTML = "请输入回答内容";
                        text.focus();
                        return false;
                    } else if (text.value == null || text.value == "") {
                        alert('请输入回答内容');
                        text.focus();
                        return false;
                    } else if (text.value.length < 5) {
                        error.innerHTML = "回答内容不能少于5个字！";
                        text.focus();
                        return false;
                    }
    createXmlHttpRequest();
    var url = "User/AjaxAction.aspx?op=re_time";
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                if (xmlHttp.responseText == "re_time_no") {
                    error.innerHTML = "灌水机制已打开,请勿灌水!";
                    return false;
                } else {
                document.forms("ReFM").submit();
                 }
            } else {
                alert("发生系统故障，故障原因:\n" + xmlHttp.status);
            }
        }
    }
    var str = window.document.getElementById("myAnswerContent");
    str.value = setContent(str.value);
    str.value = str.value.replace(/\r\n/ig, "{br}");
    xmlHttp.send(null);
    return true;
}
function redFM() {
    var word = document.search_form.word.value;
    //document.search_form._si.value="搜索";
    if (word == null || word == "") {
        alert('请输入关键词进行搜索！');
        return false;
    }
    word = encodeURIComponent(word);
    window.open('http://www.baidu.com/baidu?_si=%E6%90%9C%E7%B4%A2&tn=zyuwl_pg&cl=3&ct=2097152&si=www.yysohu.com&sts=www.yysohu.com&tr=mk3SLVN4HKm&ch=6&ie=UTF-8&word=' + word);
}
function setContent(str) {
    str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
    str.value = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
    //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
    return str;
}
function askFM() {
    var word = document.search_form.word.value;
    if (word == null || word == "") {
        alert('请输入关键词提问！');
        return false;
    }
    word = encodeURIComponent(word);
    window.open('http://www.yysohu.com/QuestionAdd.aspx?word=' + word);
}
function highlightWord(node, word) {
    // Iterate into this nodes childNodes     
    if (node.hasChildNodes) {
        var hi_cn;
        for (hi_cn = 0; hi_cn < node.childNodes.length; hi_cn++) {
            highlightWord(node.childNodes[hi_cn], word);
        }
    }
    // And do this node itself   
    if (node.nodeType == 3) {
        // text node         
        tempNodeVal = node.nodeValue.toLowerCase();
        tempWordVal = word.toLowerCase();
        if (tempNodeVal.indexOf(tempWordVal) != -1) {
            pn = node.parentNode;
            if (pn.className != "searchword") {
                // word has not already been highlighted!                 
                nv = node.nodeValue;
                ni = tempNodeVal.indexOf(tempWordVal);
                // Create a load of replacement nodes               
                before = document.createTextNode(nv.substr(0, ni));
                docWordVal = nv.substr(ni, word.length);
                after = document.createTextNode(nv.substr(ni + word.length));
                hiwordtext = document.createTextNode(docWordVal);
                hiword = document.createElement("span");
                hiword.className = "searchword";
                hiword.appendChild(hiwordtext);
                pn.insertBefore(before, node);
                pn.insertBefore(hiword, node);
                pn.insertBefore(after, node);
                pn.removeChild(node);
            }
        }
    }
}
function googleSearchHighlight() {
    if (!document.createElement) return;
    ref = document.referrer;
    if (ref.indexOf('?') == -1) return;
    qs = ref.substr(ref.indexOf('?') + 1);
    qsa = qs.split('&');
    for (i = 0; i < qsa.length; i++) {
        qsip = qsa[i].split('=');
        if (qsip.length == 1) continue;
        if (qsip[0] == 'q' || qsip[0] == 'p') {
            // q= for Google, p= for Yahoo             
            words = unescape(decodeURIComponent(qsip[1].replace(/\+/g, ' '))).split(/\s+/);
            for (w = 0; w < words.length; w++) {
                highlightWord(document.getElementsByTagName("body")[0], words[w]);
            }
        }
    }
}
function KeyDown() {
    redFM();
}
function Shref(str) {
    str = encodeURIComponent(str);
    window.open('http://www.baidu.com/s?tn=zyuwl_pg&ie=utf-8&wd=' + str);
}
function Caina(Reid, qid) {
    if (confirm('确定要采纳这个答案?')) {
        document.location.href = "../Action.aspx?op=caina&rid=" + Reid + "&qid=" + qid;
    }
}
window.onload = googleSearchHighlight; 
