﻿function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) 
		return true;
    else{
		if (tecla != 8) 
			return false;
		else 
			return true;
	}
}

function FormataValor(el, portugues){
	if(portugues)
	{
    	var val = el.value
        var answer_match = "$1,$2"                         // Delimitador casas decimal , (virgula)
        var aux;
        val = val.replace(/\D/g, "")                       // Remove tudo o que não é dígito
        val = val.replace(/(\d)(\d{2})$/, answer_match)    // Coloca a separação de decimal
        answer_match = "$1.$2"                             // Delimitador milhares
        aux = val.match(/(\,\d+)$/)                        // Salva o valor decimal na variavel auxiliar
        if (aux) {                                          // Verifica se existe decimais, senão não existirá milhar
            aux = aux[0]
            var compare = val.replace(aux,"")              // Pega apenas o valor a ser comparado
            while (compare.match(/(\d+)(\d{3})/)){
                compare = compare.replace(/(\d+)(\d{3})/, answer_match)    // Coloca o delimitador a cada milhar
            }
            val = compare + aux
        }
        // Change this val
        el.value = val
    }else{
    	var val = el.value
        var answer_match = "$1.$2"                         // Delimitador casas decimal , (virgula)
        var aux;
        val = val.replace(/\D/g, "")                       // Remove tudo o que não é dígito
        val = val.replace(/(\d)(\d{2})$/, answer_match)    // Coloca a separação de decimal
        answer_match = "$1,$2"                             // Delimitador milhares
        aux = val.match(/(\.\d+)$/)                        // Salva o valor decimal na variavel auxiliar
        if (aux) {                                          // Verifica se existe decimais, senão não existirá milhar
            aux = aux[0]
            var compare = val.replace(aux,"")              // Pega apenas o valor a ser comparado
            while (compare.match(/(\d+)(\d{3})/)){
                compare = compare.replace(/(\d+)(\d{3})/, answer_match)    // Coloca o delimitador a cada milhar
            }
            val = compare + aux
        }
        // Change this val
        el.value = val
    }
} 

function SomenteNumeroComPontuacao(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58) || ( tecla == 44 || tecla == 46)) 
		return true;
    else{
		if (tecla != 8) 
			return false;
		else 
			return true;
	}
}

function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;


if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla >= 47) && (nTecla <= 58)); } 
      else { // qualquer caracter...
        return true;
      } 
    }
    else {
      return true;
    }
}

function exibeerro(msg) {    
    mensagem(msg);
    document.getElementById('erro').className = "erro";
}

function errar(msg) {
    mensagem(msg);
    document.getElementById('erro').className = "erro";
}

function sucesso(msg) {
    mensagem(msg);
    document.getElementById('erro').className = "sucesso";
}

function mensagem(msg) {
    document.getElementById('erro').innerHTML = msg;
}

function ocultar() {
    document.getElementById('erro').innerHTML = "";
    document.getElementById('erro').className = "invisivel";
}