//paises js
//NAO APAGAR A LINHA DE COMENTARIO DE CIMA !!!

$(document).ready(function () {

    initPaises();
    extendLinkToTR(".listTable", ".header");


// When the user scrolls down 20px from the top of the document, show the button
    window.onscroll = function() {scrollFunction()};

    $(".grafico_pt.togglNext").click(function () {
        let graph = $(this).next();
        if (graph.is(":visible"))
            $(this).children("div").removeClass("upArrow");
        else
            $(this).children("div").addClass("upArrow");
        graph.toggle();
        graph[0].scrollIntoView(true);
    });


    $(".quadro_title_areas.togglNext").click(function () {
        var attr = $(this).attr("id");
        let $1 = $("#d" + attr);

        if ($(this).find("div").hasClass("downArrow"))
            $(this).find("div").removeClass("downArrow").addClass("upArrow");
        else
            $(this).find("div").addClass("downArrow").removeClass("upArrow");
        $1.toggle("slow");
    });

    $("#catBody .entry a").each(function () {
        let where = $(this).attr("href");
        $(this).parents(".entry").bind("click", function () {
            go(where);
        });
    });

    $.each($(".listTable"), function () {
        if ($("tr td a", this).length === 0) {
            $("tr,td", this).css("cursor", "auto");
        }
        $("tr td", this).bind("mouseover", function () {
            $(this).css("whiteSpace", "normal");
        });
        $("tr td", this).bind("mouseout", function () {
            $(this).css("whiteSpace", "noWrap");
        });
    });


});

function addPais() {

    var boxName = "meta_paises";
    var box = $("input[name=" + boxName + "]");
    var boxValue = $(box).val();
    var country = $("#country");
    var val = $(country).val();

    if (boxValue.indexOf(val) === -1) {
        boxValue = boxValue + " " + val;
        boxValue.replace("  ", " ");
        $(box).val(boxValue);

        refreshCountries();
    }

}


function scrollFunction() {
    if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
        $("#myBtn").show();
    } else {
        $("#myBtn").hide();
    }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
    document.body.scrollTop = 0; // For Safari
    document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

function removeCountry(code) {

    var boxName = "meta_paises";
    var box = $("input[name=" + boxName + "]");
    var boxValue = $(box).val();

    if (boxValue && boxValue.indexOf(code) !== -1) {
        boxValue = boxValue.replace(code, "");
        boxValue.replace("  ", " ");
        $(box).val(boxValue);

        refreshCountries();
    }
}

function refreshCountries() {

    var boxName = "meta_paises";

    var box = $("input[name=" + boxName + "]");

    var boxValue = $(box).val();

    if (box.length == 0)
        return;

    //list current countries with delete buttons
    var divCurrent = $("#divCurrent");

    $("#divCurrent").load("./plugs/Administracao/listInsertedCountries.jsp #list", {value: boxValue});

}

function initPaises() {

    var boxName = "meta_paises";

    var box = $("input[name=" + boxName + "]");

    var boxValue = $(box).val();

    if (box.length == 0)
        return;


    // turn the text box into a hidden box
    var newBox = document.createElement("input");

    $(newBox).attr("type", "hidden");

    $(newBox).attr("name", boxName);

    $(newBox).attr("value", boxValue);

    $(box.after(newBox));

    $(box).remove();


    //a separator
    var sep = document.createElement("br");

    $(newBox).after(sep);

    var lastElement = sep;

    sep = document.createElement("br");

    $(lastElement).after(sep);

    lastElement = sep;

    //list current countries with delete buttons
    var divCurrent = document.createElement("div");
    $(divCurrent).attr("id", "divCurrent");
    $(lastElement).after(divCurrent);


    refreshCountries();

    lastElement = divCurrent;

    sep = document.createElement("br");

    $(lastElement).after(sep);

    lastElement = sep;


    //get the select list of countries
    var selDiv = document.createElement("div");

    $(selDiv).load("./plugs/Administracao/selectCountries.jsp #divSelect");

    $(lastElement).after(selDiv);

    lastElement = selDiv;


}

function extendLinkToTR(element, elementToFilter) {

    $("tr", element).not($("tr:has(" + elementToFilter + ")", element)).bind("click", function () {
        if (this.getElementsByTagName("A") && this.getElementsByTagName("A").length) {
            document.location.href = this.getElementsByTagName("A")[0].href;
            return false;
        }
        return true;
    });
}

function go(where) {
    document.location.href = where;
}


function nova(url) {
    window.open(url);
}


function delEle(idx) {
    $(idx).closest("tr").remove();
}


function addEle(ele) {

    var clas = $("." + $(ele).siblings("select").attr("class"));
    var paises = $(clas).last();
    var num = $(clas).length - 1;
    var paisTR = $(paises).closest("tr");
    var paisTrNovo = $(paisTR).clone();
    var select = $(paisTrNovo).find("select");
    var ipt = $(paisTrNovo).find("input");

    $(paisTrNovo).removeAttr("id").addClass($(ele).closest("tr").attr("class"));
    $(select).attr("id", $(paisTR).find("select").attr("id") + num);
    $(select).attr("name", $(paisTR).find("select").attr("id") + num);
    $(paisTrNovo).show();
    $(paisTrNovo).insertAfter($("." + $(ele).closest("tr").attr("class")).last());
}


function addObj(obj) {

    var origin = $("#" + obj);
    var clone = $(origin).clone();
    var clas = $(origin).attr("class").split(" ")[0];
    var length = $("." + clas).length;

    $(clone).find("input[type=text]").each(function () {
        $(this).attr({
            id: $(this).attr("id") + length,
            name: $(this).attr("name") + length
        });
        $(this).val($(origin).find("input[type=text]").val());
        $(origin).find("input[type=text]").val("");

    });

    $(clone).find("select").each(function () {
        $(this).attr({
            id: $(this).attr("id") + length,
            name: $(this).attr("name") + length
        });
        $(this).val($(origin).find("select option:selected").val());
        $(origin).find("select").val("");
    });

    $(clone).find("input[type=hidden]").each(function () {
        $(this).attr({
            id: $(this).attr("id") + length,
            name: $(this).attr("name") + length
        });
    });
    $(this).val($(origin).find("input[type=hidden]").val());
    $(origin).find("input[type=hidden]").val("");


    $(clone).attr("id", $(clone).attr("id") + length);

    var btn = $(clone).find("input[type=button]");

    $(btn).val("-");
    $(btn).attr({
        id: "delEle",
        name: "delEle"
    });
    $(btn).addClass("minusBtn");
    $(btn).removeAttr("onclick");
    $(btn).bind("click", function () {
        delObj($(btn));
    });

    $(origin).find("td:first-child").html("");

    $(clone).insertBefore($(origin));
}

function numberFormat(num) {
    return (
        num
            .toFixed(0) // always two decimal digits
            .replace('.', ',') // replace decimal point character with ,
            .replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
    ) // use . as a separator
}

