/**
 * AJAX操作方法集合
 *
 * @author: gao shangyong <shangyong@staff.sina.com.cn>
 * @version: 1.0
 *
 *  Example:
 *  
 *  ajax = new class_ajax();
 *	ajax.init();
 *	ajax.set_method('GET');
 *	ajax.set_event(func);   //参数不能加引号
 *	ajax.set_url(url);
 *	ajax.add_param("classId", itemId);
 *	ajax.add_param("layer", layer);
 *	ajax.submit();
 *
 *  var text = ajax.get_content();
 */
function class_ajax()
{
	/**
	 * xml请求对象
	 */
	this.xmlHttp = null;
	/*
	 * 请求的目标url
	 */
	this.url = "";
	/**
	 * http请求的方法 GET or POST
	 */
	this.method = "";
	/**
	 * 状态改变的事件触发函数
	 */
	this.event = "";
	/**
	 * 是否为异步连接
	 */
	this.async = true;
	/**
	 * REQUEST参数
	 */
	this.params = new Array;
	
	
	/**
	 * 初始化xmlHttpRequest对象
	 *
	 */
	this.init = function()
	{
		if(this.xmlHttp == null)
		{
			if (window.XMLHttpRequest)
		    {
		        this.xmlHttp = new XMLHttpRequest();
		    }
		     else if (window.ActiveXObject)
		     {
		        this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		        if(!this.xmlHttp)
		        {
		        	this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		        }
		    }
		}
		if(this.xmlHttp == null)
		{
			alert("invalid xmlHttpRequest!");
			return false;
		}
	}//end function
	
	/**
	 * 设置目标请求方法
	 */
	this.set_method = function(method)
	{
		if(method != 'POST' && method != 'GET')
		{ 
			alert("invalid method!");
			return false;
		}
		this.method = method;
	}//end function
	
	/**
	 * 获取目标请求方法
	 */
	this.get_method = function()
	{
		return this.method;
	}//end function
	
	/**
	 * 设置状态该表要触发的事件
	 */
	this.set_event = function(event)
	{
		this.event = event;
	}//end function
	
	/**
	 * 获取 event
	 */
	this.get_event = function()
	{
		return this.event;
	}//end function
	
	/**
	 * 设置要请求的目标url
	 */
	this.set_url = function(url)
	{
		this.url = url;
	}//end function
	
	/**
	 * 获取 url
	 */
	this.get_url = function()
	{
		return this.url;
	}//end function
	
	/**
	 * 添加REQUEST 参数
	 */
	this.add_param = function(key, val)
	{
		var tmpCount = this.params.length;
		this.params[tmpCount] = key + "=" + val; 
 	}//end function
 	
 	/**
 	 * 设置请求为异步连接
 	 */
 	this.set_async = function()
 	{	
 		this.async = true;
 	}//end function
 	
 	/**
 	 * 取消异步连接，即设置为同步连接
 	 */
 	this.set_sync = function()
 	{
 		this.async = false;
 	}//end function
 	
 	/**
	 * 发送请求
	 */
	this.submit = function()
	{
		this.xmlHttp.onreadystatechange = this.event;
     	var send_params = null;
		if(this.params.length > 0)
		{
			send_params = this.params.join('&');
			if(this.method == 'POST')
			{
				this.xmlHttp.open(this.method, this.url, this.async);
				//this.xmlHttp.setRequestHeader("Method", this.method + this.url + " HTTP/1.1");
				this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}else
			{
				this.xmlHttp.open(this.method, this.url + "?" + send_params, this.async);
			}
		}else
    {
      this.xmlHttp.open(this.method, this.url, this.async);
    }
		this.xmlHttp.send(send_params);
	}//end function
	
	/**
	 * return Data
	 * @param string type : "xml" or "text"
	 */
	this.get_content = function(type)
	{
		if(type == 'xml')
		{
			return this.xmlHttp.responseXML.documentElement;
		}else
		{
			return this.xmlHttp.responseText;
		}
	}
	
	/**
	 * 检测事件是否完成
	 */
	this.complete = function()
	{
		if(this.xmlHttp.readyState == 4)
		{
			return true;
		}
		return false;
	}//end function
	
	/*
	 * 检测请求是否成功
	 */
	this.success = function()
	{
		if (this.complete() && this.xmlHttp.status == 200) {
 			return true;       	
        }
        return false;
	}//end function
	
	/**
	 * 获取消息提示
	 */
	this.get_statusText = function()
	{
		return this.xmlHttp.statusText;
	}//end function
}//end class