﻿/******************************************************************************
 *                          HISTÓRICO DE ALTERAÇÕES                           *
 * ************************************************************************** *
 * Autor     : Max Moraes            Chamado: 26397   Data: 03/05/2010        *
 * Descrição : Adicionado parametro opcional no método OnlyDecimal() para     *
 *             limitar o número de digitos fornecidos antes da virgula.       *
 * ************************************************************************** *
 * Autor     : Max Moraes            Chamado: 25447   Data: 18/06/2010        *
 * Descrição : Ajuste para tratar quando o input box esta com o texto         *
 *             selecionado.                                                   *
 **************************************************************************** *
 * Autor     : Walsuir Antonio Sarto Chamado: 31426   Data: 09/09/2010        *
 * Descrição : Implementação do lançamento dos creditos de IPTU               *
 *             Correção de um erro no selecionar de textBox vazio no IE       *
 *****************************************************************************/
function getKey(e) {
    if (window.event) {
        return e.keyCode;
    }
    else {
        return e.which;
    }
    return null;
}

// Chamado 25447. Max Moraes. 18/06/2010 .
function isSelected(obj)
{
    var isFF = navigator.userAgent.toString().indexOf("Firefox") != -1;
    var isOP = navigator.userAgent.toString().indexOf("Opera") != -1;
    var selectedText = "";

    if (document.selection != undefined)
        try { selectedText = document.selection.createRange().text } catch (ex) {};
    else if (obj.selectionStart != undefined)
        selectedText = obj.value.substring(obj.selectionStart, obj.selectionEnd);
    return (selectedText != "");
}

function SpecialKeys(tecla)
{
    // Chamado 25447. Max Moraes. 18/06/2010 .
    var isFF = navigator.userAgent.toString().indexOf("Firefox") != -1;
    var isOP = navigator.userAgent.toString().indexOf("Opera") != -1;

    // Chamado 25447. Max Moraes. 18/06/2010 .
    if ((tecla == 8) || (tecla == 9) || ((isFF || isOP) && (tecla == 0)))
        return true;
    else
        return false;
}

// Chamado 26397. Max Moraes. 03/05/2010.
function OnlyDecimal(obj, e, dCases, iCases) 
{
    var tecla = getKey(e);
    var isSpecialKey = SpecialKeys(tecla);
    
    if (e.type.toString() == "keypress" && !isSpecialKey) 
    {
        if ((tecla >= 48 && tecla <= 57) || (tecla == 44)) 
        {

            // Chamado 25447. Max Moraes. 18/06/2010 .
            if (isSelected(obj))
                obj.value = "";

            var indexComma = obj.value.toString().indexOf(',');

            if (tecla == 44 && indexComma == -1) 
            {
                if (obj.value.toString().length == 0)
                    obj.value = '0';
                return true;
            }
            else if (dCases != null && indexComma >= 0) 
            {
                if (indexComma != obj.value.toString().length - 1) 
                {
                    var decimalPart = obj.value.toString().substring(indexComma + 1, obj.value.toString().length);
                    if (decimalPart.length < dCases)
                        return true;
                    else {
                        stopEventPropagation(e);
                        return false;
                    } 
                }
                else 
                {
                    if (tecla == 44) {
                        stopEventPropagation(e);
                        return false;
                    }
                    else
                        return true;
                }
            }
            // ----------------------------------------------------------
            // Chamado 26397. Max Moraes. 03/05/2010.
            // Adicionado parametro opcional no método para limitar o 
            // número de digitos fornecidos antes da virgula.
            // ----------------------------------------------------------
            else if (iCases != null && indexComma < 0) 
            {
                if (indexComma != obj.value.toString().length - 1) 
                {
                    var integerPart = obj.value.toString();
                    if (integerPart.length < iCases)
                        return true;
                    else {
                        stopEventPropagation(e);
                        return false;
                    }
                }
                else 
                {
                    if (tecla == 44) {
                        stopEventPropagation(e);
                        return false;
                    }
                    else
                        return true;
                }
            }
            // ----------------------------------------------------------
            else
                return true;
        }
        else {
            stopEventPropagation(e);
            return false;
        }
    }
}

function stopEventPropagation(e) {
    e.cancelBubble = true;
    e.returnValue = false;
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}


