$('a#calculate').click(function() {	
	var amount = $('input#amount').val();
	var originalAmount = amount;
	var iteration = 0;
	var price = 0;
	var amountBlocksArray = [50000,50000,150000,250000,250000,500000,999999999999];
	var pricePerBlockArray = [3,2.5,2,1.5,1,0.8,0.7];
	//var pricePerBlockArray = [1,0.8,0.7,0.6,0.5];
	
	while (amount > 0 ) {
	
		//if the amount is lower or matches this threshold then...
		if (amount <= amountBlocksArray[iteration]) {
			price=price + amount*pricePerBlockArray[iteration];
		} else {
			price=price + amountBlocksArray[iteration]*pricePerBlockArray[iteration];
		}
		//deduct the amountBlocks from the amount and go again
		amount = amount - amountBlocksArray[iteration];
		iteration++;	
	}
	
	var realSaving = addCommas((((originalAmount * pricePerBlockArray[0]) - price)/100).toFixed(2));
	var actualPrice = 5+(price/100);
	var realPrice = addCommas(actualPrice.toFixed(2));
		
	$('p#price').hide().html("£"+(realPrice));
	$('p#saving').hide().html("(save &pound; " + (realSaving) + ")");
	$('div#calculator').animate({height: "290px"}, 500, function(){
		$('p#price').fadeIn('slow'),
		$('p#saving').fadeIn('slow');
	});
	return false;
});

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
