// 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);
					//alert(resposta);
					// 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 = "<div class='textoDefault'>aguarde...</div>";
				} else {
					if(xmlhttp.readyState == 2) {
						document.getElementById("carregador"+local.id).innerHTML = "";
					}
					if(xmlhttp.readyState == 3) {
						document.getElementById("carregador"+local.id).innerHTML = "<div class='textoDefault'>processando...</div>";
					}
				}
			}
			
		}			
		xmlhttp.send(null);
	}
}
var xmlBool = function  (arquivo, metodo, local, funcao,msg, txt) {
	// 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;
			}
		}
	}
	//
	if(!txt) {
		txt = "carregando...";
	}
	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 = "<span class='texto_normal' style='color:#FF0000;text-align:left'>"+msg+"</span>";
				} 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 = "<div align='left' style='linkPequeno1;text-align:left'>"+txt+"</div>";
				} else {
					if(xmlhttp.readyState == 2) {
						document.getElementById("carregador"+local.id).innerHTML = "";
					}
				}
			}
			
		}			
		xmlhttp.send(null);
	}
}