function OnlyDate(obj, e) 
{
    var tecla = getKey(e);
    var isSpecialKey = SpecialKeys(tecla);
    
    if (e.type.toString() == "keypress" && !isSpecialKey)
    {
        if (tecla >= 48 && tecla <= 57)
        {
            // Chamado 25447. Max Moraes. 18/06/2010 .
            if (isSelected(obj))
                obj.value = "";
            
            if (obj.value.length == 0)
            {
                if (tecla >= 52) {
                    obj.value = "0";
                }
                return true;
            }
            else if (obj.value.length == 1)
            {
                switch (obj.value.toString())
                {
                    case "0":
                    case "1":
                    case "2":
                        obj.value += String.fromCharCode(tecla) + "/";
                        return false;
                        break;

                    case "3":
                        if (tecla >= 48 && tecla <= 49) 
                        { 
                            obj.value += String.fromCharCode(tecla) + "/"; 
                            return false; 
                        } 
                        else
                            return false;
                        break;
                }
                return false;
            }
            else if (obj.value.length == 2) 
            {
                obj.value += "/";
                if (tecla >= 50) 
                {
                    obj.value += "0";
                    return true;
                }

                return true;
            }
            else if (obj.value.length == 3) 
            {
                if (tecla >= 50) 
                {
                    obj.value += "0" + String.fromCharCode(tecla) + "/";
                    return false;
                }
                return true;
            }
            else if (obj.value.length == 4) 
            {
                if (tecla >= 48 && tecla <= 50) 
                {
                    obj.value += String.fromCharCode(tecla) + "/";
                    return false;
                }
            }
            else if (obj.value.length == 5) 
            {
                obj.value += "/";
            }
            return true;
        }
        else
            return false;
    }
}

function OnlyNumbers(obj, e) 
{
    var tecla = getKey(e);
    var isSpecialKey = SpecialKeys(tecla);
    if (e.type.toString() == "keypress" && !isSpecialKey) 
    {
        if (tecla >= 48 && tecla <= 57) 
        {
            // Chamado 25447. Max Moraes. 18/06/2010 .
            if (isSelected(obj))
                obj.value = "";

            return true;
        }
        else
            return false;
    }
}

function MultiLineValidator(obj, objCounter, maxLength,e) {
    var objLength = obj.value.toString().length;
    if (objLength < maxLength) {
        var tecla = getKey(e);
        if (tecla == 8 || tecla == 46) {
            if (objLength > 0)
                objCounter.innerText = (objLength - 1).toString();
        } else if (tecla == 37 || tecla == 38 || tecla == 39 || tecla == 40 || tecla == 13 || tecla == 9 || tecla == 35 || tecla == 36)
            return true;
        else
            objCounter.innerText = (objLength + 1).toString();
        return true;
    } else
        return false;

}

function MultLineKeyPressed(obj, objCounter, maxLength, maxRows) {
    var nMaxRows = 0;
    if (maxRows != null)
        nMaxRows = maxRows;

    if (nMaxRows > 0) {
        var qtdLinhas = obj.value.replace(/\r/g, '').split('\n');

////            var changeHappens = false;
////            for (var i = 0; i < qtdLinhas.length; i++) {
////                var linha = qtdLinhas[i];
////                if (linha.length > 107) {
////                    if (linha.charAt(linha.length - 1) != ' ') {
////                        var lastIndexOfSpace = linha.lastIndexOf(" ");
////                        if (qtdLinhas.length > i + 1) {
////                            qtdLinhas[i] = linha.substr(0, lastIndexOfSpace).trim().replace(/\n/g, '').trim();
////                            qtdLinhas[i + 1] = (linha.substr(lastIndexOfSpace).trim().replace(/\n/g, '') + ' ' + qtdLinhas[i + 1].replace(/\n/g, '')).trim();
////                        } else {
////                            qtdLinhas[i] = linha.substr(0, lastIndexOfSpace).trim().replace(/\n/g, '').trim();
////                            qtdLinhas.push(linha.substr(lastIndexOfSpace).trim().replace(/\n/g, '').trim());
////                            changeHappens = true;
////                        }
////                    }
////                }
////            }
////            if (changeHappens) {
////                obj.value = '';
////                for (var i = 0; i < qtdLinhas.length; i++) {
////                    obj.value += qtdLinhas[i] + '\n';
////                }
////            }

        qtdLinhas = obj.value.split('\n');
        if (qtdLinhas.length > nMaxRows) {
            obj.value = qtdLinhas.slice(0, -1).join('\n');
            return;
        }
    }
        
    var objLength = obj.value.toString().length;
    if (objLength > maxLength)
        obj.value = obj.value.toString().substr(0, maxLength);
    MostrarQuantidadeCaracteres(obj, objCounter);
}

