/*
=============================== FUNCOES DE VALIDACAO =================================
VERSAO: 1.1.0.0
DATA DA ULTIMA ATUZALIZACAO: 18/05/42009
RESPONSAVEL: VINICIUS BALDUINO GARCIA

------------------------------- INDICE FUNCOES ----------------------------------------

1. CADASTRO DE PESSOAS FISICAS
	1.1 verifica_cpf_cnpj(objeto,tipo,msg)
		Descricao: Funcao que verifica valor de CPF e CNPJ
		objeto: Objeto que possui o valor do cpf ou cnpj
		tipo: Digite 1 para CPF e 2 para CNPJ
		msg: Mensagem em caso de campo possuir valores incorretos
		exemplo: <input type="text" name="exemplo" id="exemplo" onKeyDown="verifica_cpf_cnpj(this,1,'CPF Inválido')" />

2. FUNCOES NUMERICAS
	2.1 verifica_numeros_inteiro(objeto,msg)
		Descricao: Funcao que verifica se coteudo contém somente números inteiros
		objeto: Objeto que possui o valor a ser verificado
		msg: Mensagem em caso de campo possuir valores diferentes de numeros inteiros
		exemplo: <input type="text" name="exemplo" id="exemplo" onKeyDown="verifica_numeros_inteiro(this, 'Digite somente Numeros Inteiros')" />
		
3. MASCARAS
	3.1 formatar_mascara(objeto, mascara,tecla)
		Descricao: Funcao que inseri mascaras
		objeto: Objeto que possui o valor a ser verificado
		mascara: Mascara a ser utilizada
		tecla: Tecla Digitada pelo usuario
		exemplo: <input type="text" name="exemplo" id="exemplo" onKeyDown="formatar_mascara(this, '###.###.###-##',window.event.keyCode)" />
	
	3.2 formatar_moeda(campo, separador_milhar, separador_decimal, tecla)
		Descricao: Funcao que formata moeda
		campo: Objeto que possui o valor a ser verificado
		separador_milhar: Separador usado para separar unidades de milhar
		separador_decimal: Separador usado para separar unidades de decimal
		tecla: Tecla Digitada pelo usuario
		exemplo: <input type="text" name="exemplo" id="exemplo" onkeypress="return formatar_moeda(this,',','.',event);" />
		
4. GERAL
	4.1 substitui_caractere(str,strsai,strentra)
		Descricao: Funcao que executa a troca de varaveis de entrada e saida para string
		str: texto a ser trabalhado
		strsai: texto a ser substituido
		strentra: texto que vai substituir
		exemplo: <input type="text" name="exemplo" id="exemplo" onKeyDown="substitui_caractere(str,strsai,strentra)" />
		
	4.2 mostra_display(id,valor,tr)
		Descricao: Funcao que exibe e esconde objetos
		id: id do objeto
		valor: 1 para mostar e 2 para esconder
		tr: true para indicar que o objeto é uma linha ou false para indicar que não é uma linha
		exemplo: <input type="text" name="exemplo" id="exemplo" onKeyDown="substitui_caractere(str,strsai,strentra)" />
		
	4.3 popup(url,largura,altura)
		Descricao: Funcao que executa a troca de varaveis de entrada e saida para string
		str: texto a ser trabalhado
		strsai: texto a ser substituido
		strentra: texto que vai substituir
		exemplo: <input type="text" name="exemplo" id="exemplo" onKeyDown="substitui_caractere(str,strsai,strentra)" />



*/


//======================================= 1. CADASTRO DE PESSOAS FISICAS ===============================================


