// JavaScript Document
// criando o contrutor
/* @autor: Artur T. Magalhães Jr.
arquivo  - Arquivo de Destino a ser requisitado
metodo - POST ou GET
local - endereco aonde vai ser amazernado os dados HTML
added - true se for adicionar o conteúdo ao "local" e false se for substituir
*/

var xml = function  (arquivo, metodo, local, added, funcao) {
	// carregando o objeto XML HttpRequest
	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;
			}
		}
	}
	//
	var resposta;
	// Verificando o Objeto XML Request
	if(xmlhttp) {
	// fazendo a requisição do arquivo
		xmlhttp.open(metodo,arquivo,true);
	// definindo a funcão
		xmlhttp.onreadystatechange = function () {	
	/* redyState = 4  - Carregamento Completo */
			if(!document.getElementById("carregador"+local.id)) {
				local.innerHTML = "<div align='center' id='carregador"+local.id+"'></div>";
			}
			if(xmlhttp.readyState == 4) {
	// Status == 200 carregamento CORRETO
				if(xmlhttp.status == 200) {
					// armazendo os dados
					resposta = encodeURI(xmlhttp.responseText);
					// Verificando se há ou não a substituição
					if(added == false) {
						local.innerHTML = "";
					}
					local.innerHTML += decodeURI(resposta);
					if(funcao != null)
						eval(funcao);
				} else {
					// Inserindo a MSG de Erro
					local.innerHTML = encodeURI("Problemas em carregar o arquivo");
				}
			} else {
				// Mostrando a Mensagem de Carregamento
				if(xmlhttp.readyState == 1) {
					document.getElementById("carregador"+local.id).innerHTML = "Carregando ..";
				} else {
					if(xmlhttp.readyState == 2) {
						document.getElementById("carregador"+local.id).innerHTML = "";
					}
				}
			}
			
		}			
		xmlhttp.send(null);
	}
}
var xmlBool = function  (arquivo, metodo, local, funcao,msg) {
	// carregando o objeto XML HttpRequest
	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;
			}
		}
	}
	//
	var resposta;
	// Verificando o Objeto XML Request
	if(xmlhttp) {
	// fazendo a requisição do arquivo
		xmlhttp.open(metodo,arquivo,true);
	// definindo a funcão
		xmlhttp.onreadystatechange = function () {	
	/* redyState = 4  - Carregamento Completo */
			if(!document.getElementById("carregador"+local.id)) {
				local.innerHTML = "<div align='center' id='carregador"+local.id+"'></div>";
			}
			if(xmlhttp.readyState == 4) {
	// Status == 200 carregamento CORRETO
				if(xmlhttp.status == 200) {
					// armazendo os dados
					resposta = xmlhttp.responseText;
					// Verificando se há ou não a substituição
					if(eval(resposta))					
						eval(funcao);
					else
						local.innerHTML = "<p align='center' class='texto_normal'><font color='#FF0000'>"+msg+"</font></p>";
				} else {
					// Inserindo a MSG de Erro
					local.innerHTML = encodeURI("Problemas em carregar o arquivo");
				}
			} else {
				// Mostrando a Mensagem de Carregamento
				if(xmlhttp.readyState == 1) {
					document.getElementById("carregador"+local.id).innerHTML = "<img align='center' src='imgs/carregador.gif' />";
				} else {
					if(xmlhttp.readyState == 2) {
						document.getElementById("carregador"+local.id).innerHTML = "";
					}
				}
			}
			
		}			
		xmlhttp.send(null);
	}
}