//CONEXAO XMLHTTPREQUEST (QUEUE)

function apareceCarregando()
{
	if(window.parent)
		{
			doc = window.parent.document;
			jan = window.parent;
		}
	else
		{
		doc = window.document;
		jan = window;
		}
		if(!doc.getElementById('dv_carregando_xml'))
		{
			dv = doc.createElement("DIV");
			
			dv.style.width = '80px';
			dv.style.height = '18px';
			dv.style.paddingTop = '2px';
			dv.style.textAlign = 'center';
			
			dv.style.position = 'absolute';
			dv.style.backgroundColor = '#666666';
			dv.style.color = '#FFFFFF';
			dv.style.fontFamily = 'Arial';
			dv.style.fontSize = '11px';
			dv.style.fontWeight = 'bold';		
			dv.id = 'dv_carregando_xml';
			dv.innerHTML = 'Carregando...';
				
			
			if(doc.body)
				doc.body.appendChild(dv);
			else
				doc.all.appendChild(dv);
		}
	
	div = doc.getElementById('dv_carregando_xml');

	div.style.top = '0px';
	lf = jan.screen.availWidth - 100;
	div.style.left = lf+'px';
	div.style.visibility='visible';
}

function someCarregando()
{
	if(window.parent)
		doc = window.parent.document;
	else
		doc = window.document;

	div = doc.getElementById('dv_carregando_xml');
	div.style.visibility='hidden';
}

