/*
	doCalcs
	Perform consolidation loan calculation
*/

function doCalcs(){
var point=0;
var p_pos=0;
var aprarray=new Array(0.00982,0.00831,0.00755,0.00631,0.00561,0.00482);
var payarray=new Array();
var recip=0;
var result=0;
var loan=0;
var choice=0;
var ch_dec=0;
var mystr= /£/g
var mydec= /\./;
var current=new Number(0);
var fcount=0;
var t_current=new Number(0);
var months=document.addLoan.conmonths.value;
var allpay = new Array(document.addLoan.loan1.value,document.addLoan.loan2.value,document.addLoan.loan3.value,document.addLoan.loan4.value,document.addLoan.loan5.value,document.addLoan.loan6.value,document.addLoan.loan7.value,document.addLoan.loan8.value,document.addLoan.loan9.value,document.addLoan.loan10.value);
var nTotalPayment = 0;
var sTotalPayment = "0.00";

for (i=0;i<=9;i++){
	//check for leading pound sign and delete if found
	if (allpay[i].charAt(0) == "£"){ allpay[i]=allpay[i].replace(mystr, ""); }
	
	// check if less than 1 or more than 10000
	if (allpay[i] && allpay[i]< 1){alert("Minimum amount Is £1. Please correct field "+(i+1)+"");return}
	if (allpay[i] && allpay[i]> 10000){alert("Maximum amount Is £10000. Please correct field "+(i+1)+"");return}
	
	//check for decimal point
	ch_dec=allpay[i].search(mydec)
	if (ch_dec > -1){alert("Please Enter A Whole Number , Without The Decimal Point In Field "+(i+1)+"");return}
	
	if (isNaN(allpay[i]) == true){alert('Please Correct loan '+(i+1)+' field. Numbers 0-9 Only Please.');return}
	else{current=allpay[i]/1;t_current=t_current+current}
	if (allpay[i]==""){fcount++}
}// end for


if (fcount==10){alert ('Please Enter Your Current Loan Details');return}

//check months field
if(months==""){alert("Please Enter The Length of The Loan");document.addLoan.conmonths.focus();return}
ch_dec=months.search(mydec)
if (ch_dec > -1){alert("Please Enter A Whole Number of Months Between 12 and 300 , Without The Decimal Point Please.");document.addLoan.conmonths.select();return}
if (isNaN(months) == true){alert("Please Correct The Length Of The Loan\nOnly Numbers 0-9 Please");document.addLoan.conmonths.select();return}
if (months<60){alert("Please Select At Least 60 Monthly loans");document.addLoan.conmonths.select();return}
if (months>300){alert("Maximum Monthly Reloans Are 300");document.addLoan.conmonths.select();return}

//calculations
for (i=0;i<6;i++){
	recip=1/(1+aprarray[i]);
	recip=Math.pow(recip,months);
	result=1-recip;
	result=result/aprarray[i];
	loan=t_current*result;
	loan=loan+0.005
	
	payarray[i]=loan;
}//end for ///\\\ calculations

for (i=0;i<6;i++){
	if (payarray[0]<9999){apr="12.4%";choice=payarray[0]}
	if (payarray[1]>=10000 && payarray[1]<=14999){apr="10.4%";choice=payarray[1]}
	if (payarray[2]>=15000 && payarray[2]<=24999){apr="9.4%";choice=payarray[2]}
	if (payarray[3]>=25000 && payarray[3]<=49999){apr="7.8%";choice=payarray[3]}
	if (payarray[4]>=50000 && payarray[4]<=74999){apr="6.9%";choice=payarray[4]}
	if (payarray[5]>=75000) {apr="5.9%";choice=payarray[5]}
}// end for

loan=choice;
point=loan.toString()
p_pos = point.search(mydec)
if (point.charAt(p_pos+2) == ""){point=point+"0"}
loan=point.slice(0,p_pos+3)


// Calculate the total payable amount
nTotalPayment	= Math.round(t_current * months * 100)/100;
sTotalPayment	= toTwoDecimal(nTotalPayment);

// set fields
var balance=document.addLoan.balance.value;
loan=Math.round(loan);
loan=loan+".00";
document.addLoan.conborrow.value=loan;
document.addLoan.apr.value=apr;
document.addLoan.concurrent.value=t_current+".00";
document.addLoan.contotpay.value=sTotalPayment;

// work out the monthly loan.
var roi=0;

// set interest rate (APR)
if (balance<9999){roi=0.00982;apr="12.4%"}
if (balance>=10000 && balance<=14999){roi=0.00831;apr="10.4%"}
if (balance>=15000 && balance<=24999){roi=0.00755;apr="9.4%"}
if (balance>=25000 && balance<=49999){roi=0.00631;apr="7.8%"}
if (balance>=50000 && balance<=74999){roi=0.00561;apr="6.9%"}
if (balance>=75000){roi=0.00482;apr="5.9%"}
recip=1/(1+roi);
recip=Math.pow(recip,months)
result=1-recip
result=result/roi
loan=balance/result

loan=loan+0.005
if (loan == "" || loan<1){document.addLoan.newmonthly.value="Not Available"}
else{
document.addLoan.newmonthly.value=Math.round(loan)+".00"
}
var totalsave=t_current-loan;
if (balance ==""){document.addLoan.saving.value="Not Available"}
else if (totalsave<1){document.addLoan.saving.value="None"}
else{document.addLoan.saving.value=Math.round(totalsave)+".00"}

}

function toTwoDecimal( nVal )
	{	
	var sResult = "";
	var nPointPos = 0;
	
	sResult = nVal.toString();
	// Find decimal
	nPointPos = sResult.indexOf(".");
	// No decimal, add them
	if( nPointPos == -1 )
		{
		sResult += ".00";
		nPointPos = sResult.indexOf(".");
		}

	// Extend to two decimal places if not already
	if( sResult.charAt(nPointPos + 2)=="" ) sResult += "0";
	// Trim to two decimal places
	sResult = sResult.slice(0, nPointPos+3 );
	return sResult;
	}
