﻿var ie = (document.all) ? true : false;
var cor;

function selecionaGrid(elemento) {
    cor = elemento.style.backgroundColor;
    elemento.style.backgroundColor = "#FFFF99"; //"#ECF3B8";

}

// Função para mudar a linha ao de-selecionar 
function deSelecionaGrid(elemento) {
    elemento.style.backgroundColor = cor;
}

function AbrirPopup1(url, largura, altura) {
    topo = (screen.height / 2) - (altura / 2);
    esquerda = (screen.width / 2) - (largura / 2);

    return window.open(url, "popup", 'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + topo + ',left=' + esquerda);
}

function AbrirPopup2(url, largura, altura, scroll) {
    topo = (screen.height / 2) - (altura / 2);
    esquerda = (screen.width / 2) - (largura / 2);

    if (scroll != "1") {
        scroll = "no";
    }
    else {
        scroll = "yes";
    }

    return window.open(url, "popup", 'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scroll + ',resizable=no,top=' + topo + ',left=' + esquerda);
}

function enterToTab(formRef, focusAny) {
    for (var i = 0, e = formRef.elements, len = e.length, hasNext = true; i < len && hasNext; i++)
        if (e[i].type && /^text|password|file/.test(e[i].type)) {
        for (var j = i + 1; j < len && (!e[j].type || /submit|reset/.test(e[j].type) || (focusAny ? /hidden/.test(e[j].type) : !/^text|password|file/.test(e[j].type))); j++)
            ;
        hasNext = j != len;

        e[i].onkeypress = (function(index, notLast) {
            return function() {
                var ta = false, k = (arguments[0] ? arguments[0].which : window.event.keyCode) != 13;

                if (!k && !(ta = (this.type == 'textarea' && this.value.length > 0)) && notLast) {
                    try {
                        this.form.elements[index].focus();
                    } catch (err) {
                        try {
                            this.form.elements[index + 1].focus();
                        } catch (errr) {
                            try {
                                this.form.elements[index + 2].focus();
                            } catch (errrr) {

                            }
                        }
                    }
                }

                return k || ta;
            }
        })(j, hasNext);
    }
}

function SomenteNumeros() {
    var keyCode = event.keyCode;

    if (!(keyCode > 47 && keyCode < 58)) {
        event.keyCode = 0;
    }
}

function SomenteNumerosMascara() {
    var keyCode = event.keyCode;

    if (!(keyCode > 47 && keyCode < 58 || keyCode == 40 || keyCode == 41 || keyCode == 45)) {
        return false;
    }
    else {
        return true;
    }
}


function replaceAll(str, from, to) {
    var idx = str.indexOf(from);


    while (idx > -1) {
        str = str.replace(from, to);
        idx = str.indexOf(from);
    }

    return str;
}

function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}

function Data_Valida(data) {
    var barra1 = data.indexOf('/');
    var barra2 = data.lastIndexOf('/');
    var ano = data.substring(barra2 + 1, data.length);
    var dia = data.substring(0, barra1);
    var mes = data.substring(barra1 + 1, barra2);
    var anoMod = ano % 4;
    var mesMod = mes % 2;
    var retorno = true;

    //verifica as barras
    if ((barra1 == -1) || (barra1 == barra2)) {
        retorno = false;
    }

    // verifica se os campos dia, ano , mes são numéricos e se possuem a quantidade de caracteres suficientes
    if ((isNaN(ano)) || (isNaN(dia)) || (isNaN(mes)) || (ano < 1900) || (ano > 2200) || (mes < 1) || (mes > 12) || (dia > 31) || (dia < 1)) {
        retorno = false;
    }

    // ferifica o mes de fevereiro
    if ((((anoMod != 0) && (dia > 28)) || ((anoMod == 0) && (dia > 29))) && (mes == 2)) {
        retorno = false;
    }

    if (mesMod == 1) {
        if ((mes >= 9) && (dia > 30)) {
            retorno = false;
        }
    }
    else {
        if ((mes < 7) && (dia > 30)) {
            retorno = false;
        }
    }

    if ((retorno == false)) {
        return false;
    }
    else {
        return (true);
    }
}

function validarCodigoBarras() {
    if (window.event.keyCode != 13) 
    {
        var letra = String.fromCharCode(window.event.keyCode);
        var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYWZabcdefghijklmnopqrstuvxywz";

        if (digits.indexOf(letra) == -1) {
            window.event.keyCode = 0;
        }
    }
}

function validarCaracterEspecialEmail()
{
    if (window.event.keyCode != 13)
    {
        var letra = String.fromCharCode(window.event.keyCode);
        var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYWZabcdefghijklmnopqrstuvxywz_@.";

        if (digits.indexOf(letra) == -1)
        {
            window.event.keyCode = 0;
        }
    }
}

function MaxlengthJS(objText) {
    var txt = null;
    var valor = null;
    var maximo = 0;
    var tamanho = 0;

    txt = $('#' + objText.id);

    valor = replaceAll(txt.val(), ',', '');
    valor = replaceAll(valor, '.', '');

    maximo = parseInt(txt.attr('tamanhomaximo'));
    tamanho = parseInt(valor.length);

    if (tamanho >= maximo) {
        window.event.keyCode = 0;

        return false;
    }
    
    return true;
}

function IsValidEmail(pEmail) {
    if (pEmail != null) {
        if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(pEmail))) {
            return false;
        }
    }
    return true;
}

