﻿//code by jxj for home.soufun.com/bbs
function AjaxRequest(nocache){
if(nocache){this.nocache=true;}else{this.nocache=false;}
this.Ajax =this.init();
this.createxml=this.xml();
this.header=new Array();
this.list=new Array();
this.limit=2;
this.num=0;
}
AjaxRequest.prototype={
	init:function(){
	if(window.XMLHttpRequest){
		return function(){return new XMLHttpRequest();}
	} else if(window.ActiveXObject){
		var test=null,MSXML=new Array("Microsoft.XMLHTTP","MSXML2.XMLHTTP","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.7.0","MSXML2.XMLHTTP.8.0");
		for(var i=MSXML.length-1;i>-1;i--){
			try	{		
				test=new ActiveXObject(MSXML[i]);
				return function(){return new ActiveXObject(MSXML[i])};
			} catch(e){}
		}
		return function(){return null;};
	}
	},
	xml:function(req){
	if(window.DOMParser){return function(req){return new DOMParser().parseFromString(req.responseText,"text/xml");};}
	else{return function(req){return req.responseXML;};}
	},
	Header:function(name,value){
	this.header.push(Array(name,value));
	},
	addlist:function(url,forxml,action,param){
	this.list.unshift(Array(this.nocache,url,forxml,action,param));
	},
	checklist:function(){
	var cmd=null;
	this.num--;
	do{
	cmd=this.list.pop();
	if(cmd){this.num++;this.run(cmd[0],cmd[1],cmd[2],cmd[3],cmd[4]);}
	} while(this.num<this.limit&&cmd);
	},
	run:function(nocache,url,forxml,action,param){
	var req=this.Ajax();
	if(req){
	req.onreadystatechange = this.response(req,forxml,action,param);
	req.open("GET", url, true);
	//if(forxml){req.setRequestHeader("Content-Type","text/xml;charset=gb2312");}
	if(nocache){req.setRequestHeader("Cache-Control","No-cache");}
	req.send("");
	} else{action(false,null);}
	},
	OpenXml:function(url,action,param){this.Open(url,true,action,param);},
	OpenText:function(url,action,param){this.Open(url,false,action,param);},
	Open:function(url,forxml,action,param){
	if(this.num<this.limit){
	this.num++;
	this.run(this.nocache,url,forxml,action,param);
	} else{this.addlist(url,forxml,action,param);}	
	},
	response:function(req,forxml,action,param){
	var op,ajax=this;
	if(!action){op=function(){};}
	else if(forxml){op=function(ajax,req){action(true,ajax.createxml(req),param);}}
	else{op=function(ajax,req){action(true,req.responseText,param);}}
	
	return function(){				
		if (req.readyState == 4){
		var status=req.status;
		ajax.checklist();
		if(status==0||(status>199&&status<300)){op(ajax,req);}
		else{action(false,null);}			
		}
	};
	},
	Default:function(state,html,ID){
	if(state&&html&&ID){
	var divnode=$(ID);
	if(divnode){divnode.innerHTML=html;}
	}
	}
};

