//Fila de conexões
var fila=[]
var ifila=0
var z = 0;
var postdata
var bvalue
var xmlhttp

function CarregaXMLHTTP() {
	try{
		xmlhttp = new XMLHttpRequest()
		// Definimos o cabeçalho da resposta como sendo text/xml,
		// pois algumas versões dos browsers Mozilla exigem este mimetype
		if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml');
	} catch(ee) {
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
		}catch(e){

			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
			}catch(E){
				xmlhttp = false
			} 
		} 
	} 
	return xmlhttp
}

//Carrega via XMLHTTP a url recebida e coloca seu valor no objeto com o id recebido
function ajaxHTML(url, div_id, postdata, geraHistorico){
	if (!div_id) div_id = 'base';
	var conteudo=$$$(div_id)
	//alert(url);
	try {
		if (geraHistorico&&$$$('historicoCheck').value==0) $$$('sthistorico').value=conteudo.innerHTML
	} catch(e) {};
	if(!postdata) postdata=getpostdata();
	if (url == '') {
		conteudo.innerHTML = 'Sem referência.';
		return false;
	}
	if (!CarregaXMLHTTP) abrir(url,'','top=100,left=100,width=600,height=550,scrollbars=yes');
	if(typeof(xmlhttp) != 'object') CarregaXMLHTTP()
	if (conteudo.innerHTML != '') z = 1;
	//Carregando...
	conteudo.innerHTML = '<div id=\'carregando\'></div><div id=\'carrega\'></div>';
	//Adiciona à fila
	fila[fila.length]=[div_id,url,postdata,geraHistorico]
	//Se não há conexões pendentes, executa
	if((ifila+1)==fila.length)ajaxRun()
} //////////////////////////////////////////////////////////////////////////////////////////

function ajaxRun(){ ///////////////////////////////////////Executa a próxima conexão da fila
	if (fila[ifila][3] == 1) {	//--------------------------------- historico do browser
		try {
			$$$('historicoControle').value = parseInt($$$('historicoControle').value) + 1;
			$$$('historico').contentWindow.location.href=dominio+'includes/historico.asp?pagina='+ fila[ifila][1] +'&amp;div='+ fila[ifila][0];
		} catch(e) {};
	} 							//--------------------------------- historico do browser
	//Abre a conexão
	xmlhttp.open("POST",fila[ifila][1],true);
	//	xmlhttp.open("POST",'teste.php',true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	//Função para tratamento do retorno
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState==4){
		//Mostra o HTML recebido
		retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
		$$$(fila[ifila][0]).innerHTML=retorno
		//Roda o próximo
		ifila++
		if(ifila<fila.length) setTimeout("ajaxRun()", 20)
		if ($$$('carregai')) $$$('carregai').style.display = 'none';
		}
	}
	if (fila[ifila][0]=='base') window.location.hash=fila[ifila][1];
	setTimeout('xmlhttp.send("'+ fila[ifila][2] +'")',5)
} /////////////////////////////////////////////////////////////////////////////////////////

function ad(){
	block = $$$('carregai').style
	posY = scrollPos()

	if((posY % 2) !== 0) posY-=1;
	if(posY+12 != parseInt(block.top)){
		for(i=0; i<5; i++){
			if (posY+12 > parseInt(block.top)) a+= 2; else a-=2;
			block.top = a +'px';
		}
		
		status = (posY+12) + ' ' + a + ' ' + parseInt(block.top);
	}
	setTimeout("ad()",5);
}	

function showauxC(content, top, left) {
	if(browser().indexOf('ns') != -1) { 
		posy += window.scrollY
	} else {
		posy += window.document.documentElement.scrollTop;
	}
	
	if (!top) top = posy
	if (!left) left = posx
	
	$$$('aux').style.top	= top + 'px';
	$$$('aux').style.left	= left + 'px';
	$$$('aux').style.padding    = '5px';
	$$$('aux').style.margin	    = 0;
	$$$('aux').style.display    = 'block';
	
	$$$('aux').innerHTML    = content;
}

