﻿var docform = document.IFGForm;
var bInitialRunThrough = false;

//
//	Generic functions for all sections
//
function requiredText(thisItem, sTextFields, sMessage) {
    arFields = sTextFields.split(",");
    bValid = true;
    for (field in arFields) {
        itemField = eval("docform." + arFields[field]);
        if (itemField.value.length == 0) {
            bValid = false;
        }
    }

    if (sMessage.length == 0) {
        sMessage = "You must fill out the required fields for this item before you can check it";
    }

    if (thisItem.checked) {
        if (!bValid) {
            alert(sMessage);
            thisItem.checked = false;
        }
    } else {
        if (bValid) {
            alert("If you turn this off, any data you have in the required fields will be ignored");
        }
    }
}

function checkFinalize() {
    bContinue = confirm("By clicking the “Finalize and Submit” button assurance is being made that the program information contained in this application is accurate and can be documented for audit purposes. Misrepresentation of the program will result in ineligibility for funding.\n\n" +
						"Be sure you print a copy of the finalized version for your records.\n\n" +
						"This will save your latest changes and mark the application complete.  You will not be able to make changes to the application after this.  Do you want to continue?\n\n");
    if (!bContinue) {
        alert("Be sure to click the save button to save your changes and come back later to finalize your application");
    }
    return bContinue;
}

//	Set a pair of objects to be mutually exclusive
function mutualExclusion(thisItem, sPartnerName) {
    if (thisItem.checked) {
        ePartner = eval("docform." + sPartnerName);
        ePartner.checked = false;
    }
}

function checkMinValue(thisItem, nMinValue) {
    if (thisItem.value.length > 0 && thisItem.value < nMinValue) {
        alert("You entered " + thisItem.value + " for this field.  You must enter a value no less than " + nMinValue + " to get credit");
        thisItem.value = "";
        return false;
    } else if (thisItem.value.length == 0) {
        return false;
    }
    return true;
}

function calcParentValue(nMaxValue, thisItem, sParentName, sChildren, nMinValue) {
    bParentChecked = false;
    nParentXValue = 0;
    arChildOptions = sChildren.split(",");
    for (element in arChildOptions) {
        eItem = eval("docform." + arChildOptions[element]);
        bParentChecked = bParentChecked || eItem.checked;
        nParentXValue = nParentXValue + eItem.checked;
    }
    if (nParentXValue > nMaxValue) {
        nParentXValue = nMaxValue;
    }
    if (nMinValue != null) {
        if (nParentXValue > 0 && nParentXValue < nMinValue) {
            nParentXValue = nMinValue;
        }
    }

    if (document.getElementById(sParentName + "XCell") != null) {
        if (nMinValue == null) {
            if (nParentXValue == 0 && nMaxValue != 1) {
                document.getElementById(sParentName + "XCell").innerHTML = "0-" + nMaxValue + "X";
            } else {
                document.getElementById(sParentName + "XCell").innerHTML = nParentXValue + "X";
            }
        } else {

            if (nParentXValue > 0 && nParentXValue < nMinValue) {
                document.getElementById(sParentName + "XCell").innerHTML = nMinValue + "X";
            }
        }
    }
    eParent = eval("docform." + sParentName);
    if (sParentName == thisItem.name) {
        alert("To change this item's values, alter the sub options below it.");
    }
    eParent.checked = bParentChecked;
    eParent.value = nParentXValue;
}

// Calculate the adjusted X value
function calcSectionValue(thisItem) {
    sSection = thisItem.name;
    sSection = sSection.substring(0, 1);

    changeSection(sSection);
    bInitialRunThrough = true;
}

function changeSection(sSection) {
    eSectionTotal = document.getElementById("Section" + sSection + "XPts")
    eSectionSummaryTotal = document.getElementById("SectionSummary" + sSection)
    eSummaryTotal = document.getElementById("SummaryTotal")
    nSectionTotal = parseInt(eval("docform.presetX" + sSection + ".value"));
    nCurrentSummaryTotal = parseInt(eSummaryTotal.innerHTML);

    if (checkMinimums(sSection)) {
        if (sSection == "A") {
            sChildren = "A4a,A4b,A4c,A4d,A4e,A7c,A7d,A10";
        } else if (sSection == "B") {
            sChildren = "B2,B3,B4,B5";
        } else if (sSection == "C") {
            sChildren = "C2,C3a,C3b,C4,C4b,C5c,C5d,C6,C9a,C9b";
        } else if (sSection == "D") {
            sChildren = "D2,D2b,D3,D4,D5a,D5b,D6";
        } else if (sSection == "E") {
            sChildren = "E3,E4,E5b,E7,E8";
        } else if (sSection == "F") {
            sChildren = "F2,F3,F4,F5,F6,F7,F8";
        } else if (sSection == "G") {
            sChildren = "G2a,G2b,G3a,G3b,G3c,G3d,G3e,G3f,G3g,G4";
        } else if (sSection == "H") {
            sChildren = "H2,H3a,H3b,H4a,H4b,H5";
        }
        arChildOptions = sChildren.split(",");
        //	arChildValues = sChildValues.split(",");
        for (element in arChildOptions) {
            eItem = eval("docform." + arChildOptions[element]);
            if (typeof (eItem) != "undefined" && eItem != null) {
                nSectionTotal = nSectionTotal + (eItem.checked * eItem.value);
                //		alert(eItem.checked + ":" + arChildValues[element]);
            }
        }
    } else {
        nSectionTotal = 0;
    }
    nCurrentSummaryTotal = nCurrentSummaryTotal - parseInt(eSectionSummaryTotal.innerHTML) + nSectionTotal;
    eSectionTotal.innerHTML = nSectionTotal;
    eSectionSummaryTotal.innerHTML = nSectionTotal;
    eSummaryTotal.innerHTML = nCurrentSummaryTotal;
}


