
/*-----------------------------------------------
ajax应用类
时间:2007-6-20
-----------------------------------------------*/
Ajax = function(){
	var xmlhttp;

	try
	{
		xmlhttp = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();		
	}
	catch (e)
	{
		return false;		
	}

	/*参数初始化*/
	this.url = arguments[0]?arguments[0]:"";
	this.func = arguments[1]?arguments[1]:function(obj){return null};
	this.param = arguments[2]?arguments[2]:"";
	this.method = arguments[3]?arguments[3]:"POST";
	this.async = arguments[4]?arguments[4]:true;
	this.type = arguments[5]?arguments[5]:"text"; /*如果操作XML较频繁请设置成xml*/

	this.send = function(){
		var exefunc;

		/*参数初始化*/
		this.url = arguments[0]?arguments[0]:this.url;
		this.func = arguments[1]?arguments[1]:this.func;
		this.param = arguments[2]?arguments[2]:this.param;
		this.method = arguments[3]?arguments[3]:this.method;
		this.async = arguments[4]?arguments[4]:this.async;
		this.type = arguments[5]?arguments[5]:this.type;
		
		exefunc = this.func;
		
		/*对象设置*/
		if (!this.url)
		{
			return false;
		}

		/*处理发送*/
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4)
			{
				if(xmlhttp.status==200)
				{
					if (this.type=="xml")
					{
						return exefunc(xmlhttp.responseXML);
					}
					else
					{
						return exefunc(xmlhttp.responseText);
					}
				}
				else
				{
					return exefunc("加载出错!");
				}
			}
		}		

		xmlhttp.open(this.method,this.url,this.async);

		if(this.type=="xml")
		{
			xmlhttp.setRequestHeader("Content-Type","text/xml");
		}
		else
		{
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		}

		if(this.method=="POST")
		{
			xmlhttp.setRequestHeader("Content-Length",(this.param.length)); 
			xmlhttp.send(this.param);
		}
		else
		{
			xmlhttp.send(null);
		}
	}

	/*get*/
	this.get = function(){
		this.url = arguments[0]?arguments[0]:this.url;
		this.func = arguments[1]?arguments[1]:this.func;
		this.type = arguments[2]?arguments[2]:this.type;
		if (!this.url&&!this.func&&!this.type)
		{
			return false;
		}
		this.send(this.url,this.func,"","GET",true,this.type);
	}
	
	//同步
	this.tbget = function(){
		this.url = arguments[0]?arguments[0]:this.url;
		this.func = arguments[1]?arguments[1]:this.func;
		this.type = arguments[2]?arguments[2]:this.type;
		//this.async=arguments[3]?arguments[3]:this.async;
		if (!this.url&&!this.func&&!this.type)
		{
			return false;
		}
		
		this.send(this.url,this.func,"","GET","false",this.type);
		
	}

	/*post*/
	this.post = function(){
		this.url = arguments[0]?arguments[0]:this.url;
		this.func = arguments[1]?arguments[1]:this.func;
		this.param = arguments[2]?arguments[2]:this.param;
		this.method = arguments[3]?arguments[3]:this.method;
		this.type = arguments[4]?arguments[4]:this.type;


	
		if(!this.url&&!this.func&&!this.param)
		{
			return false;
		}

		if(this.method.toUpperCase()=="POST")
		{
			this.send(this.url,this.func,this.param,"POST",true,this.type);
		}
		else
		{
			if(this.url.indexOf("?")>0)
			{
				this.send(this.url+"&"+this.param,this.func,"","GET",true,this.type);
			}
			else
			{
				this.send(this.url+"?"+this.param,this.func,"","GET",true,this.type);
			}
		}
	}
}



   




