/*-----------------------------------------------------------------------
Máscara para o campo Telefone
-----------------------------------------------------------------------*/

function Telefone(evento, objeto){
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval (objeto);
	if (campo.value == '(00)00000000')
	{
		campo.value=""
	}

	caracteres = '0123456789';
	separacao1 = '(';
	separacao2 = ')';
	conjunto1 = 0;
	conjunto2 = 3;

	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (25))
	{
		if (campo.value.length == conjunto1 )
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto2)
		campo.value = campo.value + separacao2		
	}
	else
		event.returnValue = false;
}

/*-----------------------------------------------------------------------
Formata número tipo moeda usando o evento onKeyDown
-----------------------------------------------------------------------*/

function Limpar(valor, validos) {
// retira caracteres invalidos da string
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
tam = vr.length;
dec=decimal

if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

if (tecla == 8 )
{ tam = tam - 1 ; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
{

if ( tam <= dec )
{ campo.value = vr ; }

if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
} 

}

/*-----------------------------------------------------------------------
Máscara para o campo Cnpj
-------------------------*/

function Cnpj(evento, objeto){
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval (objeto);
	if (campo.value == '000.000.000/0000-00')
	{
		campo.value=""
	}

	caracteres = '0123456789';
	separacao1 = '.';
	separacao2 = '-';
	separacao3 = '/';
	conjunto1 = 3;
	conjunto3 = 7;
	conjunto4 = 11;
	conjunto5 = 16;

	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (25))
	{
		if (campo.value.length == conjunto1 )
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto3)
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto4)
		campo.value = campo.value + separacao3;
		else if (campo.value.length == conjunto5)
		campo.value = campo.value + separacao2;
	}
	else
		event.returnValue = false;
}

/*-----------------------------------------------------------------------
Máscara para o campo Data
-----------------------------------------------------------------------*/

function Data(evento, objeto){
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval (objeto);
	if (campo.value == '00/00/0000')
	{
		campo.value=""
	}

	caracteres = '0123456789';
	separacao1 = '/';
	conjunto1 = 2;
	conjunto2 = 5;

	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (19))
	{
		if (campo.value.length == conjunto1 )
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto2)
		campo.value = campo.value + separacao1;
	}
	else
		event.returnValue = false;
}

/*-----------------------------------------------------------------------
Máscara para o campo CEP
-----------------------------------------------------------------------*/
function Cep(evento, objeto){
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval (objeto);
	if (campo.value == '00000-000')
	{
		campo.value=""
	}

	caracteres = '0123456789';
	separacao = '-';
	conjunto1 = 5;

	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (19))
	{
			
		if (campo.value.length == conjunto1)
		campo.value = campo.value + separacao;
	}
	else
		event.returnValue = false;
}
/*-----------------------------------------------------------------------
Máscara para o campo CPF
-----------------------------------------------------------------------*/

function Cpf(evento, objeto){
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval (objeto);
	if (campo.value == '000.000.000-00')
	{
		campo.value=""
	}

	caracteres = '0123456789';
	separacao1 = '.';
	separacao2 = '-';
	conjunto1 = 3;
	conjunto2 = 4;
	conjunto3 = 7;
	conjunto4 = 11;

	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (25))
	{
		if (campo.value.length == conjunto1 )
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto2)
		campo.value = campo.value + separacao1;		
		else if (campo.value.length == conjunto3)
		campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto4)
		campo.value = campo.value + separacao2;
	}
	else
		event.returnValue = false;
}

/*-----------------------------------------------------------------------
Máscara para o validar CPF
-----------------------------------------------------------------------*/


 function validarCPF(){
   var cpf = document.form.cpf.value;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("CPF inválido. Tente novamente.");
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	// window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
   //  window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }
 

 // So numeros no input 
function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    else return true;
    }
}

/* Mostra e esconde div  */
function exibe(id) {
	if(document.getElementById(id).style.display=="none") {
		document.getElementById(id).style.display = "inline";
	}
	else {
		document.getElementById(id).style.display = "none";
	}
}