/************************/	
/* Função Validar Data **/
/************************/	
function validarData(campo){//valida datas
var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
var msgErro = 'Data incorreta.';
if (campo.value=="") return true;


if ((campo.value.match(expReg)) && (campo.value!='')){
		var dia = campo.value.substring(0,2);
		var mes = campo.value.substring(3,5);
		var ano = campo.value.substring(6,10);
	if(mes==4 || mes==6 || mes==9 || mes==11 && dia > 30) {
		alert("Data incorreta!");
		campo.value="";
		return false;
	} 
	else {
		if(ano%4!=0 && mes==2 && dia>28) {
			alert("Data incorreta!");
			campo.value="";
		return false;
		} 
		else {
			if(ano%4==0 && mes==2 && dia>29) {
				alert("Data incorreta!");
				campo.value="";
				return false;
			} 
			else { 
				return true;
			}
		}
	}
} 
else {
	alert(msgErro);
	campo.value="";	
	campo.focus();
	return false;
	}
}


/*******************/	
/* Validar CNPJ ****/
/*******************/	
function ValidaCNPJ(campo) {
			 CNPJ = campo;
			 erro = new String;
			 if (CNPJ.length < 18) return false; 
			 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
			 if (erro.length == 0) return false;
			 }
			 //substituir os caracteres que nao sao números
		   if(document.layers && parseInt(navigator.appVersion) == 4){
				   x = CNPJ.substring(0,2);
				   x += CNPJ. substring (3,6);
				   x += CNPJ. substring (7,10);
				   x += CNPJ. substring (11,15);
				   x += CNPJ. substring (16,18);
				   CNPJ = x; 
		   } else {
				   CNPJ = CNPJ. replace (".","");
				   CNPJ = CNPJ. replace (".","");
				   CNPJ = CNPJ. replace ("-","");
				   CNPJ = CNPJ. replace ("/","");
		   }
		   var nonNumbers = /\D/;
		   if (nonNumbers.test(CNPJ)) return false; 
		   var a = [];
		   var b = new Number;
		   var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		   for (i=0; i<12; i++){
				   a[i] = CNPJ.charAt(i);
				   b += a[i] * c[i+1];
}
		   if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
		   b = 0;
		   for (y=0; y<13; y++) {
				   b += (a[y] * c[y]); 
		   }
		   if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
		   if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
				   return false;
		   }
		   if (erro.length > 0){
				   return false;
		   } else {
				 return true;
		   }
		   return true;
   }	

/******************/	
/* Validar CPF ****/
/******************/	
function ValidaCPF (numero) {
	var numcpf, cpf;
	numcpf = numero.substr(0,3);
	numcpf += numero.substr(4,3);
	numcpf += numero.substr(8,3);
	numcpf += numero.substr(12,2);
	cpf = numcpf;

	if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
	return false;
	add = 0;
		for (i=0; i < 9; i ++)
		add += parseInt(cpf.charAt(i)) * (10 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
		rev = 0;
	if (rev != parseInt(cpf.charAt(9)))
		return false;
	add = 0;
	for (i = 0; i < 10; i ++)
		add += parseInt(cpf.charAt(i)) * (11 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
	rev = 0;
	if (rev != parseInt(cpf.charAt(10))) return false;
	return true;
	}	


/**********************/	
/* Valida o e-mail ****/
/**********************/	
function ValidarEmail(email) {
    er = /^[a-z0-9\._-]+@([a-z0-9-]{2,26}(\.[a-z0-9-]{2,26})+)|([0-9-]{1,3}\.[0-9-]{1,3}\.[0-9-]{1,3}\.[0-9-]{1,3})1$/;
    if (!er.test(email.toLowerCase())) { 
		return false;
	}
    else { 
		return true; 
	}
}

/*****************/	
/* Abre URL*******/	
/*****************/	

function abrirurl_param2(tipo,url,pagina){

	if (pagina   >  "0") pag = "&np="+pagina;
	url = 'index.php?pg='+ tipo + url + pag;
	document.location.href = url;
}

/*****************/	
/* Mensagem*******/
/*****************/	
function enviar_email(tipo) {
	if (document.getElementById("descassunto").value==""){
		window.alert("É necessário informar o Assunto!");
		document.getElementById("descassunto").focus();
	}
	else if (document.getElementById("desnom").value==""){
		window.alert("É necessário informar seu NOME!");
		document.getElementById("desnom").focus();
	}
	else if (document.getElementById("emailan").value==""){
		window.alert("É necessário informar seu E-MAIL!");
		document.getElementById("emailan").focus();
	}
	else if (document.getElementById("desufs").value=="" || document.getElementById("desufs").value=="0"){
		window.alert("É necessário informar seu ESTADO!");
		document.getElementById("desufs").focus();
	}
	else if (document.getElementById("codcid").value=="" || document.getElementById("codcid").value=="0"){
		window.alert("É necessário informar sua CIDADE!");
		document.getElementById("codcid").focus();
	}
	else if (trim(document.getElementById("desmens").value)==""){
		window.alert("É necessário informar sua MENSAGEM!");
		document.getElementById("desmens").focus();
	}
	else if(!ValidarEmail(document.getElementById("emailan").value)){
		window.alert("O E-MAIL informado está inválido!");
		document.getElementById("emailan").focus();
	}
	else {
			document.getElementById("status").value = "enviar";
			campos = carregadados("frmgeral");
			document.getElementById("mensagem").style.display='block';
			enviardados("http://www.gersonmartins.jor.br/canais/contato/enviaremail.php?tp="+tipo, campos, "mensagem",tipo,"0","POST");
			limpardados("frmgeral");
	}
}


/*******************/	
/* Newsletter*******/
/*******************/	
function enviar_newsletter(tipo) {
	if (document.getElementById("desnome").value=="" || document.getElementById("desnome").value=="Nome" ){
		window.alert("É necessário informar seu NOME!");
		document.getElementById("desnome").focus();
	}
	else if (document.getElementById("desema").value=="" || document.getElementById("desema").value=="E-mail"){
		window.alert("É necessário informar seu E-MAIL!");
		document.getElementById("desema").focus();
	}
	else if(!ValidarEmail(document.getElementById("desema").value)){
		window.alert("O E-MAIL informado está inválido!");
		document.getElementById("desema").focus();
	}
	else {
			campos = carregadados("frmgeral");
			document.getElementById("corpomensagem").style.display='block';
			enviardados("http://www.gersonmartins.jor.br/canais/newsletter/enviarnewsletter.php?tp="+tipo, campos, "mensagemNews",tipo,"0","POST");
			limpardados("frmgeral");
	}
}


/**************************/	
/* Buscar Conteúdos *******/
/*************************/	
function buscaConteudo(texto) {
	if (texto=="Buscar") {
		window.alert("Informe uma palavra para a Busca!");
		document.getElementById("buscaPalavra").focus();
	}
	else document.location.href = "http://www.gersonmartins.jor.br/busca/pagina1/0/"+texto;
}

/*****************************/	
/* Remove espaços em Branco***/
/*****************************/	
function trim(str){
	var palavra = str; 
	return palavra.replace(/^\s+|\s+$/g,"");
}

function abrirurl_param(tipo,url){
	url = tipo + url;
	document.location.href = url;
}

