var leave = 0;
document.addEventListener("DOMContentLoaded", ()=>{
    var table = document.getElementById("maintable");
    var row = document.createElement("tr");
    var cell = document.createElement("th");
    row.appendChild(cell);
    for(i = 0; i < 31; i++){
        cell = document.createElement("th");
        cell.innerText = `${i+1}`
        row.appendChild(cell);
    }
    table.appendChild(row);
    var weekdaycounter = 1;
    for(var i = 1; i <= 12; i++){
        row = document.createElement("tr");
        var first = document.createElement("th");
        first.innerText = `${i}`;
        row.appendChild(first);
        for(var j = 0; j < 31; j++){
            var innercell = document.createElement("td");
            if (([2, 4, 6, 9, 11].includes(i) && j == 30) || (i == 2 && j > 27)) {
                innercell.classList.add("inval");
            }
            else{
                weekdaycounter += 1;
            }
            
            if(weekdaycounter > 5){
                innercell.classList.add("weekend");
            }

            if(weekdaycounter == 7){
                weekdaycounter = 0;
            }
            if(innercell.classList.length == 0){
                innercell.addEventListener("click", function () {
                    if (this.classList.contains("away")) {
                        this.classList.remove("away");
                        leave -= 1;
                    } else {
                        this.classList.add("away");
                        leave += 1;
                    }
                    document.getElementById("leaves").innerText = `Kivett szabadságok összesen: ${leave} nap.`
                });
            }
            row.appendChild(innercell);            
        }
        table.appendChild(row);
    }
});