
//round a floating point number to an exact number of pence
function Currency(rnum)
{
	//round it
	//javascript can only round to an integer - so need to shift to pence first, and then back again !
	var number =  Math.round(rnum*Math.pow(10,2))/Math.pow(10,2);
	
	//make sure 2 pence columns
	var n = new String(number);
	var dot = n.indexOf(".");

	//deal with no pence
  	if( dot == -1 ){
    	return n + ".00";
  	}

  	//make sure only 2 pence
  	n = n + "00";
  	return( n.substring(0,dot+3));

}


function Calculate() {

	//change these if cooleraid prices change
	// *** also need to change the default value in the form  ***
	var one_two_price = 23.79;
	var three_five_price = 21.62;
	var six_plus_price = 19.46;
	
	var f = document.calculator_form;
	
	//adjust price depending on quantity, if price set to standard value
	if (f.rental.value == one_two_price || f.rental.value == three_five_price || f.rental.value == six_plus_price ){
		switch( Number(f.num_coolers.value) ){
			case 1:
			case 2:
				f.rental.value = one_two_price;
				break;
			case 3:
			case 4:
			case 5:
				f.rental.value = three_five_price;
				break;
			default:
				f.rental.value = six_plus_price;

		}

}
	
//monthly machine costs	
f.existing_machine_cost.value = Currency( (Number(f.existing_rental.value) + Number(f.existing_cooler_care.value) + Number(f.existing_filter.value)) * Number(f.num_coolers.value));
f.machine_cost.value = Currency( (Number(f.rental.value) + Number(f.cooler_care.value) + Number(f.filter.value)) * Number(f.num_coolers.value));

//monthly water costs
f.existing_water_cost.value = Currency(Number(f.existing_bottle_cost.value) * Number(f.existing_bottles_used.value));
f.existing_monthly_cost.value = Currency(Number(f.existing_machine_cost.value) + Number(f.existing_water_cost.value));
f.monthly_cost.value = Currency(Number(f.machine_cost.value));

//deposit costs
f.existing_deposit_cost.value = Currency( Number(f.existing_bottle_deposit.value) * Number(f.existing_bottles_in_stock.value) );

//total annual costs
f.existing_annual_costs.value = Currency( (Number(f.existing_monthly_cost.value)*12) + Number(f.existing_deposit_cost.value) + Number(f.existing_installation.value) + Number(f.existing_other_costs.value) );
f.annual_costs.value = Currency( (Number(f.monthly_cost.value)*12) + Number(f.other_costs.value) );

//savings
f.monthly_saving.value = Currency( Number(f.existing_monthly_cost.value) - Number(f.monthly_cost.value) );
f.cooler_saving.value = Currency( Number(f.monthly_saving.value) / Number(f.num_coolers.value) );
f.deposit_saving.value = f.existing_deposit_cost.value;
f.annual_saving.value = Currency( (Number(f.monthly_saving.value) * 12) +  Number(f.deposit_saving.value) ); //should this include installation and others as well ?

//percentage savings
f.percent_saving.value = Currency( ( Number(f.annual_saving.value) / Number(f.existing_annual_costs.value) ) * 100 );

}


