function Get(obj) {
    return document.getElementById(obj);
}


$(document).ready(function() {
    /* menu de idiomas */
    $("#idioma ul").css({ display: "none" }); // Opera Fix
    $("#idioma li").hover(
        function() {
            $(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(400);
        },
        function() {
            $(this).find('ul:first').css({ visibility: "hidden" });
        }
    );

    /* menu de principal */

    $("#principal ul").css({ display: "none" }); // Opera Fix
    $("#principal li ").hover(
        function() {
            $(this).find('ul:first').css({ visibility: "visible", display: "block" }).show(400);
        },
        function() {
            $(this).find('ul:first').css({ visibility: "hidden", display: "none" });
        }
    );

    // Ver banners home //
    var esconderProduto = "0";
    var exibirProduto = null;

    $('.menuProdutos a').hover(
        function(event) {
            exibirProduto = $(this).attr('id');
            exibirProduto = exibirProduto.replace('_', '#');
            event.preventDefault();
            if (esconderProduto != "0") {
                $(esconderProduto).hide();
            }
            $(exibirProduto).show();
            esconderProduto = exibirProduto;
        },
        function(event) {
            event.preventDefault();
            $(esconderProduto).hide();
        }
    );

    // Ver subitems menu topo //

    $('.submenuItem').hover(
        function(event) {
            submenu = $(this).find('ul:first');
            event.preventDefault();
            $(submenu).show();
        },
        function(event) {
            submenu = $(this).find('ul:first');
            event.preventDefault();
            $(submenu).hide();
        }
    );

    // Empresas Home //
    var esconderEmpresa = "0";
    var exibirEmpresa = null;
    $('#empresas a').click(
        function(event) {
            exibirEmpresa = $(this).attr('href');
            event.preventDefault();
            if (esconderEmpresa != "0") {
                $(esconderEmpresa).hide('slow');
            }
            $('#colunas').css('opacity', 0.5);
            $('#empresas').css('opacity', 0.5);
            $(exibirEmpresa).show('slow');
            esconderEmpresa = exibirEmpresa

        }
    );
    $('#empresasTexto div').click(
        function(event) {
            esconderEmpresa = exibirEmpresa;
            $('#colunas').css('opacity', 1);
            $('#empresas').css('opacity', 1);
            $(esconderEmpresa).hide('slow');
        }
    );




    $('#imageOpen').click(
        function() {
            $('#imageOpen').hide();
        }
	);
});

function ampliaImagem(imgSrc,legend) {
    var pos = imgSrc.lastIndexOf('.');
    var imgName = imgSrc.substr(0, pos);
    var imgExt = imgSrc.substr(pos);
    var imgPopup = imgName + '_g' + imgExt;
    var title = document.title;
    var postitle = title.indexOf('|');
    title = title.substr(0, postitle);
    title = title.replace(/&/g,"%26")
    var ww = 450;
    var hh = 400;
    var url = "/tools/foto.asp?title=" + title + "&img=" + imgSrc + "&lgd=" + legend;
    amplia = window.open(url, "amplia", "width=" + ww + ",height=" + hh + ",status=0,scrollbars=1,resizable=0,screenX=20,screenY=40,left=20,top=40");
    amplia.focus();
}


////////////////////////////////////////////////////////////////////////////////////////////
//Nome: ValidaConteudoCampo
//Descrição: Função para impedir que seja digitado um conteúdo inválido no campo 
//Parâmetros   intervalo = 'A..Z' ; mais de uma combinação = 'A..Z;a..z;0..9' 
//             caracter = '-/@!$' 
//Exemplo: <input onkeypress="JavaScript:ValidaConteudoCampo('1..5','NULL');"> 
function ValidaConteudoCampo(intervalo, caracter) {
    var bvalido = false;
    if (((caracter.toUpperCase) != 'NULL') || (caracter != '')) {
        if (caracter == "PONTUACAO") { caracter = 'ãáâéêíîõóôúûçÃÁÂÉÊÍÎÕÓÔÚÛÇ '; }
        for (i = 0; i <= (caracter.length); i++) {
            if ((event.keyCode) == (caracter.charCodeAt(i))) { bvalido = true; }
        }
    }
    if (!bvalido) {
        var Inicio = "";
        var Fim = "";
        var bRetornaFalso = false;
        var bEncontrou = false;
        var location = -1;
        location = intervalo.indexOf(";");
        if ((location) > -1) {
            var restricoes = intervalo.split(";");
            for (i = 0; i <= (restricoes.length - 1); i++) {
                Inicio = restricoes[i].substring(0, 1);
                Fim = restricoes[i].substring(3, 4);
                if ((event.keyCode >= Inicio.charCodeAt()) && (event.keyCode <= Fim.charCodeAt())) { bEncontrou = true; i = restricoes.length; } //48 57 
            }
            if (!bEncontrou) { bRetornaFalso = true; }
        }
        else if (intervalo.length == 4) {
            Inicio = intervalo.substring(0, 1);
            Fim = intervalo.substring(3, 4);
            if ((event.keyCode >= Inicio.charCodeAt()) && (event.keyCode <= Fim.charCodeAt())) { bEncontrou = true; } //48 57 
            if (!bEncontrou) { bRetornaFalso = true; }
        }
        if (bRetornaFalso) { event.returnValue = false; }
    }
}



////////////////////////////////////////////////////////////////////////////////////////////

function validaEmail(obj,lang) {
    if (obj.value.length == 0) {
        if (lang == "po") {
            alert("Por favor, preencha o seu e-mail!");
        } else if (lang == "es") {
            alert("¡Por favor, introduzca su email!");
        } else {
            alert("Please, fill in your e-mail!");
        }
        obj.value = "";
        obj.focus();
        return false;
    }
    if ((obj.value.indexOf('@', 0) < 4 || obj.value.indexOf('.') < 1)) {
       if (lang == "po") {
            alert("O seu e-mail não está no formato correto. \r\nEle deve ser no formato seunome@empresa.com.br!");
        } else if (lang == "es") {
            alert("¡Tu e-mail no tiene el formato correcto. Él debe estar en el formato sunombre@compania.com.br!");
        } else {
            alert("Your e-mail is not in the correct format. He should be in the format name@company.com.br!");
        }
        obj.value = "";
        obj.focus();
        return false;
    }
    return true;
    
}

////////////////////////////////////////////////////////////////////////////////////////////
//Nome: validaCPF
//Descrição: valida CPF
//Parâmetros   st = número do cpf
function validaCPF(st) {
    if (st == "") { return (false); }
    l = st.length;
    //alterando para se usuário não digitar os zeros na frente do CPF, completar sozinho
    if ((l == 9) || (l == 8)) {
        for (i = l; i < 10; i++) { st = '0' + st }
    }
    l = st.length;
    st2 = "";
    for (i = 0; i < l; i++) {
        caracter = st.substring(i, i + 1);
        if ((caracter >= '0') && (caracter <= '9')); { st2 = st2 + caracter; }
    }
    if ((st2.length > 11) || (st2.length < 10)) { return (false); }
    if (st2.length == 10)
        st2 = '0' + st2;
    digito1 = st2.substring(9, 10);
    digito2 = st2.substring(10, 11);
    digito1 = parseInt(digito1, 10);
    digito2 = parseInt(digito2, 10);
    sum = 0; mul = 10;
    for (i = 0; i < 9; i++) {
        digit = st2.substring(i, i + 1);
        tproduct = parseInt(digit, 10) * mul;
        sum += tproduct;
        mul--;
    }
    dig1 = (sum % 11);
    if (dig1 == 0 || dig1 == 1)
        dig1 = 0;
    else
        dig1 = 11 - dig1;
    if (dig1 != digito1) { return (false); }
    sum = 0;
    mul = 11;
    for (i = 0; i < 10; i++) {
        digit = st2.substring(i, i + 1);
        tproduct = parseInt(digit, 10) * mul;
        sum += tproduct;
        mul--;
    }
    dig2 = (sum % 11);
    if (dig2 == 0 || dig2 == 1)
        dig2 = 0;
    else
        dig2 = 11 - dig2;
    if (dig2 != digito2) { return (false); }
    return (true);
}



//////////////////////////////////////////////////////////////////
//   valida CNPJ   //////////////////////////////////
//////////////////////////////////////////////////////////////////

function validaCNPJ(field) {
    var i;
    s = limpa_string(field.value);
    var c = s.substr(0, 12);
    var dv = s.substr(12, 2);
    var d1 = 0;
    for (i = 0; i < 12; i++) {
        d1 += c.charAt(11 - i) * (2 + (i % 8));
    }
    if (d1 == 0) {
        //alert("CNPJ Invalido1"); 
        return false;
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) {
        field.value = "";
        alert("CNPJ Invalido");
        field.focus();
        return false;
    }
    d1 *= 2;
    for (i = 0; i < 12; i++) {
        d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8));
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1) {
        field.value = "";
        alert("CNPJ Invalido");
        field.focus();
        return false;
    }
    return true;
}

