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 enviaemail(){
    enviar("objs/envia.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("objs/home2.php", function() { processado(); });
}

function Homelogado(){
    carrega("objs/arquivoss.php", function() { clienteslogado(); });
}

function Sair(){
    carrega("incs/logout.php", function() { clienteslogado(); });
}

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

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

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

function paginacao(pagina){ 
	
	    http.open('get', 'objs/arquivoss.php?pagina='+pagina+'&rand='+Math.floor(Math.random()*100), true);
		http.onreadystatechange = clienteslogado;
        http.send(null);
}

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

function noticias(){ 
	
	    http.open('get', 'objs/noticiass.php?rand='+Math.floor(Math.random()*100), true);
		http.onreadystatechange = processado;
        http.send(null);
}

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

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

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

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

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

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) {

		document.getElementById('conteudo').innerHTML = unescape(http.responseText.replace(/\+/g," "));
		document.getElementById('loading').innerHTML = " ";
        } else {
        alert("Problemas ao carregar o arquivo.");
        }
    }
}
/*===============================================================================================================================*/

function clienteslogado(){
    // 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) {

	   if(http.responseText == 2) {
	   document.getElementById("centroinside").innerHTML = "<center><b>Redirecionando</b></center>";
	   window.location.href = "index.php";    // Redireciona para uma pagina....
	   } 
	 
	    else
		
		document.getElementById('centroinside').innerHTML = unescape(http.responseText.replace(/\+/g," "));
		document.getElementById('loading').innerHTML = " ";
        } else {
        alert("Problemas ao carregar o arquivo.");
        }
    }
}
/*===============================================================================================================================*/

