// mascara para campo FONE
function mascaraData(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	//alert(tecla);
	var vr = new String(Campo.value);	
	
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");			

	tam = vr.length + 1;	
	// && ((tecla>=48)&&(tecla<=57))
	if (tecla != 9 && tecla != 8){				
		if(tam==3)
			Campo.value = vr.substr(0, 2) + '/';
		if(tam==4)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2);		
		if(tam==5)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/';
		if(tam>=6)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2)+ '/' + vr.substr(4, 3);
	}	
}

// mascara para campo FONE
function mascaraDataHora(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	//alert(tecla);
	var vr = new String(Campo.value);	
	
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");			
	vr = vr.replace(":", "");
	vr = vr.replace("", "");

	tam = vr.length + 1;	
	
	// 
	bEspecialValido	= (
		(tecla == 46) || (tecla == 9)  || (tecla == 8)  || 
		(tecla == 35) || (tecla == 36) || (tecla == 37) || 
		(tecla == 39)
	);
	
	bDigito			= ((tecla>=48)&&(tecla<=57));	
	
	//alert('key:' + tecla);
	
	if (bDigito){				
		if(tam==3)
			Campo.value = vr.substr(0, 2) + '/';
		if(tam==4)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2);		
		if(tam==5)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/';
		if((tam>=6) && (tam<=8))
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2)+ '/' + vr.substr(4, 4);
		if(tam==9)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2)+ '/' + vr.substr(4, 4) + ' ' + vr.substr(8, 1);
		if(tam==11)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2)+ '/' + vr.substr(4, 4) + ' ' + vr.substr(9, 2);
		if(tam==12)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2)+ '/' + vr.substr(4, 4) + ' ' + vr.substr(9, 2) + ':' + vr.substr(11, 1);
		if(tam>=13)
			Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2)+ '/' + vr.substr(4, 4) + ' ' + vr.substr(9, 2) + ':' + vr.substr(11, 2);			
	} else {
		
		if(!bEspecialValido) {
			return false;
		}
	}
}

// mascara para campo FONE com Prefixo
function mascaraFone(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	//alert(tecla);
	var vr = new String(Campo.value);	
	
	vr = vr.replace("(", "");
	vr = vr.replace(")", "");		

	tam = vr.length + 1;
	
	// && ((tecla>=48)&&(tecla<=57))
	if (tecla != 9 && tecla != 8){
		if(tam==1)
			Campo.value = '(' + vr.substr(0, 1);
		if(tam==3)
			Campo.value = '(' + vr.substr(0, 2) + ') ';					
		if (tam > 3 )
			Campo.value = '(' + vr.substr(0, 2) + ')' + vr.substr(2, tam);
	}	
}

// mascara para campo FONE SEM Prefixo
function mascaraFoneSemPrefixo(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	//alert(tecla);
	var vr = new String(Campo.value);	

	tam = vr.length + 1;
	
	// && ((tecla>=48)&&(tecla<=57))
	if (tecla != 9 && tecla != 8){
		Campo.value = vr.substr(0, 7);
	}	
}

// mascara para campo PREFIXO FONE
function mascaraPrefixoFone(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	//alert(tecla);
	var vr = new String(Campo.value);	
	
	tam = vr.length + 1;
	
	// && ((tecla>=48)&&(tecla<=57))
	if (tecla != 9 && tecla != 8){
		if(tam>2)
			Campo.value = vr.substr(0, 2);					
	}	
}


// mascara para campo CEP
function mascaraCEP(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	//alert(tecla);
	var vr = new String(Campo.value);	
	
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	// && ((tecla>=48)&&(tecla<=57))
	if (tecla != 9 && tecla != 8){
		if (tam > 5)
			Campo.value = vr.substr(0, 5) + '-' + vr.substr(5, tam);
		if (tam >= 8)
			Campo.value = vr.substr(0, 5) + '-' + vr.substr(5, 2);
}	
}



/////////////////////////////////////////////////////////////////
// mascara para campo CPF
function mascaraCPF(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8){
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
}


/////////////////////////////////////////////////////////////////
// mascara CNPJ
function mascaraCNPJ(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;
	//alert(tecla);
	
	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 14)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,2);
		if (tam >= 14)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,1);
		}
}


/////////////////////////////////////////////////////////////////
// mascara para campo Número Cartão de Crédito
function mascaraCartaoCredito(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");

	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8){
		if (tam > 4 && tam < 9)
			Campo.value = vr.substr(0, 4) + '.' + vr.substr(4, tam);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0, 4) + '.' + vr.substr(4, 4) + '.' + vr.substr(8, tam-8);
		if (tam >= 13 && tam < 16)
			Campo.value = vr.substr(0, 4) + '.' + vr.substr(4, 4) + '.' + vr.substr(8, 4) + '.' + vr.substr(12, tam-12);
		if (tam >= 16)
			Campo.value = vr.substr(0, 4) + '.' + vr.substr(4, 4) + '.' + vr.substr(8, 4) + '.' + vr.substr(12, 3);
		}
}


