﻿
/*
    Füllt die Kategorienliste, und erstellt CheckListe Toggle Box
*/
function fillKategorien() {
    if (!document.getElementById("checklisteKategorien").hasChildNodes()) {
        var oCheckbox;
        var oLabel;
        var oSpan;
        var x = 0;

        for (i in liste) {
            x++;
            oSpan = document.createElement("span");
            
            oCheckbox = document.createElement("input");
            oCheckbox.setAttribute("type", "checkbox");
            oCheckbox.setAttribute("onClick", "cbClick(this);");

            if (liste[i][0] == "checked") {
                oCheckbox.setAttribute("checked", "checked");
            }
            
            oCheckbox.setAttribute("name", i);
            oCheckbox.setAttribute("id", i);

            oSpan.appendChild(oCheckbox);

            oLabel = document.createElement("label");
            oLabel.setAttribute("for", i);
            oLabel.innerHTML = i;

            oSpan.appendChild(oLabel);

            document.getElementById("checklisteKategorien").appendChild(oSpan);

            addCheckList(i, liste[i][0]);

            if(x == 3)
            {
                document.getElementById("checklisteKategorien").innerHTML += "<br>"; // .appendChild(document.createElement("br"))
                x = 0;
            }
        }


   
        
        //jQuery Toggle Box
        
        $(document).ready(function () {

            //Hide (Collapse) the toggle containers on load
            //$(".toggle_container").hide();

            //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
            $("h2.trigger").click(function () {
                $(this).toggleClass("active").next().slideToggle("slow");
                return false; //Prevent the browser jump to the link anchor
            });

        });
    }
}



/*
    Erstellt eine checklist toggle box
*/
function addCheckList(sKategorie, checked) {
    var oCheckbox;
    var oLabel;
    var checkListe = liste[sKategorie];
    var sChecked = "";
    var x = 0;

    if (checked != "checked")
        sChecked = "style='display:none;'";

    var sListe =

    "<div " + sChecked + " id='toggle_" + sKategorie + "'> <h2 class='trigger'><a href='#'>" + sKategorie + "</a></h2>" +
    "<div class='toggle_container'>" +
        "<div class='toggleMenu'>" +
            "<input type='checkbox' id='selectAll_" + sKategorie + "' checked='checked' selectparent='" + sKategorie + "' onClick='toggleBoxChecklisteMakierung(this);'/><label for='selectAll_" + sKategorie + "'>Alle markieren / Markierung entfernen</label>" +
        "</div>" +
	    "<div class='block'>" +
            "<div class='toggleCheckList'>";

    for (var i = 1; i < checkListe.length; i++) {
        if (checkListe[i].indexOf("LOGO|") == -1) {
            x++;
            sListe += "<span><input type='checkbox' id='" + checkListe[i] + "' checked='checked' name='" + checkListe[i] + "' parent='" + sKategorie + "' /><label id='lbl_" + checkListe[i] + "' for='" + checkListe[i] + "'>" + checkListe[i] + "</label></span>";

            if (x == 2) {
                x = 0;
                sListe += "<br>";
            }
        }
    }

    sListe += "<div class='ChecklisteBannerContainer'>";
        for (var i = 1; i < checkListe.length; i++) {
            if (checkListe[i].indexOf("LOGO|") >= 0)
                sListe += "<a href='" + checkListe[i].split('|')[2] + "' target='_blank' title='" + checkListe[i].split('|')[3] + "'><img src='ChecklisteBanner/" + checkListe[i].split('|')[1] + "'></img></a>";
        }
                sListe += "</div>" +
                "</div>" +
	    "</div>" +
    "</div> </div>";

    document.getElementById("checklistItems").innerHTML += sListe;
}



/*
    Bei klick auf eine Kategorie
*/
function cbClick(oCheckbox) {

    if (!oCheckbox.checked)
        document.getElementById("toggle_" + oCheckbox.getAttribute("name")).style.display = "none";
    else
        document.getElementById("toggle_" + oCheckbox.getAttribute("name")).style.display = "";
}



/*
    Gibt die CheckListe einer bestimmten Kategorie als Array zurück
*/
function getCheckListByKategorie(sKategorie) {
    var oInputs = document.getElementsByTagName("input");
    var arrCheckList = new Array();
    var x = 0;

    for (var i = 0; i < oInputs.length; i++) {
        if (oInputs[i].getAttribute("parent") == sKategorie) {
            arrCheckList[x] = oInputs[i];
            x++;
        }
    }

    return arrCheckList;
}



/*
    Gibt nur die gecheckten CheckListe einer bestimmten Kategorie als Array zurück
*/
function getCheckedCheckListByKategorie(sKategorie) {
    var oInputs = document.getElementsByTagName("input");
    var arrCheckList = new Array();
    var x = 0;

    for (var i = 0; i < oInputs.length; i++) {
        if (oInputs[i].getAttribute("parent") == sKategorie && oInputs[i].checked) {
            arrCheckList[x] = oInputs[i];
            x++;
        }
    }

    return arrCheckList;
}



/*
    Alle makieren für toggle Box
*/
function toggleBoxChecklisteMakierung(oCheckbox) {
    var oCheckboxListe = getCheckListByKategorie(oCheckbox.getAttribute("selectparent"));

    if (oCheckbox.checked)
        for (var i = 0; i < oCheckboxListe.length; i++)
            oCheckboxListe[i].checked = true;
    else
        for (var i = 0; i < oCheckboxListe.length; i++)
            oCheckboxListe[i].checked = false;

}



/*
    Gibt die checked Kategorien Array zurück
*/
function getCheckedKategorie() {
    var oInputs = document.getElementById("checklisteKategorien").getElementsByTagName("input");
    var arrCheckList = new Array();
    var x = 0;

    for (var i = 0; i < oInputs.length; i++) {
        if (oInputs[i].checked) {
            arrCheckList[x] = oInputs[i];
            x++;
        }
    }

    return arrCheckList;
}



/*
    Druckansicht
*/
function druckAnsicht() {

    var arrKategorien = getCheckedKategorie();
    var sDruckHTML = "<div class='druckcontent'>";
    var arrChecklist;

    for (var i = 0; i < arrKategorien.length; i++) {

        arrChecklist = getCheckedCheckListByKategorie(arrKategorien[i].getAttribute("name"));

        if (arrChecklist.length > 0) {

            sDruckHTML +=
        "<div class='druckansicht_kategorie'>" +
            "<h4>" + arrKategorien[i].getAttribute("name") + "</h4>" +
            "<div class='druckansicht_content'>";

            for (var x = 0; x < arrChecklist.length; x++) {
                if (arrChecklist[x].checked)
                    sDruckHTML += "<span class='druckansicht_item'><span class='checkbox'>[&nbsp;&nbsp;]</span> <span>" + arrChecklist[x].getAttribute("name") + "</span></span>";
            }

            sDruckHTML +=
            "</div>" +
        "</div>";
        }
    }

    document.getElementById("druckansicht").innerHTML = sDruckHTML + "</div><br><input style='margin-left:15px;' value='Drucken' type='button' onclick='window.print();' />";
}


