try {
 this.xmlhttp = new XMLHttpRequest();
 } 
 //cria o objeto XMLHttpRequest pra IE 6.0 e posteriormente para IE7+
 catch (e) {
 try {
 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } 
 
 catch (e) {
 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 }
 }

campo = "not_id";
tipo = "DESC";
pagina = "";
flag="";
url="";


var ajax = {
	
	carregarCSS : function(url) {
		var lnk = document.createElement('link');
		lnk.setAttribute('type', "text/css" );
		lnk.setAttribute('rel', "stylesheet" );
		lnk.setAttribute('href', url );
		document.getElementsByTagName("head").item(0).appendChild(lnk);
		var lnk = document.createElement('link');
		lnk.setAttribute('type', "text/css" );
		lnk.setAttribute('rel', "stylesheet" );
		lnk.setAttribute('href', "css/principal.css" );
		document.getElementsByTagName("head").item(0).appendChild(lnk);
	},

		
	camposForm : function(oForm){
		var aParams = new Array();
		for (var i=0 ; i < oForm.length; i++) {
			var sParam = oForm[i].id;
			sParam += "=";
			sParam += oForm[i].value;
			aParams.push(sParam);
		}
		return aParams.join("&");
	},
	
	smiler : function(emoticom) {
		document.getElementById("LVmsg").value +=  " "+emoticom;
	},
	
	cadastraComentario : function(){
		param = ajax.camposForm(document.form_comentario.elements);
		flag=document.form_comentario.elements[6].value;
		if(validarFomComentario()){
			xmlhttp.open('POST', 'portal_noticia/coment.php', true);
			xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			xmlhttp.onreadystatechange = ajax.repostasComentario;
			xmlhttp.send(param);
		}
		
	},
	
	repostasComentario : function(){
		
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				ajax.listaCometario();
				document.form_comentario.elements[0].value="";
				document.form_comentario.elements[1].value="";
				document.form_comentario.elements[2].value="";
				document.form_comentario.elements[3].value="200";
			}
		}
		
	},
	
	listaCometario : function(){
	
		local = document.getElementById('comentarios');
		xmlhttp.open('GET', 'portal_noticia/comentarios.php?not_id='+flag, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200)
					local.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
		
	},
	
	denunciarRecado : function(id){
		
			xmlhttp.open("GET", "portal_mural/include/denuncia.php?idrecado="+id, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						alert('Obrigado por ajudar a manter nosso site limpo.');
					}
				}
			}
			xmlhttp.send(null);
		
	},
	
	cadastraRecado : function(){
		param = ajax.camposForm(document.form_comentario.elements);
		if(validarFomRecado()){
			xmlhttp.open('POST', 'portal_mural/include/ajax.php?cadastro=1', true);
			xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			xmlhttp.onreadystatechange = ajax.repostasRecado;
			xmlhttp.send(param);
		}
		
	},
	
	repostasRecado : function(){
		
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				document.form_comentario.reset();
				ajax.listaRecados();
			}
		}
		
	},
	
	listaRecados : function(){
	
		local = document.getElementById('livrovisita');
		xmlhttp.open('GET', 'portal_mural/busca_mural_recados.php', true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200)
					local.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
		
	},
	
	pesquisa: function pesquisa(pag, urlpagina){
		
		pagina = pag;
		url = urlpagina;
		ajax.listaTudo();
		
	},
	
	listaTudo : function(){
		
		local = document.getElementById('conteudo');
		xmlhttp.open('GET', url+'?_pagi_pg='+pagina, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 1) {
					local.innerHTML = '<center><img src=\"../imagens/carregando.gif\" /> <br /><br />&nbsp;&nbsp;</center>';
			}
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
					local.innerHTML = xmlhttp.responseText;
				}
			}
		}
		xmlhttp.send(null);
		
	},
	
	realcaCampo : function(id, opc){
		if(opc){
			document.getElementById(id).style.border = '1px solid #2A5178';
			document.getElementById(id).style.background = '#EBEDFE';
		}else{
			document.getElementById(id).style.border = '1px solid #C0C0C0';
			document.getElementById(id).style.background = '#FFFFFF';
		}
	},
	
	verificaEnquete : function(){
	 
		for(var x =0; x < document.formEnquete.idResposta.length; x++){
			if(document.formEnquete.idResposta[x].checked == true){
				flag = document.formEnquete.idResposta[x].value;
				return true;
			}
		}
		alert("Selecione uma resposta primeiro!");
		return false;
		
	},
	
	
	abreEnquete : function(id){
		
		xmlhttp.open('GET', 'portal_enquete/enquete.php?id='+id, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
					document.getElementById('enquete').innerHTML = xmlhttp.responseText;
				}
			}
		}
		xmlhttp.send(null);
	},
	
	votacaoEnquete : function(){
		
		formulario = document.formEnquete.elements;
		if(ajax.verificaEnquete()){
			xmlhttp.open('GET', 'portal_enquete/votar.php?idPergunta='+formulario[0].value+'&idResposta='+flag, true);
			xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			xmlhttp.onreadystatechange = ajax.repostasVotacao;
			xmlhttp.send(null);
		}
		
	},
	
	repostasVotacao : function(){
		
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				flag = document.formEnquete.elements[0].value;
				ajax.resultadoEnquete(flag);
				document.formEnquete.reset();
			}
		}
		
	},
	
	resultadoEnquete : function(id){
		
		xmlhttp.open('GET', 'portal_enquete/resultEnquete.php?idPergunta='+id, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
					document.getElementById('enquete').innerHTML = xmlhttp.responseText;
				}
			}
		}
		xmlhttp.send(null);
		
	},
	
		enviaEmailPedido : function(){
		
		if(validarFormPedido()){
			
			param = ajax.camposForm(document.frmpedido.elements);
			xmlhttp.open("POST", "portal_pedido_oracao/send10.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					document.getElementById('mensagemcontato').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('mensagemcontato').innerHTML = '<span>'+xmlhttp.responseText+'</span>';
					}
				}
			}
			xmlhttp.send(param);
			
		}
		
	},
	
	enviaEmailContato : function(){
		
		if(validarFormContato()){
			
			param = ajax.camposForm(document.frmcontato.elements);
			xmlhttp.open("POST", "portal_contato/send10.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					document.getElementById('mensagemcontato').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('mensagemcontato').innerHTML = '<span>'+xmlhttp.responseText+'</span>';
					}
				}
			}
			xmlhttp.send(param);
			
		}
		
	},
	
	abreGaleria : function(pasta, evento){

	janela = window.open("galeria/album.php?pasta="+pasta+"&evento="+evento, "Geleria", "width=650, height=400, top=0, left=0, scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no");

	},
	
	denunciarVela : function(id){
		
			xmlhttp.open("GET", "portal_vela/include/denuncia.php?idvela="+id, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						alert('Obrigado por ajudar a manter nosso site limpo.');
					}
				}
			}
			xmlhttp.send(null);
		
	},
	
	cadastraVela : function(){
		param = ajax.camposForm(document.form_comentario.elements);
		if(validarFormVela()){
			xmlhttp.open('POST', 'portal_vela/include/ajax.php?cadastro=1', true);
			xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			xmlhttp.onreadystatechange = ajax.repostasVela;
			xmlhttp.send(param);
		}
		
	},
	
	repostasVela : function(){
		
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				alert("A sua vela permanecerá acesa por 7 dias.");
				document.form_comentario.reset();
				ajax.listaVela();
			}
		}
		
	},
	
	listaVela : function(){
	
		local = document.getElementById('livrovisita');
		xmlhttp.open('GET', 'portal_vela/busca_vela.php', true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200)
					local.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
		
	},
	
	abreLiturgia : function(dataparam){
		if(dataparam){
			data = dataparam;
			xmlhttp.open('GET', 'portal_liturgia/portal_liturgia.php?data_liturgia='+data, true);

		}else{
			data = document.getElementById('data_6').value;
			xmlhttp.open('GET', 'portal_liturgia/leitor_liturgia.php?data_liturgia='+data, true);
		}
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
					if(dataparam)
						document.getElementById("conteudo").innerHTML = xmlhttp.responseText;
					else
						document.getElementById("conteudo_liturgia").innerHTML = xmlhttp.responseText;
				}
			}
		}
		xmlhttp.send(null);
		
	}
	
	
	/*,
	
	carregarPagina : function(linkpagina, div){
		
		local = document.getElementById(div);
		xmlhttp.open("GET", linkpagina+"", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 1) {
				local.innerHTML = '<div id="enviandomail" style="top:40%;left:45%;position:absolute;"><img src=\"../imagens/carregando.gif\" /> <br />&nbsp;&nbsp;<span>ola</span></div>';
			}
			if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200)
					local.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
		
	}*/
		
}