function assyncRequest(nome)
{
	var name;
	this.name = nome;	
	this.req=null;
	this.erro;
	this.poolXml = new Array();
	this.execPool = new Array();
	this.retJSON = new Array();
	this.poolVarMethod = new Array();
	this.poolVarPage = new Array();
	this.poolVarArgs = new Array();
	this.poolVarObj = new Array();
	this.poolVarRet = new Array();
	this.lastRet = false;
	this.lastExec = null;
	var newPool = false;
	this.astURL = null;
	this.poolLocked = false;
		
	this.ex = 0;
	this.ped =0;
	
	try {  this.req = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { this.req = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { this.req = new XMLHttpRequest(); }
	catch (e) { this.req = false; }}}

	if(!this.req)
	{
		alert("Erro");
		return false;
	}

	this.loadXML=function(url,method,arr_1,arr_2,ret,json)
	{
			var name = this.name;
			this.ped++;
			newPool = true;
			var x=0;
			arg = '';
				
			var arr_vals = arr_2;
			var arr_var = arr_1;
			
			a=new Date();
			
			apareceCarregando();
			
			if((arr_var))
			{
	/*
				if((arr_var.constructor==Array)&&(arr_vals))
				{*/
					for(x=0;x<arr_var.length;x++)
					{
						if(method == 'POST')
						{
							if(arr_vals[x])
								if(x==0)
									arg+=this.urlEncode(arr_var[x])+'='+this.urlEncode(arr_vals[x]);
								else
									arg+="&"+this.urlEncode(arr_var[x])+'='+this.urlEncode(arr_vals[x]);
							else
								if(x==0)
									arg+=this.urlEncode(arr_var[x])+'=';
								else
									arg+="&"+this.urlEncode(arr_var[x])+'=';
									
/*								if(arr_var[x] == 'requisitos')
									alert(this.urlEncode(arr_var[x])+'='+this.urlEncode(arr_vals[x],1));*/
						}
						else
						{
							if(arr_vals[x])
								if(x==0)
									arg+=arr_var[x]+'='+arr_vals[x];
								else
									arg+="&"+arr_var[x]+'='+arr_vals[x];
							else
								if(x==0)
									arg+=arr_var[x]+'=';
								else
									arg+="&"+arr_var[x]+'=';
						}
					}
			}
			
			this.PoolXML(method,url,arg,ret,json);
	} 
	
	
	this.urlEncode=function(str,alerta)
	{
		ret = "";
		var x =0;
		
		str = new String(str);
		
		for(x=0;x<str.length;x++) 
			{
				chr = str.charCodeAt(x);
				if(alerta)
					alert(chr);
			
				if((chr > 31) && ((chr < 47) || (chr >57)))
				{
					number = new Number(chr);
					hex='%'+number.toString(16);
					ret+=hex;
				}
				else if(chr == 10)
				{
					ret+="%0A";
				}
				else
					ret+=str.charAt(x);
		}
		return ret;
	}


	this.PoolXML=function (method,page,arg,ret,json)
	{
		if(this.poolLocked)
		{
			//ert('pool travado!');
			setTimeout(this.name+".PoolXML('"+method+"','"+page+"','"+arg+"','"+ret+"',"+json+")",100);
		}
		else
		{
			//debug("ENTROU NO POOL URL "+page);
			this.poolLocked = true;
			var NPOOL =this. poolXml.length;
			this.poolVarMethod[NPOOL] = method;
			this.poolVarPage[NPOOL] = page;
			this.poolVarArgs[NPOOL] = arg;
			this.poolVarRet[NPOOL] = ret;
			this.retJSON[NPOOL] = json;
	
			this.poolXml[NPOOL] = 1;

			if((this.poolXml.length-1)==this.execPool.length)
			{
				//alert("Entrou no EXECPOOL");
				this.execXML(NPOOL);
			}
			newPool = false;
			this.poolLocked = false;
		}
		
	}
	
	this.execXML=function(nPool,v)
	{
		method = this.poolVarMethod[nPool];
		url = this.poolVarPage[nPool];
		arg = this.poolVarArgs[nPool];
		this.lastJSON = this.retJSON[nPool];
		//debug("EXECUTANDO "+url);;
		//alert("EXECUTANDO "+url)		
		
		this.lastRet = this.poolVarRet[nPool];
		this.lastExec = nPool;
		this.lastURL = url;
		this.lastArg = arg;
		
		if(!this.req)
		{ 
			alert("Erro !!!Este Browser nao oferece suporte para xmlHTTPRequest!!\nAtualize a versao do mesmo ou baixe Firefox (http://mozilla.org/firefox).");
		}
		else
		{
			this.ex++
			var req = this.req;
			var exName = this.name;

			this.req.onreadystatechange = new Function("{var acTh = eval("+exName+"); req = acTh.req; if (req.readyState == 4) { acTh.processXML(req); } }");
			
			if(method == 'GET')
			{
				this.req.open('GET',url+"?"+arg, true);
				
				this.req.setRequestHeader('Content-Type','text/plain; charset=iso8859-1');
				
				this.req.send(null);
			}
			else
			{
				this.req.open('POST',url, true);
				this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso8859-1');
				

				
				/*if(req.overrideMimeType)
					req.overrideMimeType('text/html; charset=iso-8859-1');
				*/
						
				this.req.send(arg);
				//alert(arg);
			}
			//alert(this.req.getAllResponseHeaders);
			
		}
	
	}
	
	this.abort = function(all)
	{
		this.req.abort();
		this.execPool[this.execPool.length] = "ABORT()";
		if(all)
		{
			this.limpaPoolXml();
		}
	}		

	
	this.processXML=function(req)
	{ 
		//alert(this.lastURL+" =\n "+this.req.responseText);
		__XMLEnableDebug = false;
		
		if(__XMLEnableDebug)
		{
			var dbg = this.req.responseText;
			
			while(dbg.indexOf(">")>-1)
				dbg=dbg.replace(">","&gt;");
			
			while(dbg.indexOf("<")>-1)
				dbg=dbg.replace("<","&lt;");
			
			if(document.getElementById("DEBUG"))
				document.getElementById("DEBUG").innerHTML+='<br><br>'+ dbg;
		}

		if((this.req.responseText!="")&&(this.lastJSON))
		{
			var ret = eval("("+this.req.responseText+")");	
		}
		else if(this.req.responseText!="")
		{
			ret = this.req.responseText;
		}
		else
		{
			var ret = null;
		}

		if(this.lastRet)
			eval(this.lastRet+"(ret)");
		
		try {  this.req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { this.req = new ActiveXObject("Microsoft.XMLHTTP");}
		catch (e) { try { this.req = new XMLHttpRequest(); }
		catch (e) { this.req = false; }}}
	
	
		//alert("FIM DA EXECUCAO "+url);
		someCarregando();
		
		this.execPool[this.execPool.length] = true;
		
		if((this.execPool.length < this.poolXml.length)&&(this.poolVarPage[this.lastExec+1]))
		{
			//debug("CHAMA O PROXIMO DA FILA");
			this.execXML(this.lastExec+1);
		}
		else if(!newPool)
		{
			this.limpaPoolXml();
		}
	}
	
	this.limpaPoolXml=function()
	{
		if(!this.poolLocked)
		{
			//debug("LIMPA A FILA");
			this.poolLocked = true;
			this.poolVarMethod=null;
			this.poolVarPage=null;
			this.poolVarArgs=null;
			this.poolVarRet=null;
			this.execPool=null;
			this.poolXml=null;
			this.poolVarMethod=new Array();
			this.poolVarPage=new Array();
			this.poolVarArgs=new Array();
			this.poolVarRet=new Array();
			this.execPool=new Array();
			this.poolXml=new Array();
			this.poolLocked = false;
		}
	}
	
	this.Replace=function(oq,pq,onde)
	{
		var lastOcur = -1;
		contat = "";
		while(onde.indexOf(oq)>-1 && onde.indexOf(oq)>lastOcur)
		{
			pos = onde.indexOf(oq);
			inicio = onde.substr(0,pos);
			fim = onde.substr(pos+oq.length,onde.length);
			inicio+=pq;
			onde = fim;
			contat+=inicio;
		}
		return ""+contat+fim;
	}

}

function debug(text)
{
	erMm = /\>/;
	erMn = /\</;	
	text = text.replace(erMm,"&gt;");
	text = text.replace(erMn,"&lt;");	
	document.getElementById('debug').innerHTML+=text+"<br />";
}

if(!window.assyncRequest)
	alert("Deve importar a classe aasyncRequest!");
else
	reqAssync = new assyncRequest('reqAssync');