function DataValida(data) {
    var barra1 = data.indexOf('/');
    var barra2 = data.lastIndexOf('/');
    var ano = data.substring(barra2 + 1, data.length);
    var dia = data.substring(0, barra1);
    var mes = data.substring(barra1 + 1, barra2);
    var anoMod = ano % 4;
    var mesMod = mes % 2;
    var retorno = true;

    //verifica as barras
    if ((barra1 == -1) || (barra1 == barra2)) {
        retorno = false;
    }

    // verifica se os campos dia, ano , mes são numéricos e se possuem a quantidade de caracteres suficientes
    if ((isNaN(ano)) || (isNaN(dia)) || (isNaN(mes)) || (ano < 1900) || (ano > 2200) || (mes < 1) || (mes > 12) || (dia > 31) || (dia < 1)) {
        retorno = false;
    }

    // ferifica o mes de fevereiro
    if ((((anoMod != 0) && (dia > 28)) || ((anoMod == 0) && (dia > 29))) && (mes == 2)) {
        retorno = false;
    }

    if (mesMod == 1) {
        if ((mes >= 9) && (dia > 30)) {
            retorno = false;
        }
    }
    else {
        if ((mes < 7) && (dia > 30)) {
            retorno = false;
        }
    }

    if ((retorno == false)) {
        return false;
    }
    else {
        return (true);
    }
}

function DeixarZero(obj) {
    if (obj.value == '' ||
        obj.value == '0') {
        obj.value = 1;
    }
}

function TrocarFotoProduto(idProduto, nomeArquivo) {
    document.getElementById('ctl00_masterContent_imgProduto').src = 'FotosProdutos/' + idProduto + '/' + nomeArquivo;
}

function DetalhesPedido(idPedido) {
    AbrirPopup2('Pedido_Detalhe.aspx?id=' + idPedido, 600, 400, 1);

    return false;
}

function autotab(elemento) {
    if (elemento.value.length < elemento.getAttribute("maxlength"))
        return;
        
    var formulario = elemento.form;
    var els = formulario.elements;
    var x, autotab;

    len = els.length; 
    
    for (var i = 0; i < len; i++) 
    {
        x = els[i];
        
        if (elemento == x && (autotab = els[i + 1])) {
            if (autotab.focus)
                autotab.focus();
        }
    } 
}