////////////////////////////////////////////////////////////////////////////
// NOME : validaNumero()
// Descrição : Valida Apenas Números *****Deve ser usado do evento KeyPress
// Parametros
function validaNumero() {
    if (event.keyCode < 45 || event.keyCode > 57)
        event.returnValue = false;
}


////////////////////////////////////////////////////////////////////////////
// NOME : validaAlfanumerico()
// Descrição : Valida Apenas Alfa Númericos *****Deve ser usado do evento KeyPress
// Parametros
///////////////////////////////////////////////////////////////////////////

function validaAlfanumerico() {
    if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97))
        event.returnValue = false;
}


////////////////////////////////////////////////////////////////////////////
// NOME : ignoraEspaco()
// Descrição : Tirar os espaços da String 
// Parametros: string = texto a ser ignorado

function ignoraEspaco(string) {
    var temp = "";
    string = '' + string;
    splitstring = string.split("  ");
    for (i = 0; i < splitstring.length; i++) { temp += splitstring[i]; }
    return temp;
}

///////////////////////////////////////////////////////////////////////////
// NOME       : limpa_string
// Descricao  : retira sinais de pontução do cpf
// Parametros : field -> campo
function limpa_string(S) {
    // Deixa so' os digitos no numero
    var Digitos = "0123456789";
    var temp = "";
    var digito = "";
    for (var i = 0; i < S.length; i++) {
        digito = S.charAt(i);
        if (Digitos.indexOf(digito) >= 0) { temp = temp + digito }
    }
    return temp
}



