// written by BCASE 10/29/2007 - needs jquery to run


// only known issue right now is top position not right in IE on PC, but close
//
//
$(document).ready(function(){
    // put calc function onto every text box with onblur trigger  
    //(avoids "nocalc" (non-numerical) text boxes - must have class="nocalc")
    $("input[type='text'][class!='nocalc']").blur(function(){
	calc();	
    });

    // run calc right now to set up page
    calc();
});

function calc() {
    var total = 0;

    // gets any text boxes starting with "numberof_" for income calc
    $("input[name^='numberof_']").each( function () {
	var this_fieldname = $(this).attr("name").substring(9);
	var this_fielddisplay_id = 'x-' + this_fieldname;
	var numberof_units = $(this).val();
	var costof_unit = $(this).attr('cost');
	var unit_total = Number(numberof_units * costof_unit );
	$("#" + this_fieldname).val(unit_total);
	$("#" + this_fielddisplay_id).val(unit_total);

    });

    // run function to get subtotals for every "sub_" (subtotal) field
    $("input[name^='sub_']").each( function() {
	var this_subsection = $(this).attr("name").substring(4) + "_";
	var this_subtotal = 0;
	// add every field that matches right side of "sub_" (subtotal) field
        // but avoid all class="nocalc" fields
	$("input[name^='" + this_subsection + "'][class!='nocalc']").each( function () {
	    this_subtotal += Number($(this).val());	
	});

	var this_subdisplay_id = 'x-' + $(this).attr("name");
	$(this).val(this_subtotal);
	$("#" + this_subdisplay_id).val(this_subtotal);
	if ( $("#" + this_subdisplay_id).attr('total') == 'no' ) {
		// do nothing	
	} else {
	    total += this_subtotal;
	}
    });

    // update value for CS 'total' field and display total field
    $("#total").val(total);
    $("#x-total").val(total);

    // update value for CS 'balance' field and display balance field
    var income = $('#sub_income').val();
    $("#balance").val(Number(income - total));
    $("#x-balance").val(Number(income - total));

    // Check Special Fields and Calc Limits/Percentages
    var any_errors = "";
    
    // percent limit (INCOME, FIELD, HIGH LIMIT, LOW LIMIT, EXACT LIMIT)
    //  	NOTE: basically, if you don't want something considered
    //		then put 'NA'
    //
    //		And the quirk is the it goes in order: high, low, exact

    any_errors = percent_limit(income,'ss_child_care','5','NA','NA');
    any_errors = percent_limit(income,'admin_indirect','8','NA','NA');
    any_errors = percent_limit(income,'sub_admin','10','NA','NA');
// removed by BCASE (10/21/2009 - task 9474183)
//    any_errors = percent_limit(income,'sub_ss','15','NA','NA');
    any_errors = percent_limit(income,'sub_ds','NA', '75','NA');
    any_errors = percent_limit(income,'balance','NA', 'NA','0');
   
    // IF any_errors then display final_msg error as well  
    if ( any_errors ) {
	tog_msg('final_msg','er','<nobr>There are errors in your budget.  Please check the above error messages.</nobr>','');
    } else {
	tog_msg('final_msg','e','OK!','');
    } 
}

function percent_limit(income,id,high,low,exact){
    var this_percent = Math.round(Number( ( $("#" + id).val() / income ) * 100 ) * 100) / 100;
    var this_msg = id + "_msg";
    var error = "";
    if ( high != 'NA' && this_percent > high ) {
    	var max_money = Math.round(Number( ( income * high ) * 100 ) / 100) / 100;
	tog_msg(this_msg,'er','<nobr>Over ' + high + '%. (Max: $' + max_money + ')</nobr>','move');
	error = "yes";
    } else if ( low != 'NA' && this_percent < low ) {
    	var min_money = Math.round(Number( ( income * low ) * 100 ) / 100) / 100;
	tog_msg(this_msg,'er','<nobr>Under ' + low + '%. (Min: $' + min_money + ')</nobr>','move');
	error = "yes";
    } else if ( exact != 'NA' && $("#" + id).val() != exact ) {
	tog_msg(this_msg,'er','<nobr>Must equal $' + exact + '.</nobr>','move');
	error = "yes";
    } else {
	tog_msg(this_msg,'e','OK!');
    }
    return error;
}

function tog_msg(this_id,this_class,msg,move) {

	$("#" + this_id).html(msg);
	$("#" + this_id).removeClass('e');
	$("#" + this_id).removeClass('er');
	$("#" + this_id).addClass(this_class);
	
        //var stored_orig_top = Number($("#" + this_id).attr('original_top'));
        var stored_orig_top = $("#" + this_id).attr('original_top');
	// if we've never calculated the new position for the error
	// 	then do it now - otherwise "original_top" will be set and used
	if ( ( typeof(stored_orig_top)=="undefined" ) 
		&& ( move == "move" ) 
		&& ( this_class == "er" ) ) {

            var table_coord = $('#budget_table').offset();
            var table_left = table_coord.left;
            var table_width = $('#budget_table').width();
            var table_right = table_left + table_width;
            var orig_top = $("#" + this_id).offset().top;

            $("#" + this_id).attr("original_top", orig_top);
            var new_top = orig_top - 16;
            var new_left = table_right + 10;
            //$("#" + this_id).attr("style", 'Top: ' + new_top + 'px; Left: ' + new_left + 'px;')
            $("#" + this_id).css('top', new_top).css('left', new_left);
        }
}
