// JavaScript Document
/*封装XMLHTTP对象*/
function AjaxObject(){
	var self = this;//映射自己
	var requestType = "get";//请求类型 get或post 默认get
	var isSynchro = false;//是否同步 true或flse 默认true
	var contentType = "text";//接收返回类型 text或xml
	var listenerList = new Array();//监听器集合
	var coreObject;//核心对象
	
	/**
	* 请求状态改变
	* 注:在状态改变时,该函数会通知接口.
	* 所以要想得到通知,必须放入监听函数
	*/
	this.readyStateChange = function(){
		if(coreObject.readyState == 4){
			if(coreObject.status == 200){
				for(var i=0;i<listenerList.length;i++){
					if(contentType == "text")listenerList[i](coreObject.responseText);
					else listenerList[i](coreObject.responseXML);
				}
			}
		}
	}
	
	/**
	* 放入数据监听函数
	* functionObject - 函数对象
	* 该函数对象接收1个参数:centent
	*/
	this.addLoadedListener = function(functionObject){
		if(functionObject && typeof(functionObject) == "function"){
			listenerList.push(functionObject);
		}
	}
	
	/**
	* 移除监听函数
	*/
	this.removeLoadedListener = function(functionObject){
		for(var i=0;i<listenerList.length;i++){
			if(listenerList[i] == functionObject){
				listenerList.splice(i,1);
				break;
			}
		}
	}
	
	/**
	* 创建新的核心对象
	*/
	this.careteObject = function(){
		if(window.ActiveXObject){
			try{
				coreObject = new ActiveXObject("MSXML2.XMLHTTP");
				coreObject.onreadystatechange = this.readyStateChange;
			}
			catch(e){
				coreObject = new ActiveXObject("Microsoft.XMLHTTP");
				coreObject.onreadystatechange = this.readyStateChange;
			}
		}
		else{
			coreObject = new XMLHttpRequest();
		}
	}
	
	/**
	* 设置接收对象类型 
	* type - text或xml
	*/
	this.setContentType = function(type){
		if(type == "text" || type == "xml"){
			contentType = type;
		}
	}
	
	/**
	* 获取接收对象类型
	*/
	this.getContentType = function(){
		return contentType;
	}
	
	/**
	* 设置请求类型
	* type - "get"或"post"
	*/
	this.setRequestType = function(type){
		if(type == "get" || type == "post"){
			requestType = type;
		}
	}
	
	/**
	* 获取请求类型
	*/
	this.getRequestType = function(){
		return requestType;
	}
	
	/**
	* 设置请求是否与服务器同步
	* synchro - true或false;
	*/
	this.setSynchro = function(synchro){
		if(synchro == false)isSynchro = false;
		else isSynchro = true;
	}
	
	/**
	* 获取请求是否与服务器同步
	*/
	this.getSynchro = function(){
		return isSynchro;
	}
	
	/**
	* 发出请求服务器
	* url - 请求地址
	*/
	this.request = function(url){
		this.careteObject();
		coreObject.open(requestType,url,isSynchro);
		coreObject.send(null);
	}
}