function ContarCaracteres(campo, contador, limite) {
        if (campo.value.length > limite){
          campo.value = campo.value.substring(0, limite);
		  alert("Limite máximo de caracteres atingido!");
		}
        else
          contador.value = limite - campo.value.length;
      }

function validarFomComentario(){
	if(document.form_comentario.strComAuthor.value==''){
		alert("ATENÇÃO!\nNome - Campo obrigatório.");
		document.form_comentario.strComAuthor.focus();
		return false;
	}
	if(document.form_comentario.strComAuthorEmail.value==''){
		alert("ATENÇÃO!\nE-mail - Campo obrigatório.");
		document.form_comentario.strComAuthorEmail.focus();
		return false;
	}else{
		if(isEmail(document.form_comentario.strComAuthorEmail.value)==false){
			alert("ATENÇÃO!\nE-mail inválido.");
			document.form_comentario.strComAuthorEmail.focus();
			return false;
		}
	}
	if(document.form_comentario.strComText.value==''){
		alert("ATENÇÃO!\nPedido - Campo obrigatório.");
		document.form_comentario.strComText.focus();
		return false;
	}
	return true;
}

function validarFomIndicacao(){
	if(document.mandamai.seu_nome.value==''){
		alert("ATENÇÃO!\nSeu Nome - Campo obrigatório.");
		document.mandamai.seu_nome.focus();
		return false;
	}
	if(document.mandamai.email_amigo.value==''){
		alert("ATENÇÃO!\nE-mail do seu Amigo - Campo obrigatório.");
		document.mandamai.email_amigo.focus();
		return false;
	}else{
		if(isEmail(document.mandamai.email_amigo.value)==false){
			alert("ATENÇÃO!\nE-mail do seu Amigo inválido.");
			document.mandamai.email_amigo.focus();
			return false;
		}
	}
	if(document.mandamai.nome_amigo.value==''){
		alert("ATENÇÃO!\nNome do seu Amigo - Campo obrigatório.");
		document.mandamai.nome_amigo.focus();
		return false;
	}
	return true;
}

