var YzXmlHttpRequest=function(url,data,callback,method)
{
	if(!method)method="get";
	
	YzXmlHttpRequest.addInstance(this);
	
	this.url=url;
	this.data=null;
	this.callback=callback;
	this.method=method.toLowerCase();
	
	if(typeof data=="object")
	{
		var a=[];
		for(var k in data)
		{
			a.push(k+"="+encodeURIComponent(data[k]));
		}
		this.data=a.join("&");
	}
	if(this.data!=null && this.data!="")
		this.method="post";
};

YzXmlHttpRequest.instances={};
YzXmlHttpRequest.addInstance=function(inst)
{
	var handle=Yz.util.newGuid();
		inst.handle=handle;
	YzXmlHttpRequest.instances[handle]=inst;
	return handle;
};

YzXmlHttpRequest.removeInstance=function(handle)
{
	var inst=YzXmlHttpRequest.instances[handle];
	if(inst)inst.dispose();
	YzXmlHttpRequest.instances[handle]=null;
	delete YzXmlHttpRequest.instances[handle];
};

YzXmlHttpRequest.dispose=function()
{
	for(var handle in YzXmlHttpRequest.instances)
	{
		YzXmlHttpRequest.removeInstance(handle);
	}
	YzXmlHttpRequest.instances=null;
};

YzXmlHttpRequest.asyncRequest=function(url,data,callback,method)
{
	var r=new YzXmlHttpRequest(url,data,callback,method);
		r.asyncRequest();
};

YzXmlHttpRequest.syncRequest=function(url,data,method)
{
	var r=new YzXmlHttpRequest(url,data,null,method);
	var resText=r.syncRequest();
		YzXmlHttpRequest.removeInstance(r.handle);
		r=null;
		return resText;
}

YzXmlHttpRequest.state={OPENED : 1, SENT : 2,RECEIVING : 3,COMPLETED : 4};
YzXmlHttpRequest.onreadystatechange=function(xhrHandle)
{
	var xhrInst=YzXmlHttpRequest.instances[xhrHandle];
	var readyState=xhrInst.xmlHttp.readyState;
	switch(readyState)
	{
		case YzXmlHttpRequest.state.OPENED: 
		case YzXmlHttpRequest.state.SENT:
			xhrInst.callback(readyState);
		break;
		
		case YzXmlHttpRequest.state.RECEIVING:
			if(Yz.isIE==false)
 
			{
				//TODO:这里IE会引起未指定错误，需要修复{
				var bError=xhrInst.xmlHttp.status!=200;
				xhrInst.callback(readyState,bError,(bError ? "" : xhrInst.xmlHttp.responseText));
			}
		break;
		
		case YzXmlHttpRequest.state.COMPLETED:
			var bError=xhrInst.xmlHttp.status!=200;
 
			xhrInst.callback(readyState,bError,(bError ? "" : xhrInst.xmlHttp.responseText));
			
			YzXmlHttpRequest.removeInstance(xhrHandle);
			xhrInst=null;
		break;	
		
	}
}
YzXmlHttpRequest.prototype=
{
	asyncRequest : function()
	{
		var handle=this.handle;
		this.xmlHttp=Yz.util.createXmlObject("XmlHttp");
		if(typeof this.callback=="function")
		{
			this.xmlHttp.onreadystatechange=function(){YzXmlHttpRequest.onreadystatechange(handle);};
		}
		
		this.xmlHttp.open(this.method,this.url,true);
		
		this.xmlHttp.setRequestHeader("Cache-Control","no-cache");
 
		this.xmlHttp.setRequestHeader("If-Modified-Since","0"); 
		
		if(this.method=="post")
		{
			this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		}
		this.xmlHttp.send(this.data);
	},
	syncRequest : function()
	{
		this.xmlHttp=Yz.util.createXmlObject("XmlHttp");
		this.xmlHttp.open(this.method,this.url,false);
 
		
		this.xmlHttp.setRequestHeader("Cache-Control","no-cache");
  		this.xmlHttp.setRequestHeader("If-Modified-Since","0"); 

		if(this.method=="post")
		{
			this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		}
		this.xmlHttp.send(this.data);
		return this.xmlHttp.responseText;
	},
	dispose : function()
	{
		this.xmlHttp=null;
		this.url=null;
		this.data=null;
		this.callback=null;
	}
}

if(typeof Yz!="undefined")
{
	Yz.xmlhttp=YzXmlHttpRequest;
	Yz.cleanup.add(YzXmlHttpRequest);
}