
function floor(number)
{return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);}
function numFormat(num)
{num=""+num;if(num.indexOf('.')!=-1)
{tmp=num.split('.');}
else
{tmp=new Array;tmp[0]=num;tmp[1]='';}
if(tmp[0].length<=3)return num;var format="";for(i=0;i<tmp[0].length;i++)
{format=tmp[0].charAt(tmp[0].length-i-1)+format;if((i+1)%3==0&&(i!=tmp[0].length-1))format=","+format;}
if(tmp[1]!='')
{format=format+'.'+tmp[1];}
return format;}
function dosum(recalc_price,has_100)
{if(has_100)
{if(recalc_price==true)
{var product=document.getElementById('repayments_calculator').IR.options[document.getElementById('repayments_calculator').IR.options.selectedIndex].text;var price=0;if(product.indexOf("100%")!=-1)
{price=document.getElementById('repayments_calculator').full_price.value;}
else
{price=document.getElementById('repayments_calculator').full_price.value*ltv;}
document.getElementById('repayments_calculator').LA.value=numFormat(price);}}
var val1=new String(document.getElementById('repayments_calculator').LA.value);var val2=val1.replace(/,/g,'');var years=document.getElementById('repayments_calculator').years.options[document.getElementById('repayments_calculator').years.options.selectedIndex].value;var grossRate=document.getElementById('repayments_calculator').IR.options[document.getElementById('repayments_calculator').IR.options.selectedIndex].value/100;var loanAmount=val2;var debitsPerYear=4;var repaymentsPerYear=12;var totalRepayments=repaymentsPerYear*years;var year=0;var res=floor(((loanAmount)*Math.pow(1+grossRate*365.25/365/debitsPerYear,(debitsPerYear*(totalRepayments-(year*repaymentsPerYear))/repaymentsPerYear)))*(Math.pow(1+grossRate*365.25/365/debitsPerYear,(debitsPerYear/repaymentsPerYear))-1)/(Math.pow(1+grossRate*365.25/365/debitsPerYear,((debitsPerYear*(totalRepayments-(year*repaymentsPerYear)))/repaymentsPerYear))-1));if(isNaN(res))res="0";else
{res=parseFloat(res).toString();if(res.indexOf(".")>0)
{var postfix=res.substring(res.indexOf(".")+1,res.indexOf(".")+3);if(postfix.length==1)
{res=res+"0";}}}
document.getElementById('repayments_calculator').PI.value=numFormat(res)+"*";}