function resposta() {

   if (http.readyState == 4) {    
   if (http.status == 200) {
     
	if(document.forms[0].nome.value=="")	{
		window.alert('Por favor preencha corretamente o seu nome.');
		document.forms[0].nome.focus();
		return false;
	}

	if(document.forms[0].nome.value.length < 3 )	{
		window.alert('Nome não pode ser inferiror a 3 caracteres.');
		document.forms[0].nome.focus();
		return false;
	}

	if (/^([\w\.\-])+\@(([\w\-\0-9])+\.)+([\w ]{2,4})$/.test(document.forms[0].email.value) == false)   {
	alert("E-mail inválido!");
	document.forms[0].email.focus();
	return false;
	}

	if(document.forms[0].assunto.value=="")	{
		window.alert('Por favor preencha corretamente o Assunto.');
		document.forms[0].assunto.focus();
		return false;
	}

	if(document.forms[0].departamento.value=="null")	{
		window.alert('Selecione um Departamento.');
		document.forms[0].departamento.focus();
		return false;
	}

	if(document.forms[0].mensagem.value.length < 10 )	{
		window.alert('Mensagem não pode ser inferiror a 10 caracteres.');
		document.forms[0].mensagem.focus();
		return false;
	}

   else

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

} // fecha resposta
/*===============================================================================================================================*/

/* Chama a função acima */
function autenticar(){
    enviar("incs/valida.php", function() { validar(); });
}

function Alterar(){
    enviar("incs/action.php", function() { alterardados(); });
}

function Esqueci(){
    enviar("incs/processa-envia-senha.php", function() { enviasenha(); });
}

/*================================================================================================================================*/
/* Chama a função padrão */

function Minhasenha(){
    carrega("incs/envia-senha.php", function() { senha(); });  /* div onde se encontra login e eaqueci minha senha */
}

function Perfil(){
    carrega("incs/usuarios.php", function() { clienteslogado(); });
}

function Logout(){
    carrega("incs/logout.php", function() { clienteslogado(); });
}

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

/* Valida Login */

function validar() {

   document.getElementById("resposta").style.color = 'green';	
   document.getElementById("resposta").innerHTML = "<center><b>Processando Aguarde...</b></center>";
   if (http.readyState == 4) {    
   if (http.status == 200) {
     
   if(http.responseText == 1) {
   document.getElementById("resposta").innerHTML = "<center><b>Redirecionando Aguarde ...</b></center>";
   window.location.href = "inside.php";    // Redireciona para uma pagina....
   } 
   
   else if(http.responseText == 2) {
   document.getElementById("resposta").style.color = 'red';	   
   document.getElementById("resposta").innerHTML = "<center><b>Login Inexistente</b></center>";
   } 
   
   else if(http.responseText == 3) {
   document.getElementById("resposta").style.color = 'red';
   document.getElementById("resposta").innerHTML = "<center><b>Senha Inválida</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 validalogin

/*================================================================================================================================*/
/* Alterar Senha */

function alterardados() {

   document.getElementById("retorno").style.color = 'green';	
   document.getElementById("retorno").innerHTML = "<center><b>Alterando Aguarde</b></center>";
   if (http.readyState == 4) {    
   if (http.status == 200) {
	   
   if(http.responseText == 2) {
   document.getElementById("retorno").innerHTML = "<center><b>Redirecionando</b></center>";
   window.location.href = "index.php";    // Redireciona para uma pagina....
   } 
   
  	if(document.form1.cliente.value == ""){
		window.alert('O NOME NÃO PODE SER EM BRANCO');
		document.form1.cliente.focus();
		return false;
	}

	if(document.form1.endereco.value == ""){
		window.alert('O ENDEREÇO NÃO PODE SER EM BRANCO');
		document.form1.endereco.focus();
		return false;
	}

	if(document.form1.numero.value == ""){
		window.alert('O NUMERO NÃO PODE SER EM BRANCO');
		document.form1.numero.focus();
		return false;
	}

	if(document.form1.bairro.value == ""){
		window.alert('O BAIRRO NÃO PODE SER EM BRANCO');
		document.form1.bairro.focus();
		return false;
	}

	if(document.form1.cep.value == ""){
		window.alert('O CEP NÃO PODE SER EM BRANCO');
		document.form1.cep.style.background = "#EEEEEE";
		document.form1.cep.focus();
		return false;
	}

	if(document.form1.cidade.value == ""){
		window.alert('A CIDADE NÃO PODE SER EM BRANCO');
		document.form1.cidade.focus();
		return false;
	}

	if(document.form1.email.value == ""){
		window.alert('O EMAIL NÃO PODE SER EM BRANCO');
		document.form1.email.focus();
		return false;
	} else {
	
	if (/^([\w\.\-])+\@(([\w\-\0-9])+\.)+([\w ]{2,4})$/.test(document.form1.email.value) == false) {
	alert("E-MAIL INVÁLIDO");
	document.form1.email.focus();
	return false;
	}
	}
	
	if(document.form1.senha.value == ""){
		window.alert('O SENHA NÃO PODE SER EM BRANCO');
		document.form1.senha.focus();
		return false;
	}

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

} // fecha validalogin

/*================================================================================================================================*/
/* Envia senha */

function enviasenha() {

   document.getElementById("resposta").style.color = 'green';	
   document.getElementById("resposta").innerHTML = "<center><b>Processando Aguarde...</b></center>";
   if (http.readyState == 4) {    
   if (http.status == 200) {
     
   if(http.responseText == 1) {
   document.getElementById("resposta").style.color = 'red';
   document.getElementById("resposta").innerHTML = "<center><b>E-mail inválido ...</b></center>";
   } 
  
   else if(http.responseText == 2) {
   document.getElementById("resposta").style.color = 'red';
   document.getElementById("resposta").innerHTML = "<center><b>E-mail não cadastrato ...</b></center>";
   } 
   
   else if(http.responseText == 3) {
   document.getElementById("resposta").style.color = 'green';	   
   document.getElementById("resposta").innerHTML = "<center><b>E-mail enviado com sucesso ...</b></center>";
   window.location.href = "index.php";    // Redireciona para uma pagina....
   } 

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

} // fecha enviasenha

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

function senha() {

   document.getElementById("conteudo").style.color = 'green';	
   document.getElementById("conteudo").innerHTML = "<center><b>Processando Aguarde...</b></center>";
   if (http.readyState == 4) {    
   if (http.status == 200) {
     
	 document.getElementById("conteudo").innerHTML = unescape(http.responseText.replace(/\+/g," "));
     
       } else {
           alert("Problemas ao carregar o arquivo.");
       }         // fecha http.readyState
   }            //  fecha http.status

} // fecha enviasenha

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