function mascara(obj, mask, evtKey) {
  var i, count, strValue, strSize, mskSize, bolMask, newStr, nKey;

  /* captura o evento keypress de acordo com o navegador utilizado */
  if (document.all) { /* Internet Explorer */
    nKey = evtKey.keyCode;
  }
  else {
    if (document.layers) { /* Mozzila Firefox, safari... */
      nKey = evtKey.which;
    }
    else {
      nKey = evtKey.which;
      if (nKey == 8) {
        return true;
      }
    }
  }

  strValue = obj.value;

  /* limpa todos os caracteres de formatações que estiverem no campo */
  strValue = strValue.toString().replace("-", "");
  strValue = strValue.toString().replace("-", "");
  strValue = strValue.toString().replace(".", "");
  strValue = strValue.toString().replace(".", "");
  strValue = strValue.toString().replace("/", "");
  strValue = strValue.toString().replace("/", "");
  strValue = strValue.toString().replace(":", "");
  strValue = strValue.toString().replace(":", "");
  strValue = strValue.toString().replace("(", "");
  strValue = strValue.toString().replace("(", "");
  strValue = strValue.toString().replace(")", "");
  strValue = strValue.toString().replace(")", "");
  strValue = strValue.toString().replace(" ", "");
  strValue = strValue.toString().replace(" ", "");
  strSize = strValue.length;
  mskSize = mask.length;

  i = 0;
  count = 0;
  newStr = "";
  mskSize = strSize;

  /* começa a rotina de concatenação do valor e mascara */
  while (i < mskSize) {
    bolMask = ((mask.charAt(i) == '-') || (mask.charAt(i) == '.') || (mask.charAt(i) == '/') || (mask.charAt(i) == ':'));
    bolMask = bolMask || ((mask.charAt(i) == '(') || (mask.charAt(i) == ')') || (mask.charAt(i) == ' '));

    /* se bolMask for true */
    if (bolMask) {
      newStr += mask.charAt(i);
      mskSize++;
    }
    else {
      newStr += strValue.charAt(count);
      count++;
    }

    /* incrementa o i e volta ou sai do while*/
    i++;
  }

  obj.value = newStr;

  if (nKey != 8) { /* backspace */
    if (mask.charAt(i - 1) == '9') {
      return ((nKey > 47) && (nKey < 58));
    }
    else {
      return true;
    }
  }
  else {
    return true;
  }
}

/* funções destinadas a formatar os campos de moeda */
function clear(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;
}

function mascMoeda(campo, tammax, teclapres, decimal) {
  var tecla = teclapres.keyCode;
      vr  = clear(campo.value, '0123456789');
      tam = vr.length;
      dec = decimal;

  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 <= dec) {
      campo.value = vr;
    }
    if ((tam > dec) && (tam <= 5)) {
      campo.value = vr.substr(0, tam - 2) + "," + vr.substr(tam - dec, tam);
    }
    if ((tam >= 6) && (tam <= 8)) {
      campo.value = vr.substr(0, tam - 5) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, 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 - dec, 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 - dec, 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);
    }
  }
}
