//作者:付学宝

//接下来的任务是考虑其内存的使用率
function AJaxObject(uid,isXML)
{
  this.uid=uid;
  this.httpRequest=false;
  //存放要处理的请求
  this.requestQueue=new Queue();
  this.isXML=isXML;
  this.currentRequest=null;
  //空闲位 如果为false说明此对象可以使用 否则 说明对象正在处理请求
  this.busy=false;
	this.type='I';

  if(!this.init())
  {
     window.alert("can not create XMLHttpRequest Object.");
  }
}
//初始化对象
AJaxObject.prototype.toString=function()
{
   return "AJaxObject";
}

//初始化对象
AJaxObject.prototype.init=function()
{
  if(window.XMLHttpRequest)//Mozilla IE7
  {
			this.httpRequest=new XMLHttpRequest();
			this.type='M';

			if(this.httpRequest.overrideMimeType)
			{
				if(this.isXML)
				{
					 this.httpRequest.overrideMimeType("text/xml");
				}else
				{
					 this.httpRequest.overrideMimeType("text/html");
				}
			}
  }
  else if(window.ActiveXObject)//IE
  {
		var msxmls = new Array('Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP.2.6','Msxml2.XMLHTTP','Microsoft.XMLHTTP.1.0','Microsoft.XMLHTTP.1','Microsoft.XMLHTTP');

		for(var i= msxmls.length-1;i>=0; i--)
		{
			 try {
			    this.httpRequest = new ActiveXObject(msxmls[i]);
				 return true;
			 } catch (ex){}
		}
  }

  if(!this.httpRequest)
  {
    return false;
  }
  return true;
}

function handlerResponse(ajaxObject)
{
  if(ajaxObject.httpRequest.readyState==4)
  {
		if(ajaxObject.httpRequest.status==200)
		{
				if(ajaxObject.isXML)
				{
					ajaxObject.currentRequest.callback(ajaxObject.httpRequest.responseXML);
				}
				else
				{
					ajaxObject.currentRequest.callback(ajaxObject.httpRequest.responseText);
					/*
					if(ajaxObject.type=='I'){
						ajaxObject.currentRequest.callback(bytes2BSTR(ajaxObject.httpRequest.responseBody));
					}else{
						ajaxObject.currentRequest.callback(ajaxObject.httpRequest.responseText);
					}*/
				}
		}else
		{
				//出错了也要调用回调函数以便用户处理
				try{
					ajaxObject.currentRequest.callback(null);
				}catch(e)
				{}
			  window.status="Page Error.";
		}

		ajaxObject.currentRequest=null;
		ajaxObject.busy=false;
		ajaxObject.httpRequest=false;
		//针对IE7 做出的必要的让步，以弥补IE7的bug
		if(!ajaxObject.init())
		{
		  window.alert("can not create XMLHttpRequest Object.");
		}

		ajaxObject.handlerRequest();
  }
}


AJaxObject.prototype.handlerRequest=function()
{
	if (this.busy==true || this.requestQueue.length() <= 0)
	{
		return;
	}

  //加锁
  this.busy=true;

	var tmpRequest=this.requestQueue.get();
	this.currentRequest=tmpRequest;

    try {
		url=encodeURI(tmpRequest.url);
		var ajaxObject=this;//保存当前处理的请求,下面不能用this
		this.httpRequest.onreadystatechange=function(){handlerResponse(ajaxObject);};
		if(tmpRequest.method.toLowerCase()=="get")
		{
			if(tmpRequest.param!=undefined && tmpRequest.param!=null && tmpRequest.param!="")
			{
				url=url+"/?"+encodeURI(tmpRequest.param);
			}
		   this.httpRequest.open(tmpRequest.method,url,tmpRequest.asyn);
		   this.httpRequest.send("");
		}
		else if(tmpRequest.method.toLowerCase()=="post")
		{
			this.httpRequest.open(tmpRequest.method,url,tmpRequest.asyn);
			this.httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			if(tmpRequest.param==undefined || tmpRequest.param==null)
			{
				tmpRequest.param="";
			}
			this.httpRequest.send(tmpRequest.param);
		}
		else
		{
			window.alert("http request Error.");
			this.httpRequest=false;
			return false;
		}
	}
	catch(e)
	{
		this.currentRequest=null;
		this.busy=false;

		this.httpRequest=false;
		//针对IE7 做出的必要的让步，以弥补IE7的bug
		if(!this.init())
		{
			window.alert("can not create XMLHttpRequest Object.");
		}

		this.handlerRequest();
	}
}


