<!--

/*
constant event.SUBMIT, event.CHANGE //Might be useful for event capturing in terms of identifying the event that called the data validation function.
*/

//BEGIN verifyLetterGradeBoxChange.
// Verify that the Letter Grade box contains either a NULL value or only a letter w/ perhaps + or -.

function changeBoxLettergrade(textObject)  {
    if(textObject.length > 2)   {
        textObject.value = ""   //Empty the incorrectly filled textbox.
        textObject.focus()  //Give text box w/ incorrect information focus.
        alert("The box for your '" + textObject.name + "' contains too many characters.  Only one or two characters are permitted.  Please REPLACE these before going further.");
        return false
    }
    else    {   //Text length is one or two characters (or NULL).
        if (textObject.value != "")   {   //Text value is non-NULL.
            if ((textObject.value.charAt(0)<"A")    ||
                ((textObject.value.charAt(0)>"F") && (textObject.value.charAt(0)<"a"))  ||
                (textObject.value.charAt(0)>"f"))   {   //The text object contains a character that is not a permissible letter grade.
                textObject.value = ""   //Empty the incorrectly filled textbox.
                textObject.focus()  //Give text box w/ incorrect information focus.
                alert("The box for your '" + textObject.name + "' contains a character that is not a permissible letter grade.  Please REPLACE it before going further.");
                return false    //  if any character is not a letter.
            }
            else    {   //First character is acceptable.
                if (textObject.value.length == 1)    {
                    return true
                }
                if ((textObject.value.charAt(1) != "+") &&
                    (textObject.value.charAt(1) != "-"))    {   //The 2nd character in the text object is not acceptable.
                    textObject.value = ""   //Empty the incorrectly filled textbox.
                    textObject.focus()  //Give text box w/ incorrect information focus.
                    alert("The box for your '" + textObject.name + "' contains a character that is not part of a permissible letter grade.  Please REPLACE it before going further.");
                    return false    //  if any character is not a letter.
                }
            }
        }
        return true
    }
}

//END verifyLetterGradeBoxChange.

//BEGIN verifyWeightingsBoxChange.
// THIS SCRIPT handles changes made to Percent Weightings Boxes:

function verifyOnlyNumbers(textObject)   {
// Verify that the Weightings box contains either a NULL values or a value between 0 and 100 inclusive.
    if ((isNaN(textObject.value)) &&
        (textObject.value !=""))   {
        //It's not a number & not null.
        textObject.focus()  //Give text box w/ incorrect information focus.
        alert("The box for '" + textObject.name + "' contains one or more characters that are NOT numbers.  Please REPLACE these.");
        return false
    }
    else    {
        if (textObject.value == "")    {   //Weightings box is NULL and needs converting to zero.
            textObject.value = " 0"
        }
        if ((textObject.value < 0) ||
            (textObject.value > 100))  {
            textObject.focus()  //Give text box w/ incorrect information focus.
            alert("The box for '" + textObject.name + "' contains a numeric value that is outside those permitted.  Please replace it.");
            return false
        }
    }
return true
}

function recalcSumWts(formObject) {
// Update the sum of the weightings.
    formObject.elements[16].value = parseFloat(formObject.elements[1].value) + parseFloat(formObject.elements[3].value) + parseFloat(formObject.elements[5].value) + parseFloat(formObject.elements[7].value) + parseFloat(formObject.elements[9].value) + parseFloat(formObject.elements[11].value) + parseFloat(formObject.elements[13].value) + parseFloat(formObject.elements[15].value)
}

function changeBoxWeight(textObject, formObject)  {
    if(verifyOnlyNumbers(textObject))   {
        recalcSumWts(formObject)
    }
}

//END verifyWeightingsBoxChange.

//BEGIN calculateFinalCourseGrade.

function verifyInputData(formObject)  {

    //Verify if one of a pair is filled, they both are.  I DON'T THINK THIS IS NEEDED.

    //Verify sum of weights satisfactorily close to 100.
    if ((formObject.elements[16].value < 99.999)    ||
        (formObject.elements[16].value > 100.001))   {   //Between 99.999 and 100.001.
        alert("The sum of your letter grade weightings is " + formObject.elements[16].value + ".  This is not close enough to the REQUIRED value of 100 +/- .001.  Please make appropriate changes.");
        return false
    }
    return true
}

function courseGradeProjector(formObject)    {

    if (!(verifyInputData(formObject)))  {
        return false
    }

    // Create & initialize arrays for LG's, NE's, & NE final cut-off's & other variables.
        var upLetterGrade = new Array ("F-","F","F+","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+");
        var loLetterGrade = new Array ("f-","f","f+","d-","d","d+","c-","c","c+","b-","b","b+","a-","a","a+");
        numericEquiv = new Array(15);
        gradeCutoff = new Array(15);
        var sumProducts = 0, sumWeights = 0, projFinalCourseNumericalAverage = 0

        for (var i=0; i<15; i++)    {
            numericEquiv[i] = i;
            gradeCutoff[i] = i - .5
        }

    // Do calculation of average grade & display result.
        for (var i=0; i<10; i=i+2)  {   //Do 5 times.
            for (var j=0; j<15; j++)    {   //Do 15 times.
                if ((formObject.elements[i].value == upLetterGrade[j]) ||
                    (formObject.elements[i].value == loLetterGrade[j]))   {   //Letter grade boxes having NULL values are skipped over.
                      sumProducts += (numericEquiv[j] * parseFloat(formObject.elements[i+1].value));//Sum of NE times Weighting.
                      sumWeights += parseFloat(formObject.elements[i+1].value);
                    break   //out of j loop; continue i loop.
                }
            }
        }
        projFinalCourseNumericalAverage = sumProducts/sumWeights
        formObject.elements[19].value = projFinalCourseNumericalAverage  //Assign numeric average to appropriate form text box.

    // Match Numeric Average to a Final Letter Grade & display result:
        formObject.elements[20].value = "F"    //Initialize w/ default value.
        for (var j=0; j<15; j++)    {
            if (projFinalCourseNumericalAverage >= gradeCutoff[j])  {
                formObject.elements[20].value = upLetterGrade[j];
            }
            else    {
                break   //out of j loop.
            }
        }

    return true

}
//END calculateFinalCourseGrade.

// -->