
function formataData(src,event) {
 var i;
 
 if (document.selection) // Internet Explorer
  i = Math.abs(document.selection.createRange().moveStart("character", -1000000));
 else // Firefox
  i = src.selectionStart;
 
 var saida = '##/##/####'.substring(0,1);
 var texto = '##/##/####'.substring(i); 
 var rcode = (window.Event) ? event.which : event.keyCode;
 
 // Enter, backspace, delete e setas direcionais
 if ((rcode == 13) || (rcode == 0) || (rcode == 8)) {
  return true;
 }
 
 if (rcode >= 48 && rcode <= 57) {
  if (texto.substring(0,1) != saida)
   src.value = src.value.substring(0,i) + texto.substring(0,1) + src.value.substring(i);
  return true;
 } else {
  return false;
 }
 
}

//------------------------------------------

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;
      }

 }//fim de for

return result;

}//fim de function


function formataValor(campo,tammax,event) {
 
 var rcode = (window.Event) ? event.which : event.keyCode;
 
 // Enter - 13, Tab - 9, backspace, delete - 46 e setas direcionais (37,38,39,40)
 if ((rcode == 13) ||(rcode == 9) ||(rcode == 0) || (rcode == 46) || (rcode == 8) || (rcode >= 37 && rcode <= 40)) {
  return true;
 }
 
 var vr = campo.value;
 
 vr = Limpar(vr,"0123456789");
 
 tam = vr.length;

 if (tam < tammax && rcode != 8){ tam = vr.length + 1 ; }

 if (rcode == 8 ){ tam = tam - 1 ; }

 if ((rcode == 8) || ( rcode >= 48 && rcode <= 57 ) || (rcode >= 96 && rcode <= 105)){
  if ( tam <= 2 ){
    campo.value = vr ; }
   if ( (tam > 2) && (tam <= 5) ){
    campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
   if ( (tam >= 6) && (tam <= 8) ){
    campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, 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 - 2, 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 - 2, 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 ) ;}
 } else
 {
   return false; 
 }
}

function retiraFormatoNumerico(campo){

var texto = campo.value;

for (var i=0; i < texto.length; i++) {
texto = texto.replace( " ", "" );
texto = texto.replace( ".", "" );
}

campo.value = texto;

}