function showaux(page, auxwidth, top, left) {
	if (!$$$('aux')) {
		rectangleDiv = document.createElement('div');
		rectangleDiv.id	= 'aux';
		$$$('form1').appendChild(rectangleDiv);
	}
		
	$$$('aux').style.backgroundColor = '#369';
	
	if(browser().indexOf('ns') != -1) { 
		posy += window.scrollY
	} else {
		posy += window.document.documentElement.scrollTop;
	}
	
	if (!top) top = posy
	if (!left) left = posx
	
	$$$('aux').style.top	= top + 'px';
	$$$('aux').style.left	= left + 'px';
	$$$('aux').style.padding  = 0;
	$$$('aux').style.margin	= 0;
	$$$('aux').style.border	= 'none';
	$$$('aux').style.display  = 'block';
	
	if (page) ajaxHTML(page, 'aux', getpostdata() );
	if (auxwidth) $$$('aux').style.width = auxwidth;
}

function showtitle (obj, w, pd, clr) {
	var posX = 0
	var posY = 0
	try {
		posX = posx
		posY = posy + scrollPos() + 15;
	} catch(e) {
		posX = 25
		posY = 25
	}
	if (!$$$('aux')) return false
	if (!w) w = 200
	$$$('aux').style.padding 	= '3px';	
	$$$('aux').style.height 	= '';		
	$$$('aux').style.fontSize = '10px'; 
	$$$('aux').style.border 	= '2px #369 solid';
	$$$('aux').style.display	= 'block';
	$$$('aux').style.width	= w + 'px';
	$$$('aux').innerHTML		= obj.title;
	$$$('aux').style.left		= posX +'px';
	$$$('aux').style.top		= posY +'px'
	
	if (clr) {
		$$$('aux').style.backgroundColor = clr;
	} else {
		$$$('aux').style.backgroundColor = '#cc9';
	}
	
	if(browser() == 'ie6') {
		var hts_ = hitTest($$$('aux'), document.getElementsByTagName("select"))
		for(var i = hts_.length; i; hts_[--i].style.visibility = "hidden");
	}

	obj.onmouseout = function () {
		$$$('aux').style.display = 'none';
		showElms($$('select'));
	}
}

function getpostdata(form){ // Pega a informações do form indicado para serem enviadas via POST
	var postdata = '';
	if (!form) form = $$$('form1')
	if (!form) { alert('Sem form de entrada'); return false }
	
	for (i=0;i<form.elements.length;i++) {
		if (form.elements[i].type != 'button' && form.elements[i].type != 'submit') {
	
			if (form.elements[i].type == 'checkbox') {
	
				if(form.elements[i].checked) postdata = form.elements[i].name +'='+ form.elements[i].value +'&'+ postdata
	
			} else if (form.elements[i].type == 'radio') {
	
				if(form.elements[i].checked) postdata = form.elements[i].name +'='+ form.elements[i].value +'&'+ postdata
	
			} else {
	
	//postdata = postdata.replace(" ","|#|")
	//alert('XXX'+ postdata.substring(postdata.indexOf('Servico') + 7,postdata.indexOf('Servico') + 9) +'XXX');
	
				if (form.elements[i].value) {
					postdata = form.elements[i].name +'='+ form.elements[i].value.replace(/&/g,'#e#') +'&'+ postdata
				}
			}
		}
	}
	
	if (postdata.indexOf('\n') != -1 ){
		var tira_esp = postdata.split('\n')
		postdata = ''
	
		for (i=0; i < tira_esp.length; i++) {
			postdata += tira_esp[i].substr(0,tira_esp[i].length - 1)
			if (i > 0) postdata += ' '
		}
	}
	
	if (postdata.indexOf('teste_X') != -1) alert(postdata)
	postdata = postdata.replace(/"/g,'XXXADUPLAS');
	postdata = postdata.replace(/%/g,'XXXpor cento');
	postdata = postdata.replace(/ /g,'+');
	//postdata = postdata.replace(/à/g,'#a#');
//	try { postdata = postdata.replace(/Á/g,'#A#') } catch(e) {};
//	postdata = postdata.replace(/É/g,'#E#');
//	postdata = postdata.replace(/Í/g,'#I#');
//	postdata = postdata.replace(/Ó/g,'#O#');
//	postdata = postdata.replace(/Ú/g,'#U#');
	//postdata = postdata.replace(/_/g,'#Under#');
	return (postdata.slice(0,postdata.length - 1));
}

hitTest = function(o, l){
	function getOffset(o){
		for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
			o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
		return r.r += r.l, r.b += r.t, r;
	}
	for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i;
		b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
		&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
	return j ? !!r.length : r;
};
