/*
/*

	Assyncrous Request alpha 0.2 - 13/02/2007
	Desenvolvida por Anderson Triacca - elmariachibg@gmail.com
	Conjunto de funções de auxílio na manipulação de dados assincronamente
	
*/
var i = 0; //Índice do objeto de requisição
var http = new Array; //Objeto que servirá de requisição
var ligarDebug = 0; //Acionador do debug

/*
/*
	Função que faz os pedido
*/
function efetuarPedido(url, alvo, loader, metodo, conteudo) {
	i++;
	http[i] = getHTTPObject();
	if(loader != null) {
		display = document.getElementById(loader).style.display; 
		document.getElementById(loader).style.display = 'none';
	}
	else {
		loader = null;
	}
	if(metodo == null) {
		metodo = 'GET';
	}
	try { 
		requisicao(url, alvo, loader, metodo, conteudo, i);
	}
	catch(e) {
		debug(e);
	}
}

/*
/*
	Função que cria o objeto de acordo com o navegador
*/
function getHTTPObject() {
  var xmlhttp;
  <!-- Se for Internet Explorer, e testa a versão do interpretador, se voce abrir no dreamweaver o código abaixo pode parecer um comentário, mas não é.
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
		debug(e);
      }
    }
  @else
	xmlhttp = false;
	debug(e);
  @end @*/
  
  <!-- Se for Baseado no Gecko
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
		xmlhttp = false;
		debug(e);
    }
  }
  return xmlhttp;
}



/*
/*
	Função para manipulação da resposta da requisição
*/
function manipularResposta(alvo, loader, i){
	if(loader != null) {
		if(http[i].readyState == 1) {
			document.getElementById(loader).style.display = 'block';
		}
		if (http[i].readyState == 4) {
			document.getElementById(loader).style.top = 'none';
			document.getElementById(alvo).innerHTML = http[i].responseText;
		}
	}
	else {
		if (http[i].readyState == 4) {
			document.getElementById(alvo).innerHTML = http[i].responseText;
		}
	}
}


/*
/*
	Função que efetua a requisição
*/
function requisicao(url, alvo, loader, metodo, conteudo, i) {
	http[i].open(metodo, url, true);
	http[i].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http[i].setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate"); 
	http[i].setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	http[i].setRequestHeader("Pragma", "no-cache");
	http[i].onreadystatechange = function() {
		try{
			manipularResposta(alvo, loader, i);
		}
		catch(e) {
			debug(e);
		}
	}
	try {
		http[i].send(conteudo);
	}
	catch(e) {
		debug(e);
	}
}

/*
/*
	Função de debug
*/
function debug(e) {
	if(ligarDebug == 1) {
		alert(e);
	}
}
