function calculateRepayment(amount,term,apr,period) {
	var repayment,rate,periodMod; 
	if (period=='M') {
		periodMod = 12;
	} else if (period=='F') {
		periodMod = 365.25 / 14;
	} else if (period=='W') {
		periodMod = 365.25 / 7;
	}
	rate = apr / (periodMod * 100);
	repayment = (amount * rate) / (1 - Math.pow( ( 1.0/(1.0+rate) ), Math.round(term * periodMod)));
	return roundToTwoDecimals(repayment);
	
}

function calculateNumberOfRepayments(amount,payment,apr,period) {
	var repayment,rate,periodMod; 
	if (period=='M') {
		periodMod = 12;
	} else if (period=='F') {
		periodMod = 365.25 / 14;
	} else if (period=='W') {
		periodMod = 365.25 / 7;
	}
	rate = apr / (periodMod * 100);
	
	repayment = (amount * rate) / (1 - Math.pow( ( 1.0/(1.0+rate) ), Math.round(term * periodMod)));
	
	return roundToTwoDecimals(repayment);
}

function roundToTwoDecimals (val) {
	var rounded = Math.round(val*100) / 100;
	return rounded;
}

function getAmountOfPayment() {
	if (validateAmountOfPayment()) {
		var amount = document.loanCalculator.amount.value;
		var period = document.loanCalculator.period.value;
		var term = document.loanCalculator.term.value;
		var rate = document.loanCalculator.rate.value;
		var payment = calculateRepayment(amount,term,rate,period);
		document.loanCalculator.payment.value = payment.toFixed(2);
	}	
}


function getNumberOfPayments() {

	if (validateNumberOfPaymentsForm()) {
		var amount = document.termCalulator.amount.value;
		var period = document.termCalulator.period.value;
		var rate = document.termCalulator.rate.value;
		var payment = document.termCalulator.payment.value;
		var resultText = 'Monthly Repayments';
		if (period=='M') {
			periodMod = 12;
			resultText ='Monthly Repayments';
		} else if (period=='F') {
			periodMod = 365.25 / 14;
			resultText = 'Fortnightly Repayments';
		} else if (period=='W') {
			periodMod = 365.25 / 7;
			resultText = 'Weekly Repayments';
		}
		var term = interpolateNumberOfPayments(amount,rate,period,payment,1 / periodMod, 1 / periodMod);
		document.termCalulator.term.value = Math.round(term * periodMod) + ' ' + resultText;
	}	
}

function interpolateNumberOfPayments(amount,rate,period,payment,initTerm,increment) {
	var attempt = 0;
	var initialTerm = initTerm;
	attempt = calculateRepayment(amount,initialTerm,rate,period);
	if (attempt > payment) {
		
		if (period=='M' && initialTerm*12 > 65) {
			alert ('Loans must be paid back within 5 years, for longer terms contact our office at 01 8870400');
			return false;
		} else if (period=='F' && initialTerm*(365.25 / 14) > 130) {
			alert ('Loans must be paid back within 5 years, for longer terms contact our office at 01 8870400');
			return false;
		} else if (period=='W'  && initialTerm*(365.25 / 7) > 260) {
			alert ('Loans must be paid back within 5 years, for longer terms contact our office at 01 8870400');
			return false;
		} else {
			return interpolateNumberOfPayments(amount,rate,period,payment,initTerm+increment,increment);
		} 

	} else {
		return initialTerm;
	}
}

function validateNumberOfPaymentsForm() {

	var error = 'false';
	var errorMessage = 'Some Errors Occured: \n\n';
	
	if (document.termCalulator.amount.value == null 
			|| document.termCalulator.amount.value.length <= 0
			|| document.termCalulator.amount.value == '') {
		error = 'true'
		errorMessage += 'Please enter a loan amount \n';
	}
	
	if (document.termCalulator.payment.value == null 
			|| document.termCalulator.payment.value.length <= 0
			|| document.termCalulator.payment.value == '') {
		error = 'true'
		errorMessage += 'Please enter a repayment amount \n';
	}

	if (error=='true') {
		alert (errorMessage);
		return false;
	} else {
		return true;
	} 
}

function validateAmountOfPayment() {

	var error = 'false';
	var errorMessage = 'Some Errors Occured: \n\n';
	
	if (document.loanCalculator.amount.value == null 
			|| document.loanCalculator.amount.value.length <= 0
			|| document.loanCalculator.amount.value == '') {
		error = 'true'
		errorMessage += 'Please enter a loan amount \n';
	}
	if (error=='true') {
		alert (errorMessage);
		return false;
	} else {
		return true;
	} 
}

