function antiCacheRand(aurl){
    var dt = new Date();
    if(aurl.indexOf("?")>=0){// já tem parametros
        return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
    }else{ return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
}

tIc = 0;

var stsAjax        = 4;
var ajaxUrl        = new Array('');
var ajaxMetodo     = new Array('');
var ajaxModo       = new Array('');
var ajaxTagRetorno = new Array('');
var ajaxParametros = new Array('');

/*
var ajaxUrl        = [''];
var ajaxMetodo     = [''];
var ajaxModo       = [''];
var ajaxTagRetorno = [''];
var ajaxParametros = [''];
*/

function divAjax(){
	// Lê, removendo informações do início do array
	var ajaxUrlT        = ajaxUrl.shift();
	var ajaxMetodoT     = ajaxMetodo.shift();
	var ajaxModoT       = ajaxModo.shift();
	var ajaxTagRetornoT = ajaxTagRetorno.shift();
	var ajaxParametrosT = ajaxParametros.shift();
	
	if(ajaxUrlT==''){
		var ajaxUrlT        = ajaxUrl.shift();
		var ajaxMetodoT     = ajaxMetodo.shift();
		var ajaxModoT       = ajaxModo.shift();
		var ajaxTagRetornoT = ajaxTagRetorno.shift();
		var ajaxParametrosT = ajaxParametros.shift();
	}
	if(ajaxUrlT!=''&&stsAjax==4){
		ajax(ajaxUrlT, ajaxMetodoT, ajaxModoT, ajaxTagRetornoT, ajaxParametrosT);
	}
}

function adm_ajax(url, metodo, modo, tagRetorno, parametros){
	var tArray = 0;
	
	// Adiciona informações no final do array
	ajaxUrl.push(url);
	ajaxMetodo.push(metodo);
	ajaxModo.push(modo);
	ajaxTagRetorno.push(tagRetorno);
	ajaxParametros.push(parametros);
	divAjax();
	
//	ajax(url, metodo, modo, tagRetorno, parametros);
}

function ajax(url, metodo, modo, tagRetorno, parametros){
	var icContTemp = 0;
	for( ic=0 ; ic<1000 ; ic++ ){
		icContTemp++;
	}
	if (window.XMLHttpRequest){
		// code for MOZILLA
		xmlhttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		// code for IE
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	url = url+'?'+parametros+'&rnd'+ Math.random() 
	url = antiCacheRand(url);

	if(metodo == "GET") {
		xmlhttp.open("GET", url, modo);
	} else {        
		xmlhttp.open("POST", url, modo);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		xmlhttp.setRequestHeader("Pragma", "no-cache");
	}    
	xmlhttp.onreadystatechange = function() {
		stsAjax = xmlhttp.readyState;
		if(xmlhttp.readyState == 4) {
			retorno=xmlhttp.responseText
			if(document.getElementById(tagRetorno)){
				document.getElementById(tagRetorno).innerHTML=retorno
				divAjax();
			}
		}
	}
	if(metodo == "GET") {
		xmlhttp.send(null);
	} else {        
		xmlhttp.send(parametros);
	}
}

function htmlAjax(url, metodo, modo, tagRetorno, parametros){
	if (window.XMLHttpRequest){
		// code for MOZILLA
		xmlhttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		// code for IE
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	url = url+'?'+parametros+'&rnd'+ Math.random() 
	url = antiCacheRand(url);

	if(metodo == "GET") {
		xmlhttp.open("GET", url, modo);
	} else {        
		xmlhttp.open("POST", url, modo);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		xmlhttp.setRequestHeader("Pragma", "no-cache");
	}    
	xmlhttp.onreadystatechange = function() {
		stsAjax = xmlhttp.readyState;
		if(xmlhttp.readyState == 4) {
			retorno=xmlhttp.responseText
			if(document.getElementById(tagRetorno)){
				//document.getElementById(tagRetorno).innerHTML=retorno
				document.getElementById(tagRetorno).designMode = "on";
				var oRTE = document.getElementById(tagRetorno).document;
				oRTE.open();
				oRTE.write(retorno);
				oRTE.close();

			}
		}
	}
	if(metodo == "GET") {
		xmlhttp.send(null);
	} else {        
		xmlhttp.send(parametros);
	}
}
