$(document).ready(function()
{
	//	attach events to balance percentages onBlur:
	$("#inhouse_tf").blur(balancePercentages);
	$("#outsourced_tf").blur(balancePercentages);
	
	//	attach events to calculate onClick
	$('#submit_btn').click(updateSavings);
});

function balancePercentages(event)
{
	var balance_target = (event.target.id == 'inhouse_tf'
		? '#outsourced_tf'
		: '#inhouse_tf');
	
	$(balance_target).val(getBalancedPercentage($(event.target).val()));
}

function updateSavings(event)
{
	var savings = Math.round(getEstimatedAnnualSavings(
		$('#beds_tf').val(), 
		$('#inhouse_tf').val(), 
		$('#outsourced_tf').val()));
	
	var savingsString = addCommas(roundToTens(savings));
	
	$('#savings').html(savingsString);
}

function addCommas(intValue)
{
	var nStr = intValue.toString();
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + ',' + '$2');
	}
	return nStr;
}