//封装请求
function Request(callback,method,url,param , syn)
{
   this.callback=callback;
   this.method=method;
   this.url=url;
   this.param=param;
   this.asyn = (syn) ? false : true;
}
//考虑到扩容 agentId
Request.prototype.sendRequest=function()
{
   if(this.checkValid()){
				if(arguments.length==0)
				{
							_AjaxAgent.requestQueue.put(this);
							//判断通信代理是否空闲，如果空闲唤醒通信代理处理业务，否则什么都不做
							_AjaxAgent.handlerRequest();
							return;
				}else if(arguments.length==1 && typeof(arguments[0])=="object" && arguments[0].toString()=="AJaxObject")
				{
							arguments[0].requestQueue.put(this);
							//判断通信代理是否空闲，如果空闲唤醒通信代理处理业务，否则什么都不做
							arguments[0].handlerRequest();
							return;
				}
   }
       window.status="ERROR:Request parameter error!";
}

//Request合法性检验
Request.prototype.checkValid=function()
{

  if(typeof(this.callback)!="function")
  {
     return false;
  }

  if(this.method.toLowerCase()!="post" && this.method.toLowerCase()!="get")
  {
     return false;
  }

  if(typeof(this.url)!="string")
  {
     return false;
  }

  return true;
}

//实现特定的队列
function Queue()
{
  this.data=new Array();
}

Queue.prototype.put=function(e)
{
  this.data[this.data.length]=e;
}

Queue.prototype.get=function()
{
  if(this.data.length<=0)
  {
    return null;
  }
  return this.data.shift();
}

Queue.prototype.length=function()
{
  return this.data.length;
}

//定义HashMap
function HashMap()
{
   this.data=new Object();
}

HashMap.prototype.get=function(key)
{
  if(this.data[key]==null || this.data[key]==undefined)
  {
    return null;
  }
  return this.data[key];
}
//如果overlay==true则存在此属性时 覆盖 否则提示报错
HashMap.prototype.put=function(key,value,overlay)
{
  if(overlay!=true && this.data[key]!=null && this.data[key]!=undefined)
  {
    alert("key已经存在!");
	return;
  }
  this.data[key]=value;
}

HashMap.prototype.reomve=function(key)
{
  if(this.data[key]!=null && this.data[key]!=undefined)
  {
    var tmp=this.data[key];
		this.data[key]=null;
		return tmp;
  }
  return null;
}

///////////////////AJaxObject Sample/////////////////////////////////
//
//
//   function callback(txt)
//   {
//      var txt=d.getResponse();
//		 if(!txt)
//		 {
//		   return;
//		 }
//   }
//   var t=new Request("tt",callback,"post","/webapp/namecheck","eplid="+eplid.value);
//   t.sendRequest();
//
//
////////////////////////////////////////////////////


String.prototype.lTrim = function () {
    return this.replace(/^\s*/, "");
}

// remove trailing whitespace
String.prototype.rTrim = function () {
    return this.replace(/\s*$/, "");
}

// remove leading and trailing whitespace
String.prototype.trim = function () {
    return this.rTrim().lTrim();
}
var _AjaxAgent=new AJaxObject("a",false);
//IE7 有一个bug 每个请求必须用新的AJaxObject,否则不会调用回调函数,数据会正常拉到客户端
var _AjaxAgent1=new AJaxObject("b",false);