/* SVN FILE: $Id$ */
/**
 * Compound Calculator
 *
 * Requires /media/js/calculators.js
 *
 * Copyright 2008, Freeman Fox Ltd
 *
 * Licensed under The Creative Commons License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright		Copyright 2007, FreemanFox Ltd
 * @version			$Revision$
 * @modifiedby		$LastChangedBy$
 * @lastmodified	$Date$
 * @license			http://creativecommons.org/licenses/by/3.0/ The Creative Commons License
 */
 
function fox_calculate()
{
	$('#calcDisplay').hide();
	$('#current_principle, #monthly_addition, #years, #interest_rate, #compound_times').trigger('change');
	var args = {};
	$('#calcForm input, #calcForm select').each(function()
	{
		if($(this).attr('value') === '' || $(this).attr('value') === undefined)
		{
			args[$(this).attr('id')] = 0;
		}
		else
		{
			args[$(this).attr('id')] = Calculator.numval($(this).attr('value'));
		}
	});

	$('#calcDisplay').html('$' + Calculator.formatNumber(Calculator.compound(args), 2));
	$('#calcDisplay').fadeIn('slow');
	return false;
}

$(document).ready(function()
{
	$('#calcDisplay').hide();
	
	$('#current_principle').bind('change', function()
	{
		$(this).attr('value', Calculator.formatNumber($(this).attr('value'), 2, 0));
	});
	
	$('#monthly_addition').bind('change', function()
	{
		$(this).attr('value', Calculator.formatNumber($(this).attr('value'), 2, 0));
	});
	
	$('#years').bind('change', function()
	{
		$(this).attr('value', Calculator.numval($(this).attr('value'), 2, 1));
	});
	
	$('#interest_rate').bind('change', function()
	{
		$(this).attr('value', Calculator.numval($(this).attr('value'), 2, 0));
	});
	
	$('#compound_times').bind('change', function()
	{
		$(this).attr('value', Calculator.numval($(this).attr('value'), 2, 0));
	});
	
	$('#calculate').bind('click', function(event)
   {
	   fox_calculate();
	   event.preventDefault();
	   return false;
   });
	
	$('#calcForm').bind('submit', function(event)
	{
		fox_calculate();
		event.preventDefault();
		return false;
	});
	
	$('#more-disclaimer').toggle(
	function()
	{
		$(this).next().slideDown();
	},
	function()
	{
		$(this).next().slideUp();
	});
});
