﻿// JScript 文件

function ProcessXMLHttpRequest(url,onchange,SendData,other_args)
{
    var xmlHttp;        
    try//if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)// if (window.XMLHttpRequest)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    var xml_obj;
    xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 4)
            {
                switch(xmlHttp.status)
                {
                    case(12002):
                    {
                        alert("连接超时!");
                        break;
                    }
                    case(12029,12030,12031):
                    {
                        alert("连接被丢失");
                        break;
                    }
                    case(12152):
                    {
                        alert("服务器断开了连接");
                        break;
                    }
                    case(13030):
                    {
                        alert("连接错误");
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                onchange(xmlHttp.responseXML,other_args,xmlHttp.responseText,xmlHttp.status);
            }
        }
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
    xmlHttp.send(SendData);
    this.state = xmlHttp.readyState;
    this.xmlobj = xmlHttp.responseXML;
    /*0 (uninitialized) 
1 (loading) 
2 (loaded) 
3 (interactive) 
4 (complete) */
}

function SelectSingleNode(xmlobj,nodename)  //选择单个节点
{
    if (xmlobj == null)
    {
        return null;
    }
    for (var cot = 0; cot < xmlobj.childNodes.length;cot++)
    {
        if (xmlobj.childNodes[cot].nodeName.toString() == nodename.toString())
        {
            return xmlobj.childNodes[cot];
        }
    }
    return null;
}

function Bool(boolvalue)    //选择单个节点
{
    if (boolvalue != null && typeof boolvalue != "undefined")
    {
        if (boolvalue.toString().toLowerCase() == "false" || boolvalue.toString() == "0" || boolvalue.toString() == "" || boolvalue == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}
    
function $(client_id)
{
    return document.getElementById(client_id);
}

function NodeValue(Node)
{
    if (typeof Node.text != "undefined")
    {
        return Node.text;
    }
    else if (typeof Node.innerText  != "undefined")
    {
        return Node.innerText;
    }
    else if (typeof Node.textContent != "undefined")
    {
        return Node.textContent
    }
}

function ProcessSimpleHttpRequest(url,onchange,SendData,other_args)
{
    var xmlHttp;
    try//if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)// if (window.XMLHttpRequest)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    
    xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 4)
            {
                onchange(xmlHttp.responseText,other_args);
            }
        }
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(SendData);
}

function GetCookie(cookie_name) //Get the value of a specific cookie
{
    CookieExp = new RegExp("Client" + cookie_name + "=[^;]+","gi");
    var cookie_value = document.cookie.match(CookieExp);
    if (cookie_value != null)
    {
        CookieExp = new RegExp("^Client" + cookie_name + "=([^;]+)$","gi");
        cookie_value = cookie_value[0].replace(CookieExp,"$1");
        return unescape(cookie_value);
    }
    else
    {
        return "";
    }
}

function SetCookie(cookie_name,cookie_value)    //Set a value to a client cookie
{
    var expires = new Date();
    expires.setTime(expires.getTime() + 3 * 30 * 24 * 60 * 60 * 1000);
/*   三个月 x 一个月当作 30 天 x 一天 24 小时
x 一小时 60 分 x 一分 60 秒 x 一秒 1000 毫秒 */
    document.cookie = "Client" + cookie_name + "=" + escape(cookie_value) + ";expires=" + expires.toGMTString();
}

function QueryString(str_name)
{
    var query_str_regex = new RegExp("[^\\?\\&]+=[^\\&]+","g"),str_finder_regex = new RegExp("^" + str_name + "=","");
    var query_str = query_str_regex.exec(document.URL);
    while(query_str)
    {
        if (str_finder_regex.test(query_str.toString()))
        {
            return unescape(query_str.toString().replace(str_finder_regex,""));
        }
        query_str = query_str_regex.exec(document.URL);
    }
    return "";
}

function RemoveHtml(source_str)
{
    return source_str.replace(/<(.*?)>/gm,"");
}

function AdaptFromXML(source_str)
{
    source_str = source_str.replace(/\[\[(.*?)]]/gm,"<$1>");
    source_str = source_str.replace(/\[\[/,"");
    source_str = source_str.replace(/]]/,"");
    return source_str;
}

function AdaptForXML(source_str)
{
    source_str = source_str.replace(/<(.*?)>/gm,"[[$1]]");
    source_str = source_str.replace(/</,"");
    source_str = source_str.replace(/>/,"");
    return source_str;
}

function ShowHide(PanelPicker,UseClientHolder)
{
    var SideBarPanel;
    if (UseClientHolder)
    {
        SideBarPanel = $(SideBarPanelHolder[PanelPicker].toString());
    }
    else
    {
        SideBarPanel = $(PanelPicker.toString());
    }
    if (SideBarPanel.style.display == "")
    {
        SideBarPanel.style.display = "none";
    }
    else
    {
        SideBarPanel.style.display = "";
    }
}

function SetSelectorIndex(Selector,value_to_set)
{
    for (var cot = 0 ;cot < Selector.length;cot++)
    {
        if (Selector[cot].value.toString() == value_to_set.toString())
        {
            Selector.selectedIndex = cot;
            return
        }
    }
}

function StringBuilder(value)
{
    this.strings = new Array("");
    this.Append(value);
}
// Appends the given value to the end of this instance.
StringBuilder.prototype.Append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}
// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}
// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}

function $Class(class_name)
{
    var element_list = new Array();
    for (var cot = 0,element_cot = 0;cot < document.all.length;cot++)
    {
        if (document.all[cot].className == class_name)
        {
            element_list[element_cot] = document.all[cot];
            element_cot++;
        }
    }
    return element_list;
}
