function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject();

/*===============================================================================================================================*/

/* posta todos os dados do formulario */
function camposForm(oForm){
		var aParams = new Array();
		for (var i=0 ; i < oForm.length; i++) {
			var sParam = oForm[i].id;
			sParam += "=";
			sParam += escape(oForm[i].value);
			aParams.push(sParam);
		}
		return aParams.join("&");
	}

/*================================================================================================================================*/
/* prepara o post */
function enviar(url, funcao){
	
	formulario = document.forms[0].elements;
	param = camposForm(formulario);

    http.open("POST", url + "?" + Math.random(), true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
    http.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    http.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
    http.setRequestHeader("Pragma", "no-cache");
    http.onreadystatechange = eval(funcao);
    http.send(param);
}

function cadastrar(){
    enviar("func/actions.php", function() { resposta(); });
	//document.forms[0].reset();
}

/*===============================================================================================================================*/

/* Função padrão */
function carrega(url,funcao){
    http.open("GET", url + "?" + Math.random(), true);
    http.onreadystatechange = eval(funcao);
    http.send(null);
}

function Home(){
    carrega("obj/home.php", function() { processado(); });
}

/*===============================================================================================================================*/

function paginas(id){ 
	
	    http.open('get', 'obj/pagina.php?id='+id+'&rand='+Math.floor(Math.random()*100), true);
		http.onreadystatechange = processado;
        http.send(null);
}

/*===============================================================================================================================*/

function Cadastro(){
    carrega("obj/cadastro.php", function() { processado(); });
}
   
/*================================================================================================*/

function Localizacao(){
    carrega("obj/localizacao.php", function() { processado(); });
}
   
/*================================================================================================*/

function popup(){
    carrega("obj/poesia.php", function() { processado(); });
	setTimeout("toggle('Layer1')",0000);
}

/*================================================================================================*/

function poesia(){
    carrega("obj/poesia.php", function() { processado(); });
}
   
/*================================================================================================*/

function Contato(){
    carrega("obj/contato.php", function() { processado(); });
}
   
/*================================================================================================*/

function processado(){
    // apenas quando o estado for "completado"
    document.getElementById('loading').innerHTML = "<span style='font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color=#FFFFFF; background-color=#6EB2D3;'><b> Carregando </b></span>";
   if (http.readyState == 4) {    
   if (http.status == 200) {
			
		var texto = http.responseText;	
		document.getElementById('centro').innerHTML = unescape(texto.replace(/\+/g," "));
		RodarScript(texto);
		document.getElementById('loading').innerHTML = " ";
        } else {
        alert("Problemas ao carregar o arquivo.");
        }
    }
}

/*===============================================================================================================================*/

function resposta() {

   if (http.readyState == 4) {    
   if (http.status == 200) {
       
  if(http.responseText == 1) {
   document.getElementById("resposta").style.color = 'red';
   document.getElementById("resposta").innerHTML = "<center><b> Nome e email são obrigatórios </b></center>";
   } 

   else if(http.responseText == 2) {
   document.getElementById("resposta").style.color = 'red';
   document.getElementById("resposta").innerHTML = "<center><b>e-mail inválido</b></center>";
   } 

   else if(http.responseText == 3) {
   document.getElementById("resposta").style.color = 'green';
   document.getElementById("resposta").innerHTML = "<center><img src='img/imgLoading.gif'></center><br /><center><b> Cadastrado realizado </b></center>";
   setTimeout("Cadastro()",2000);    // Redireciona para uma pagina....
   } 

   else if(http.responseText == 4) {
   document.getElementById("resposta").style.color = 'red';
   document.getElementById("resposta").innerHTML = "<center><b>e-mail já cadastrado</b></center>";
   } 

   else

   document.getElementById("resposta").innerHTML = unescape(http.responseText.replace(/\+/g," "));
       } else {
           alert("Problemas ao carregar o arquivo.");
       }         // fecha http.readyState
   }            //  fecha http.status

} // fecha resposta

/*===============================================================================================================================*/

function RodarScript(texto){
    var ini = 0;
    while (ini!=-1){
        ini = texto.indexOf('<script', ini);
        if (ini >=0){
            ini = texto.indexOf('>', ini) + 1;
            var fim = texto.indexOf('</scr'+'ipt>', ini);
            codigo = texto.substring(ini,fim);
            novo = document.createElement("script");
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
}

/*===============================================================================================================================*/