function validarFomRecado(){
	if(document.form_comentario.LVnome.value==''){
		alert("ATENÇÃO!\nSeu Nome - Campo obrigatório.");
		document.form_comentario.LVnome.focus();
		return false;
	}
	if(document.form_comentario.LVemail.value==''){
		alert("ATENÇÃO!\nSeu E-mail - Campo obrigatório.");
		document.form_comentario.LVemail.focus();
		return false;
	}else{
		if(isEmail(document.form_comentario.LVemail.value)==false){
			alert("ATENÇÃO!\nSeu E-mail é inválido.");
			document.form_comentario.LVemail.focus();
			return false;
		}
	}
	if(document.form_comentario.LVcidade.value==''){
		alert("ATENÇÃO!\nSua Cidade - Campo obrigatório.");
		document.form_comentario.LVcidade.focus();
		return false;
	}
	if(document.form_comentario.LVestado.value==''){
		alert("ATENÇÃO!\nSeu Estado - Campo obrigatório.");
		document.form_comentario.LVestado.focus();
		return false;
	}
	if(document.form_comentario.LVmsg.value==''){
		alert("ATENÇÃO!\nMensagem - Campo obrigatório.");
		document.form_comentario.LVmsg.focus();
		return false;
	}
	return true;
}

function validarFormContato(){
	if(document.frmcontato.nome.value==''){
		alert("ATENÇÃO!\nSeu Nome - Campo obrigatório.");
		document.frmcontato.nome.focus();
		return false;
	}
	if(document.frmcontato.contatoemail.value==''){
		alert("ATENÇÃO!\nSeu E-mail - Campo obrigatório.");
		document.frmcontato.contatoemail.focus();
		return false;
	}else{
		if(isEmail(document.frmcontato.contatoemail.value)==false){
			alert("ATENÇÃO!\nSeu E-mail é inválido.");
			document.frmcontato.contatoemail.focus();
			return false;
		}
	}
	if(document.frmcontato.cidade.value==''){
		alert("ATENÇÃO!\nSua Cidade - Campo obrigatório.");
		document.frmcontato.cidade.focus();
		return false;
	}
	if(document.frmcontato.assunto.value==''){
		alert("ATENÇÃO!\nAssunto - Campo obrigatório.");
		document.frmcontato.assunto.focus();
		return false;
	}
	if(document.frmcontato.duvida.value==''){
		alert("ATENÇÃO!\nMensagem - Campo obrigatório.");
		document.frmcontato.duvida.focus();
		return false;
	}
	return true;
}

