"use strict";

/*===
    unobstrusive-style client-side JavaScript
        for making sure at least two of three textfields
        in a form have values before permitting it to
        be submitted

    by: Sharon Tuttle
    last modified: 2024-04-23
===*/

// once the window is loaded, attach an onsubmit to the form
//    with id="valueForm" (if it exists in the current state!)
//    to make sure it has at least two textfield
//    entries before allowing it to be submitted

window.onload =
    function()
    {
        let valueForm = document.getElementById("valueForm");

        // only try to add to this form if it currently exists!
        //    (in the current PHP response)

        if (valueForm != null)
        {
            valueForm.onsubmit = checkFields;
        }
    };

/*===
    function: checkFields: void -> boolean
    purpose: expects nothing, returns true if the form has two of
        three values entered for textfields with id values of "color",
        "flavor", and "song", and returns false otherwise
===*/

function checkFields()
{
    // get the textfields

    let colorField = document.getElementById("color");
    let flavorField = document.getElementById("flavor");
    let songField = document.getElementById("song");

    let checkResult = false;

    if ( ((colorField.value != "") && (songField.value != ""))
         || ((colorField.value != "") && (flavorField.value != ""))
         || ((flavorField.value != "") && (songField.value != "")) )
    {
        checkResult = true;
    }
    else
    {
        alert("NEED values for at least TWO of these!");
        checkResult = false;
    }

    return checkResult;
}