/* want to use JavaScript strict mode */

"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: 2026-04-30
===*/

// once the current document has finished being loaded --
//    when the window object's load event occurs --
//    attach an onsubmit attribute to the form element
//    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 muck with the form IF that form exists in
	//     the current PHP response! (in its current state)

       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 DOM objects corresponding to these textfields
    
    let colorField = document.getElementById("color");
    let flavorField = document.getElementById("flavor");
    let songField = document.getElementById("song");

    // boolean flag variable! Will be set to true if validation succeeds
    
    let checkResult = false;

    // see if at least two of these textfields are currently non-empty

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

    return checkResult;
}