﻿function IsNumber(objeto) {
    var sValor = new String();
    sValor = objeto.value;
    sValor = sValor.replace('.', '');
    sValor = sValor.replace(',', '.');
    /*if (!(/^\d+\.\d+$/.test(sValor))){
    alert('Valor inválido. Tente novamente!');
    objeto.focus();
    }*/
    //Tive problemas com este script para a tela de importação de curvas. Caso
    //alguém queira alterar de novo este script, que acredito ser mais completo,
    //favor testar a tela mensionada também. Obrigado, Lener Ribeiro - Risco
    var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

    if (!objRegExp.test(sValor)) {
        alert('Valor inválido. Tente novamente!');
        objeto.focus();
    }
}

function LockChars() {
    var a;
    a = window.event.keyCode;
    if (a < 44 || a > 57)
        window.event.keyCode = 0;
    return true;
}

//permite somente a entrada de uma data valida
//Para chamar essa function usar: MascaraData(this);
function MascaraData(objeto) {
    var campo = eval(objeto);
    var separador = '/';
    var conjunto1 = 2;
    var conjunto2 = 5;

    if (campo.value.length != conjunto1 || campo.value.length != conjunto2) {
        var keypress = event.keyCode;

        if (keypress < 48 || keypress > 57) {
            event.returnValue = false
        }
    }

    if (campo.value.length == conjunto1) {
        campo.value = campo.value + separador;
    }
    if (campo.value.length == conjunto2) {
        campo.value = campo.value + separador;
    }
}

//permite somente a entrada de uma data valida
//Para chamar essa function usar: MascaraData(this);
function MascaraHora(objeto) {
    var campo = eval(objeto);
    var separador = ':';
    var conjunto1 = 2;
    var conjunto2 = 5;

    var keypress = event.keyCode;

    var sCaracteres = '0123456789';

    if (sCaracteres.indexOf(String.fromCharCode(keypress)) != -1) {
        event.returnValue = true;
    } else
        event.returnValue = false;


    if (campo.value.length == conjunto1) {
        campo.value = campo.value + separador;
    }
    //if (campo.value.length == conjunto2){
    //	campo.value = campo.value + separador;
    //}

}

//Mascara para numeros decimais, não deixando digitar mais de 2 casas apos a virgula
//O segundo parâmetro é opcional caso o campo permita valor negativo deve ser passado true
function MascaraDecimal(objeto, permiteNegativo) {
    var keypress = event.keyCode;
    var campo = objeto.value;

    var sSeparador = ',';
    var sCaracteres = '0123456789' + sSeparador;

    if (permiteNegativo && campo.indexOf('-') == -1) {
        //Aceita sinal negativo (apenas uma ocorrência do caractere )	
        sCaracteres += '-';
    }

    if (sCaracteres.indexOf(String.fromCharCode(keypress)) != -1) {
        {
            if (String.fromCharCode(keypress) == sSeparador && campo.indexOf(sSeparador) != -1)
                event.returnValue = false;
            else
                event.returnValue = true;

        }
    } else
        event.returnValue = false;
}


function MascaraInteiro(objeto) {
    var keypress = event.keyCode;
    var campo = objeto.value;

    var sSeparador = '';
    var sCaracteres = '0123456789' + sSeparador;

    if (sCaracteres.indexOf(String.fromCharCode(keypress)) != -1) {
        {
            if (String.fromCharCode(keypress) == sSeparador && campo.indexOf(sSeparador) != -1)
                event.returnValue = false;
            else
                event.returnValue = true;

        }
    } else
        event.returnValue = false;
}



function FormataMascaraDecimal(objeto) {
    var valorOriginal = objeto.value;
    if (valorOriginal == "") valorOriginal = "0,00";
    if (valorOriginal.indexOf(",") != -1) {
        var posVirg = valorOriginal.length - valorOriginal.indexOf(",");
        if (posVirg == 2)
            valorOriginal = valorOriginal + "0";
        if (posVirg == 1)
            valorOriginal = valorOriginal + "00";
    } else {
        valorOriginal = valorOriginal + ",00";
    }

    objeto.value = valorOriginal;
}

