//wait for html site to be ready before executing init()
if (document.readyState == "complete") {
    init();
} else {
    document.addEventListener("DOMContentLoaded", init);
}

function init() {
    if(document.getElementById('table') != null && document.getElementsByClassName('edit')[0] != null) {
        highlight_row();
    }
    
    user_edit_button();
    statistic_edit_button();
    station_edit_button();
    team_edit_button();
    team_delete_button();
    station_delete_button();
    statistic_delete_button();
    user_delete_button();
}

var selected = null;

function highlight_row() {
    var table = document.getElementById('table');
    var cells = table.getElementsByTagName('td');
    
    for( var i = 0; i < cells.length; i++) {
        var cell = cells[i];
        cell.onclick = function() {
            var buttons = document.getElementsByClassName('edit');
            var rowId = this.parentNode.rowIndex;
            var rowsNotSelected = table.getElementsByTagName('tr');
            for ( var row = 0; row < rowsNotSelected.length; row++) {
                rowsNotSelected[row].style.backgroundColor = "";
                rowsNotSelected[row].classList.remove('selected');
            }
            var rowSelected = table.getElementsByTagName('tr')[rowId];
            if (selected != null && selected == rowId) {
                selected = null;
                rowSelected.style.backgroundColor = "";
                rowSelected.classList.remove('selected');
                Array.prototype.forEach.call(buttons, function(button) {
                    button.disabled = true;
                    button.className += " disabled";
                })
            } else {
                selected = rowId;
                rowSelected.style.backgroundColor = "yellow";
                rowSelected.className += " selected";
                Array.prototype.forEach.call(buttons, function(button) {
                    button.disabled = false;
                    button.classList.remove('disabled');
                })
            }
        }
    }
}

function user_edit_button() {
    var button = document.getElementById("edit_user");
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName("selected")[0];
            document.getElementById('user_id_val').value = row.id;
            this.form.submit();
        }
    }
}

function statistic_edit_button() {
    var button = document.getElementById('edit_statistic');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName('row selected')[0];
            document.getElementById('team_id').value = row.id;
            this.form.submit();
        }
    }
}

function station_edit_button() {
    var button = document.getElementById('edit_station');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName("selected")[0];
            document.getElementById('s_id').value = row.id;
            this.form.submit();
        }
    }
}

function team_edit_button() {
    var button = document.getElementById('edit_team');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName('selected')[0];
            document.getElementById('m_id').value = row.id;
            this.form.submit();
        }
    }
}

function team_delete_button() {
    var button = document.getElementById('delete_team');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName('selected')[0];
            document.getElementById('m_id_delete').value = row.id;
            let confirmAction = confirm("Wirklich löschen?");
            if (confirmAction) {
                this.form.submit();
            }
        }
    }
}

function station_delete_button() {
    var button = document.getElementById('delete_station');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName('selected')[0];
            document.getElementById('s_id_delete').value = row.id;
            let confirmAction = confirm("Wirklich löschen?");
            if (confirmAction) {
                this.form.submit();
            }
        }
    }
}

function statistic_delete_button() {
    var button = document.getElementById('delete_statistic');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName('row selected')[0];
            document.getElementById('m_id_delete').value = row.id;
            let confirmAction = confirm("Wirklich löschen?");
            if (confirmAction) {
                this.form.submit();
            }
        }
    }
}

function user_delete_button() {
    var button = document.getElementById('delete_user');
    if(button != null) {
        button.onclick = function() {
            var row = document.getElementsByClassName('selected')[0];
            document.getElementById('id_delete').value = row.id;
            let confirmAction = confirm("Wirklich löschen?");
            if (confirmAction) {
                this.form.submit();
            }
        }
    }
}