function somenteNumeros(e) {  
	if (window.event) //IE
    {
        tecla = e.keyCode;
    }

    else if (e.which) //FF
    {
     tecla = e.which;
    }
    //techa==8 é para permitir o backspace funcionar para apagar
    //Tarefa 87626 : Número do cartão e código de segurança estão aceitando mais digitos do que o permitido e também estão aceitando letras.
    //Teclas 37 e 39 são das setas para esquerda e direita e teclas 35 e 36 respectivamente são das teclas end e home
    /*  teclas de 48 até 57 são do teclado normal
        teclas de 96 até 105 são do teclado numerico */

    if (((tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105)) || (tecla == 8) || (tecla == 9) || (tecla == 37) || (tecla == 39) || (tecla == 36) || (tecla == 35))
        {
        return true;
    }
    else
    {
        return false;
    }
  
}

function FormataValor(campo,tammax,teclapres) 
 {
 	//uso:
	//<input type="Text" name="fat_vr_bruto" maxlength="17" onKeyDown="FormataValor(this,17,event)">
        
	var tecla = teclapres.keyCode;
	
	if ( tecla == 9 )
        return true;
	
	vr = campo.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) && tam < tammax)
	{
	    if ( tam ==0 ) {
	        campo.value = ''; }
		if ( tam == 1 ){ 
	 		campo.value = '0,0' + vr ; }
	 	if ( tam == 2 ){
	 	    if ( campo.value.charAt(0) == '0')  {
	 	        if (vr.charAt(0) == '0' && vr.charAt(1) == '0') {
	 	            campo.value = '';
	 	        }
	 	        else {
	 	            campo.value = '0,' + vr ; 
	 	        }
	 	    }
	 	    else {
	 		    campo.value = '0,' + vr ; }
	 	}
	 	if ( (tam > 2) && (tam <= 5) ){
	 	    if ( (tam == 3) && (campo.value.charAt(0) == '0')) {
	 	        vr = '0' + vr.substr(1,tam - 1);
	 	    }
	 	    if ( (tam == 4) && (campo.value.charAt(0) == '0')) {
	 	        vr = vr.substr(1,tam - 1); tam --;
	 	    }
	 		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 ) ;}
	 		
	 		return true;
	}
	return false;			
}

function AdicionaCasaDecimal(campo)
{
    if (campo.value.length <= 2 && campo.value.length!=0)
    {
        campo.value = campo.value + ',00';
    }
    return true;
}

function SelecionaTodos(lst)
{
    CONTROL = document.getElementById(lst);
    for(var i = 0;i < CONTROL.length;i++){
        CONTROL.options[i].selected = true; 
    }
}

function DesmarcaTodos(lst)
{
    CONTROL = document.getElementById(lst);
    for(var i = 0;i < CONTROL.length;i++){
        CONTROL.options[i].selected = false; 
    }
}

function SelecionaTodosChk(lst)
{
    CONTROL = document.getElementsByTagName("input")
    for (var i=0; i<CONTROL.length; i++)
    {
      if (CONTROL[i].type == "checkbox")
      {
          CONTROL[i].checked = "checked";
      }
    }
}

function DesmarcaTodosChk(lst)
{
    CONTROL = document.getElementsByTagName("input")
    for (var i=0; i<CONTROL.length; i++)
    {
      if (CONTROL[i].type == "checkbox")
      {
          CONTROL[i].checked = "";
      }
    }
}

function formatar_moeda(campo, separador_milhar, separador_decimal, tecla) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    if (window.event) //IE
    {
        whichCode = e.keyCode;
    }

    else if (e.which) //FF
    {
        whichCode = e.which;
    }

    //if (whichCode == 13) return true; // Tecla Enter
    //if (whichCode == 8) return true; // Tecla Delete

    if ((whichCode == 8 || whichCode >= 48 && whichCode <= 57 || whichCode >= 96 && whichCode <= 105)) {
        len = campo.value.length;
        for (i = 0; i < len; i++)
            if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
        aux = '';
        for (; i < len; i++)
            if (strCheck.indexOf(campo.value.charAt(i)) != -1) aux += campo.value.charAt(i);
        aux += key;
        len = aux.length;
        if (len == 0) campo.value = '';
        if (len == 1) campo.value = '0' + separador_decimal + '0' + aux;
        if (len == 2) campo.value = '0' + separador_decimal + aux;

        if (len > 2) {
            aux2 = '';

            for (j = 0, i = len - 3; i >= 0; i--) {
                if (j == 3) {
                    aux2 += separador_milhar;
                    j = 0;
                }
                aux2 += aux.charAt(i);
                j++;
            }

            campo.value = '';
            len2 = aux2.length;
            for (i = len2 - 1; i >= 0; i--)
                campo.value += aux2.charAt(i);
            campo.value += separador_decimal + aux.substr(len - 2, len);
        }
    }
    return false;
}
