﻿	//=================================请求类[KRequest]=============================================
	
	 var commandArray=new Array();			//请求命令队列 
	 var isBusy=false;						//服务器是否繁忙

	 
	 
	 
	 /**
	 * 定义请求队列类

	 */
	 function KRequestCommand()
	 {
		this.sendObject=null;
		this.url=null;
		this.params=null;
		this.onload=null;
		this.onerror=null;
		this.sendMethod=null;
	 }
	 
	 /**
	 * 增加请求队列
	 */
	 function KRequest_AddCommand(sendObject,url,params,onload,onerror,sendMethod)
	 {
		var command=new KRequestCommand();
		command.sendObject=sendObject;
		command.url=url;
		command.params=params;
		command.onload=onload;
		command.onerror=onerror;
		command.sendMethod=sendMethod;
		commandArray.push(command);
	 }	 
	 
	/**
	 * 定义请求类

	 */ 
	function KRequest(){}

	/**
	 * 发送请求--静态

	 * object 传送对象

	 * url    地址
	 * params 参数
	 * onload 成功载入调用方法
	 * onerror 错误时调用方法

	 * sendMethod 发送方法 GET或POST,默认为POST
	 */
	var KRequest_send = function(sendObject,url,params,onload,onerror,sendMethod)
	{	    
		if (isBusy)
		{
			KRequest_AddCommand(sendObject,url,params,onload,onerror,sendMethod);
		}
		else
		{
			isBusy=true;
			var request=new KRequest();		   
			request.loadXMLDoc(sendObject,url,params,onload,onerror,sendMethod);
		}		
	}
	
	/**
	 * 发送请求--静态

	 * object 传送对象

	 * url    地址
	 * params 参数
	 * onload 成功载入调用方法
	 * onerror 错误时调用方法

	 * sendMethod 发送方法 GET或POST,默认为POST
	 */
	var KRequest_sendInPhase = function(url,params)
	{	    
		var request=null;
		
		if (window.XMLHttpRequest)
		{
			 request = new XMLHttpRequest();//FireFox
		}
		if(KBase.getBrowserType()=="IE")
		{
			if (window.ActiveXObject)
			{
				try{request = new ActiveXObject("Msxml2.XMLHTTP.4.0");}catch (err){}
				try{request = new ActiveXObject("Msxml2.XMLHTTP");}catch (err){}
				try{request = new ActiveXObject("Microsoft.XMLHTTP");}catch (err){}
			}
		}
		
		if (request)
		{
			try
			{								
				request.open("POST",url,false);				         
				request.send(params);
				return request.responseText;
			}
			catch (err)
			{				
				return "";				
			}
		}
		return "";
	}
	
	/*
	 * 触发请求
	 */
	var KRequest_fire=function()
	{
		if (commandArray.length>0)
		{
			var command=commandArray[0];
			commandArray.splice(0,1);
			var request=new KRequest();
			request.loadXMLDoc(command.sendObject,command.url,command.params,command.onload,command.onerror,command.sendMethod);
		}
		else
		{
			isBusy=false;
		}		
	}
	
	
	
	/*
	 * 发送请求

	 */
	var KRequest_loadXMLDoc = function(object,url,params,onload,onerror,sendMethod)
	{
		this.request=null;
		this.sendObject=object;		
		this.onload = (onload) ? onload : this.defaultRight;
		this.onerror = (onerror) ? onerror : this.defaultError;		
		if (!sendMethod)	method = "POST";
		else method = sendMethod;			
		try
		{	
	    
			if (window.XMLHttpRequest)
			{
				this.request = new XMLHttpRequest();//FireFox
			}
			if(KBase.getBrowserType()=="IE")
			{
				if (window.ActiveXObject)
				{
					try{this.request = new ActiveXObject("Msxml2.XMLHTTP.4.0");}catch (err){}
					try{this.request = new ActiveXObject("Msxml2.XMLHTTP");}catch (err){}
					try{this.request = new ActiveXObject("Microsoft.XMLHTTP");}catch (err){}
				}
			}	
				
		}
		catch (err)
		{				
			KEvent.trigger(object, "error", ["创建HttpRequest异常[KRequest_loadXMLDoc]-->",url + params]);				
		}
		
		if (this.request)
		{
			try
			{
				var loader = this;
				this.request.onreadystatechange=function(){KRequest.onReadyState.call(loader);}				
				this.request.open(method,url,true);				         
				this.request.send(params);				
			}
			catch (err)
			{				
				KRequest_fire();
				KEvent.trigger(object, "error", ["发送请求异常[KRequest_loadXMLDoc]-->",url + params + err.description.toString()]);
				
			}
		}
	}
	
	/*
	 * 请求状态改变

	 */
	var KRequest_onReadyState = function()
	{	    
		var request = this.request;
		var ready = request.readyState;
		if (ready == 4)
		{
			var httpStatus = request.status;        
			if (httpStatus == 200 || httpStatus == 0)
			{
				this.onload.call(this);
				KRequest_fire();
			}
			else 
			{
				KRequest_fire();
				this.onerror.call(this);				
			}
			
			//this.request.readyState = null;
			this.request = null;
			
		}
	}
	
	/*
	 * 默认错误处理方法
	 */
	var KRequest_defaultError = function()
	{
		KEvent.trigger(this.sendObject, "error", ["发送请求返回错误[KRequest_defaultError]-->",this.request.getAllResponseHeaders()]);
	}
	
	/*
	 * 默认正确处理方法
	 */
	var KRequest_defaultRight = function()
	{
		
	}
	
	KConfig.defineClass("KRequest", KRequest);
	KConfig.defineStaticFunction(KRequest, "fire", KRequest_fire);
	KConfig.defineStaticFunction(KRequest, "send", KRequest_send);
	KConfig.defineStaticFunction(KRequest, "sendInPhase", KRequest_sendInPhase);
	KConfig.defineFunction(KRequest, "loadXMLDoc", KRequest_loadXMLDoc);
	KConfig.defineStaticFunction(KRequest, "onReadyState", KRequest_onReadyState);
	KConfig.defineFunction(KRequest, "defaultError", KRequest_defaultError);
	KConfig.defineFunction(KRequest, "defaultRight", KRequest_defaultRight);
	
	//=================================请求类[END]=============================================
	