$j(document).ready(function(){
	setTimeout(function(){
		income_Total_calc();
	},500);
	setTimeout(function(){
		expense_Total_calc();
	},500);
	$j(document).on('hidden.bs.modal','.modal',function(){
		var income_amount = localStorage.getItem('income_amount');		
		if(income_amount)
		{
			income_amount=parseFloat(income_amount);
			var total_income=parseFloat($j('#total_income').text());
			var total_expense=parseFloat($j('#total_expense').text());
			var available_budget=parseFloat($j('#available_budget').text());
			if($j.isNumeric(total_income) && $j.isNumeric(total_expense) && $j.isNumeric(available_budget) && $j.isNumeric(income_amount))
			{
				total_income=total_income+income_amount;
				available_budget=total_income-total_expense;
				$j('#total_income').html(total_income.toFixed(2));
				$j('#total_expense').html(total_expense.toFixed(2));
				$j('#available_budget').html(available_budget.toFixed(2));
				localStorage.removeItem('income_amount');
				setTimeout(function(){
					income_Total_calc();
				},800);
			}
			else
			{
				localStorage.removeItem('income_amount');
			}			
		}
		else
		{
			localStorage.removeItem('income_amount');
		}
		
		var expense_amount = localStorage.getItem('expense_amount');
		if(expense_amount)
		{
			expense_amount=parseFloat(expense_amount);
			var total_income=parseFloat($j('#total_income').text());
			var total_expense=parseFloat($j('#total_expense').text());
			var available_budget=parseFloat($j('#available_budget').text());
			if($j.isNumeric(total_income) && $j.isNumeric(total_expense) && $j.isNumeric(available_budget) && $j.isNumeric(expense_amount))
			{
				total_expense=total_expense+expense_amount;
				available_budget=total_income-total_expense;
				$j('#total_income').html(total_income.toFixed(2));
				$j('#total_expense').html(total_expense.toFixed(2));
				$j('#available_budget').html(available_budget.toFixed(2));
				localStorage.removeItem('expense_amount');
				setTimeout(function(){
					expense_Total_calc();
				},800);
			}
			else
			{
				localStorage.removeItem('expense_amount');
			}			
		}
		else
		{
			localStorage.removeItem('expense_amount');
		}
	});
});
function income_Total_calc()
{
	$j('#panel_incomes-nom_prenom table tbody').append('<tr><td  colspan="3" class="text-right">Total</td> <td class="text-right" id="total_income_ictable"></td><td></td></tr>');
	var income=0;
	$j('.incomes-amount').each(function(){
		var new_income=$j(this).text();
		if($j.isNumeric(new_income))
		{
			income=parseFloat(income)+parseFloat(new_income);
			$j('#total_income_ictable').html(parseFloat(income).toFixed(2));
		}
	});
}
function expense_Total_calc()
{
	$j('#panel_expenses-nom_prenom table tbody').append('<tr><td  colspan="3" class="text-right">Total</td> <td class="text-right" id="total_expense_etable"></td><td></td></tr>');
	var expense=0;
	$j('.expenses-amount').each(function(){
		var new_expense=$j(this).text();
		if($j.isNumeric(new_expense))
		{
			expense=parseFloat(expense)+parseFloat(new_expense);
			$j('#total_expense_etable').html(parseFloat(expense).toFixed(2));
		}
	});
}