/*
	doCalcs
	Perform consolidation loan calculation
*/

function doCalcs(){
var point=0;
var p_pos=0;
var aprarray=new Array(0.00944,0.00869,0.00778,0.00639);
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);

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++}
}
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<36){alert("Please Select At Least 36 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<4;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;

}
for (i=0;i<4;i++){
if (payarray[i]<10000){apr="11.9%";choice=payarray[0];break}
if (payarray[i]>=10000 && payarray[i]<=19999){apr="10.9%";choice=payarray[1];break}
if (payarray[i]>=20000 && payarray[i]<=34999){apr="9.7%";choice=payarray[2];break}
if (payarray[i]>=35000) {apr="7.9%";choice=payarray[3];break}
}
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)

// 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";

// work out the monthly loan.
var roi=0;

// set interest rate (APR)
if (t_current<10000){roi=0.00944;apr="11.9%"}
if (t_current>=10000 && value<=19999){roi=0.00869;apr="10.9%"}
if (t_current>=20000){roi=0.00793;apr="8.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"}

}
