document.addEventListener("DOMContentLoaded", () => {
    var listak = Array.from(document.getElementsByTagName("select"));
    var dic = {};
    for (var i in adatbazis) {
        for (j in adatbazis[i]) {
            dic[adatbazis[i][j]["megallo"]] = 0;
        }
    }
    for (i in dic) {
        var newop = document.createElement("option");
        newop.innerText = i;
        newop.value = i;
        listak[0].appendChild(newop.cloneNode(true));
        listak[1].appendChild(newop.cloneNode(true));
    }
});

function search(event) {
    event.preventDefault();
    var listak = Array.from(document.getElementsByTagName("select"));
    if (listak[0].value === listak[1].value) {
        alert("A cél, és az indulás megegyeznek!");
        return 0;
    }
    var sections = Array.from(document.getElementsByClassName("tajekoztato"));
    var sectionlist = [];
    sectionlist.push(sections[0]);
    for (i in adatbazis) {
        var indul = null;
        var megall = null;
        for (j in adatbazis[i]) {
            if (adatbazis[i][j]["megallo"] === listak[0].value) {
                indul = adatbazis[i][j];
            }
            if (adatbazis[i][j]["megallo"] === listak[1].value && indul != null) {
                megall = adatbazis[i][j];
            }
        }
        if (indul != null && megall) {
            var train = document.createElement("section");
            var indul = new Date(indul["indul"]);
            train.classList = "idobe";

            let title = document.createElement("h2");
            title.innerText = i;
            train.appendChild(title);

            let pindul = document.createElement("p");
            pindul.innerText = "Indulás: " + indul["indul"];
            train.appendChild(pindul);

            let perkez = document.createElement("p");
            perkez.innerText = "Érkezés: " + megall["erkezik"];
            train.appendChild(perkez);

            sectionlist.push(train);
        }
    }
    console.log(sectionlist)
    var olds = Array.from(document.getElementsByTagName("section"));
    for (var i = 0; i < olds.length; i++) {
        olds[i].remove();
    }
    for (var i = 0; i < sectionlist.length; i++) {
        document.getElementsByTagName("article")[0].appendChild(sectionlist[i]);
    }
}