﻿$(document).ready(function() {

    /*
    * Custom function for removing table COLUMNS
    * http://jquery-howto.blogspot.com/2009/05/remove-nth-table-column-jquery-plugin.html
    */
    $.fn.removeCol = function(col) {
        // Make sure col has value
        if (!col) { col = 1; }
        $('tr td:nth-child(' + col + '), tr th:nth-child(' + col + ')', this).remove();
    };

    // Hide Panels
    $("#pnlActiveTime").hide();
    $("#pnlSchoolTerms").hide();
    $("#pnlTermCredits").hide();
    $("#pnlTuition").hide();
    $("#pnlResults").hide();

    // 3-5th Credit Amount Blocks Start Out Hidden //
    hideTermCreditBoxes();
    // End Credit Amount Block Hides //


    $("#btnState").click(function(e) {
        try {
            if (($("#ddlCity").val()).length > 0) {
                $("#pnlActiveTime").fadeIn(2000);
                $("#ddlActiveTime").focus();
                $("#btnState").hide();
            }
            else {
                $("#pnlLocation").effect("shake", { times: 1 }, 150);
            }
        } catch (err) { alert(err); }
        e.preventDefault();
    });

    /*
    * An event function that triggers when the
    * user clicks on the 'Next' button in the 
    * 'Active Time' Panel.
    */
    $("#btnActiveTime").click(function(e) {
        $("#pnlSchoolTerms").fadeIn(2000);
        $("#ddlSchoolTerms").focus();
        $("#btnActiveTime").hide();
        e.preventDefault();
    });

    /*
    * An event function that triggers when the
    * user clicks on the 'Next' button in the 
    * 'School Terms' Panel.
    */
    $("#btnSchoolTerms").click(function(e) {
        var numTerms = $("#ddlSchoolTerms").val();
        var num = numTerms[0];
        hideTermCreditBoxes();
        for (var i = 0; i < num; i++) {
            if (i == 0) { $("#txtTermOne").show(); $("#lblTermOne").show(); }
            else if (i == 1) { $("#txtTermTwo").show(); $("#lblTermTwo").show(); }
            else if (i == 2) { $("#txtTermThree").show(); $("#lblTermThree").show(); }
            else if (i == 3) { $("#txtTermFour").show(); $("#lblTermFour").show(); }
            else if (i == 4) { $("#txtTermFive").show(); $("#lblTermFive").show(); }
        }
        $("#pnlTermCredits").fadeIn(2000);
        $("#txtTermOne").focus();
        $("#btnSchoolTerms").hide();
        e.preventDefault();
    });

    /*
    * An event function that triggers when the
    * user clicks on the 'Next' button in the 
    * 'Term Credits' Panel.
    */
    $("#btnTermCredits").click(function(e) {
        var numTerms = $("#ddlSchoolTerms").val();
        var num = numTerms[0];
        var failed = false;
        if (num >= 1 && $("#txtTermOne").val().length < 1) { failed = true; }
        if (num >= 2 && $("#txtTermTwo").val().length < 1) { failed = true; }
        if (num >= 3 && $("#txtTermThree").val().length < 1) { failed = true; }
        if (num >= 4 && $("#txtTermFour").val().length < 1) { failed = true; }
        if (num >= 5 && $("#txtTermFive").val().length < 1) { failed = true; }

        if (failed) {
            $("#pnlTermCredits").effect("shake", { times: 1 }, 150);
        }
        else {
            $("#pnlTuition").fadeIn(2000);
            $("#txtTuition").focus();
            $("#btnTermCredits").hide();
        }
        e.preventDefault();
    });

    /*
    * An event function that triggers when the
    * user clicks on the 'Next' button in the 
    * 'Tuition and Fees' Panel.
    */
    $("#btnTuition").click(function(e) {
        if ($("#txtTuition").val().length < 1 || $("#txtFees").val().length < 1) {
            $("#pnlTuition").effect("shake", { times: 1 }, 150);
        }
        else {
            $("#pnlResults").fadeIn(2000);
            $("#btnTuition").hide();
            calculateBenefits();
        }
        e.preventDefault();
    });

    $("#recalcButton").click(function(e) {
        e.preventDefault();
        calculateBenefits();
    });

    $("#resetButton").click(function(e) {
        e.preventDefault();
        location.reload();
    });

    function hideTermCreditBoxes() {
        $("#txtTermThree").hide();
        $("#txtTermFour").hide();
        $("#txtTermFive").hide();
        $("#lblTermThree").hide();
        $("#lblTermFour").hide();
        $("#lblTermFive").hide();
    }

    function adjustMainPanel() {
        var total = $("#pnlLoading:visible").height() + $("#pnlLocation:visible").height() + $("#pnlActiveTime:visible").height() + $("#pnlSchoolTerms:visible").height() + $("#pnlTermCredits:visible").height() + $("#pnlTuition:visible").height() + $("#pnlResults:visible").height() + $("#pnlCopyright:visible").height() + 21;
        $("#pnlMain").height(total);
    }

    function calculateBenefits() {

        // Gather all information
        var temp = $("#ddlCity").val();
        var parts = temp.split("|");
        var _allowance = parseInt(parts[2]); //$("#ddlCity").val();
        var _tuitionPerCredit = parseInt(parts[0]);
        var _feesCap = parseInt(parts[1]);
        var _benefitPercentage = $("#ddlActiveTime").val();
        var _termQuantity = parseInt($("#ddlSchoolTerms").val().substr(0, 1));
        var _creditsTermOne = parseInt($("#txtTermOne").val());
        var _creditsTermTwo = parseInt($("#txtTermTwo").val());
        var _creditsTermThree = parseInt($("#txtTermThree").val());
        var _creditsTermFour = parseInt($("#txtTermFour").val());
        var _creditsTermFive = parseInt($("#txtTermFive").val());
        var _tuition = parseInt($("#txtTuition").val());
        var _fees = parseInt($("#txtFees").val());


        // The following variables are inputs that are not yet
        // in the calculator GUI, and are assumed for functionality.
        var _fullTimeCredits = 12;
        var _allClassesOnline = false;
        var _veteranIsAD = false;

        /* Figure Tuition/Fee Benefits */
        var remainingTuition = _tuition;
        var maxMonthlyFee = _fees / _termQuantity;
        if (maxMonthlyFee > _feesCap)
            maxMonthlyFee = _feesCap;
        var tuitionsTerm1 = 0;
        var tuitionsTerm2 = 0;
        var tuitionsTerm3 = 0;
        var tuitionsTerm4 = 0;
        var tuitionsTerm5 = 0;
        var _tuitionTermCap = 0;

        /* First Term Calculations */
        tuitionsTerm1 = ((_creditsTermOne * _tuitionPerCredit > remainingTuition) ? remainingTuition : _creditsTermOne * _tuitionPerCredit);

        /* Second Term Calculations */
        if (_termQuantity > 1) {
            remainingTuition = (remainingTuition - tuitionsTerm1);
            tuitionsTerm2 = ((_creditsTermTwo * _tuitionPerCredit > remainingTuition) ? remainingTuition : _creditsTermTwo * _tuitionPerCredit);
        }

        /* Third Term Calculations */
        if (_termQuantity > 2) {
            remainingTuition = (remainingTuition - tuitionsTerm2);
            tuitionsTerm3 = ((_creditsTermThree * _tuitionPerCredit > remainingTuition) ? remainingTuition : _creditsTermThree * _tuitionPerCredit);
        }

        /* Fourth Term Calculations */
        if (_termQuantity > 3) {
            remainingTuition = (remainingTuition - tuitionsTerm3);
            tuitionsTerm4 = ((_creditsTermFour * _tuitionPerCredit > remainingTuition) ? remainingTuition : _creditsTermFour * _tuitionPerCredit);
        }

        /* Fifth Term Calculations */
        if (_termQuantity > 4) {
            remainingTuition = (remainingTuition - tuitionsTerm4);
            tuitionsTerm5 = ((_creditsTermFive * _tuitionPerCredit > remainingTuition) ? remainingTuition : _creditsTermFive * _tuitionPerCredit);
        }
        tuitionsTerm1 += maxMonthlyFee;
        tuitionsTerm2 += ((_termQuantity > 1) ? maxMonthlyFee : 0);
        tuitionsTerm3 += ((_termQuantity > 2) ? maxMonthlyFee : 0);
        tuitionsTerm4 += ((_termQuantity > 3) ? maxMonthlyFee : 0);
        tuitionsTerm5 += ((_termQuantity > 4) ? maxMonthlyFee : 0);

        // Figure Living Allowance
        var allowanceTerm1 = 0;
        var allowanceTerm2 = 0;
        var allowanceTerm3 = 0;
        var allowanceTerm4 = 0;
        var allowanceTerm5 = 0;

        /* Max if > "Half Time"
        * None if <= "Half Time"
        */
        if (_creditsTermOne >= (Math.round(_fullTimeCredits / 2))) {
            allowanceTerm1 = _allowance;
        }

        if (_termQuantity > 1 && _creditsTermTwo >= (Math.round(_fullTimeCredits / 2))) {
            allowanceTerm2 = _allowance;
        }

        if (_termQuantity > 2 && _creditsTermThree >= (Math.round(_fullTimeCredits / 2))) {
            allowanceTerm3 = _allowance;
        }

        if (_termQuantity > 3 && _creditsTermFour >= (Math.round(_fullTimeCredits / 2))) {
            allowanceTerm4 = _allowance;
        }

        if (_termQuantity > 4 && _creditsTermFive >= (Math.round(_fullTimeCredits / 2))) {
            allowanceTerm5 = _allowance;
        }

        // Fire Book Stipend$41.65
        var maxStipend = 1000;
        var stipendTerm1 = 0;
        var stipendTerm2 = 0;
        var stipendTerm3 = 0;
        var stipendTerm4 = 0;
        var stipendTerm5 = 0;

        /* First Term Stipend */
        stipendTerm1 = ((_creditsTermOne * 41.65 > maxStipend) ? maxStipend : _creditsTermOne * 41.65);

        /* Second Term Stipend */
        if (_termQuantity > 1) {
            stipendTerm2 = ((_creditsTermTwo * 41.65 > maxStipend) ? maxStipend : _creditsTermTwo * 41.65);
        }

        /* Third Term Stipend */
        if (_termQuantity > 2) {
            stipendTerm3 = ((_creditsTermThree * 41.65 > maxStipend) ? maxStipend : _creditsTermThree * 41.65);
        }

        /* Fourth Term Stipend */
        if (_termQuantity > 3) {
            stipendTerm4 = ((_creditsTermFour * 41.65 > maxStipend) ? maxStipend : _creditsTermFour * 41.65);
        }

        /* Fifth Term Stipend */
        if (_termQuantity > 4) {
            stipendTerm5 = ((_creditsTermFive * 41.65 > maxStipend) ? maxStipend : _creditsTermFive * 41.65);
        }

        /* Apply Percentages */
        tuitionsTerm1 *= (_benefitPercentage / 100);
        tuitionsTerm2 *= (_benefitPercentage / 100);
        tuitionsTerm3 *= (_benefitPercentage / 100);
        tuitionsTerm4 *= (_benefitPercentage / 100);
        tuitionsTerm5 *= (_benefitPercentage / 100);
        allowanceTerm1 *= (_benefitPercentage / 100);
        allowanceTerm2 *= (_benefitPercentage / 100);
        allowanceTerm3 *= (_benefitPercentage / 100);
        allowanceTerm4 *= (_benefitPercentage / 100);
        allowanceTerm5 *= (_benefitPercentage / 100);
        stipendTerm1 *= (_benefitPercentage / 100);
        stipendTerm2 *= (_benefitPercentage / 100);
        stipendTerm3 *= (_benefitPercentage / 100);
        stipendTerm4 *= (_benefitPercentage / 100);
        stipendTerm5 *= (_benefitPercentage / 100);

        $("#pnlResults").fadeIn(2000);

        $("#tuition1").text(formatCurrency(Math.round(tuitionsTerm1))).fadeIn("slow");
        $("#tuition2").text(formatCurrency(Math.round(tuitionsTerm2))).fadeIn("slow");
        $("#tuition3").text(formatCurrency(Math.round(tuitionsTerm3))).fadeIn("slow");
        $("#tuition4").text(formatCurrency(Math.round(tuitionsTerm4))).fadeIn("slow");
        $("#tuition5").text(formatCurrency(Math.round(tuitionsTerm5))).fadeIn("slow");
        $("#totalTuition").text(formatCurrency(Math.round(tuitionsTerm1 + tuitionsTerm2 + tuitionsTerm3 + tuitionsTerm4 + tuitionsTerm5))).fadeIn("slow");

        $("#allowance1").text(formatCurrency(Math.round(allowanceTerm1))).fadeIn("slow");
        $("#allowance2").text(formatCurrency(Math.round(allowanceTerm2))).fadeIn("slow");
        $("#allowance3").text(formatCurrency(Math.round(allowanceTerm3))).fadeIn("slow");
        $("#allowance4").text(formatCurrency(Math.round(allowanceTerm4))).fadeIn("slow");
        $("#allowance5").text(formatCurrency(Math.round(allowanceTerm5))).fadeIn("slow");
        $("#totalAllowance").text(formatCurrency(Math.round(allowanceTerm1 + allowanceTerm2 + allowanceTerm3 + allowanceTerm4 + allowanceTerm5))).fadeIn("slow");

        $("#stipend1").text(formatCurrency(Math.round(stipendTerm1))).fadeIn("slow");
        $("#stipend2").text(formatCurrency(Math.round(stipendTerm2))).fadeIn("slow");
        $("#stipend3").text(formatCurrency(Math.round(stipendTerm3))).fadeIn("slow");
        $("#stipend4").text(formatCurrency(Math.round(stipendTerm4))).fadeIn("slow");
        $("#stipend5").text(formatCurrency(Math.round(stipendTerm5))).fadeIn("slow");
        $("#totalStipend").text(formatCurrency(Math.round(stipendTerm1 + stipendTerm2 + stipendTerm3 + stipendTerm4 + stipendTerm5))).fadeIn("slow");

        $("#totalTerm1").text(formatCurrency(Math.round(tuitionsTerm1 + allowanceTerm1 + stipendTerm1))).fadeIn();
        $("#totalTerm2").text(formatCurrency(Math.round(tuitionsTerm2 + allowanceTerm2 + stipendTerm2))).fadeIn();
        $("#totalTerm3").text(formatCurrency(Math.round(tuitionsTerm3 + allowanceTerm3 + stipendTerm3))).fadeIn();
        $("#totalTerm4").text(formatCurrency(Math.round(tuitionsTerm4 + allowanceTerm4 + stipendTerm4))).fadeIn();
        $("#totalTerm5").text(formatCurrency(Math.round(tuitionsTerm5 + allowanceTerm5 + stipendTerm5))).fadeIn();
        $("#totalAll").text(formatCurrency(Math.round(stipendTerm1 + stipendTerm2 + stipendTerm3 + stipendTerm4 + stipendTerm5) + Math.round(tuitionsTerm1 + tuitionsTerm2 + tuitionsTerm3 + tuitionsTerm4 + tuitionsTerm5) + Math.round(allowanceTerm1 + allowanceTerm2 + allowanceTerm3 + allowanceTerm4 + allowanceTerm5))).fadeIn();

        /*
        * Remove the term columns that are not relevant.
        if (_termQuantity < 5) $("#calculationTable").removeCol(6);
        if (_termQuantity < 4) $("#calculationTable").removeCol(5);
        if (_termQuantity < 3) $("#calculationTable").removeCol(4);
        if (_termQuantity < 2) $("#calculationTable").removeCol(3);
        */
    }
});

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

