//variáveis
var controleBox = 0;

function inicializaXmlHttp(){
	var xmlhttp;
  	try {
   		xmlhttp = new XMLHttpRequest();
  	} 
  	catch(ee){
   		try{
    		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   		} 
   		catch(e){
    		try{
     			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
    		catch(E){
     			xmlhttp = false;
			}
   		}
  }
  return xmlhttp;
}

var resultados;
function iniciaAjax(linksql, vars, div, acaoPosterior){
	var args = iniciaAjax.arguments.length;
	var xmlhttp=inicializaXmlHttp();
	xmlhttp.open("POST", linksql, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState==4){
			t = xmlhttp.responseText
			resultados=t;
			//alert(resultados);
			if (args >= 3)
			{
				if(div)
					document.getElementById(div).innerHTML=unescape(resultados);	
				if(controleBox==1)
					setBox();
			}
			if(acaoPosterior)
				setTimeout(acaoPosterior,1);		
		}
		else
		{
			if(div)
				document.getElementById(div).innerHTML = strCarregando;//peq
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(vars);
}

function alerta(texto, titulo, naomostrarefeito)
{
	if(!naomostrarefeito)
		exibeEfeito();
	if(!titulo)
		titulo="";
	iniciaAjax('ajx_informacao.aspx','texto='+escape(texto)+'&titulo='+escape(titulo), 'divResultado');
}

function iniciaPost(linksql,div,form,acaoPosterior)
{	
	try
	{				
		var vars="";
		for(var i = 0; i < form.elements.length; i++) 
		{
			vars=vars + form.elements[i].name + "=" + escape(form.elements[i].value) + "&"
			if (form.elements[i].type == "text") {
				form.elements[i].value = "";
			}
		}
		while(vars.indexOf(" ")>=0)
		{
			vars=vars.replace(" ","%20");
		}			
		var xmlhttp=inicializaXmlHttp();
		xmlhttp.open("POST", linksql, true);		
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)		
			{
				var t = xmlhttp.responseText;
				t = unescape(t.replace(/\+/g," "));
				document.getElementById(div).innerHTML=t;
				if(controleBox==1)
					setBox();
				if(acaoPosterior)
					setTimeout(acaoPosterior,1);
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(vars);		
		document.getElementById(div).innerHTML = strCarregando;
	}
	catch(e)
	{
	}
}

function toogle(objt)
{
	if(objt.style.display=='')	
		objt.style.display='none';
	else
		objt.style.display='';
}

var divResultado;
var strCarregando='<img src="../images/carregando.gif" />';

function exibeEfeito(config){
	var div, div2;	
	var objSelect = document.getElementsByTagName("select");
	for (i=0;i<objSelect.length;i++){
		objSelect[i].style.visibility = "hidden";
	}	
	var tudo=document.getElementsByTagName("body")[0];
	tudo.scrollTop = 0;
	div2=document.createElement("div");
 	div2.id=("divJanela");
	var y;
	if (document.body.scrollHeight > document.body.offsetHeight)// all but Explorer Mac
		y = document.body.scrollHeight;
	else// Explorer Mac;
		y = document.body.offsetHeight;
	div2.style.width = "100%";
	div2.style.height = y+"px";
	if(config)
		adicionaEvento(div2, "click", apagaEfeito);
	
	tudo.appendChild(div2);	
	div=document.createElement("div");
 	div.id=("divResultado");
	tudo.appendChild(div);
	document.onkeydown=function(event){tecla(event)};
	divResultado=document.getElementById("divResultado");
	document.getElementById("divResultado").focus();	
	controleBox=1;
}
 
function setBox(){
	var box = document.getElementById("divResultado");
	var x,y, xp, yp;
	if (box.scrollHeight > box.offsetHeight){// all but Explorer Mac
		x = box.scrollWidth;
		y = box.scrollHeight;
		xp = document.body.scrollWidth;
		yp = document.body.scrollHeight;
	}
	else{// Explorer Mac;
		x = box.offsetWidth;
		y = box.offsetHeight;
		xp = document.body.offsetWidth;
		yp = document.body.offsetHeight;
	}
	box.style.left = eval(xp)/2-eval(x)/2+"px";
	box.style.top = "20%";//eval(yp)/2-eval(y)/2+"px";
}

function apagaEfeito(){
	var objSelect = document.getElementsByTagName("select");
	for (i=0;i<objSelect.length;i++){
		objSelect[i].style.visibility = "visible";
	}
  	var tudo=document.getElementsByTagName("body")[0]
	
  	var div1 = document.getElementById("divResultado");
	var div2 = document.getElementById("divJanela");	
	div1.innerHTML="";
  	tudo.removeChild(div1);
	tudo.removeChild(div2);

	document.getElementsByTagName("html")[0].style.overflow="";
	document.onkeydown='';
	controleBox=0;
}  
function tecla(e){
	if (!e) 
		e = window.event;	

	if (e.keyCode==27)// || e.keyCode==13
	{
		apagaEfeito();
		document.onkeydown='';
	}
}
function gel(id){
	return document.getElementById(id);
}
function gels(name){
	return document.getElementsByName(name);
}



