/**
 * Função que cria o objeto de requisição AJAX
 *
**/
function criarAJAX()
{
	http_request = false;
	if(window.XMLHttpRequest)// Mozilla, Safari,...
	{ 
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType) 
		{
				http_request.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)// IE
	{
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e)
			{
				alert("Seu brouser não tem suporte a AJAX");
				return false;
			}
		}
	}
	return http_request;
}

/**
 * Faz uma requisição assincrona com uma pagina, e executa a função passada como parametro
 *
 * string url
 * string dados
 * string funcao
**/
function assincronosXML(url,dados,funcao)
{
	http_request = criarAJAX();
	if(!http_request) 
	{
		alert('Erro: http_request não foi criado');
		return false;
	}
	var tratamento = "http_request.onreadystatechange = "+funcao;
	eval(tratamento);
	http_request.open('GET', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(dados);
}

/**
 * Faz uma requisição sincrona com uma pagina e retorna um objeto XML
 *
 * string url
 * string dados
**/
function sincronosXML(url, dados)
{
    var http_request = criarAJAX();
	if(!http_request) 
	{
		alert('Erro: http_request não foi criado');
		return false;
	}
    http_request.open("GET", url, false);
    http_request.send(dados);
    return http_request.responseXML;
}

/**
 * Faz uma requisição sincrona com uma pagina e retorna o resultado em modo texto
 *
 * string url
 * string dados
**/
function sincronosTexto(url, dados)
{
    var http_request = criarAJAX();
	if(!http_request) 
	{
		alert('Erro: http_request não foi criado');
		return false;
	}
    http_request.open("GET", url, false);
    http_request.send(dados);
    return http_request.responseText.trim();
}


/**
 * Exibe a targeta "Carregando"
**/
function mostraCarregando()
{
	if(document.getElementById("carrega")) 
	{
		carregando = document.getElementById("carrega");
	} 
	else 
	{
		carregando = document.createElement("div");
		carregando.id = "carrega";
		carregando.innerHTML = "Carregando...";
		document.getElementById("menu").appendChild(carregando);
	}
	// coloca o stylo na targeta
	carregando.className = 'targeta_carregando';
	//Calcula a posição horizontal
	carregando.style.left = document.body.offsetWidth - 110;
	//Calcula a posição vertical
	carregando.style.top = document.body.scrollTop;
	// mostra a targeta
	carregando.style.visibility = "visible";
}

/**
 * Esconde a targeta "Carregando"
**/
function escondeCarregando()
{
	document.getElementById("carrega").style.visibility = "hidden";	
}
