// JavaScript Document

//function validaCPF(campo) {
jQuery.validator.addMethod("isCPF", function(cpf, element) {
	var erro = 0;
	if (cpf.length < 11){ erro = 1; } 
	var nonNumbers = /\D/;
	cpf = cpf.replace(/\./g, "");
	cpf = cpf.replace(/\-/g, "");
	if (nonNumbers.test(cpf)){ erro = 1; } 
	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || 
		cpf == "88888888888" || cpf == "99999999999"){
			erro = 1;
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] * --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] * c--); 
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
		erro = 1;
	}	
	if (erro == 1){
		return false;
	}else{
		return true;
	}
}, "Informe um CPF válido.");

jQuery.validator.addMethod("isCNPJ", function(cnpj, element) {

	// DEIXA APENAS OS NÚMEROS
   cnpj = cnpj.replace('/','');
   cnpj = cnpj.replace(/\./g, "");
   cnpj = cnpj.replace(/\_/g, "");
   cnpj = cnpj.replace('-','');

	if (cnpj.length > 0){
	 
	   var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	   digitos_iguais = 1;
	 
	   if (cnpj.length < 14 && cnpj.length < 15){
		  return false;
	   }
	   for (i = 0; i < cnpj.length - 1; i++){
		  if (cnpj.charAt(i) != cnpj.charAt(i + 1)){
			 digitos_iguais = 0;
			 break;
		  }
	   }
	 
	   if (!digitos_iguais){
		  tamanho = cnpj.length - 2
		  numeros = cnpj.substring(0,tamanho);
		  digitos = cnpj.substring(tamanho);
		  soma = 0;
		  pos = tamanho - 7;
	 
		  for (i = tamanho; i >= 1; i--){
			 soma += numeros.charAt(tamanho - i) * pos--;
			 if (pos < 2){
				pos = 9;
			 }
		  }
		  resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		  if (resultado != digitos.charAt(0)){
			 return false;
		  }
		  tamanho = tamanho + 1;
		  numeros = cnpj.substring(0,tamanho);
		  soma = 0;
		  pos = tamanho - 7;
		  for (i = tamanho; i >= 1; i--){
			 soma += numeros.charAt(tamanho - i) * pos--;
			 if (pos < 2){
				pos = 9;
			 }
		  }
		  resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		  if (resultado != digitos.charAt(1)){
			 return false;
		  }
		  return true;
	   }else{
		  return false;
	   }
	}else{
		return true;
	}
}, "Informe um CNPJ válido."); // Mensagem padrao 

jQuery.validator.addMethod("brDate", function(data, element) {
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	var msgErro =  "Formato inválido de data. Utilize DD/MM/YYYY.";
	
	if ((data.match(expReg)) && (data !='')){
		var dia = data.substring(0,2);
		var mes = data.substring(3,5);
		var ano = data.substring(6,10);
		if((mes==4 || mes==6 || mes==9 || mes==11) && dia > 30){
			//msgErro = "Dia incorreto !!! O mes especificado contém no máximo 30 dias.";
			return false;
		}else{
			if(ano%4!=0 && mes==2 && dia>28){
				//msgErro = "Data incorreta!! O mes especificado contém no máximo 28 dias.";
				return false;
			}else{
				if(ano%4==0 && mes==2 && dia>29){
					//msgErro = "Data incorreta!! O mes especificado contém no máximo 29 dias.";
					return false;
				}else{
					return true;
				}
			}
		}
	}else {
		return false;
	}

}, "Formato inválido de data. Utilize DD/MM/YYYY." );

jQuery.validator.addMethod("telefones", function(element) {							 
												 
	var telefoneResidencial = document.formularioInscricao.telefoneResidencial.value;
	var telefoneComercial 	= document.formularioInscricao.telefoneComercial.value;
	var telefoneCelular		= document.formularioInscricao.telefoneCelular.value;						 
	
	telefoneResidencial 	= telefoneResidencial.replace(/[-()_ ]/g, "");
	telefoneComercial 		= telefoneComercial.replace(/[-()_ ]/g, "");
	telefoneCelular 		= telefoneCelular.replace(/[-()_ ]/g, "");
	
	if (telefoneResidencial.length == 10 || telefoneComercial.length == 10 || telefoneCelular.length == 10){
		return true;
	} else {
		return false;
	}

}, "Informe pelo menos um número de telefone para contato!" );

jQuery.validator.addMethod("cep", function(cep, element) {							 
	cep 	= cep.replace(/[_.-]/g, "");									 
	if (cep.length == 8){
		return true;
	} else {
		return false;
	}

}, "Informe seu cep corretamente!" );