function FormataMascaraDecimalMilheiro(objeto) {
    var valorOriginal = objeto.value;
    var novoValor = "";
    if (valorOriginal == "") valorOriginal = "0,00";
    var negativo = false;
    if (valorOriginal.substring(0, 1) == "-") {
        negativo = true;
        valorOriginal = valorOriginal.substring(1);
    }
    if (valorOriginal.indexOf(",") != -1) {
        novoValor = valorOriginal.substring(valorOriginal.indexOf(","));
        valorOriginal = valorOriginal.substring(0, valorOriginal.indexOf(","));
        if (novoValor.length == 2)
            novoValor = novoValor + "0";
        if (novoValor.length == 1)
            novoValor = novoValor + "00";
    } else {
        novoValor = ",00";
    }

    while (valorOriginal.length > 3) {
        novoValor = "." + valorOriginal.substring(valorOriginal.length - 3) + novoValor;
        valorOriginal = valorOriginal.substring(0, valorOriginal.length - 3);
    }
    novoValor = valorOriginal + novoValor;
    objeto.value = novoValor;
    if (negativo) {
        objeto.value = "-" + objeto.value;
    }
}
function DesformataMascaraDecimalMilheiro(objeto) {
    var novoValor = objeto.value;
    while (novoValor.indexOf(".") != -1) {
        novoValor = novoValor.replace(".", "");
    }

    objeto.value = novoValor;
    objeto.select();
}

function retornaDataHora() {
    var dthr = new Date();
    var sDay = formataNumero(dthr.getDate(), 2);
    var sMonth = formataNumero(dthr.getMonth() + 1, 2);
    var sHour = formataNumero(dthr.getHours(), 2);
    var sMin = formataNumero(dthr.getMinutes(), 2);
    var sSec = formataNumero(dthr.getSeconds(), 2);
    var sMill = formataNumero(dthr.getMilliseconds(), 3);

    return sDay + "/" + sMonth + "/" + dthr.getFullYear() + " " +
           sHour + ":" + sMin + ":" + sSec + "." + sMill;
}

function formataNumero(value, length) {
    if (value.toString().length < length)
        return textPad("0", length - value.toString().length) + value.toString();
    return value;
}

function textPad(value, length) {
    var sAux = value;
    for (var i = 1; i < length; ++i) {
        sAux = sAux + value;
    }
    return sAux;
}

//
// Formata para moeda
//
function formataMoeda(txt) {
    var tecla = event.keyCode;
    if (tecla < 48 || tecla > 57) { event.returnValue = false; return; }
    var moeda = txt.value;
    tam = moeda.length;

    if (tam == 1)
        txt.value = moeda + ","

    if (tam == 4) {
        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 2) + ","
        b = txt.value.substr(2, 2)
        txt.value = a + b
    }

    if (tam == 5) {
        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 3) + ","
        b = txt.value.substr(3, 2)
        txt.value = a + b
    }


    if (tam == 6) {
        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 1) + "."
        b = txt.value.substr(1, 3) + ","
        c = txt.value.substr(4, 2)
        txt.value = a + b + c
    }


    if (tam == 8) {
        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 2) + "."
        b = txt.value.substr(2, 3) + ","
        c = txt.value.substr(5, 2)
        txt.value = a + b + c
    }

    if (tam == 9) {

        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 3) + "."
        b = txt.value.substr(3, 3) + ","
        c = txt.value.substr(6, 2)
        txt.value = a + b + c
    }

    if (tam == 10) {
        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 1) + "."
        b = txt.value.substr(1, 3) + "."
        c = txt.value.substr(4, 3) + ","
        d = txt.value.substr(7, 2)
        txt.value = a + b + c + d
    }

    if (tam == 11) {
        txt.value = txt.value.substr(0, tam)
        txt.value = txt.value.replace('.', '')
        txt.value = txt.value.replace(',', '')
        a = txt.value.substr(0, 2) + "."
        b = txt.value.substr(2, 3) + "."
        c = txt.value.substr(5, 3) + ","
        d = txt.value.substr(8, 2)
        txt.value = a + b + c + d
    }
}


function IsValidTime(sender, args) {
    // Checks if time is in HH:MM:SS AM/PM format.
    // The seconds and AM/PM are optional.

    timeStr = args.Value;
    var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

    var matchArray = timeStr.match(timePat);
    if (matchArray == null) {
        args.IsValid = false;
        return false;
    }

    hour = matchArray[1];
    minute = matchArray[2];
    second = matchArray[4];

    if (hour < 0 || hour > 23) {
        args.IsValid = false;
        return false;
    }

    if (minute < 0 || minute > 59) {
        args.IsValid = false;
        return false;
    }

    if (second != null && (second < 0 || second > 59)) {
        args.IsValid = false;
        return false;
    }
    args.IsValid = true;
    return true;
}

function ReadOnly(objeto) {
    event.keyCode = 0;
}