﻿// JScript 文件

function Custom_Ajax()
{
    var ajax_xmlHttp = null;
    var ajax_ReceiveFunction = null;
    var ajax_IsReturnXml = null;
    
    var ajax_Control = null;
    var ajax_DataTable = null;
    var ajax_DataValue = null;
    var ajax_DataText = null;
    var ajax_Hidden = null;
    var ajax_IsFirstValue = null;
    var ajax_FirstValue = null;
    
    //Create XmlHttp Object
    this.CreateXmlHttp = function()
    {
        if (window.ActiveXObject)
        {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
            return new XMLHttpRequest();
        }
    }
    
    //Send Data With XmlHttp Object
    this.Send = function(ReceivePage, ReceiveContent, ReceiveFunction, IsReturnXml)
    {
        if (ReceivePage != null)
        {
            ajax_ReceiveFunction = ReceiveFunction;
            ajax_IsReturnXml = IsReturnXml;
            
            ajax_xmlHttp = this.CreateXmlHttp();
            
            ajax_xmlHttp.onreadystatechange=this.Receive;
            ajax_xmlHttp.open("post", ReceivePage + ReceiveContent, true);
            ajax_xmlHttp.send(null);
        }
    }
    
    this.SendSelectControl = function(ReceivePage, ReceiveContent, ReceiveControl, DataTable, DataValue, DataText, HiddenControl, IsFirstValue, FirstValue, IsReturnXml)
    {
        if (ReceivePage != null)
        {
            ajax_Control = ReceiveControl;
            ajax_DataTable = DataTable;
            ajax_DataValue = DataValue;
            ajax_DataText = DataText;
            ajax_Hidden = HiddenControl;
            ajax_IsFirstValue = IsFirstValue;
            ajax_FirstValue = FirstValue;
            ajax_IsReturnXml = IsReturnXml;
            
            ajax_xmlHttp = this.CreateXmlHttp();
            
            ajax_xmlHttp.onreadystatechange=this.ReceiveSelectControl;
            ajax_xmlHttp.open("post", ReceivePage + ReceiveContent, true);
            ajax_xmlHttp.send(null);
        }
    }
    
    //Receive Data With XmlHttp Object
    this.Receive = function()
    {
        if (ajax_xmlHttp.readyState == 4)
        {
            if (ajax_xmlHttp.status == 200)
            {
                if (ajax_IsReturnXml)
                {
                    ajax_ReceiveFunction(ajax_xmlHttp.responseXML);
                }
                else
                {
                    ajax_ReceiveFunction(ajax_xmlHttp.responseText);
                }
            }
        }
    }
    
    this.ReceiveSelectControl = function()
    {
        if (ajax_xmlHttp.readyState == 4)
        {
            if (ajax_xmlHttp.status == 200)
            {
                if (ajax_IsReturnXml)
                {
                    if (ajax_Control != null)
                    {
                        var xmlDoc = ajax_xmlHttp.responseXML;
                        
                        if (xmlDoc != null)
                        {
                            var nodes = xmlDoc.getElementsByTagName(ajax_DataTable);
                            
                            while(ajax_Control.childNodes.length > 0)
                            {
                                ajax_Control.removeChild(ajax_Control.childNodes[ajax_Control.childNodes.length-1]);
                            }
                            
                            if (ajax_IsFirstValue)
                            {
                                var coption = document.createElement("option");
                                ajax_Control.options.add(coption);
                                coption.value = "0";
                                coption.text = ajax_FirstValue;
                            }
                            
                            for (var i=0, j=nodes.length; i<j; i++)
                            {
                                var coption = document.createElement("option");
                                ajax_Control.options.add(coption);
                                coption.value = nodes[i].getElementsByTagName(ajax_DataValue)[0].firstChild.nodeValue;
                                coption.text = nodes[i].getElementsByTagName(ajax_DataText)[0].firstChild.nodeValue;
                            }
                            
                            if (nodes.length > 0 && ajax_Hidden != null)
                            {
                                    ajax_Hidden.value = nodes[0].getElementsByTagName(ajax_DataValue)[0].firstChild.nodeValue;
                            }
                        }
                    }
                }
            }
        }
    }
}

var CAjax = new Custom_Ajax();