///////////////////////////////////////////////////////////////////////////
// NOME       : formatarMoeda
// Descricao  : formata valor numérico
// Parametros : fld     -> campo 
//               milSep - > separador de milhar
//               decSep - > separador de decimais
//               e      - > evento


function formatarMoeda(fld, milSep, decSep, 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;  // Enter    
    key = String.fromCharCode(whichCode);  // recebe o valor da chave vinda da chave do código   
    if (strCheck.indexOf(key) == -1) return false;  // Chave não válida   
    len = fld.value.length;
    for (i = 0; i < len; i++)
        if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(fld.value.charAt(i)) != -1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0' + decSep + '0' + aux;
    if (len == 2) fld.value = '0' + decSep + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += milSep;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        fld.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            fld.value += aux2.charAt(i);
        fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}





////////////////////////////////////////////////////////////////////////////
// NOME : formatarDataHora
// Descrição : máscara para data e hora enqto digita
// Parametro : objeto, evento
///////////////////////////////////////////////////////////////////////////
function formatarDataHora(objeto, evento) {
    var keypress = (window.event) ? event.keyCode : evento.which;
    campo = eval(objeto);
    if (campo.value == '00/00/0000 00:00:00') {
        campo.value = ""
    }

    caracteres = '0123456789';
    separacao1 = '/';
    separacao2 = ' ';
    separacao3 = ':';
    conjunto1 = 2;
    conjunto2 = 5;
    conjunto3 = 10;
    conjunto4 = 13;
    conjunto5 = 16;
    if ((caracteres.search(String.fromCharCode(keypress)) != -1) && campo.value.length < (19)) {
        if (campo.value.length == conjunto1)
            campo.value = campo.value + separacao1;
        else if (campo.value.length == conjunto2)
            campo.value = campo.value + separacao1;
        else if (campo.value.length == conjunto3)
            campo.value = campo.value + separacao2;
        else if (campo.value.length == conjunto4)
            campo.value = campo.value + separacao3;
    }
    else
        event.returnValue = false;
}


////////////////////////////////////////////////////////////////////////////
// NOME : formatarData
// Descrição : máscara para data enqto digita
// Parametro : objeto, e
///////////////////////////////////////////////////////////////////////////
function formatarData(objeto, e) {
    var keypress = (window.Event) ? e.which : e.keyCode;
    campo = eval(objeto);
    if (campo.value == '00/00/0000') {
        campo.value = ""
    }

    caracteres = '0123456789';
    separacao1 = '/';
    separacao2 = '';
    conjunto1 = 2;
    conjunto2 = 5;
    conjunto3 = 10;
    if ((caracteres.search(String.fromCharCode(keypress)) != -1) && campo.value.length < (19)) {
        if (campo.value.length == conjunto1)
            campo.value = campo.value + separacao1;
        else if (campo.value.length == conjunto2)
            campo.value = campo.value + separacao1;
        else if (campo.value.length == conjunto3)
            campo.value = campo.value + separacao2;
    }
    else
        e.returnValue = false;
}