//////////////////////////////////////////////////////////////////////
// 
function MascaraDigitoDecimal(Campo, teclaPres, valorMaximo)
{
	var tecla = teclaPres.keyCode;
	var valor = new String(Campo.value);
	
	// se for tab ou enter entao nao faz nada
	if ((tecla==9)||(tecla==13))
		return;

	// verifica se a tecla pressionada é numero ou ,
	// se for número, testa se é maior que valorMaximo (passado por parâmetro), se for zera o campo
	// se nao for número, remove o último caracter digitado. 188 é o keyCode da vírgula
	if ( ((tecla>=48)&&(tecla<=57)) || (tecla==188) || ((tecla>=96)&&(tecla<=105)) || (tecla==110) ){
		if (valor > valorMaximo)
			Campo.value = "";
	}		
	else {
		if (valor.length == 1)
			Campo.value = "";
		else
			Campo.value = valor.substr(0, valor.length-1);
	}		
}
//////////////////////////////////////////////////////////////////////////////

// campo = textbox sendo editado
// decimais = nro de decimais, -1 se for nro inteiro
// onkeyup="MascaraDecimal(this,2)
function MascaraDecimal(campo,decimais) 
{
	var tam;
	var valor;

	if (event.keyCode==9) 
		return false
	
	valor = campo.value;
		
	while ((valor.indexOf('.')!=-1) || (valor.indexOf(',')!=-1)){
		valor = valor.replace('.','');
		valor = valor.replace(',','');
	}
	tam = valor.length;
	//exclui os zeros antes dos numeros
	if  ((decimais==-1) || (tam > decimais+1 ) ){
		if ( valor.charAt(0) == '0' ){
			aux = '';
			for(i = 1; i < tam; i++)
				aux += valor.charAt(i);
			
			valor = aux;
			tam = valor.length;
		}
	}
	if (tam>0){
		// coloca separador decimal 
		if (decimais!=-1){
			valor = valor.substr(0,tam-decimais) + ',' + valor.substr(tam-decimais,decimais)		
		}
		// coloca separador de milhar
		var iStart = valor.indexOf(",");
		if (iStart < 0)
			iStart = valor.length;
		iStart -= 3;
		while (iStart >= 1){
			valor = valor.substring(0,iStart) + "." + valor.substring(iStart,valor.length)
			iStart -= 3;
		}

		//coloca 0 à esquerda enquanto tiver apenas as casas depois da vírgula, se for o caso
		if (valor.length < 3)
			valor = "0" + valor;
	}
	
	campo.value = valor;

	return true
}
//////////////////////////////////////////////////////////////////////
/*
    Função para limitar o uso de teclas desnecessárias em certos campos.
    Não implementei a permissão dos caracteres alfabéticos, pois, o uso principal dessa função é em campos onde só se pode digitar
    números. Mais não ha nada que impeça sua codificação mais tarde.
    IMPORTANTE
    A função deve ser chamada no evento ONKEYDOWN, pelo seguinte motivo:
    O IE só valida os digitos por keyCode no evento onkeydown, no evento onkeypress a validação é feita por caracter ascii.
    Com isso não é possível validar por exemplo as teclas de setas, home, end, etc.
    Para termos o código dessas teclas, a função tem de ser chamada no evento onkeydown.
    Outra coisa importante, no evento onkeydown onde a validação é feita por keyCode, as teclas numéricas do teclado alfa, e do teclado numérico
    são diferentes: Por exemplo, o "1" do teclado alfa, ou seja, que está em cima do "Q", tem o código 49,
    já no teclado numérico esse código é 97.
    No evento onkeypress a validação é feita pelo caractere ascii, e nesse caso
    o código referente ao "1" dos dois teclados é o mesmo, 49.
*/
 function MascaraDigitoInteiro(Campo, teclaPres, valorMaximo, numMaxDigitos)
{
	var tecla = teclaPres.keyCode;
	
	//testa se é keyCode do teclado numérico (range de 0 à 9)
	if ((tecla>=96)&&(tecla<=105)){
	    //nesse caso diminui 48 para ficar com o mesmo keyCode do teclado alfa
	    tecla = tecla - 48;
	}
	var digitoNovo = String.fromCharCode(tecla);
	var valor = new String(Campo.value);
	
	// se for tab ou enter entao nao faz nada
	if ((tecla==9)||(tecla==13))
		return;
		
	tam = valor.length + 1;
	
	if (tam > numMaxDigitos){
	    Campo.value = valor.substr(0, numMaxDigitos-1);
	  //  return;
	}
	
	// verifica se a tecla pressionada é numero ou ,
	// se for número, testa se, concatenado com o que já tinha, 
	//    é maior que valorMaximo passado por parâmetro, se for, elimina digitação
	// se nao for número, remove o último caracter digitado.
	if ((tecla>=48)&&(tecla<=57)){
	    // concatena novo dígito digitado com o valor que já tinha:
	    valor = valor + digitoNovo;

		if (valor > valorMaximo){
			Campo.value = valor.substr(0, numMaxDigitos-2);
		    return;
		}
	}	
	else{
		teclaPres.keyCode = 0;
	}		
}


