/**
*   封装了XMLHTTP的通用对象
*	author wx
*   用法：
*   var cl = new net.ContentLoader("process.aspx",cb_getBulletin,cb_onerror);
*/



/**********ContentLoader对象的封装***************/

var net = new Object();  //名称空间对象

//状态常量
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

//ContentLoader对象  构造函数
//  url:提交的链接地址
//  onload: 处理结果的函数
//  onerror: 处理出错结果的函数 
net.ContentLoader = function(url, onload,loading,onerror,oConfig){
    this.url = url;
    this.req = null;  //xmlhttp对象
    this.onload = onload;
    this.loading = loading;
    this.conf = oConfig;
    this.onerror = (onerror) ? onerror : this.defaultError;  //如果未设置则调用默认的错误处理函数
    this.loadXMLDoc(url);
}

//ContentLoader对象的方法
net.ContentLoader.prototype={
    loadXMLDoc : function(url){
    
        //创建xmlhttp对象
        if(window.XMLHttpRequest){
            this.req = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
        if(this.req){
            try{
                var loader = this;  //设置一个变量存储ContentLoader对象本身的引用
                //设置回调函数
                this.req.onreadystatechange = function(){
                    loader.onReadyState.call(loader);
                }
                //发送请求
                this.req.open('Get',url,true);
                this.req.send(null)
            }
            catch(err){
                this.onerror.call(this);
            }
        }
    },//end loadXMLDoc
    
    onReadyState : function(){
        var req = this.req;             //获取当前XMLHTTP对象
        var ready = req.readyState;     //获取XMLHTTP的执行状态
        if(this.loading!=null&&this.loading!=undefined&&this.loading!="")
        {
			this.loading.call(this,this.conf);
        }
        if(ready == net.READY_STATE_COMPLETE){
            var httpStatus = req.status;
            if(httpStatus==200 || httpStatus==0){
                this.onload.call(this,this.conf);    //当请求执行正常时调用处理结果的函数
            }else{
                //this.onerror.call(this);   //否则调用错误时处理结果的函数
            }
        }
    },//end onReadyState
    
    defaultError:function(){
        alert("error fetching data!"
            +"\n\n readyState: "+ this.req.readyState
            +"\nstatus: "+ this.req.status
            +"\nheaders: "+ this.req.getAllResponseHeaders());
    }
    
}

/**********ContentLoader对象的封装结束***************/
