﻿function copiaValor(input) {
    var idOrigem = input.id;
    var tamanho = idOrigem.length;
    var idDestino = idOrigem.substring(0, tamanho - 11) + "TBFake";
    var destino = document.getElementById(idDestino);
    destino.value = input.value;
}
function MascaraCNPJ(e, input) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58)) {
        if ((input.value.length == 2) || (input.value.length == 6)) {
            input.value = input.value + ".";
        } else if (input.value.length == 10) {
            input.value = input.value + "/";
        } else if (input.value.length == 15) {
            input.value = input.value + "-";
        }
        return true;
    } else {
        if (tecla != 8 && tecla != 46 && tecla != 9) return false;
        else return true;
    }
}
function SomenteNumero(e) {
    if (e.keyCode == 9) return true;
    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 MascaraTelefone(e, input) {
    if (e.keyCode == 9) return true;
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58)) {
        if (input.value.length == 4) {
            input.value = input.value + "-";
        }
        return true;
    } else {
        if (tecla != 8 && tecla != 46 && tecla != 9) return false;
        else return true;
    }
}
function Data(e, input) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58)) {
        if ((input.value.length == 2) || (input.value.length == 5)) {
            input.value = input.value + "/";
        }
        return true;
    } else {
        if (tecla != 8 && tecla != 46 && tecla != 9) return false;
        else return true;
    }
}
function CEP(e, input) {
    if (e.keyCode == 9) return true;

    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58)) {
        if ((input.value.length == 5)) {
            input.value = input.value + "-";
        }
        return true;
    } else {
        if (tecla != 8 && tecla != 46 && tecla != 9) return false;
        else return true;
    }
}
function Trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

function PreencheInput(id, valor) {
    var input = document.getElementById(id);
    if (Trim(input.value) == valor) {
        input.value = "";
    } else if (Trim(input.value) == "") {
        input.value = valor
    }
}

function PreencheInputSenha(id, valor) {
    var input = document.getElementById(id);
    if (Trim(input.value) == valor) {
        input.value = "";
        input.type = "password";
        //input.setAttribute('type','password');
    } else if (Trim(input.value) == "") {
        input.value = valor;
        input.type = "text";
        //input.setAttribute('type', 'text');
    }
}

function validaEmail(v) {
    var email = document.getElementById(v).value;
    email = Trim(email);
    try {
        if (email == "") {
            throw "Preencher email";
            return false;
        } else {

            var aroba = email.indexOf("@");
            var ponto = email.lastIndexOf(".");
            var tamanho = email.length;
            if (aroba <= 0 || ponto <= 0) {
                throw "Email Inválido";
            }
            if (aroba > ponto) {
                throw "Email Inválido";
            }
            if (ponto > (tamanho - 2)) {
                throw "Email Inválido";
            }
            return true;
        }
    } catch (e) {
        alert(e.toString());
        return false;
    }
}
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for (i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0' + SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function MascaraMoeda2(cur, len) {
    n = '__0123456789';
    d = cur.value;
    l = d.length;
    r = '';
    if (l > 0) {
        z = d.substr(0, l - 1);
        s = '';
        a = 2;
        for (i = 0; i < l; i++) {
            c = d.charAt(i);
            if (n.indexOf(c) > a) {
                a = 1;
                s += c;
            };
        };
        l = s.length;
        t = len - 1;
        if (l > t) {
            l = t;
            s = s.substr(0, t);
        };
        if (l > 2) {
            r = s.substr(0, l - 2) + ',' + s.substr(l - 2, 2);
        }
        else {
            if (l == 2) {
                r = '0,' + s;
            }
            else {
                if (l == 1) {
                    r = '0,0' + s;
                };
            };
        };
        if (r == '') {
            r = '0,00';
        }
        else {
            l = r.length;
            if (l > 6) {
                j = l % 3;
                w = r.substr(0, j);
                wa = r.substr(j, l - j - 6);
                wb = r.substr(l - 6, 6);
                if (j > 0) {
                    w += '.';
                };
                k = (l - j) / 3 - 2;
                for (i = 0; i < k; i++) {
                    w += wa.substr(i * 3, 3) + '.';
                };
                r = w + wb;
            };
        };
    };
    if (r.length <= len) {
        cur.value = r;
    }
    else {
        cur.value = z;
    };
    return 'ok';
};
