﻿/*******************************************************************************
FORMATACAO GENERICA DE MASCARA NUMERICA
********************************************************************************/
function formatar(src, mask,e) {

      var tecla;  
      if (e.keyCode) { // IE  
        tecla = e.keyCode;  
      }  
      else if (e.which) { // FIREFOX
        tecla = e.which;  
      }  
 

    // SOMENTE NUMEROS DE 0 A 9
    if ((tecla > 47 && tecla < 58)) {
        var i = src.value.length;
        var saida = mask.substring(1, 2);
        var texto = mask.substring(i)
        if (texto.substring(0, 1) != saida) {
            src.value += texto.substring(0, 1)
        };
    }
    else {
        if (tecla != 8) // backspace
            event.keyCode = 0
            return false;
        }

    }

    /*******************************************************************************
    INPUT DE DADOS NO FORMATO MOEDA
    ******************************************************************************/
 
    function BlockKeybord() {
        if (window.event) // IE
        {

            if ((event.keyCode < 48) || (event.keyCode > 57)) {
                event.returnValue = false;
            }

        }
        else if (e.which) // Netscape/Firefox/Opera
        {

            if ((event.which < 48) || (event.which > 57)) {
                event.returnValue = false;
            }

        }


    }

    function troca(str, strsai, strentra) {
        while (str.indexOf(strsai) > -1) {
            str = str.replace(strsai, strentra);
        }

        return str;
    }

    function FormataMoeda(campo, tammax, teclapres, caracter) {
        if (teclapres == null || teclapres == "undefined") {
            var tecla = -1;
        }
        else {
            var tecla = teclapres.keyCode;
        }

        if (caracter == null || caracter == "undefined") {
            caracter = ".";
        }

        vr = campo.value;

        if (caracter != "") {
            vr = troca(vr, caracter, "");
        }

        vr = troca(vr, "/", "");
        vr = troca(vr, ",", "");
        vr = troca(vr, ".", "");

        tam = vr.length;

        if (tecla > 0) {
            if (tam < tammax && tecla != 8) {
                tam = vr.length + 1;
            }

            if (tecla == 8) {
                tam = tam - 1;
            }
        }

        if (tecla == -1 || tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 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) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
            }

            if ((tam >= 9) && (tam <= 11)) {
                campo.value = vr.substr(0, tam - 8) + caracter + vr.substr(tam - 8, 3) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
            }

            if ((tam >= 12) && (tam <= 14)) {
                campo.value = vr.substr(0, tam - 11) + caracter + vr.substr(tam - 11, 3) + caracter + vr.substr(tam - 8, 3) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
            }

            if ((tam >= 15) && (tam <= 17)) {
                campo.value = vr.substr(0, tam - 14) + caracter + vr.substr(tam - 14, 3) + caracter + vr.substr(tam - 11, 3) + caracter + vr.substr(tam - 8, 3) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
            }
        }
    }

    function maskKeyPress(objEvent) {
        var iKeyCode;

        if (window.event) // IE
        {
            iKeyCode = objEvent.keyCode;

            if (iKeyCode >= 48 && iKeyCode <= 57)
                return true;

            return false;
        }
        else if (e.which) // Netscape/Firefox/Opera
        {
            iKeyCode = objEvent.which;

            if (iKeyCode >= 48 && iKeyCode <= 57)
                return true;

            return false;
        }


    }
  
/**************************************************************************************
  INPUT DE DADOS NO FORMATO MOEDA COM ENTRADA INVERTIDA (IGUAL AO DO CAIXA ELETRONICO)
**************************************************************************************/

    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 = '';
        var whichCode = (window.Event) ? tecla.which : tecla.keyCode;

        if (whichCode == 13)
            return true; // TECLA ENTER

        if (whichCode == 8)
            return true; // TECLA DELETE

        key = String.fromCharCode(whichCode); // PEGANDO OS VALORES DIGITADOS

        if (strCheck.indexOf(key) == -1)
            return false; // VALOR VALIDO (NÃO INTEIRO)

        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;
    }
