/* not sure if I can get this any cleaner. needs to be looked at */
function CheckFieldIn(fldval) {
    if(fldval == "0") { return ""; } else { return fldval; }
}

function CheckFieldOut(fldval) {
    fldval = fldval.replace(",", "").replace("$", "");
    if(fldval == "" || isNaN(fldval)) { return "0"; } else { return fldval; }
}

$(document).ready(function() {

    var Focus14 = function() {
        $("#no14").focus();
        $("#no14").select();
    };

    var CalculateCNI = function() {
        /* ask DLF about this. if the number is between these, does it jump to the higher or lower? right now, it jumps to the higher. */
        var pos_cni_chart = new Array(10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, 0);
        var neg_cni_chart = new Array(0, -999, -1999, -2999, -3999, -4999, -5999, -6999, -7999, -8999, -9999, -10999, -11999, -12999, -13999, -14999, -15999, -16999, -17999, -19999, -21999, -23999);
        var pos_percent_chart = new Array(0, 5, 5, 10, 15, 20, 20, 25, 30, 30, 35);
        var neg_percent_chart = new Array(35, 40, 40, 45, 45, 50, 55, 55, 60, 60, 65, 75, 80, 80, 85, 85, 90, 90, 95, 95, 95, 100);
        var tuition = 4900;
        var exemption = 0;
        var percent = 0;
        var cni = 0;
        var i = 0;
        var total_income = Number($("#no1").val()) + Number($("#no2").val()) + Number($("#no3").val()) + Number($("#no4").val()) + Number($("#no5").val()) + Number($("#no6").val());
        switch($("#no14").val()) {
            case "2":
                exemption = 30170;
                break;
            case "3":
                exemption = 36435;
                break;
            case "4":
                exemption = 43480;
                break;
            case "5":
                exemption = 51075;
                break;
            case "6":
                exemption = 59040;
                break;
            case "7":
                exemption = 65095;
                break;
            default:
                if($("#no14").val() < 2) {
                    $("#dlg_fin_aid").text("Personal exemptions must be at least 2!");
                    $("#dlg_fin_aid").bind("dialogclose", Focus14);
                    $("#dlg_fin_aid").dialog("open");
                    return true;
                    break;
                } else if($("#no14").val() > 7) {
                    exemption = ($("#no14").val() * 1465) + 65095;
                    break;
                }
        }
        var total_adjustments = Number($("#no7").val()) + Number($("#no8").val()) + Number($("#no9").val()) + Number($("#no10").val()) + Number($("#no10").val()) + Number($("#no11").val()) + Number($("#no12").val()) + Number($("#no13").val()) + Number(exemption);
        var int_cni = Number(total_income) - Number(total_adjustments);

        if(int_cni < 0) {
/*            for(i = neg_cni_chart.length-1; i >= 0; i--) { */
              for(i = 0; i < neg_cni_chart.length; i++) {
                if(Number(int_cni) < Number(neg_cni_chart[i])) {
                    percent = neg_percent_chart[i];
                    cni = neg_cni_chart[i];
                }
            }
        } else {
            for(i = 0; i < pos_cni_chart.length; i++) {
                if(Number(int_cni) < Number(pos_cni_chart[i])) {
                    percent = pos_percent_chart[i];
                    cni = pos_cni_chart[i];
		}
            }
        }
        percent = percent * 0.01;
        var total_financial_aid = (Number(tuition) * Number(percent)) *0.9;
        $("#dlg_fin_aid").html("<div>Based on the information you've entered, your <u>estimated</u> amount of financial assistance is:</div><br /><center><b>$" + total_financial_aid.toFixed(2) + "</b></center><br /><div>If you have any questions or concerns regarding financial assistance, please contact Mrs. Diana Finkbeiner at (989)793-1041 ext. 225 or <a href=\"mailto:dlf@mlsem.org\">dlf@mlsem.org</a>.</div><div>percent: " + percent + "</div><div>cni chart: " + Number(cni) + "</div><div>cni actual: " + Number(int_cni) + "</div><div>iterated to: " + i +"</div>");
        $("#dlg_fin_aid").unbind("dialogclose", Focus14);
        $("#dlg_fin_aid").dialog("open");
        return true;
    };

    $("#btn_calculate").click(CalculateCNI);
    $("#dlg_fin_aid").dialog({
        autoOpen: false,
        height: 250,
        width: 300,
        resizable: false,
        draggable: false,
        modal: true,
        buttons: {
            'Ok': function() {
                $(this).dialog('close');
            }
        }
    });

/*******************************************************************************
 * Focus handlers to format boxes correctly
 */
    /* this may need to be cleaned up a bit */
    $("#no1").focusin(function() { $("#no1").val(CheckFieldIn($("#no1").val())); });
    $("#no1").focusout(function() { $("#no1").val(CheckFieldOut($("#no1").val())); });
    $("#no2").focusin(function() { $("#no2").val(CheckFieldIn($("#no2").val())); });
    $("#no2").focusout(function() { $("#no2").val(CheckFieldOut($("#no2").val())); });
    $("#no3").focusin(function() { $("#no3").val(CheckFieldIn($("#no3").val())); });
    $("#no3").focusout(function() { $("#no3").val(CheckFieldOut($("#no3").val())); });
    $("#no4").focusin(function() { $("#no4").val(CheckFieldIn($("#no4").val())); });
    $("#no4").focusout(function() { $("#no4").val(CheckFieldOut($("#no4").val())); });
    $("#no5").focusin(function() { $("#no5").val(CheckFieldIn($("#no5").val())); });
    $("#no5").focusout(function() { $("#no5").val(CheckFieldOut($("#no5").val())); });
    $("#no6").focusin(function() { $("#no6").val(CheckFieldIn($("#no6").val())); });
    $("#no6").focusout(function() { $("#no6").val(CheckFieldOut($("#no6").val())); });
    $("#no7").focusin(function() { $("#no7").val(CheckFieldIn($("#no7").val())); });
    $("#no7").focusout(function() { $("#no7").val(CheckFieldOut($("#no7").val())); });
    $("#no8").focusin(function() { $("#no8").val(CheckFieldIn($("#no8").val())); });
    $("#no8").focusout(function() { $("#no8").val(CheckFieldOut($("#no8").val())); });
    $("#no9").focusin(function() { $("#no9").val(CheckFieldIn($("#no9").val())); });
    $("#no9").focusout(function() { $("#no9").val(CheckFieldOut($("#no9").val())); });
    $("#no10").focusin(function() { $("#no10").val(CheckFieldIn($("#no10").val())); });
    $("#no10").focusout(function() { $("#no10").val(CheckFieldOut($("#no10").val())); });
    $("#no11").focusin(function() { $("#no11").val(CheckFieldIn($("#no11").val())); });
    $("#no11").focusout(function() { $("#no11").val(CheckFieldOut($("#no11").val())); });
    $("#no12").focusin(function() { $("#no12").val(CheckFieldIn($("#no12").val())); });
    $("#no12").focusout(function() { $("#no12").val(CheckFieldOut($("#no12").val())); });
    $("#no13").focusin(function() { $("#no13").val(CheckFieldIn($("#no13").val())); });
    $("#no13").focusout(function() { $("#no13").val(CheckFieldOut($("#no13").val())); });
    $("#no14").focusin(function() { $("#no14").val(CheckFieldIn($("#no14").val())); });
    $("#no14").focusout(function() { $("#no14").val(CheckFieldOut($("#no14").val())); });
});

