// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(document).ready(function()	
{
		setTimeout(hideFlash,5000);	
		
		$("a.add_new_occupant").click(function()	{
			add_new_nested_item("#occupants_table", "#occupants table", occupant, false);
		});
		$("a.delete_current_item").click(function(obj)	{
			remove_current_item(obj);
		});
		$("a.delete_current_saved_item").click(function(obj)	{
			remove_current_saved_item(obj);
		});
		$("a.add_new_expense").click(function()	{
			add_new_expense();
		});
		$("a.add_new_income").click(function()	{
			add_new_income();
		});
		$("select#review").change(function()	{
			document.location.href = '/review/' + $(this).val()
		});
		$("a.add_new_standing_debt").click(function()	{
			add_new_nested_item("#standing_debt_table", "#standing_debt_table", standing_debt, true);
		});
		$("a.add_new_consumer_debt").click(function()	{
			add_new_nested_item("#consumer_debt_table", "#consumer_debt_table", consumer_debt, true);
		});
		$("input#future_benefits_deduction").change(function(obj)	{
			change_futures_form(this.checked, true);
		});
});

function hideFlash()	
{
	$(".flash").fadeOut();
}
function remove_current_saved_item( obj )	{
	$(obj.target).prev("input[type=checkbox]").attr("checked", "checked");
	$(obj.target).closest("tr").hide();
}
function remove_current_item( obj )
{
	$(obj.target).closest("tr").remove();
}

function add_new_nested_item( table_node, append_node, item, block_alpha )
{
	$(table_node).show()
	var new_id = new Date().getTime();
	$(append_node).append(item.replace(/NEW_RECORD/g, new_id));
	if (block_alpha) block_alpha_characters();
	
	$("a.delete_current_item").click(function(obj)	{
		remove_current_item(obj);
	});
}

function add_new_income()
{
	add_new_nested_item("#income_table", "#income_table", income, true);
	$(".income").change(function(obj)	{
		update_income_total();
	});
}

function add_new_expense()
{
	add_new_nested_item("#expenses_table", "#expenses_table", expense, true);
	$(".expenses").change(function(obj)	{
		update_expense_total();
	});
}

function change_futures_form(value, highlight)	{
	if (value)	{
		highlight ? $("#future_yes").effect("highlight",{}, 1000).fadeIn() : $("#future_yes").fadeIn();
		$("#future_no").hide();
		$("#future_no input").val("");
	} else {
		highlight ? $("#future_no").effect("highlight",{}, 1000).fadeIn() : $("#future_no").fadeIn();
		$("#future_yes").hide();
		$("#future_yes input").val("");
	}
}