// JavaScript Document

function Ajax()
{
	this.xmlHttp = null;

	if (typeof (Ajax.prototype._initialized) == 'undefined')
	{
		/**
		 *Ajax.prototype.getXmlHttpObject()函数负责根据不同的浏览器初始化XmlHttpObject对象
		 */
		
		Ajax.prototype.getXmlHttpObject = function ()
		{
			var xmlHttp=null;
			
			try
			{
				// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			}
			catch (e)
			{
				// Internet Explorer
				try
				{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e)
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			
			return xmlHttp;
		}		
		
		/**
		 *request函数,负责发送get或post请求,函数中this关键字的含义会根据函数被调用时的环境解析，
		 *而不是根据函数定义时的环境。
		 */
		
		Ajax.prototype.request = function (url,method)
		{
			var onreadystatechange = null;
			var tail = null;
			var asynchronism = null;
			
			function assign(argument)
			{
				switch (typeof argument)
				{
					case 'function':
						onreadystatechange = argument;
						break;
					case 'string':
						tail = argument;
						break;
					case 'boolean':
						asynchronism = argument;
						break;
					default:
						break;
				}
			}
			
			assign(arguments[2]);
			assign(arguments[3]);
			assign(arguments[4]);
			
			if ( (typeof onreadystatechange) != 'function' )
			{
				onreadystatechange = function(){};
			}
			
			if ( (typeof tail) != 'string' )
			{
				tail = '';
			}
			
			if ( (typeof asynchronism) != 'boolean' )
			{
				asynchronism = true;
			}
			
			if (this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0)//XMLHttp对象有空
			{
				try{
				this.xmlHttp.onreadystatechange = function(){};
				}
				catch(e)
				{
					alert(e.description+'haha');	
				}
												//先把上一次使用XMLHttp对象时赋予的旧的onreadystatechange方法(函数)置null，
												//因为若上次使用XMLHttp对象的open方法的方式是异步的而这次使用同步的方式，
												//那么这次就不会用到XMLHttp对象的onreadystatechange属性函数，这样的情况下，
												//未置null的XMLHttp对象的onreadystatechange属性函数在个别一些浏览器下(oprea)
												//，在XMLHttp对象的readyState属性的值变成4时会被浏览器私自调用
				method = method.toUpperCase();
				this.xmlHttp.open(method,url,asynchronism);
				
				var _this = this;
				
				var send = function ()
				{
						switch (method)
					{
						case 'GET':
							_this.xmlHttp.send(null);
							break;
						case 'POST':
							_this.xmlHttp.setRequestHeader("Content-Length",tail.length);
						
							_this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
							
							//this.send(encodeURIComponent(tail));
			
							_this.xmlHttp.send(tail);
							break;
						default :
							return false;
							
							break;
					}
				}

				if (asynchronism)//异步
				{
					try
					{
						this.xmlHttp.onreadystatechange = onreadystatechange;
					}
					catch(e)
					{
						alert(e.description);	
					}
					send();
				}
				else//同步
				{
					send();
					this.handle = onreadystatechange;
					this.handle();
				}
			}
			else//XMLHttp对象没空
			{
				var _this = this;
				var fun = function()
				{
					_this.request(url,method,onreadystatechange,tail,asynchronism);	
				}
				setTimeout(fun,100);
			}
			
			return true;
			
		}
		
		Ajax.prototype._initialized = true;
	}
	this.xmlHttp = this.getXmlHttpObject();
}