//******************************************************************
//**** FUNCAO QUE FORMATA UM VALOR PARA MOSTRAR NA TELA         ****
//**** Variáveis: sNumero (Valor a ser modificado)              ****
//**** By Kerley Gontijo                                        ****
//******************************************************************
function FormataValorCompleto(campo,tammax,decimal) { 
    vr = Limpar(campo,"0123456789"); 
    tam = vr.length; 
    dec=decimal 

    if ( tam <= dec ){
        campo = vr ; 
    } 

    if ( (tam > dec) && (tam <= 5) ){ 
        campo = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
    } 

    if ( (tam >= 6) && (tam <= 8) ){ 
        campo = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;  
    } 

    if ( (tam >= 9) && (tam <= 11) ){ 
        campo = 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 = 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 = 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 );
    } 
    return campo;
} 

//******************************************************************
//**** FUNCAO QUE RETIRA CARACTERES INVALIDOS DE UMA STRING     ****
//**** Variáveis: VALOR, VALIDOS                                ****
//**** By Kerley Gontijo                                        ****
//******************************************************************
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; 
} 
// JavaScript Document
function formataCpfCnpj(Item, teclapres, tipoCad) {
		var tecla = teclapres.keyCode;
		if (tecla > 31 && (tecla < 48 || tecla > 57)) {return false;}
		if (tipoCad == "J")
			return formataCnpj(Item,14,teclapres);
		else
			return formataCpf(Item,11,teclapres);
}


//#############################  Formatação para qualquer tipo de campo  ############################

function formataTudo(src, mask) 
{
  
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
	
	if (texto.substring(0,1) != saida) 
	{
		src.value += texto.substring(0,1);
	}
}

//CEP  -   OnKeyPress="formatar(this, '#####-###')"
//CPF  -   OnKeyPress="formatar(this, '###.###.###-##')"
//DATA -	  OnKeyPress="formatar(this, '##/##/####')"

//******************************************************************************************
//**** FUNCAO QUE FORMATA OS CAMPOS DE VALORES											    ****
//**** Variáveis: 								                                        ****
//**** Item -> qual o campo do formulario												****
//**** tammax -> tamanho Máximo
//**** teclapres -> qual a tecla prescionada											****
//******************************************************************************************
function formataValor(Item,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = Item.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	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 <= 2 ){ 
	 		Item.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		Item.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		Item.value = vr.substr( 0, tam - 5 ) + '' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		Item.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) ){
	 		Item.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) ){
	 		Item.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 formataNumero(e,Item) {
	 var CodCaracter = (window.Event) ? e.which : e.keyCode;
	 var Caracter = String.fromCharCode(CodCaracter);
	 var tamanho = Item.value.length;
	 var Numeros = "0123456789";
	 var AchouNumero = true;
	 
	 for (i=0;i<Numeros.length;i++)
	  if (Caracter!=Numeros.charAt(i)) {
	   AchouNumero = false;
	   break;
	  }
	 
	 if (trim(Caracter)=="")
	  return false;
 
	 if (!isNaN(Caracter))
	  return true;
	 else
	  return false;
}

function formataData(e,Item) {
	var CodCaracter = (window.Event) ? e.which : e.keyCode;
	var Caracter = String.fromCharCode(CodCaracter);
	var tamanho = Item.value.length;
	var Numeros = "0123456789";
	var AchouNumero = true;
	
	for (i=0;i<Numeros.length;i++)
		if (Caracter!=Numeros.charAt(i)) {
			AchouNumero = false;
			break;
		}
	
	if (trim(Caracter)=="")
		return false;
	
	if (tamanho==2)
		if (!AchouNumero) Item.value = Item.value.substr(0, 2)+"/";
	
	if (tamanho==5)
		if (!AchouNumero) Item.value = Item.value.substr(0, 5)+"/";
	
	if (!isNaN(Caracter))
		return true;
	else
		return false;
}

//******************************************************************************************
//**** FUNCAO QUE FORMATA CPF														    ****
//**** Variáveis: 								                                        ****
//**** CAMPO -> qual o campo do formulario												****
//**** TAMMAX -> qual o TAMANHO MÁXIMO DO CAMPO											****
//**** teclapres -> qual a tecla prescionada											****
//******************************************************************************************
function formataCpf(Item,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = Item.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;
	
	if (tam >= tammax) {return false;}

	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 <= 2 ){ 
	 		Item.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		Item.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		Item.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		Item.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) ){
	 		Item.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) ){
	 		Item.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 formataCnpj(Item,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = Item.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;

	if (tam >= tammax) {return false;}
	
	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 <= 2 ){ 
	 		Item.value = vr ; }
	 	if ( (tam > 2) && (tam <= 6) ){
	 		Item.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 7) && (tam <= 9) ){
	 		Item.value = vr.substr( 0, tam - 6 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 10) && (tam <= 12) ){
	 		Item.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 13) && (tam <= 14) ){
	 		Item.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		Item.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 ) ;}
	}		
}

//******************************************************************
//**** FUNCAO QUE MUDA O VALOR PARA FORMATO DE MOEDA BRASILEIRA ****
//**** Variáveis: sNumero (Valor a ser modificado)              ****
//******************************************************************
function brazilianFloatNumber(sNumero)
{
	if (sNumero != "")
	{
		var ponto = /\./;
		var virgula = /\,/;
		sNumero = sNumero.replace(ponto, "");
		sNumero = sNumero.replace(virgula, ".");
		return parseFloat(sNumero);
	}
	else
		return "";
}


//******************************************************************
//**** FUNCAO QUE MUDA O VALOR DE "FLOAT" PARA TEXTO (FORMATA)  ****
//**** Variáveis: fNumero (Valor a ser modificado)              ****
//******************************************************************
function floatToString(fNumber)
{
	var ponto = /\./;
	var sFinal = new String(fNumber.toString());
	var pontoPos = sFinal.search(ponto);
	if (pontoPos != -1) {
		sFinal = sFinal.substr(0, pontoPos + 3);
	}
	sFinal = sFinal.replace(ponto, ",");
	sFinal = casaDecimal(sFinal);
	return sFinal;
}

//******************************************************************
//**** FUNCAO QUE COLOCA 2 ZEROS APÓS A VIRGULA                 ****
//**** Variáveis: fNumero (Valor a ser modificado)              ****
//******************************************************************
function casaDecimal(fNumber)
{
	var virgula = /\,/;
	var sFinal = new String(fNumber.toString());
	var Pos = sFinal.search(virgula);
	if (Pos != -1) {
		sTamanho = sFinal.substr(Pos, Pos + 3);
		tamanho = sTamanho.length;
		
		if (tamanho == 2)
			fNumber = fNumber + '0';
	}
	else {
		if (fNumber != "")
			fNumber = fNumber + ',00';
		
	}

	return fNumber;
}

//******************************************************************************************
//**** FUNCAO QUE FORMATA CEP															****
//**** VariÑveis: 																		****
//**** Item -> qual o campo do formulario												****
//**** teclapres -> qual a tecla prescionada											****
//******************************************************************************************

function formataCep(Item, teclapres) {
	var tecla = teclapres.keyCode;
	vr = Item.value;
	vr = vr.replace( "-", "" );
	tam = vr.length;
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if ( tam <= 5 )
	 		Item.value = vr ; 
			
	 	if ( (tam > 5) && (tam <= 8) )
	 		Item.value = vr.substr( 0, 5 ) + '-' + vr.substr( 5, 3 ) ; 
	}
	
}


