// JavaScript Document
 // Função responsável de conectar a uma página externa e retornar os resultados, no nosso caso a busca_nome.php
var xmlhttp; 

function ajax() { 

   if (window.XMLHttpRequest) { 
      xmlhttp = new XMLHttpRequest(); 
   } else if (window.ActiveXObject) { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
       alert("Seu navegador não suporta XMLHttpRequest (AJAX)."); 
      return; 
   } 

    xmlhttp.open("GET", url, true); 
    xmlhttp.onreadystatechange = processReqChange; 
    xmlhttp.send(null); 
} 


function processReqChange() { 
    if (xmlhttp.readyState == 4) { 
      if (xmlhttp.status == 200) { 
           document.getElementById("colDir").innerHTML = url_decode(xmlhttp.responseText);
      } else { 
          alert("Problemas ao carregar o arquivo."); 
      } 
    } 
}

//Ajax Noticias
var xmlhttpNot; 

function ajaxNot() { 

   if (window.XMLHttpRequest) { 
      xmlhttpNot = new XMLHttpRequest(); 
   } else if (window.ActiveXObject) { 
      xmlhttpNot = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
       alert("Seu navegador não suporta XMLHttpRequest (AJAX)."); 
      return; 
   } 

    xmlhttpNot.open("GET", url, true); 
    xmlhttpNot.onreadystatechange = processReqChangeNot; 
    xmlhttpNot.send(null); 
} 


function processReqChangeNot() { 
    if (xmlhttpNot.readyState == 4) { 
      if (xmlhttpNot.status == 200) { 
           document.getElementById("box_not_capa").innerHTML = url_decode(xmlhttpNot.responseText);
      } else { 
          alert("Problemas ao carregar o arquivo."); 
      } 
    } 
}

//Ajax Formulário
var xmlhttpForm; 

function ajaxForm() { 

   if (window.XMLHttpRequest) { 
      xmlhttpForm = new XMLHttpRequest(); 
   } else if (window.ActiveXObject) { 
      xmlhttpForm = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
       alert("Seu navegador não suporta XMLHttpRequest (AJAX)."); 
      return; 
   } 

    xmlhttpForm.open("GET", url, true); 
    xmlhttpForm.onreadystatechange = processReqChangeForm; 
    xmlhttpForm.send(null); 
} 


function processReqChangeForm() { 
    if (xmlhttpForm.readyState == 4) { 
      if (xmlhttpForm.status == 200) { 
        document.getElementById("nomeForm").value = '';
		document.getElementById("emailForm").value = '';
		document.getElementById("assuntoForm").value = '';
		document.getElementById("mensagemForm").value = '';
		
		if (url_decode(xmlhttpForm.responseText) == 1) {
		alert ("Sua mensagem foi enviada com sucesso!");
		} else {
		alert ("Não foi possível enviar a mensagem!");
		}
		
      } else { 
          alert("Problemas ao carregar o arquivo."); 
      } 
    } 
} 

// url_encode version 1.0 
function url_encode(str) { 
    var hex_chars = "0123456789ABCDEF"; 
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    var n, strCode, hex1, hex2, strEncode = ""; 

    for(n = 0; n < str.length; n++) { 
        if (noEncode.test(str.charAt(n))) { 
            strEncode += str.charAt(n); 
        } else { 
            strCode = str.charCodeAt(n); 
            hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            hex2 = hex_chars.charAt(strCode % 16); 
            strEncode += "%" + (hex1 + hex2); 
        } 
    } 
    return strEncode; 
} 

// url_decode version 1.0 
function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
} 