// New rounding function by JGC
function roundoff(n) {
	var sign = "";
	if (n < 0) {
		n = n * -1;
		sign = "-";
	}
	var temp = "" + Math.round(n*100);
	if (temp.length < 3) { temp.length == 2 ? temp = "0" + temp : temp = "00" + temp }
	return (temp==0 ? "0.00" : sign + temp.substr(0,temp.length -2) + "." + temp.substr(temp.length -2,2));
}

// **********************************************************
// *************** FIXED RATE LOAN CALCULATOR *****************

// 13 June 2003: new version baselined from cFL for cFRL
// 1 Dec 2003: new rate

function calcfrl() {
	if(document.LayoutRegion1FORM.Borrowing.value.length==0) {
		alert("Can't calculate.\nPlease complete 'how much would you like to borrow?'");
		return false;
		}
	if(isNaN(document.LayoutRegion1FORM.Borrowing.value)) {
		alert("Can't calculate.\nThe value entered into 'how much would you like to borrow?'\ndoes not appear to be a number.");
		return false;
		}
	if(document.LayoutRegion1FORM.Borrowing.value<2000 || document.LayoutRegion1FORM.Borrowing.value>20000) {
		alert("Please enter an amount between £2,000 and £20,000. For loans under £2,000 please look at our Flexible Loan.");
		return false;
		}
	// Replacement code - JGC
	if (parseInt(document.LayoutRegion1FORM.Borrowing.value) % 100) {
		var tempval;
		if ((parseInt(document.LayoutRegion1FORM.Borrowing.value) % 100) <=50) {
			tempval = parseInt(document.LayoutRegion1FORM.Borrowing.value) - (parseInt(document.LayoutRegion1FORM.Borrowing.value) % 100);
		}
		else
		{
			tempval = parseInt(document.LayoutRegion1FORM.Borrowing.value) - (parseInt(document.LayoutRegion1FORM.Borrowing.value) % 100) + 100;
		}
		alert("Please enter a value that is a multiple of £100.\ninto 'how much would you like to borrow?'\ne.g. 2000.");
		return false;
		}
	// End replacement code
	
   var borrowing=document.LayoutRegion1FORM.Borrowing.value;
	//     0.061
   var APR=0.061;
   var Term;
   var Monthly_Flat_Rate;
   var ave_monthly;
	//           4900 
   if(borrowing >4990)
   {
	   // alert("rate being borrowed is between £2,000 and £20,000 at 5.8%");
	   APR=0.061;   // 0.058
   }
   else
   {
	   	// the else condion should not take effect for this web release
	   //alert("rate being borrowed is between at 8.9%");
	   APR=0.089;   // 0.089
   }

	//Replacement code - JGC
	Term = document.LayoutRegion1FORM.repayTerm.options[document.LayoutRegion1FORM.repayTerm.selectedIndex].value;
   Monthly_Flat_Rate=Math.pow(1+APR,1/12)-1;
	ave_monthly=borrowing/((1-(Math.pow(1/(1+Monthly_Flat_Rate), Term)))/Monthly_Flat_Rate); 
	ave_monthly = roundoff(ave_monthly);
   if (document.LayoutRegion1FORM.ppi[0].checked == true) {
		ave_monthly_with_ppi = roundoff(1.15*ave_monthly);
		self.location.replace("fixed_rate_loan_details_results.html?" + roundoff(borrowing) + "&" + Term + "&" + ave_monthly_with_ppi + "&" + roundoff(Term*ave_monthly_with_ppi));
     	}
	else {
		self.location.replace("fixed_rate_loan_details_results.html?" + roundoff(borrowing) + "&" + Term + "&" + roundoff(ave_monthly) + "&" + roundoff(Term*ave_monthly));
      }
   //APR100 = APR*100;
   APR100=Math.round(APR*1000)/10
   alert("This calculation is based on a typical "+APR100+"% APR*");
}



// **********************************************************
// *************** FLEXIBLE LOAN CALCULATOR *****************

// 3 Dec 2001: references to form elements by explicit name removed due to Netscape errors

var test = false;

function calcflex(){
	
	if (document.LayoutRegion1FORM.Borrowing.value.length==0) {
		alert("Can't calculate.\nPlease complete 'how much would you like to borrow?'");
		return false;
	}
	if (isNaN(document.LayoutRegion1FORM.Borrowing.value)) {
		alert("Can't calculate.\nThe amount entered into 'how much would you like to borrow?'\ndoes not appear to be a number.");
		return false;
	}
	
	if (document.LayoutRegion1FORM.Borrowing.value<100 || document.LayoutRegion1FORM.Borrowing.value>20000) {
		alert("Please enter a value between £100 and £20,000\ninto 'how much would you like to borrow?'");
		return false;
	}
   
	var borrowing=document.LayoutRegion1FORM.Borrowing.value;
	var APR=0.091;
	var Term;
	var Monthly_Flat_Rate;
	var ave_monthly;
	var minpay_percent=0.02;
	var minpay_amount=50;

	// New code - JGC
	Term = document.LayoutRegion1FORM.Term_Combo.options[document.LayoutRegion1FORM.Term_Combo.selectedIndex].value;
	// End New Code

	var Calculated_term;
	Monthly_Flat_Rate=Math.pow(1+APR,1/12)-1;

	for(i=Term;i>0;--i) {
		Calculated_term=i;
		ave_monthly=roundoff(borrowing/((1-(Math.pow(1/(1+Monthly_Flat_Rate), Calculated_term)))/Monthly_Flat_Rate)); 
		if(ave_monthly>=minpay_amount) break;
	}
	
	if (i==1 && ave_monthly<minpay_amount) {
		alert("repaid in < 1 month");
	}
	else {
		var minpayment;
		var errMessage = "";
		
		if (borrowing*minpay_percent < minpay_amount) {
		   minpayment = roundoff(minpay_amount);
		}
		else {
		   minpayment = roundoff(borrowing*minpay_percent);
		}
		
		if (Term !=Calculated_term) {
			errMessage = Term;
		}
		self.location.replace("flexible_loan_sub_results.html?" + roundoff(borrowing) + "&" + ave_monthly + "&" + Calculated_term + "&" + roundoff(Calculated_term*ave_monthly-borrowing) + "&" + roundoff(Calculated_term*ave_monthly) + "&" + minpayment + "&" + errMessage);
		
	}
	//APR100 = APR*100;
   APR100=Math.round(APR*1000)/10
   alert("This calculation is based on a typical "+APR100+"% APR*");
}