////////////////////////////////////////////////////////////////////////////
// NOME : formatarData
// Descrição : valida a data
// Parametro : obj
///////////////////////////////////////////////////////////////////////////
function formatoData(obj) {
    var bissexto = 0;
    var data = obj.value;
    var tam = data.length;
    if (tam == 10 || tam == 16) {
        var dia = data.substr(0, 2)
        var mes = data.substr(3, 2)
        var ano = data.substr(6, 4)

        if ((ano > 1990) || (ano < 2100)) {
            switch (mes) {
                case '01':
                case '03':
                case '05':
                case '07':
                case '08':
                case '10':
                case '12':
                    if (dia <= 31) {
                        return true;
                    }
                    break

                case '04':
                case '06':
                case '09':
                case '11':
                    if (dia <= 30) {
                        return true;
                    }
                    break
                case '02':
                    /* Validando ano Bissexto / fevereiro / dia */
                    if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
                        bissexto = 1;
                    }
                    if ((bissexto == 1) && (dia <= 29)) {
                        return true;
                    }
                    if ((bissexto != 1) && (dia <= 28)) {
                        return true;
                    }
                    break
            }
        }
    }
    if (tam > 0) {
        alert("A Data " + data + " é inválida!");
        obj.value = '';
        obj.focus();
        return false;
    }
    return true;
}


////////////////////////////////////////////////////////////////////////////
// NOME : formatarHora
// Descrição : máscara para hora enqto digita
// Parametro : objeto, evento
///////////////////////////////////////////////////////////////////////////
function formatarHora(objeto, e) {
    var keypress = (window.Event) ? e.which : e.keyCode;
    campo = eval(objeto);
    if (campo.value == '00:00') {
        campo.value = ""
    }

    caracteres = '0123456789';
    separacao1 = ':';
    separacao2 = ' ';
    conjunto1 = 2;
    if ((caracteres.search(String.fromCharCode(keypress)) != -1) && campo.value.length < (19)) {
        if (campo.value.length == conjunto1)
            campo.value = campo.value + separacao1;
    }
    else
        event.returnValue = false;
}


////////////////////////////////////////////////////////////////////////////
// NOME : formatarFone
// Descrição : máscara para fone enqto digita
// Parametro : objeto, e
///////////////////////////////////////////////////////////////////////////
function formatarFone(objeto, e) {
    var keypress = (window.Event) ? e.which : e.keyCode;
    campo = eval(objeto);
    if (campo.value == '(00) 0000-0000') {
        campo.value = ""
    }

    caracteres = '0123456789';
    separacao1 = '(';
    separacao2 = ') ';
    separacao3 = '-';
    conjunto1 = 2;
    conjunto2 = 9;
    if ((caracteres.search(String.fromCharCode(keypress)) != -1) && campo.value.length < (19)) {
        if (campo.value.length == conjunto1)
            campo.value = separacao1 + campo.value + separacao2;
        else if (campo.value.length == conjunto2)
            campo.value = campo.value + separacao3;
    }
    else
        e.returnValue = false;
}

////////////////////////////////////////////////////////////////////////////
// NOME : formatarCEP
// Descrição : máscara para CEP enqto digita
// Parametro : objeto, evento
///////////////////////////////////////////////////////////////////////////
function formatarCEP(objeto, e) {
    var keypress = (window.Event) ? e.which : e.keyCode;
    campo = eval(objeto);
    if (campo.value == '00000-000') {
        campo.value = ""
    }

    caracteres = '0123456789';
    separacao1 = '-';
    conjunto1 = 5;
    if ((caracteres.search(String.fromCharCode(keypress)) != -1) && campo.value.length < (19)) {
        if (campo.value.length == conjunto1)
            campo.value = campo.value + separacao1;
    }
    else
        event.returnValue = false;
}

////////////////////////////////////////////////////////////////////////////
// NOME : formatarCNPJ
// Descrição : máscara para CNPJ enqto digita
// Parametro : objeto, e
///////////////////////////////////////////////////////////////////////////
function formatarCNPJ(objeto, e) {
    var keypress = (window.Event) ? e.which : e.keyCode;
    campo = eval(objeto);
    if (campo.value == '00.000.000/0000-00') {
        campo.value = ""
    }

    caracteres = '0123456789';
    separacao1 = '.';
    separacao2 = '/';
    separacao3 = '-';
    conjunto1 = 2;
    conjunto2 = 6;
    conjunto3 = 10;
    conjunto4 = 15;
    if ((caracteres.search(String.fromCharCode(keypress)) != -1) && campo.value.length < (19)) {
        if (campo.value.length == conjunto1)
            campo.value = campo.value + separacao1;
        else if (campo.value.length == conjunto2)
            campo.value = campo.value + separacao1;
        else if (campo.value.length == conjunto3)
            campo.value = campo.value + separacao2;
        else if (campo.value.length == conjunto4)
            campo.value = campo.value + separacao3;
    }
    else
        e.returnValue = false;
}
