function formataTelefone( v, event ){
	  var tecla;
	  var strValidos = "0123456789";
	  var key;
	  
	  if( v.value.length > 12 ){
		return( false );
	  }
		
	  if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 //BACKSPACE
	 if ( tecla == 8 ){ 
		  return( true ); 
	 }	  
	 
	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	

	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9  ){
		return( false );
	  }
	
	var strText =  new String();
	strText = v.value;	
	strText = strText.replace( "(", "" );	
	strText = strText.replace( ")", "" );
	strText = strText.replace( "-", "" );	
	var tam = strText.length;	
	if( tam > 0 ){
	  strText = "(" + strText;
	}
	
	if( tam > 1  ) {
	  strText = strText.substr( 0, 3 ) + ")" + strText.substr( 3,  tam );
	}
	
	if( tam > 5 ){
	  strText = strText.substr( 0, 8 ) + "-" + strText.substr( 8, tam );
	} 	
	
	v.value = strText;
}



function formataCEP( v, event ){
	  var tecla;
	  var strValidos = "0123456789";
	  var key;
	  
	  if( v.value.length > 8 ){
		return( false );
	  }
		
	  if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	

	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	  
	 
	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
		return( false );
	  }
	
	var strText =  new String();
	strText = v.value;	
	strText = strText.replace( "(",  "" );	
	strText = strText.replace( ")",  "" );
	strText = strText.replace( "-",  "" );
	
	var tam = strText.length;
	
	if( tam > 4  ) {
	  strText = strText.substr( 0, 5 ) + "-" + strText.substr( 5,  tam );
	}
	
	v.value = strText;
}


function formataData( v, event ){
	  var tecla;
	  var strValidos = "0123456789";
	  var key;
		
	  if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 9 ) {
		 return( true );  
	 }	 
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	

	   
	 if ( tecla == 0 ){ 
		  return( true ); 
	 }		 

    if(navigator.appName.indexOf("Netscape") == -1) {
		 var selectedText = document.selection;
		 if (selectedText.type == 'Text') {
			 var newRange = selectedText.createRange();
			 if( newRange.text.length >9 ){
				 v.value = "";
			 }
		 }	 
    } 
	 
	  if( v.value.length > 9 ){
		return( false );
	  }	 
	 
	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
		return( false );
	  }
	
	var strText =  new String();
	strText = v.value;	
	strText = strText.replace( "/",  ""  );	

    var tam = strText.length;
	
	if( tam > 1  ) {
	  strText = strText.substr( 0, 2 ) + "/" + strText.substr( 2,  tam );
	}
	
	if( tam > 3 && tam < 5 ) {
	  strText = strText.substr( 0, 5 ) + "/" + strText.substr( 5,  tam );
	}

  v.value = strText;
}


function formataData2( v, event ){
	  var tecla;
	  var strValidos = "0123456789";
	  var key;
	  
     if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	   
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	  

	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	
	 // Verifica selecionado 
    if(navigator.appName.indexOf("Netscape") == -1) {
		 var selectedText = document.selection;
		 if (selectedText.type == 'Text') {
			 var newRange = selectedText.createRange();
			 if( newRange.text.length >9 ){
				 v.value = "";
			 }
		 }	 
    }
	 



	 if( v.value.length > 7 ){
		return( false );
	  }
		

	  

	 
	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
		return( false );
	  }
	
	var strText =  new String();
	strText = v.value;	
	strText = strText.replace( "/",  ""  );	

    var tam = strText.length;
	
	if( tam > 1  ) {
	  strText = strText.substr( 0, 2 ) + "/" + strText.substr( 2,  tam );
	}
	
	if( tam > 3 && tam < 5 ) {
	  strText = strText.substr( 0, 5 ) + "/" + strText.substr( 5,  tam );
	}

  v.value = strText;
}



function formataCNPJ( v, event ){
	  var tecla;
	  var strValidos = "0123456789";	    
	  var key;
	  
	  //alert("casp");
	  
	  if( v.value.length > 17 ){
		return( false );
	  }

     if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	  

	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	
	 
	 key = String.fromCharCode( tecla );
	 
	if( strValidos.indexOf( key ) == -1 && tecla != 9  ){
		return( false );
	  }
	  

    var strText =  new String();
  
	strText = v.value;	
	strText = strText.replace( "/",  ""  );	
	strText = strText.replace( ".",  ""  );	
	strText = strText.replace( ".",  ""  );	
    strText = strText.replace( "-",  ""  ) 

    var tam = strText.length;
	
	if( tam > 11  ) {		  
	  strText = strText.substr( 0, 2 ) + "." + 
	            strText.substr( 2, 3 ) + "." + 
			    strText.substr( 5, 3 ) + "/" + 
			    strText.substr( 8, 4 ) + "-" + 
			    strText.substr( 12, tam - 11 ); 
       v.value = strText;
	   return;
   } 
	
	if( tam > 7  ) {		  
	  strText = strText.substr( 0, 2 ) + "." + 
	            strText.substr( 2, 3 ) + "." + 
			    strText.substr( 5, 3 ) + "/" + 
			    strText.substr( 8, tam - 7 ); 
       v.value = strText;
	   return;
	}
	
 
	if( tam > 4  ) {		  
	  strText = strText.substr( 0, 2 ) + "." + 
	            strText.substr( 2, 3 ) + "." + 
			    strText.substr( 5, tam - 4 ); 
       v.value = strText;
	   return;
	}

	if( tam > 1  ) {		  
	  strText = strText.substr( 0, 2 ) + "." + 
			    strText.substr( 2, tam - 1 ); 
       v.value = strText;
	   return;
	}
}


