//Funcao da Mascara
//Responsavél: Marcelo Abib Cardoso
//Data: 10/02/2006
function fnMascara(objeto, evt, mask)
{ 
	var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
	var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';	
	var Numeros = '0123456789';
	var Fixos  = '().-:/ ';
	var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

	evt = (evt) ? evt : (window.event) ? window.event : "";
	var value = objeto.value;
	if (evt) {
 	var ntecla = (evt.which) ? evt.which : evt.keyCode;
 	tecla = Charset.substr(ntecla - 32, 1);
 	if (ntecla < 32) return true;

 	var tamanho = value.length;
 	if (tamanho >= mask.length) return false;

 	var pos = mask.substr(tamanho,1);
 	while (Fixos.indexOf(pos) != -1)
	 	{
 	 		value += pos;
 	 		tamanho = value.length;
 	 		if (tamanho >= mask.length) return false;
 	 		pos = mask.substr(tamanho,1);
		}

 	switch (pos) 
		{
   			case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
   			case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
   			case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
   			case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
   			case '*' : objeto.value = value; return true; break;
   			default : return false; break;
 		}
	}
	objeto.value = value;
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @author "Desconhecido" projetos@logicadigital.com.br
* @copyright (c) 2005 Lógica Digital
* Descrição: Script para validar C.P.F.
*/

function fVerificaCPF(campo) {
	var CPF = campo.value; // Recebe o valor digitado no campo
	CPF = CPF.replace("-", "");
	CPF = CPF.replace(".", "");
	CPF = CPF.replace(".", "");
	
	if( (CPF == '11111111111') || (CPF == '22222222222') || (CPF == '33333333333') || (CPF == '44444444444') ||
  		(CPF == '55555555555') || (CPF == '66666666666') || (CPF == '77777777777') || (CPF == '88888888888') ||
   		(CPF == '99999999999') || (CPF == '00000000000') )
	{
		  document.getElementById('cpf_erro').style.display = '';
		  document.getElementById('cpf_ok').style.display = 'none';
		  return false;				  
	}
	else
	{
		
		// Aqui começa a checagem do CPF
		var posicao, i, soma, dv, dv_informado;
		var digito = new Array(10);
		dv_informado = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado
		
		// Desemembra o número do CPF na array DIGITO
		for (i=0; i<=8; i++) {
		  digito[i] = CPF.substr( i, 1);
		}
		
		// Calcula o valor do 10º dígito da verificação
		posicao = 10;
		soma = 0;
		   for (i=0; i<=8; i++) {
			  soma = soma + digito[i] * posicao;
			  posicao = posicao - 1;
		   }
		digito[9] = soma % 11;
		   if (digito[9] < 2) {
				digito[9] = 0;
		}
		   else{
			   digito[9] = 11 - digito[9];
		}
		
		// Calcula o valor do 11º dígito da verificação
		posicao = 11;
		soma = 0;
		   for (i=0; i<=9; i++) {
			  soma = soma + digito[i] * posicao;
			  posicao = posicao - 1;
		   }
		digito[10] = soma % 11;
		   if (digito[10] < 2) {
				digito[10] = 0;
		   }
		   else {
				digito[10] = 11 - digito[10];
		   }
		
		// Verifica se os valores dos dígitos verificadores conferem
		dv = digito[9] * 10 + digito[10];
		   if (CPF.length > 0)
		   {
			   if (dv != dv_informado)  
			   {
				  document.getElementById('cpf_erro').style.display = '';
				  document.getElementById('cpf_ok').style.display = 'none';
				  return false;
			   }
			   else
			   {
				  document.getElementById('cpf_erro').style.display = 'none';
				  document.getElementById('cpf_ok').style.display = '';
				  return false;
			   }
			}
			else
			{
				document.getElementById('cpf_erro').style.display = 'none' ;
				document.getElementById('cpf_ok').style.display = 'none' ;
			}
		}
}

//CNPJ
function fun_gera_verificacao_cnpj(CNPJ)
{
	erro = new String;
	if (CNPJ.length < 18) erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";
	
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))
	{
		if (erro.length == 0) erro += "É necessarios preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	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("/","");
	}
	
	if( (CNPJ == '11111111111111') || (CNPJ == '22222222222222') || (CNPJ == '33333333333333') || (CNPJ == '44444444444444') ||
		(CNPJ == '55555555555555') || (CNPJ == '66666666666666') || (CNPJ == '77777777777777') || (CNPJ == '88888888888888') ||
		(CNPJ == '99999999999999') || (CNPJ == '00000000000000') )
	{
		  document.getElementById('cnpj_erro').style.display = '';
		  document.getElementById('cnpj_ok').style.display = 'none';
		  return false;				  
	}
	else
	{
		var nonNumbers = /\D/;
		if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";
		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]))
		{
			erro +="Digito verificador com problema!";
		}
		if (erro.length > 0)
		{
		    document.getElementById('cnpj_erro').style.display = '';
		    document.getElementById('cnpj_ok').style.display = 'none';
			return false;
		}
		else
		{
			document.getElementById('cnpj_erro').style.display = 'none';
			document.getElementById('cnpj_ok').style.display = '';
		}
		return true;
	}
}
//Ainda Cnpj
function fun_toggle_instrucoes(div){
	_div = document.getElementById(div);

	novo_display = (_div.style.display == 'block') ? 'none' : 'block';
	novo_display_selects = (novo_display == 'none') ? '' : 'none';

	_div.style.display = novo_display;
	showOrHideAllDropDowns(novo_display_selects);
}

function showOrHideAllDropDowns(newState) {
	var elements = document.documentElement.getElementsByTagName('select');

	for (var i=0; i<elements.length; i++) {
		elements[i].style.display = newState;
	}
}