function aplicaMascara(campo) {

 var texto = campo;
 
 var tam = campo.length;

 var i = campo.indexOf(",");
 
 var is = campo.indexOf("-");
  
 //Se texto apresenta sinal de negativo remover ele (será inserido no final).	
 if (is != -1)
	texto = texto.replace( "-", "" );	

	
 //Se vazio
 if (tam == 0) {
	return texto;
 } 
 
 //Se for zero
 if (texto==0) {
   texto = "0";
 }
 
 //Se texto for apenas uma virgula 
 if (texto==",")
	texto="0";

  
 //verifica se tem: 
 // 1 - Não têm casas decimais.
 if (i==-1) {
 
   texto = Limpar(texto,"0123456789");
   
   tam = texto.length;
    
   //acrescenta decimais
   texto = texto + '00';
   tam = tam + 2;
 
   if ( tam <= 2 ){
    texto = texto ; }
   if ( (tam > 2) && (tam <= 5) ){
    texto = texto.substr( 0, tam - 2 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 6) && (tam <= 8) ){
    texto = texto.substr( 0, tam - 5 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 9) && (tam <= 11) ){
    texto = texto.substr( 0, tam - 8 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 12) && (tam <= 14) ){
    texto = texto.substr( 0, tam - 11 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 15) && (tam <= 17) ){
    texto = texto.substr( 0, tam - 14 ) + '.' + texto.substr( tam - 14, 3 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ;}
    
 }
 // 2 - Com casa decimal
 else {
 
 // 2.1 - Trata com duas casas decimais ex: 0,5 -> 0,50
 if ((tam - i) == 1) {
	texto = texto + '00';
 }
 
 if ((tam - i) == 2) {
	texto = texto + '0';
 }
 
 // 2.2 - Trata sem o zero - Casa decimal ex: ,5 -> 0,5
 if ( (i == 0) && (tam > 1) ) {
  texto = '0'+ texto;
 }
  
 // 2.3 - Tira casas decimais com mais de 3
 if ((tam - i) > 3) {
    texto = texto.substr(0, i) + texto.substr(i, i + 2);
 }
 
 // 2.3 - Aplica máscara para casa decimal
 texto = Limpar(texto,"0123456789");
 
 tam = texto.length;
 
 if ( tam <= 2 ){
    texto = texto ; }
 if ( (tam > 2) && (tam <= 5) ){
    texto = texto.substr( 0, tam - 2 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 6) && (tam <= 8) ){
    texto = texto.substr( 0, tam - 5 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 9) && (tam <= 11) ){
    texto = texto.substr( 0, tam - 8 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 12) && (tam <= 14) ){
    texto = texto.substr( 0, tam - 11 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 15) && (tam <= 17) ){
    texto = texto.substr( 0, tam - 14 ) + '.' + texto.substr( tam - 14, 3 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ;}
 }
 
 //Se apresenta sinal de negativo aplica o sinal novamente
 if (is != -1)
	texto = '-'+ texto;	

 return texto;
}

function formataNumerico(campo) {

var texto = campo.value;

for (var i=0; i < texto.length; i++) {
	texto = texto.replace( " ", "" );
	texto = texto.replace( ".", "" );
}

texto = aplicaMascara(texto);

campo.value = texto;

}

function caixaNumericoPositivo(campo,event,tam) {

 var rcode = (window.Event) ? event.which : event.keyCode;
 
 //alert(rcode);
 
 var str = campo.value;
 
 var vl = String.fromCharCode(rcode);
 
 var i = -1;
 
 //verifica se é vírgula
 if ((rcode == 188)||(rcode==110)) {
   i = str.indexOf(",");
 }
 else {
   i = str.indexOf(vl);
 }
 
 //Pega posição da vírgula para cálculos
 var iv = str.indexOf(",");

 // Se apertar Enter - 13 pula para próximo campo
 if ((rcode == 13))  {
  return true; //Não implementado
 }
 else {
	// Virgula-188 e 110, Backspace-8, Tab-9,Del - 46,- Home e End (35,36), Teclas direcionais  ------>   Numeros (48-57/96-105)
	if (((rcode == 188) ||(rcode == 110) || (rcode == 8) || (rcode == 9) || (rcode == 46) || (rcode >= 35 && rcode <= 40) || (rcode >= 48 && rcode <= 57 ) || (rcode >= 96 && rcode <= 105) )== false){
		return false
	}
	else {
	 //Não duplicar as vírgulas 
	 if (((rcode == 188) ||(rcode == 110)) && (i > 0)) {
	    return false;	 
	 } else {
		
		// Virgula-188 e 110, Backspace-8, Numeros - Teclas direcionais
		if ((rcode == 188) ||(rcode == 110) || (rcode == 8) || (rcode == 9) || (rcode == 46) || (rcode >= 35 && rcode <= 40)){
			return true
		} else {
			
			if (iv > 0) {
			  var tamanho = str.length;
			  var tamDecimais = tamanho - iv;
			  
			  if (tamDecimais > 3)
				tamDecimais = 3;
				
			  tam = tam + tamDecimais;
			}
			
			if (str.length > tam) {
				return false;	
			} else {
				return true;
		    }
		}
	 }
    }
  }
}

//Para valores negativos

function caixaNumerico(campo,event,tam) {

 var rcode = (window.Event) ? event.which : event.keyCode;
 
 //alert(rcode);
 
 var str = campo.value;
 
 var vl = String.fromCharCode(rcode);
 
 var i = -1;
 
 //verifica se é vírgula
 if ((rcode == 188)||(rcode==110)) {
   i = str.indexOf(",");
 }
 else {
   i = str.indexOf(vl);
 }
 
 //Pega posição da vírgula para cálculos
 var iv = str.indexOf(",");
 
 //Verfica tamanho do texto para cálculos (sinal negativo)
 var tamanho = str.length;
 
 // Se apertar Enter - 13 pula para próximo campo
 if ((rcode == 13))  {
  return true; //Não implementado
 }
 else {
	//Sinal de menos - 189 e 109 Virgula-188 e 110, Backspace-8, Tab-9,Del - 46,- Home e End (35,36), Teclas direcionais  ------>   Numeros (48-57/96-105)
	if (((rcode == 189) ||(rcode == 109) ||(rcode == 188) ||(rcode == 110) || (rcode == 8) || (rcode == 9) || (rcode == 46) || (rcode >= 35 && rcode <= 40) || (rcode >= 48 && rcode <= 57 ) || (rcode >= 96 && rcode <= 105) )== false){
		return false
	}
	else {
	 //Não duplicar as vírgulas 
	 if (((rcode == 188) ||(rcode == 110)) && (i > 0)) {
	    return false;	 
	 } else {
		
		// Virgula-188 e 110, Backspace-8, Numeros - Teclas direcionais
		if ((rcode == 188) ||(rcode == 110) || (rcode == 8) || (rcode == 9) || (rcode == 46) || (rcode >= 35 && rcode <= 40)){
			return true
		} else {
		
		    //Se for o sinal de menos e o tamanho for diferente de 0 então não deixa inserir
		    if (((rcode == 109)||(rcode == 189)) && (tamanho > 0))
		      return false
		      
			if (iv > 0) {
			  var tamanho = str.length;
			  var tamDecimais = tamanho - iv;
			  
			  if (tamDecimais > 3)
				tamDecimais = 3;
				
			  tam = tam + tamDecimais;
			}
			
			if (str.length > tam) {
				return false;	
			} else {
				return true;
		    }
		}
	 }
    }
  }
}