function formataCPF( v, event ){
	  var tecla;
	  var strValidos = "0123456789";	    
	  var key;
	  
	  //alert("casp");
	  
     if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	  
	 
	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	

	  if( v.value.length > 13 ){
		return( false );
	  }


	 key = String.fromCharCode( tecla );
	 
	if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
		return( false );
	  }
	  

    var strText =  new String();
  
	strText = v.value;	
	strText = strText.replace( ".",  ""  );	
	strText = strText.replace( ".",  ""  );	
	strText = strText.replace( ".",  ""  );	
    strText = strText.replace( "-",  ""  );

    var tam = strText.length;
	
	if( tam > 8 ) {		  
	  strText = strText.substr( 0, 3 ) + "." + 
	            strText.substr( 3, 3 ) + "." + 
			    strText.substr( 6, 3 ) + "-" + 
			    strText.substr( 9, tam - 8 ); 
       v.value = strText;
	   return;
   } 
   
   if( tam > 5 ) {		  
	  strText = strText.substr( 0, 3 ) + "." + 
	            strText.substr( 3, 3 ) + "." + 
			    strText.substr( 6, tam - 5 ); 
       v.value = strText;
	   return;
   }    

   if( tam > 2 ) {		  
	  strText = strText.substr( 0, 3 ) + "." + 
			    strText.substr( 3, tam - 2 ); 
       v.value = strText;
	   return;
   }    
}


function formataHora( v, event ){
	  var tecla;
	  var strValidos = "0123456789";
	  var key;
	  
	  if( v.value.length > 4 ){
		return( false );
	  }
		
	  if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	
  
	 if ( tecla == 0 ){ 
		  return( true ); 
	 }		 

	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
		return( false );
	  }
	
	var strText =  new String();
	strText = v.value;	
	strText = strText.replace( ":",  ""  );	

    var tam = strText.length;	
	if( tam > 1  ) {
	  strText = strText.substr( 0, 2 ) + ":" + strText.substr( 2,  tam );
	}
	v.value = strText;
	return;
}
function formataMesAno( v ){
   var tecla;
   var strValidos = "0123456789";
   var key;
   
   if( v.value.length > 6 ){
  return( false );
   }
  
   if(navigator.appName.indexOf("Netscape")!= -1) {
   tecla= event.which; 
   } else {  
   tecla= event.keyCode; 
   }  
   
  if ( tecla == 13 ) {
   return( false );  
  }
  
  if ( tecla == 8 ){ //BACKSPACE
    return( true ); 
  }   
  
	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	

  key = String.fromCharCode( tecla );
  
  if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
  return( false );
   }
 
 var strText =  new String();
 strText = v.value; 
 strText = strText.replace( "/",  ""  ); 

    var tam = strText.length
 
 if( tam > 1  ) {
   strText = strText.substr( 0, 2 ) + "/" + strText.substr( 2,  tam );
 }

  v.value = strText;
}


function formataNumero( v, event ){
	  var tecla;
	  var strValidos = "0123456789";
	  var key;
	  
	
	  if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	  

	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	
	 
	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9 ){
		return( false );
	  }
}

function formataNumeroReal( v, event ){
	  var tecla;
	  var strValidos = "0123456789,";
	  var key;

	  if(navigator.appName.indexOf("Netscape")!= -1) {
		 tecla= event.which; 
	  } else {  
		 tecla= event.keyCode; 
	  }	 
	  
	 if ( tecla == 13 ) {
		 return( false );  
	 }
	 
	 if ( tecla == 8 ){ //BACKSPACE
		  return( true ); 
	 }	  
	 
	 if ( tecla == 0 ){ 
		  return( true ); 
	 }	

	 key = String.fromCharCode( tecla );
	 
	 if( strValidos.indexOf( key ) == -1  && tecla != 9 && tecla != 188  ){
		return( false );
	  }
}


function formataMoeda(num, event) {
  //retira os pontos
  num = num.replace( /\./gi, "");
  //Substitui as virgulas por ponto antes de testar no número
   num = num.replace( /\,/gi, ".");
   x = 0;
   if(num<0) {
      num = Math.abs(num);
      x = 1;
   }   if(isNaN(num)) num = "0";
      cents = Math.floor((num*100+0.5)%100);

   num = Math.floor((num*100+0.5)/100).toString();

   if(cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
         num = num.substring(0,num.length-(4*i+3))+'.'
               +num.substring(num.length-(4*i+3));   ret = num + ',' + cents;   if (x == 1) ret = '-' + ret;return ret;

}

function formataMoedaEN_BR( num ) {
   x = 0;
   if(num<0) {
      num = Math.abs(num);
      x = 1;
   }   if(isNaN(num)) num = "0";
      cents = Math.floor((num*100+0.5)%100);

   num = Math.floor((num*100+0.5)/100).toString();

   if(cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
         num = num.substring(0,num.length-(4*i+3))+'.'
               +num.substring(num.length-(4*i+3));   ret = num + ',' + cents;   if (x == 1) ret = '-' + ret;return ret;

}