function validarFormPedido(){
	if(document.frmpedido.nome.value==''){
		alert("ATENÇÃO!\nSeu Nome - Campo obrigatório.");
		document.frmpedido.nome.focus();
		return false;
	}
	if(document.frmpedido.contatoemail.value==''){
		alert("ATENÇÃO!\nSeu E-mail - Campo obrigatório.");
		document.frmpedido.contatoemail.focus();
		return false;
	}else{
		if(isEmail(document.frmpedido.contatoemail.value)==false){
			alert("ATENÇÃO!\nSeu E-mail é inválido.");
			document.frmpedido.contatoemail.focus();
			return false;
		}
	}
	if(document.frmpedido.cidade.value==''){
		alert("ATENÇÃO!\nSua Cidade - Campo obrigatório.");
		document.frmpedido.cidade.focus();
		return false;
	}
	if(document.frmpedido.duvida.value==''){
		alert("ATENÇÃO!\nMensagem - Campo obrigatório.");
		document.frmpedido.duvida.focus();
		return false;
	}
	return true;
}
function validarFormVela(){
	if(document.form_comentario.LVnome.value==''){
		alert("ATENÇÃO!\nSeu Nome - Campo obrigatório.");
		document.form_comentario.LVnome.focus();
		return false;
	}
	if(document.form_comentario.LVemail.value==''){
		alert("ATENÇÃO!\nSeu E-mail - Campo obrigatório.");
		document.form_comentario.LVemail.focus();
		return false;
	}else{
		if(isEmail(document.form_comentario.LVemail.value)==false){
			alert("ATENÇÃO!\nSeu E-mail é inválido.");
			document.form_comentario.LVemail.focus();
			return false;
		}
	}
	if(document.form_comentario.LVmsg.value==''){
		alert("ATENÇÃO!\Intenção - Campo obrigatório.");
		document.form_comentario.LVmsg.focus();
		return false;
	}
	return true;
}

function isEmail(nform){
		if (nform == "") {
			return false;
		}else {
			prim = nform.indexOf("@")
			if(prim < 2) {
				return false;
			}
			if(nform.indexOf("@",prim + 1) != -1) {
				return false;
			}
			if(nform.indexOf(".") < 1) {
				return false;
			}
			if(nform.indexOf(" ") != -1) {
				return false;
			}
			if(nform.indexOf(".@") > 0) {
				return false;
			}
			if(nform.indexOf("@.") > 0) {
				return false;
			}
			if(nform.indexOf("/") > 0) {
				return false;
			}
			if(nform.indexOf("[") > 0) {
				return false;
			}
			if(nform.indexOf("]") > 0) {
				return false;
			}
			if(nform.indexOf("(") > 0) {
				return false;
			}
			if(nform.indexOf(")") > 0) {
				return false;
			}
			if(nform.indexOf("..") > 0) {
				return false;
			}
			if(nform.indexOf("*") > 0) {
				return false;
			}
			if(nform.indexOf("+") > 0) {
				return false;
			}
		}
		return true;
	}
	
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=data(v_obj.value)
}

function data(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1/$2")       //Coloca um ponto entre o segundo e o terceiro dígitos
    v=v.replace(/(\d{2})(\d)/,"$1/$2")       //Coloca um barra entre o segundo e o terceiro dígitos
    return v
}