//
// Checks the minimum requirements boxes.  If they aren't checked, no points for that section
//
function checkMinimums(sSection) {
    bMinChecked = true;
    if (sSection == "A") {
        sChildren = "A1,A2";
    } else if (sSection == "B") {
        sChildren = "B1";
    } else if (sSection == "C") {
        sChildren = "C1";
    } else if (sSection == "D") {
        sChildren = "D1";
    } else if (sSection == "E") {
        sChildren = "E1";
    } else if (sSection == "F") {
        sChildren = "F1";
    } else if (sSection == "G") {
        sChildren = "G1";
    } else if (sSection == "H") {
        sChildren = "H1";
    }
    arChildOptions = sChildren.split(",");
    for (minimum in arChildOptions) {
        eMin = eval("docform." + arChildOptions[minimum]);
        if (typeof (eMin) != "undefined" && eMin != null) {
            if (!eMin.checked) {
                bMinChecked = false;
            }
        } else {
            eMin2 = eval("docform." + arChildOptions[minimum] + "_static");
            if (eMin2.value == "False") {
                bMinChecked = false;
            }
        }
    }
    if (!bMinChecked) {
        if (bInitialRunThrough) {
            alert("You have not met the minimums for section " + sSection + " and will not receive any points for this section.");
        }
        document.getElementById("MinWarning" + sSection).style.display = "block";
        document.getElementById("Section" + sSection + "XPtsLabel").className = "WarningHighlitedCell";
    } else {
        document.getElementById("MinWarning" + sSection).style.display = "none";
        document.getElementById("Section" + sSection + "XPtsLabel").className = "HighlitedCell";
    }
    return bMinChecked;
}

function showHelp(sItem, sDist, bSampleApp) {
    tWindow = window.open("/linkpages/ifga/IFGAHelpItem.asp?itemName=" + sItem + "&Section=" + sDist + "&Sample=" + bSampleApp, "Help", "width=300, height=300, scrollbars=yes");
}

//
//	Section A fucntions
//
function A4Click(thisItem) {
    if (thisItem.checked) {
        arA4Options = Array("A4a", "A4b", "A4c", "A4d", "A4e");
        for (element in arA4Options) {
            eItem = eval("docform." + arA4Options[element]);
            if (eItem.checked && eItem.name != thisItem.name) {
                eItem.checked = false;
            }
        }
    }
}

//
//	Section C functions
//
function C3calcParentTrigger(thisItem, sChildren) {
    bParentChecked = false;
    arChildOptions = sChildren.split(",");
    for (element in arChildOptions) {
        eItem = eval("docform." + arChildOptions[element]);
        if (eItem) {
            bParentChecked = bParentChecked || eItem.checked;
        }
    }
    return bParentChecked;
}

function C3checkChildren(thisItem) {
    if (thisItem.checked) {
        if (C3calcParentTrigger(thisItem, 'C3a2,C3b2,C3c2,C3d2,C3e2,C3f2,C3g2,C3h2,C3i2,C3j2,C3k2')) {
            // Do nothing
            alert('True');
            thisItem.checked = true;
        } else {
            alert("You must choose at least one curriculum before you can choose this option");
            thisItem.checked = false;
        }
    } else {
        if (C3calcParentTrigger(thisItem, 'C3a2,C3b2,C3c2,C3d2,C3e2,C3f2,C3g2,C3h2,C3i2,C3j2,C3k2')) {
            // Do nothing
            alert('To uncheck this item, uncheck each curriculum below.');
            thisItem.checked = true;
        }
    }
}

function C3checkParent(thisItem) {
    //	eParent1 = docform.C3a;
    eParent2 = docform.C3b;
    if (!thisItem.checked) {
        if (C3calcParentTrigger(thisItem, 'C3a2,C3b2,C3c2,C3d2,C3e2,C3f2,C3g2,C3h2,C3i2,C3j2,C3k2')) {
            // Do nothing
        } else {
            //			if ( eParent1.checked || eParent2.checked ) {
            if (eParent2.checked) {
                alert("You must choose at least one curriculum before you can choose a course selection.");
                //				eParent1.checked = false;
                eParent2.checked = false;
            }
        }
    } else {
        eParent2.checked = true;
    }
}

//
//	Section E Functions
//
function calcE8ParentValue(nMaxValue, thisItem, sParentName, sChildren, nPreloadValue) {
    if (nPreloadValue > 0) {
        bParentChecked = true;
    } else {
        bParentChecked = false;
    }
    nParentXValue = nPreloadValue;
    arChildOptions = sChildren.split(",");
    for (element in arChildOptions) {
        eItem = eval("docform." + arChildOptions[element]);
        bParentChecked = bParentChecked || eItem.checked;
        nParentXValue = nParentXValue + eItem.checked;
    }
    if (nParentXValue > nMaxValue) {
        nParentXValue = nMaxValue;
    }
    eParent = eval("docform." + sParentName);
    eParent.checked = bParentChecked;
    eParent.value = nParentXValue;

    if (document.getElementById(sParentName + "XCell") != null) {
        if (nParentXValue == 0 && nMaxValue != 1) {
            document.getElementById(sParentName + "XCell").innerHTML = "1-" + nMaxValue + "X";
        } else {
            document.getElementById(sParentName + "XCell").innerHTML = nParentXValue + "X";
        }
    }
}