//1.1 verifica_cpf_cnpj(objeto,tipo,msg)
function verifica_cpf_cnpj(objeto,tipo,msg)
{

var numero = objeto;
numero = substitui_caractere(numero.value,'.','');
numero = substitui_caractere(numero,'-','');
numero = substitui_caractere(numero,'/','');

if(numero == 0)
	{
		return(false);
	}
	else
	{
		g=numero.length-2;
		if(TestDigit(objeto,tipo,g,msg))
		{
			g=numero.length-1;
			if(TestDigit(objeto,tipo,g,msg))
			{	
				return(true);
			}
			else
			{
				return(false);
			}
		}
		else
   		{
     			return(false);
   		}
 		}
}

function TestDigit(objeto,tipo,g,msg)
{

var numero = objeto;
numero = substitui_caractere(numero.value,'.','');
numero = substitui_caractere(numero,'-','');
numero = substitui_caractere(numero,'/','');
var dig=0;
 		var ind=2;
 		for(f=g;f>0;f--)
 		{
  	  		dig+=parseInt(numero.charAt(f-1))*ind;
    		if (tipo==2)
	 	  		{ if(ind>8) {ind=2} else {ind++} }
   			else
   				{ ind++ }
 		}
 
 		dig%=11;
 
  		if(dig<2)
		{
   			dig=0;
 		}
 		else
		{
   			dig=11-dig;
 		}
 		if(dig!=parseInt(numero.charAt(g)))
 		{
			alert(msg);
			objeto.focus();
	   		return(false);
 		}
		else
 		{
   			return(true);
 		}
}


//======================================= 2. FUNCOES NUMERICAS ===============================================


//2.1 verifica_numeros_inteiro(objeto,msg)
function verifica_numeros_inteiro(objeto,msg)
{ 
if(isNaN(objeto.value))
{
	alert("Digite somente Números!");
	objeto.focus();
	return false;
}
}


//======================================= 3. MASCARAS ========================================================

//3.1 formatar_mascara(objeto, mascara, tecla)
function formatar_mascara(objeto, mascara,tecla) 
{
	if(tecla != 8)
	{
		var campo = objeto.value.length;
		var saida = mascara.substring(0,1);
		var texto = mascara.substring(campo);
		if(texto.substring(0,1) != saida) {
			objeto.value += texto.substring(0,1);
		}
	}
}

//3.2 formatar_moeda(campo, separador_milhar, separador_decimal, tecla)
function formatar_moeda(campo, separador_milhar, separador_decimal, tecla) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? tecla.which : tecla.keyCode;

	if (whichCode == 13) return true; // Tecla Enter
	if (whichCode == 8) return true; // Tecla Delete
	key = String.fromCharCode(whichCode); // Pegando o valor digitado
	if (strCheck.indexOf(key) == -1) return false; // Valor inválido (não inteiro)
	len = campo.value.length;
	for(i = 0; i < len; i++)
	if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ separador_decimal + '0' + aux;
	if (len == 2) campo.value = '0'+ separador_decimal + aux;

	if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += separador_milhar;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}

		campo.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		campo.value += aux2.charAt(i);
		campo.value += separador_decimal + aux.substr(len - 2, len);
	}

	return false;
}


//======================================= 4. GERAL ===========================================================

//4.1 substitui_caractere(str,strsai,strentra)
function substitui_caractere(str,strsai,strentra)
{

    while(str.indexOf(strsai)>-1)
    {
        str = str.replace(strsai,strentra);
    }
    return str;
}

function exige_valor(objeto, msg)
{
	if(objeto.value == "")
	{
		alert(msg);
		objeto.focus();
		return false;
	}
}

//4.2 mostra_display(id,valor,tr)
function mostra_display(id,valor,tr)
{

if(tr == true)
{
if(navigator.appName.indexOf('Internet Explorer')>0)
mostra = "inline";
else
mostra = "table-row";
}
else
mostra = "inline";

if(valor == 1)
document.getElementById(id).style.display = mostra;
else
document.getElementById(id).style.display = "none";

}
	
//4.3 popup(url,largura,altura)
function popup(url,largura,altura)
{
	open(""+url+"","janela","width="+largura+" , height="+altura+"");
}






