function XML_HTTP(Pagina, obj) { 
	var oAjax = new ajax;
	var resposta = oAjax.enviar(Pagina, "POST", false);
	//alert("Pronto");
	obj.innerHTML = resposta; 
	//alert("Fim");
}

// Inicializador Ajax
function ajax() {
    
};
// Método iniciar
ajax.prototype.iniciar = function() {
// instanciando o objeto em cada navegador
    try {
        this.xmlhttp = new XMLHttpRequest();
    }
    catch(ee) {
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E) {
                this.xmlhttp = false;
            }
        }
    }
    return true;
}
// Metodo Ocupado
ajax.prototype.ocupado = function() {
    estadoAtual = this.xmlhttp.readyState;
    return (estadoAtual && (estadoAtual < 4));
}
// Metodo Processa
ajax.prototype.processa = function() {
    if (this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) {
        return true;
    }
}
// Metodo que faz as requisições web
ajax.prototype.enviar = function(url, metodo, modo) {
    // se nao estiver iniciado vai iniciar dae...
    if(!this.xmlhttp) {
        this.iniciar();
    }
    // se nao estiver ocupado
    if(!this.ocupado()) {
        //alert("não ocupado...")
        // se for metodo GET
        if(metodo == "GET") {
            this.xmlhttp.open("GET", url, modo);
            this.xmlhttp.setRequestHeader("encoding","ISO-8859-1");
            this.xmlhttp.send(null);
        }
        else {
			//alert("POST")
            // se for post
            this.xmlhttp.open("POST", url, modo);
            //alert("abriu")
            this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
            //alert("Header content")
            this.xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            //alert("Header 1")
            this.xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
            //alert("Header 2")
            this.xmlhttp.setRequestHeader("Pragma", "no-cache");
            //alert("Header 3")
            this.xmlhttp.setRequestHeader("encoding","ISO-8859-1");
            var parametros = new String(url);
            this.xmlhttp.send(parametros.substring(parametros.indexOf("?",1)+1,parametros.length));
            //alert("enviou")
        }
        if(this.processa) {
            // recebe o resultado da pagina... asp
            return unescape(this.xmlhttp.responseText.replace(/\+/g," "));
        }
    }
    else {
		alert("ocupado...")
    }
    return false;
}

// 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;  
}  

/*********************************************************************************************/
// FUNÇÕES ESPECÍFICAS:
/*********************************************************************************************/
function enviaPedidoOracao(Nome,Cidade,UF,Pedido, obj) {
	XML_HTTP('adm/Inserir.asp?t=PEDIDO_ORACAO&c=NOME,CIDADE,ESTADO,PEDIDO&v=\''+url_encode(Nome)+'\'|\''+url_encode(Cidade)+'\'|\''+url_encode(UF)+'\'|\''+url_encode(Pedido)+'\'&r='+url_encode("Pedido de Oração Enviado com Sucesso!"), obj);
}
function limpaCamposPedidoOracao() {
	txtNome.value = "";
	txtCidade.value = "";
	txtUF.value = "";
	txtPedido.value = "";
}

function enviaMensagem(Nome,Email,Cidade,UF,Mensagem, obj) {
	XML_HTTP('adm/Inserir.asp?t=RECADO&c=NOME,EMAIL,CIDADE,ESTADO,MENSAGEM&v=\''+url_encode(Nome)+'\'|\''+url_encode(Email)+'\'|\''+url_encode(Cidade)+'\'|\''+url_encode(UF)+'\'|\''+url_encode(Mensagem)+'\'&r='+url_encode("Recado Enviado com Sucesso!"), obj);
}
function limpaCamposMensagem() {
	txtNome2.value = "";
	txtEmail.value = "";
	txtCidade2.value = "";
	txtUF2.value = "";
	txtMensagem.value = "";
}
