	var principle = 0; var monthlyInterest; var fees;
	var currentBalance = 0; var interestRate = 0; var addPurchases = 0; var monthlyPayment = 0;
	var payLate; var missPayment; var overLimit; var fromAtm; var cashWithdrawl; var paymentBounces;
	var annualFee; var missPaymentFee; var overLimitFee; var fromAtmFee; var cashWithdrawlFee; var paymentBouncesFee;

	function getDetails() {
		
		//Credit Card Details
		currentBalance 	= stringFilter($('cc_currentBalance').value).toFloat();
		if(isNaN(currentBalance) != false) currentBalance = 0;
		interestRate 	= stringFilter($('cc_interestRate').value).toFloat();
		if(isNaN(interestRate) != false) interestRate = 0;
		addPurchases  	= stringFilter($('cc_additionalPurchases').value).toFloat();
		if(isNaN(addPurchases) != false) addPurchases = 0;
    	monthlyPayment 	= stringFilter($('cc_monthlyPayment').value).toFloat();
    	if(isNaN(monthlyPayment) != false) monthlyPayment = 0;
		
		//Set the Print Fields
		$('h_currentBalance').setHTML(currentBalance)
		$('h_interestRate').setHTML(interestRate)
		$('h_additionalPurchases').setHTML(addPurchases)
		$('h_monthlyPayment').setHTML(monthlyPayment)
		
		
		//credit spending details
		payLate			= $('cc_paylate').getText().toFloat();
		missPayment 	= $('cc_missPayment').getText().toFloat();
		overLimit 		= $('cc_overLimit').getText().toFloat();
		fromAtm 		= $('cc_fromAtm').getText().toFloat();
		cashWithdrawl 	= $('cc_cashWithdrawl').getText().toFloat();
		paymentBounces	= $('cc_paymentBounces').getText().toFloat();
		
		//alert(payLate+', '+missPayment+', '+overLimit+', '+fromAtm +','+cashWithdrawl+','+paymentBounces)
		
		//Fees and charges
		annualFee			= $('cc_annualFees').value.toFloat();
		if(isNaN(annualFee) != false) annualFee = 0;
		missPaymentFee 		= $('cc_missPaymentFee').value.toFloat(); 
		if(isNaN(missPaymentFee) != false) missPaymentFee = 0;
		overLimitFee 		= $('cc_overLimitFee').value.toFloat();
		if(isNaN(overLimitFee) != false) overLimitFee = 0;
		fromAtmFee 			= $('cc_fromAtmFee').value.toFloat();
		if(isNaN(fromAtmFee) != false) fromAtmFee = 0;
		cashWithdrawlFee 	= $('cc_cashWithdrawlFee').value.toFloat();
		if(isNaN(cashWithdrawlFee) != false) cashWithdrawlFee = 0;
		paymentBouncesFee	= $('cc_paymentBouncesFee').value.toFloat();
		if(isNaN(paymentBouncesFee) != false) paymentBouncesFee = 0;
		
		return true;
	}

	var monthCount;
	var tmpBalance;
	var totalRepayments;
	var totalFees;
	
	
	function calculate() {
		
		$('log').setStyle('display', 'none');
		totalPay = 0;
		monthCount = 0;
		
		details = getDetails();
		
		var addAnnual = 0; var originalPrinciple = 0;
		var totalFees = 0; var totalPay = 0; var amountDue = 0;
		
		if(details == true) { 
			
			monthlyInterest = ((interestRate/100)/12);
			principle = originalPrinciple = currentBalance;
			fees = calculateFees();
	
			var interestTotal = 0;

			while(principle > 0) {

				//Add the monthly purchases.
				principle += addPurchases;

				//Add the cash withdrawn from the atm onto the principle as we still have to pay this back
				//Spread the total withdrawl amount over a year
				principle += ((fromAtm * cashWithdrawl)/12);	

				//adding the interest paid for the month to the interestTotal variable
				interestTotal += principle * (monthlyInterest)					
						
				//work out the principle + interest rate
				principle += (principle * monthlyInterest);

				//divide the Fee amount and divide it 12 so we can add it to the principle.
				principle += (fees/12);

				//Take away how much the user has paid off with the monthly payment				
				principle -= monthlyPayment;

				//Adding one to the monthCounter
				monthCount++;
		
				//Last payment so make sure we do not charge the full monthly payment 
				if (principle < 0) {
					totalPay = totalPay + monthlyPayment + principle;
				} else {
					totalPay +=  monthlyPayment;
				}

				//Every twelve months we add another annual fee to the principle
				if((monthCount%12) == 0) {
					principle += annualFee;
					addAnnual++;			
				}
				//This loop is working out if the Priciple is going up. If it is going up it halts the function and calls an error to show next to Minimum Amount.
				if(monthCount > 120 && principle > originalPrinciple) {
					$('log').setHTML('<strong>Unfortunately your monthly repayment isn\'t high enough to pay off your credit card balance. Please try increasing this amount.</strong>')
					$('log').setStyle('display', 'block');
					$$('div.totalBox').setStyle('display', 'none')
					return;
				} else {
					$$('div.totalBox').setStyle('display', 'block')
				}
			}
			
			totalFees 	= ((fees + annualFee) * addAnnual) + interestTotal;
			amountDue 	= originalPrinciple + interestTotal
			$('howManyMonths').setHTML(monthCount)
			$('totalAmount').setHTML('$'+ commaFormatted(totalPay.toFixed(2)))
			$('totalFees').setHTML('$' + commaFormatted(totalFees.toFixed(2)))
		}
		
	}
	
	
	function calculateFees() {
		//fees = annualFee;
		var cwfTotal = (cashWithdrawl * fromAtm) * (cashWithdrawlFee/100);
		var atmfTotal = (fromAtm * fromAtmFee);
		fees = 0;
		//Late Payment Fees
		fees = (payLate + missPayment) * missPaymentFee;
		//Over limit fees
		fees += (overLimit 	* overLimitFee);
		//Cash withdrawl fee calculation
		if (atmfTotal > cwfTotal) {
			fees += atmfTotal;
		} else {
			fees += cwfTotal;
		}
		//Payment bounc calculation
		fees += (paymentBounces * paymentBouncesFee)

		return fees;
	}

	window.addEvent('domready', function(){
		

		/* Pay Late */
		var payLate = new Slider($('area'), $('knob'), {
			steps: 12,	
			onChange: function(pos){
				$('cc_paylate').setHTML(pos);
			}

		}).set(0);	
		
		/* Miss Payment  */
		var missPayment = new Slider($('area1'), $('knob1'), {
			steps: 12,	
			onChange: function(pos){
				$('cc_missPayment').setHTML(pos);
			}

		}).set(0);	
		
		/* Over Limit */
		var overLimit = new Slider($('area2'), $('knob2'), {
			steps: 12,	
			onChange: function(pos){
				$('cc_overLimit').setHTML(pos);
			}

		}).set(0);
		
		/* From Atm */
		var fromAtm = new Slider($('area3'), $('knob3'), {
			steps: 100,	
			onChange: function(pos){
				$('cc_fromAtm').setHTML(pos);
			}

		}).set(0);
		
		/* Cash Withdrawl */
		var cashWithdrawl = new Slider($('area4'), $('knob4'), {
			steps: 100,	
			onChange: function(pos){
				$('cc_cashWithdrawl').setHTML(pos);
			}

		}).set(0);	
		
		/* Payment Bounces */
		var paymentBounces = new Slider($('area5'), $('knob5'), {
			steps: 12,	
			onChange: function(pos){
				$('cc_paymentBounces').setHTML(pos);
			}

		}).set(0);


		$$('div.bar').setStyle('display', 'none');
		
		$('spendingDetails').setStyle('display', 'block')
		
		var detailSlide = new Fx.Slide('spendingDetails').hide();
		var detailOpen = false;						
		$('defineSpending').addEvent('click', function(f){
			f = new Event(f);
			if(detailOpen == false) {
				$('spendButton').setProperty('src', '../SiteAssets/Images/LearnAboutMoney/Calculators/button_hide.png');
				$$('div.bar').setStyle('display', 'block');
				detailOpen = true;	
			} else {
				$('spendButton').setProperty('src', '../SiteAssets/Images/LearnAboutMoney/Calculators/button_define.png');
				$$('div.bar').setStyle('display', 'none');
				detailOpen = false;
			}
			detailSlide.toggle();
			f.stop();
		});
		
		
		$('feesCharges').setStyle('display', 'block');
		
		var feeSlide = new Fx.Slide('feesCharges').hide();
		var feeOpen = false;	
		$('defineFees').addEvent('click', function(e){
			e = new Event(e);
			if(feeOpen == false) {
				$('feesButton').setProperty('src', '../SiteAssets/Images/LearnAboutMoney/Calculators/button_hide.png');
				feeOpen = true;	
			} else {
				$('feesButton').setProperty('src', '../SiteAssets/Images/LearnAboutMoney/Calculators/button_define.png');
				feeOpen = false;
			}			
			feeSlide.toggle();
			e.stop();
		});	
		
			
	});
	//document.write('<style type="text/css" media="screen">div#spendingDetails {display: none;}</style>');
	//document.write('<style type="text/css" media="screen">div#feesCharges {display: none;}</style>');