function verificaCampo( campo ) {
	var local = document.getElementById(campo);

	if (local.value == ""){
		  alert("O campo " + local.name + " deve ser preenchido!");
       	  local.focus();
        return false;
	}
	return true;
}

function validaData(campo){
var data = document.getElementById(campo);

	hoje = new Date();
    anoAtual = hoje.getFullYear();
   	barras = data.value.split("/");
    if (barras.length == 3){
		dia = barras[1];
        mes = barras[0];
   	    ano = barras[2];
        resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano <= anoAtual));
   	    if (!resultado) {
			alert("O formato da data é invalido!");
            data.focus();
            return false;
		}
	}
	else {
		alert("O formato da data é invalido!");
		data.focus();
        return false;
   	}
	return true;
}

//validar numero(verificacao se contem apenas numeros)
function validaNumero( campo ){
var numero = document.getElementById(campo);

	if ( isNaN(numero.value ) ){
		alert ("O campo " + numero.name + " deve conter apenas numeros!");
		numero.focus();
		return false;
	}
	return true;
}

function valida_email( campo )
{
	var email = document.getElementById(campo);
	
	p = email.value.indexOf('@');
	pont = email.value.indexOf('.');
	if (p<1 || p==(email.length-1) || pont<1 || pont==(email.length-1))
	{
		alert ("O endereço de e-mail é inválido.");
		email.value = '';
   		email.focus();
		return false;
	}
	return true;
}

var acessoLogin = 0;
var acessoSenha = 0;

function limpaCampoLogin() {
	if(acessoLogin == 0) {
		document.getElementById("nome_news").value = "";
		acessoLogin = 1;
	}
}

function limpaCampoEmail() {
	if(acessoSenha == 0) {
		document.getElementById("email_news").value = "";
		acessoSenha = 1;
	}
}

function recuperaCampo(campo) {
	if(!document.getElementById(campo).value && campo == "nome_news") {
		document.getElementById(campo).value = "Nome"
		acessoLogin = 0;
	}
	if(!document.getElementById(campo).value && campo == "email_news") {
		document.getElementById(campo).value = "E-mail"
		acessoSenha = 0;
	}
}
//Validação de Telefone
function valida_tel(campo) {
   var valor='';
   var digito = false;
   a = campo.value;
   if (a) {
   num = a.length;
   for (f=0;f<num;f++)
     {
     if (parseInt(a.substr(f,1)) || a.substr(f,1)=='0') 
        if ((a.substr(f,1) != '0') || digito) valor = valor + '' + a.substr(f,1);
        if (parseInt(a.substr(f,1)) && a.substr(f,1) != '0') digito = true;
     };
   num = valor.length;
   if (num < 9 || num > 10)
      {
      alert ('- Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (11) 1234-5678');
      campo.value = '';
      campo.focus();
      }
	  else
	  {
	  if (num == 9) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-7, 3) + '-' + valor.substr(num-4, 4);
	  if (num == 10) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-8, 4) + '-' + valor.substr(num-4, 4);
	  campo.value = valor;
	  }
   }
}
//Função para validar CNPJ
function valida_cnpj(campoform) {
	num_cpf = '';
	campo = campoform.value;

	if (!campo) return true;
	
	for (i=0;i<campo.length;i++)
		{
		resposta=campo.charAt(i) ;
		num = parseFloat(resposta);
		if (resposta==''+num) num_cpf=num_cpf+resposta; 
		}

	if (num_cpf.length == 14)
		{
		d1= 0;
		d4= 0;
		xx= 1;
		for (nCount = 0;nCount< num_cpf.length-2;nCount++)
			{
			if (xx < 5) fator= 6 - xx; else fator= 14 - xx;
			d1 = d1 + num_cpf.charAt(nCount) * fator;
			if (xx < 6) fator = 7 - xx; else fator = 15 - xx;
			d4 = d4 + num_cpf.charAt(nCount) * fator;
	    	xx = xx+1;
			}
		resto = d1%11;
		if (resto < 2) digito1 = 0; else digito1 = 11 - resto;
		d4 = d4 + 2 * digito1;
		resto = d4%11;
		if (resto < 2) digito2 = 0; else digito2 = 11 - resto;
		Check = digito1+''+digito2;
        if (Check != num_cpf.substring(num_cpf.length-2,num_cpf.length))
			{
			alert ('- CNPJ inválido. \nEntre com o CNPJ correto.');
    		campoform.value = '';
    		campoform.focus();
			return false;
			}
		else 
			{
			campoform.value= num_cpf.substring(0,2)
			+ '.' + num_cpf.substring(2,5) + '.' + num_cpf.substring(5,8) + '/' + num_cpf.substring(8,12)
			+ '-' + num_cpf.substring(12,14);
			return true;
			};
		}
	else
		{
		alert ('- CNPJ inválido. \nEntre com o CNPJ correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
		}
}
//Valida CPF
function valida_cpf(campoform) {
	num_cpf = '';
	campo = campoform.value;
	if (campo == "00000000000" || campo == "11111111111" || campo == "22222222222" || campo == "33333333333" || campo == "44444444444" || campo == "55555555555" || campo == "66666666666" || campo == "77777777777"  || campo == "88888888888"  || campo == "99999999999"){
		alert ('- CPF inválido. \nEntre com o CPF correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
	}
	if (campo == "000.000.000-00" || campo == "111.111.111-11" || campo == "222.222.222-22" || campo == "333.333.333-33" || campo == "444.444.444-44" || campo == "555.555.555-55" || campo == "666.666.666-66" || campo == "777.777.777-77"  || campo == "888.888.888-88"  || campo == "999.999.999-99"){
		alert ('- CPF inválido. \nEntre com o CPF correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
	}
	if (!campo) return true;
	for (i=0;i<campo.length;i++)
		{
		resposta=campo.charAt(i) ;
		num = parseFloat(resposta);
		if (resposta==''+num) num_cpf=num_cpf+resposta; 
		}

	if (num_cpf.length == 11)
		{
		soma = 0;
		for (i=0; i < 9; i ++) soma += parseInt(num_cpf.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)	resto = 0;
		soma = 0;
		for (i = 0; i < 10; i ++) soma += parseInt(num_cpf.charAt(i)) * (11 - i);
		resto2 = 11 - (soma % 11);
		if (resto2 == 10 || resto2 == 11) resto2 = 0;
		if ((resto != parseInt(num_cpf.charAt(9))) || (resto2 != parseInt(num_cpf.charAt(10))))
			{
			alert ('- CPF inválido. \nEntre com o CPF correto.');
    		campoform.value = '';
    		campoform.focus();
			return false;
			}
		else
			{
			campoform.value= num_cpf.substring(0,3) + '.' + num_cpf.substring(3,6) + '.'
			+ num_cpf.substring(6,9) + '-' + num_cpf.substring(9,11);
			return true;
			}
		}
	else
		{
		alert ('- CPF inválido. \nEntre com o CPF correto.');
   		campoform.value = '';
   		campoform.focus();
		return false;
		}	
}

