﻿/****************************************************************************************
 * Projeto:     Nota Fiscal Eletrônica
 *              Portal Boa Nota
 * Revisões:
 * Data:       Chamado  Responsável    Descrição:
 * ----------  -------  -------------  --------------------------------------------------
 * 18/06/2010  25447    Max Moraes     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 inicial do lançamento dos creditos IPTU                    *
 ***************************************************************************************/
function Format(obj, pattern, size, initialChar, finalChar) {
    var isIe = navigator.appName.indexOf("Explorer") != -1;
    var isFF = navigator.userAgent.toString().indexOf("Firefox") != -1;
    var isOP = navigator.userAgent.toString().indexOf("Opera") != -1;

    this.getFormatedValue = function(direction, size) {
        if (obj != null && obj.value != "")
            obj.value = this.removePattern().toString().format(pattern, size, direction);
    };

    this.replace = function() { };

    this.getLiveFormat = function(evt) {
        var digChar = this.getKey(evt);

        if (isFF || isOP) {
            if (digChar == 0 || this.SpecialKeys(evt))
            {
                return true;
            }
        }

        // Chamado 25447. Max Moraes. 18/06/2010 .
        if (this.isSelected(obj) && digChar >= initialChar && digChar <= finalChar)
            obj.value = "";

        if (pattern.toString().charAt(obj.value.length) == "#") {
            if (digChar >= initialChar && digChar <= finalChar)
            {
                return true;
            }
        }
        else {
            obj.value += pattern.toString().charAt(obj.value.length);
            if (digChar >= initialChar && digChar <= finalChar)
            {
                return true;
            }
            return false;
        }
        return false;

    };

    this.getKey = function(e) {
        if (isIe) {
            return e.keyCode;
        }
        else {
            return e.which;
        }
        return null;
    };

    this.SpecialKeys = function(e) {
        var tecla = this.getKey(e);
        if (tecla == 8 || tecla == 9)
            return true;
        else {
            if (!isIe && (tecla == 37 || tecla == 39 || tecla == 9))
                return true;
            return false;
        }
    }

    this.removePattern = function() {
        var unformatted = obj.value;
        var lgth = pattern.length;
        for (var i = 0; i < lgth; i++) {
            if (pattern.toString().charAt(i) != "#") {
                unformatted = unformatted.toString().replace(pattern.charAt(i), "");
            }
        }
        lgth = unformatted.length;
        var replaced = unformatted;
        for (var i = 0; i < lgth; i++) {
            var charCode = unformatted.charCodeAt(i);
            if (charCode < initialChar || charCode > finalChar)
                replaced = replaced.replaceAll(unformatted.charAt(i), "");
        }
        return replaced;
    };
    
    // Chamado 25447. Max Moraes. 18/06/2010 .
    this.isSelected = function() {
        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 CreateFormat(val, pattern, size, initialChar, finalChar) {
    if (val == null) {
        var err = new Error();
        err.message = "O valor a ser formatado não pode ser nulo.";
        throw err;
    }
    if (pattern == null) {
        var err = new Error();
        err.message = "O padrão de formatação não pode ser nulo.";
        throw err;
    }
    return new Format(val, pattern, size, initialChar, finalChar);
}

function FormatDocument(obj) {
    obj.value = obj.value.toString().replace(".", "").replace(".", "").replace("-", "").replace("/", "");
    if (obj.value.length <= 11)
        CreateFormat(obj, "###.###.###-##", 14, 48, 57).getFormatedValue("L", 11);
    else
        CreateFormat(obj, "##.###.###/####-##", 18, 48, 57).getFormatedValue("L", 14);
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, "");
};

String.prototype.padLeft = function(totalWidth, paddingChar) {
    var paddedValue = new String(this);
    paddedValue = paddedValue.trim();
    if (paddingChar == null) {
        paddingChar = "0";
    }
    while (paddedValue.length < totalWidth) {
        paddedValue = paddingChar + paddedValue;
    }
    return paddedValue;
};

String.prototype.padRight = function(totalWidth, paddingChar) {
    var paddedValue = new String(this);
    paddedValue = paddedValue.trim();
    if (paddingChar == null) {
        paddingChar = "0";
    }

    while (paddedValue.length < totalWidth) {
        paddedValue += paddingChar;
    }
    return paddedValue;
};

String.prototype.format = function(pattern, padSize, direction, paddingChar) {
    var paddedValue = new String(this);
    if (direction == "R") {
        paddedValue = paddedValue.padRight(padSize, paddingChar);
    }
    else {
        paddedValue = paddedValue.padLeft(padSize, paddingChar);
    }
    return this.replaceChar(pattern, paddedValue);
};

String.prototype.replaceAll = function(de, para) {
    var str = this;
    var index = str.indexOf(de);
    while (index > -1) {
        str = str.replace(de, para);
        index = str.indexOf(de);
    }
    return str;
};

String.prototype.replaceChar = function(pattern, value) {
    var unformatedValue = new String(value);
    var patternValue = "";
    var patternCount = 0;
    for (var i = 0; i < pattern.toString().length; i++) {
        if (pattern.toString().charAt(i) == "#") {
            patternValue += unformatedValue.charAt(i - patternCount);
        }
        else {
            patternValue += pattern.toString().charAt(i);
            patternCount++;
        }
    }
    return patternValue;
};

String.prototype.insertAt = function(index, value) {
    var valor = this;
    return valor.substring(0, index) + value + valor.substring(index, valor.length);
}

function getKey(e) 
{
    var isIe = navigator.appName.indexOf("Explorer") != -1;

    if (isIe) 
    {
        return e.keyCode;
    }
    else 
    {
        return e.which;
    }
}