function MostrarQuantidadeCaracteres(obj, objCounter) {
    objCounter.innerText = obj.value.toString().length;
}

function NoHtml(obj) {
    obj.value = obj.value = obj.value.toString().replace(/</g, "(").replace(/>/g, ")");
}

function FormataValorReal(ConteudoCampo) {
    var evt = window.event;
    var valor = (window.Event) ? ConteudoCampo.which : ConteudoCampo.keyCode;
    if ((tecla >= 48 && tecla <= 57)) {
        if (valor != 8) {

            NumDig = ConteudoCampo.value;
            TamDig = NumDig.length;

            Contador = 0;
            if (TamDig > 1) {
                numer = "";
                for (i = TamDig; (i >= 0); i--) {
                    if ((parseInt(NumDig.substr(i, 1)) >= 0) && (parseInt(NumDig.substr(i, 1)) <= 9)) {
                        Contador++;
                        if ((Contador == 3) && ((TamDig - i) <= 4)) {
                            numer = "," + numer;
                            Contador = 0;
                        }
                        else if (Contador == 3) {
                            numer = "." + numer;
                            Contador = 0;
                        }
                        numer = NumDig.substr(i, 1) + numer;
                    } //end if
                } //end for
                ConteudoCampo.value = numer;
            }; //end if tamdig > 1

        } //end key != 8
        else { //key == 8
            NumDig = ConteudoCampo.value;
            TamDig = NumDig.length;
            TamDig--;
            Contador = 0;
            if (TamDig >= 0) {
                numer = "";
                for (i = TamDig; (i >= 0); i--) {
                    if ((parseInt(NumDig.substr(i, 1)) >= 0) && (parseInt(NumDig.substr(i, 1)) <= 9)) {
                        Contador++;

                        if ((Contador == 4) && ((TamDig - i) < 5)) {
                            numer = "," + numer;
                            Contador = 0;
                        }
                        else if ((Contador == 3) && ((numer.length) > 4)) {
                            numer = "." + numer;
                            Contador = 0;
                        }
                        numer = NumDig.substr(i, 1) + numer;
                    }
                }
                /************************
                if (numer == "001")
                numer="";
                if ((numer.length) == 3 )
                numer= "0," + numer;
                ************************/

                ConteudoCampo.value = numer;
            }; //end if TamDig
        } //end else key == 8
    }
    else {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
} //end func



function FmascTempoReal(ConteudoCampo) {
    var evt = window.event;
    var valor = evt.keyCode;   //(window.event) ? ConteudoCampo.which : ConteudoCampo.keyCode;
    if ((valor >= 48 && valor <= 57)) {
        if (valor != 8) {

            NumDig = ConteudoCampo.value;
            TamDig = NumDig.length;

            Contador = 0;
            if (TamDig > 1) {
                numer = "";
                for (i = TamDig; (i >= 0); i--) {
                    if ((parseInt(NumDig.substr(i, 1)) >= 0) && (parseInt(NumDig.substr(i, 1)) <= 9)) {
                        Contador++;
                        if ((Contador == 2) && ((TamDig - i) < 4)) {
                            numer = "," + numer;
                            Contador = 0;
                        }
                        else if (Contador == 3) {
                            numer = "." + numer;
                            Contador = 0;
                        }
                        numer = NumDig.substr(i, 1) + numer;
                    } //end if
                } //end for
                ConteudoCampo.value = numer;
            }; //end if tamdig > 1

        } //end key != 8
        else { //key == 8
            NumDig = ConteudoCampo.value;
            TamDig = NumDig.length;
            TamDig--;
            Contador = 0;
            if (TamDig >= 0) {
                numer = "";
                for (i = TamDig; (i >= 0); i--) {
                    if ((parseInt(NumDig.substr(i, 1)) >= 0) && (parseInt(NumDig.substr(i, 1)) <= 9)) {
                        Contador++;

                        if ((Contador == 4) && ((TamDig - i) < 5)) {
                            numer = "," + numer;
                            Contador = 0;
                        }
                        else if ((Contador == 3) && ((numer.length) > 4)) {
                            numer = "." + numer;
                            Contador = 0;
                        }
                        numer = NumDig.substr(i, 1) + numer;
                    }
                }
                /************************
                if (numer == "001")
                numer="";
                if ((numer.length) == 3 )
                numer= "0," + numer;
                ************************/

                ConteudoCampo.value = numer;
            }; //end if TamDig
        } //end else key == 8
    }
    else {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
} //end func
