﻿/*--------------------------------------------------------------------------*/
/*                                                                          */
/*                  @author : yimeng(沂蒙老农) 2007-09-11                   */
/*                                                                          */
/*--------------------------------------------------------------------------*/
var AjaxRequest = {
    getTransport: function() {
        var xmlHttpRequest = false;
        try {
            xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e2) {
                xmlHttpRequest = false;
            }
        }
        if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
            xmlHttpRequest = new XMLHttpRequest();
        }
        return xmlHttpRequest;
    }
}
function Ajax(url, method) {
    this.xmlHttpRequest = AjaxRequest.getTransport();
    this.URL = url;
    this.method = "GET";
    if (arguments.length == 2) {
        this.method = method;
    }
}
Ajax.prototype = {
    doRequest: function() {
        var obj = this.xmlHttpRequest;
        var me = this;
        this.xmlHttpRequest.onreadystatechange = function() {
            if (obj.readyState == 4) {
                if (obj.status == 200)
                    me.onSuccess(obj.responseText, obj.responseXML);
                else
                    me.onFailure(obj.status);
            }
        }
        this.xmlHttpRequest.open(this.method, this.URL, true);
        this.xmlHttpRequest.setRequestHeader("If-Modified-Since", "0");
        this.xmlHttpRequest.send(null);
    },
    onSuccess: function() { },
    onFailure: function() { }
}



/*Example:  调用Ajax.txt
function doAjax()
{
var myAjax=new Ajax("A_GetCallOut.aspx?id=1234");
myAjax.onSuccess=function(txtResult)
{
document.getElementById("divid").innerHTML=txtResult;
}
myAjax.onFailure=function(st)
{
document.getElementById("divid").innerHTML="ErrorMsg";
}
myAjax.doRequest();
}
*/
/*****************************AJAX        处理XML*******************************************/

//   创建   XMLHttpRequest   对象(主函数不需改变) 
function createXMLHttpRequest() {
    if (window.XMLHttpRequest) {     //   Mozilla   浏览器 
        XMLHttpReq=new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {   //   IE   浏览器 
        try {
            XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
        }

        catch (e) {

            try {
                XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
        { }
        }
    }
}
//发送请求函数(提交xml格式的请求参数) 
function sendRequest(url,xml) {
    createXMLHttpRequest();
    XMLHttpReq.open("POST",url,true);
    XMLHttpReq.onreadystatechange=processResponse; //指定响应函数
    //XMLHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    XMLHttpReq.setRequestHeader("If-Modified-Since", "0");
   
    XMLHttpReq.send(xml); //发送请求 
}

//处理返回信息函数(处理xml格式的返回信息) 
function processResponse() {
    if (XMLHttpReq.readyState==4) {   //   判断对象状态
       
        if (XMLHttpReq.status==200) {   //   信息已经成功返回，开始处理信息
            try {
                procBusiness(); //处理信息函数
            } catch (e) {
                alert("读取响应出错：" + e.toString());
            } 

        } else {   //页面不正常
        window.alert("您所请求的页面有异常:" + XMLHttpReq.statusText);
        }
    }
}

