function clearInput() {
    $('#download_field').val(''); // Clear the input field
}
        
// Handle the form submission with AJAX
$(document).ready(function () {
    $('#dropout_download').submit(function (event) {
        event.preventDefault(); // Prevent the default form submission
        alert("Episode(s) now downloading");

        $.ajax({
            type: 'POST', // Use POST method to send data
            url: '/dropoutDownloader', // Target URL for the CherryPy handler
            data: $('#dropout_download').serializeArray(), // Serialize the form data
            success: function (data) {
                $('#downloadResult').html(data); // Display the result in the specified div
                alert("File has finished downloading");
            }
        });
    });
});

let showsData = [];

// Load JSON data from the file
fetch('dropout.json')
    .then(response => response.json())
    .then(data => {
        showsData = data;
        populateShows();
    });

function populateShows() {
    const showSelect = document.getElementById("download_show");
    showsData.forEach(show => {
        const option = document.createElement("option");
        option.value = show.SHOW;
        option.text = show.SHOW;
        showSelect.appendChild(option);
    });
}

function updateSeasons() {
    const showSelect = document.getElementById("download_show");
    const seasonSelect = document.getElementById("download_season");
    const selectedShow = showSelect.value;

    // Clear existing options
    seasonSelect.innerHTML = '';

    // Find the selected show and update seasons
    const show = showsData.find(s => s.SHOW === selectedShow);
    if (show) {
        show.SEASONS.forEach(season => {
            const option = document.createElement("option");
            option.value = season;
            option.text = "Season " + season;
            seasonSelect.appendChild(option);
        });
    }
}