// mascara de números
// uso: onkeypress="return(mascaraNumeros(this,event));"
function mascaraNumerosNegativos(campo,e) {
	var teclaValida = true;
	
	
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
} 


// mascara de CEP "01234-100"
// uso: onkeypress="return (mascaraCEP(this,event));" maxlength="9"
function mascaraCEP(campo,e) {

	var teclaValida = true;
	
	// Pega o código da tecla digitada
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	// Transforma o código da tecla digitada em um valor real(ex: cod. 49 -> valor. 1) 
	tecla = String.fromCharCode(whichCode);
	
	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 48-57 -> numeros de 0 a 9
	*/
	if (whichCode == '8' || whichCode == '0') {
		teclaValida = true;
	} else if (whichCode >= '48' && whichCode <= '57') {
		teclaValida = true;
	} else {
		teclaValida = false;
	}
	
	if (campo.value.length == 5 && whichCode != '8' && whichCode != '0') 
		campo.value = campo.value + '-';

	// Retorno da função (se a tecla for válida, aparece no campo)	
	if (teclaValida == true) return true;
	if (teclaValida == false) return false;
	
} 


// mascara de data e hora "01/2000"
// uso: onkeypress="return(mascaraMesAno(this,event));"
function mascaraMesAno(vdateValue,e) {
	var strSeparator='/';
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	var mesCheck ='01,02,03,04,05,06,07,08,09,10,11,12';
	if (whichCode == 13) return true;
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		//alert('Voce pressionou a tecla invalida');
		//vdateName.value=vdateName.value.substr(0,(vdateValue.length-1));
		return false;
	}
	
	if (vdateValue.value.length == 2) {
	 	vdateValue.value=vdateValue.value+strSeparator;
	}    
	
	return true;
}

// uso: onkeypress="return mascaraRG(this,event);" maxlength="12"
function mascaraRG(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
		
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 6) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + '-';

	return true;
}



// uso: onkeypress="return mascaraCPF(this,event);" maxlength="14"
function mascaraCPF(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	if (vdateValue.value.length == 3) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 7) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 11) vdateValue.value=vdateValue.value + '-';

	return true;
}


 
// uso: onkeypress="return mascaraCNPJ(this,event);" maxlength="18"
function mascaraCNPJ(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	//e.preventDefault();
	//var tecla = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 6) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 15) vdateValue.value=vdateValue.value + '-';;
		
	
	return true;
}

// uso: onkeypress="return mascaraIE(this,event);" maxlength="15"
function mascaraIE(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	//e.preventDefault();
	//var tecla = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 3) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 7) vdateValue.value=vdateValue.value + '.';
	if (vdateValue.value.length == 11) vdateValue.value=vdateValue.value + '.'
	
	
	return true;
}


// uso: onkeypress="return mascaraData(this,event);" maxlength="10"
function mascaraData(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';

	
	return true;
}

// uso: onkeypress="return mascaraHora(this,event);" maxlength="5"
function mascaraHora(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + ':';
	
	return true;
}

// uso: onkeypress="return mascaraDataHora(this,event);" maxlength="16"
function mascaraDataHora(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	

	if (vdateValue.value.length == 2) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 5) vdateValue.value=vdateValue.value + '/';
	if (vdateValue.value.length == 10) vdateValue.value=vdateValue.value + ' ';
	if (vdateValue.value.length == 13) vdateValue.value=vdateValue.value + ':';
	
	
	return true;
}

// uso: onkeypress="return mascaraNumeros(this,event);"
function mascaraNumeros(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	return true;
}


// uso: onkeypress="return mascaraNumeros(this,event);"
function mascaraPorcentagemSimples(vdateValue,e) {
	var key='';
	var whichCode = (window.Event) ? e.which : e.keyCode;	
	var strCheck = '0123456789,';
	
	if (whichCode == 13) return true; // ENTER
	if (whichCode == 8) return true; // Backspace
	if (whichCode == 0) return true; // setas e teclas de (insert-del-home-end-pageUpDown)
	
	key = String.fromCharCode(whichCode); // Pega o valor da tecla digitado
	
	if (strCheck.indexOf(key) == -1) {
		// tecla inválida
		return false;
	}
	
	return true;
}

// Mascara para campos de valores fincanceiros ex: "1.200,00"
// uso: onKeyPress="return mascaraPorcentagem(this,event);"
function mascaraPorcentagem(fld, e) {
    var sep = 0;
    var key = '';
	var decSep = ','
	var milSep = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;

	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 13 -> Enter
	*  -> Barra de Espaço
	*/

		
    if (whichCode == 13) return true; // Enter
    if (whichCode == 0) return true;  // Del
    if (whichCode == 8) return true;  // BackSpace
    if (whichCode == 32) return true;  // Barra de Espaço
	
	// se forem digitado 6 caracteres (Ex: 100,00) bloquear a digitação
	if (fld.value.length == 6) return false;
	
    key = String.fromCharCode(whichCode);  // Get key value from key code
 
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
    len = fld.value.length;
    for(i = 0; i < len; i++)
    if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
    if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
    if (j == 3) {
    aux2 += milSep;
    j = 0;
    }
    aux2 += aux.charAt(i);
    j++;
    }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}

// Mascara para campos de valores fincanceiros ex: "1.200,00"
// uso: onKeyPress="return mascaraDinheiro(this,event);"
function mascaraDinheiro(fld, e) {
    var sep = 0;
    var key = '';
	var decSep = ','
	var milSep = '.';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;

	/* TECLAS 
	* 0 -> Del
	* 8 -> BackSpace
	* 13 -> Enter
	*  -> Barra de Espaço
	*/

    if (whichCode == 13) return true; // Enter
    if (whichCode == 0) return true;  // Del
    if (whichCode == 8) return true;  // BackSpace
    if (whichCode == 32) return true;  // Barra de Espaço

    key = String.fromCharCode(whichCode);  // Get key value from key code
 
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
    len = fld.value.length;
    for(i = 0; i < len; i++)
    if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
    if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
    if (j == 3) {
    aux2 += milSep;
    j = 0;
    }
    aux2 += aux.charAt(i);
    j++;
    }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}

//FUNÇÕES USADAS PARA MÁSCARA DE DINHEIRO
// onKeyDown="Formata(this,20,event,2,event);"
// ------------------------------------------------------------------------------------------ //
// Função   : Formata + Limpar
// ------------------------------------------------------------------------------------------ //
function Formata(campo,tammax,teclapres,decimal,e) { 
  var tecla = teclapres.keyCode; 
  var vr = Limpar(campo.value,"0123456789"); 
  var tam = vr.length; 
  var key = '';  
  var dec = decimal 
/*
 -- MODIFICAÇÕES FEITAS PARA NÃO PERMITIR CARACTERES -- NÃO FUNCIONOU
  var strCheck = '0123456789';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1)return false;  // Not a valid key
*/

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 );
  } 
 } 
} 

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; 
} 



