jQuery(document).ready(function(){
	jQuery('.savings-calculator').each(function(){
		//	Every change update calculator.
		jQuery('.savings-calculator').change(function() {
			sc_update();
		});
		sc_update();
	});
});

function sc_update()
{
	//	Savings calculator object.
	var product = 249.95;
	var install = 640;
	var sc = new Object;

	//	Input variables.
	//sc.vehicles = jQuery('#sc_vehicles').get(0);
	//sc.vehicles = sc.vehicles.options[sc.vehicles.selectedIndex].value;
	sc.vehicles = jQuery('#sc_vehicles').attr('value');

	jQuery('#sc_product_cost').html(number_format(product * sc.vehicles, 2, '.', ' '));
	jQuery('#sc_installation_cost').html(number_format(install * sc.vehicles, 2, '.', ' '));

	sc.product_cost = product * sc.vehicles;
	sc.installation_cost = install * sc.vehicles;
	sc.miles = jQuery('#sc_miles').attr('value').replace(' ', '').replace(',', '').replace('.', '');
	sc.miles = intval(sc.miles);
	sc.miles_mod = jQuery('#sc_miles_mod').attr('value');
	sc.gas = jQuery('#sc_gas').attr('value');
	sc.savings = jQuery('#sc_savings').attr('value');
	sc.mileage = jQuery('#sc_mileage').attr('value').replace(' ', '').replace(',', '').replace('.', '');
	sc.mileage = intval(sc.mileage);
	sc.br = true;

	//	Calculations.
	sc.total_investment = (intval(sc.product_cost) * 1 + intval(sc.installation_cost) * 1);
	sc.single_car_months_miles = Math.round((intval(sc.miles) * intval(sc.miles_mod)) / 12);
	sc.months_gas_cost = Math.round((intval(sc.single_car_months_miles) / intval(sc.mileage)) * sc.gas) * intval(sc.vehicles);
	sc.new_mileage = (intval(sc.mileage) * (1 + (intval(sc.savings) / 100)));
	sc.new_months_gas_cost = Math.round((intval(sc.single_car_months_miles) / intval(sc.new_mileage)) * sc.gas) * intval(sc.vehicles);
	sc.save_per_month = Math.round((intval(sc.months_gas_cost) - intval(sc.new_months_gas_cost)));
	sc.recoup_months = Math.round(intval(sc.total_investment) / intval(sc.save_per_month));
	sc.save_per_year = sc.save_per_month * 12;

	//	Output results.
	jQuery('#new_mpg').html(sc.new_mileage + ' MPG');
	

	jQuery('#result_recoup_months').html(sc.recoup_months + ' month');
	if (sc.recoup_months > 1)
	{
		jQuery('#result_recoup_months').append('s');
	}
	jQuery('#result_save_per_month').html('$' + number_format(sc.save_per_month, 2, '.', ' '));
	jQuery('#result_save_per_year').html('$' + number_format(sc.save_per_year, 2, '.', ' '));

	var debug_table = jQuery('#calculator-debug table');
	debug_table.empty();
	for (var name in sc)
	{
		if ('br' == name)
		{
			debug_table.append('<tr><td colspan="2">&nbsp;</td></tr>');
		}
		else
		{
			debug_table.append('<tr><td class="name">' + name + '</td><td class="value">' + sc[name] + '</td></tr>');
		}
	}
}