/*****************************************
(C) http://www.calculator.net all right reserved.  
*****************************************/
var dataArray=new Array();var theLoanTerm=0;totalCalcCycle=0;cutoffCalcCycle=1000;cutoffPrecision=1e-8;var delayShow=true;var cadditionatbeginning=false;function popMenu(A){jY="";gObj("cendamount").style.display="block";gObj("cinvestlength").style.display="block";gObj("cstartingamount").style.display="block";gObj("creturnrate").style.display="block";gObj("ccontributeamount").style.display="block";gObj("ctype").value=A;if(A=="endamount"){jY=jY+'<li id=\'menuon\'><a href="#" onclick="popMenu(\'endamount\');">FV</a></li><li><a href="#" onclick="popMenu(\'contributeamount\');">PMT</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">I/Y</a></li><li><a href="#" onclick="popMenu(\'investlength\');">N</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">starting investment</a></li>';gObj("cendamount").style.display="none"}else{if(A=="investlength"){jY=jY+'<li><a href="#" onclick="popMenu(\'endamount\');">FV</a></li><li><a href="#" onclick="popMenu(\'contributeamount\');">PMT</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">I/Y</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'investlength\');">N</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">starting investment</a></li>';gObj("cinvestlength").style.display="none"}else{if(A=="startingamount"){jY=jY+'<li><a href="#" onclick="popMenu(\'endamount\');">FV</a></li><li><a href="#" onclick="popMenu(\'contributeamount\');">PMT</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">I/Y</a></li><li><a href="#" onclick="popMenu(\'investlength\');">N</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'startingamount\');">starting investment</a></li>';gObj("cstartingamount").style.display="none"}else{if(A=="returnrate"){jY=jY+'<li><a href="#" onclick="popMenu(\'endamount\');">FV</a></li><li><a href="#" onclick="popMenu(\'contributeamount\');">PMT</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'returnrate\');">I/Y</a></li><li><a href="#" onclick="popMenu(\'investlength\');">N</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">starting investment</a></li>';gObj("creturnrate").style.display="none"}else{if(A=="contributeamount"){jY=jY+'<li><a href="#" onclick="popMenu(\'endamount\');">FV</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'contributeamount\');">PMT</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">I/Y</a></li><li><a href="#" onclick="popMenu(\'investlength\');">N</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">starting investment</a></li>';gObj("ccontributeamount").style.display="none"}}}}}jY="<ul>"+jY+"</ul>";gObj("topmenu").innerHTML=jY;calc()}function calcEndVal(C,B,E,A,D){Zo=C*Math.pow((1+B),A);if(D){B=Math.pow((B+1),1/12)-1;A=A*12}if(cadditionatbeginning){for(i=0;i<A;i++){Zo+=E*Math.pow((1+B),(A-i))}}else{for(i=1;i<=A;i++){Zo+=E*Math.pow((1+B),(A-i))}}return Zo}function getInvLength(C,A,E,F,D,G,B){As=calcEndVal(C,A,F,G,D);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){if(As>E){return G}}if((As!=0)&&(E!=0)){if(Math.abs((As-E)/E)<cutoffPrecision){return G}}else{if(Math.abs(As-E)<cutoffPrecision){return G}}if(As>E){return getInvLength(C,A,E,F,D,(G-0.7*B),(0.7*B))}else{return getInvLength(C,A,E,F,D,(G+0.7*B),(0.7*B))}}function getStartingAmount(B,A,E,G,D,F,C){As=calcEndVal(F,A,G,B,D);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){return"too long"}if((As!=0)&&(E!=0)){if(Math.abs((As-E)/E)<cutoffPrecision){return F}}else{if(Math.abs(As-E)<cutoffPrecision){return F}}if(As>E){return getStartingAmount(B,A,E,G,D,(F-0.7*C),(0.7*C))}else{return getStartingAmount(B,A,E,G,D,(F+0.7*C),(0.7*C))}}function getReturnRate(A,C,E,F,D,G,B){As=calcEndVal(C,G,F,A,D);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){return"too long"}if((As!=0)&&(E!=0)){if(Math.abs((As-E)/E)<cutoffPrecision){return G}}else{if(Math.abs(As-E)<cutoffPrecision){return G}}if(As>E){return getReturnRate(A,C,E,F,D,(G-0.7*B),(0.7*B))}else{return getReturnRate(A,C,E,F,D,(G+0.7*B),(0.7*B))}}function getContributeAmount(B,D,G,A,E,F,C){As=calcEndVal(D,A,F,B,E);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){return"too long"}if((As!=0)&&(G!=0)){if(Math.abs((As-G)/G)<cutoffPrecision){return F}}else{if(Math.abs(As-G)<cutoffPrecision){return F}}if(As>G){return getContributeAmount(B,D,G,A,E,(F-0.7*C),(0.7*C))}else{return getContributeAmount(B,D,G,A,E,(F+0.7*C),(0.7*C))}}function calc(){showquickmsg("calculating...",true);gObj("resulttable").innerHTML="";setTimeout("process()",2)}function process(){var C=trimAll(gObj("cstartingprinciplev").value+"");var B=trimAll(gObj("cinterestratev").value+"");var A=trimAll(gObj("cyearsv").value+"");var D=trimAll(gObj("ctargetamountv").value+"");var F=trimAll(gObj("ccontributeamountv").value+"");var E=trimAll(gObj("ctype").value+"");dT=false;cadditionatbeginning=false;if(gObj("ciadditionat2").checked){cadditionatbeginning=false;jd="end"}else{cadditionatbeginning=true;jd="beginning"}if((!isNumber(C))&&(E!="startingamount")){showquickmsg("start principal need to be numeric",true);return }else{if((!isNumber(B))&&(E!="returnrate")){showquickmsg("average investment return need to be numeric",true);return }else{if(((B<0)||(B>200))&&(E!="returnrate")){showquickmsg("I/Y needs to be between 0 and 200",true);return }else{if((!isNumber(D))&&(E!="endamount")){showquickmsg("FV need to be numeric",true);return }else{if((!isNumber(F))&&(E!="contributeamount")){showquickmsg("PMT need to be numeric",true);return }else{if((!isNumber(A))&&(E!="investlength")){showquickmsg("N need to be numeric",true);return }}}}}}FC=0;Sb=0;if(typeof (C)!="undefined"){C=parseFloat(C)}if(typeof (B)!="undefined"){B=parseFloat(B)}if(typeof (A)!="undefined"){A=parseFloat(A)}if(typeof (D)!="undefined"){D=parseFloat(D)}if(typeof (F)!="undefined"){F=parseFloat(F)}tj=parseInt(A*12);theLoanTerm=A;totalCalcCycle=0;Mg="";if(E=="endamount"){}else{if(E=="investlength"){jZ=getInvLength(C,B/100,D,F,dT,100,100);A=jZ;theLoanTerm=A.toFixed(2);Mg="<p>You will need to invest <font color='green'><b>"+jZ.toFixed(3)+"</b></font> periods to reach the future value of "+formatAsMoney(D)+".</p>";tj=parseInt(A*12)}else{if(E=="startingamount"){jZ=getStartingAmount(A,B/100,D,F,dT,D,D);if(jZ=="too long"){showquickmsg("please use reasonable numbers.",true);return }C=jZ;Mg="<p>You will need to invest <font color='green'><b>"+formatAsMoney(jZ)+"</b></font> at the beginning to reach the future value of "+formatAsMoney(D)+".</p>"}else{if(E=="returnrate"){jZ=getReturnRate(A,C,D,F,dT,1,1);if(jZ=="too long"){showquickmsg("please use reasonable numbers.",true);return }jZ=jZ*100;B=jZ;Mg="<p>You will need an annual return rate of <font color='green'><b>"+jZ.toFixed(3)+"%</b></font> to reach the future value of "+formatAsMoney(D)+".</p>"}else{if(E=="contributeamount"){jZ=getContributeAmount(A,C,D,B/100,dT,D,D);if(jZ=="too long"){showquickmsg("please use reasonable numbers.",true);return }F=jZ;Mg="<p>You will need to contribute <font color='green'><b>"+formatAsMoney(jZ)+"</b></font> at the "+jd+" of each period to reach the future value of "+formatAsMoney(D)+".</p>"}}}}}if(dT){QA=0;xh=F}else{QA=F;xh=0}DW=Math.log(B/100+1);GU=DW;fB=new Array();mA=0;CS=0;for(i=1;i<=(A*12);i++){fB[(i-1)]=new Array();if(i==1){fB[(i-1)][0]=C;fB[(i-1)][1]=fB[(i-1)][0];if(cadditionatbeginning){fB[(i-1)][0]+=xh;fB[(i-1)][1]+=xh;if(((i-1)%12)==0){fB[(i-1)][0]+=QA;fB[(i-1)][1]+=QA}}}else{fB[(i-1)][0]=fB[(i-2)][4];fB[(i-1)][1]=fB[(i-2)][2];if(cadditionatbeginning){fB[(i-1)][0]+=xh;fB[(i-1)][1]+=xh;if(((i-1)%12)==0){fB[(i-1)][0]+=QA;fB[(i-1)][1]+=QA}}}fB[(i-1)][2]=fB[(i-1)][1]*Math.pow(Math.E,(DW/12));fB[(i-1)][3]=fB[(i-1)][2]-fB[(i-1)][1];fB[(i-1)][6]=fB[(i-1)][3]*FC/100;fB[(i-1)][2]=fB[(i-1)][2]-fB[(i-1)][6];if(!(cadditionatbeginning)){fB[(i-1)][4]=fB[(i-1)][0]+xh;fB[(i-1)][2]=fB[(i-1)][2]+xh;if((i%12)==0){fB[(i-1)][4]=fB[(i-1)][0]+QA;fB[(i-1)][2]=fB[(i-1)][2]+QA}}else{fB[(i-1)][4]=fB[(i-1)][0]}mA+=fB[(i-1)][3];CS+=fB[(i-1)][6];fB[(i-1)][5]=mA;fB[(i-1)][7]=CS}QU=parseInt(A*12);UI=A*12-QU;if(UI>0.0001){tj++;fB[QU]=new Array();fB[QU][0]=fB[(QU-1)][4];fB[QU][1]=fB[(QU-1)][2];if(cadditionatbeginning){fB[QU][0]+=xh;fB[QU][1]+=xh;if((QU%12)==0){fB[QU][0]+=QA;fB[QU][1]+=QA}}fB[QU][2]=fB[QU][1]*Math.pow(Math.E,(DW*UI/12));fB[QU][3]=fB[QU][2]-fB[QU][1];fB[QU][6]=fB[QU][3]*FC/100;fB[QU][2]=fB[QU][2]-fB[QU][6];fB[QU][4]=fB[QU][0];mA+=fB[QU][3];CS+=fB[QU][6];fB[QU][5]=mA;fB[QU][7]=CS}Yf="<table border=0 cellpadding=3 width='100%'>";Yf+="<tr align=right bgcolor='#E0F0FE'><td>&nbsp;</td><td><b>start principal</b></td><td><b>start balance</b></td><td><b>interest</b></td><td><b>end balance</b></td><td><b>end principal</b></td></tr>";for(i=0;i<parseInt(A);i++){kE=fB[(i*12)][0];lF=fB[(i*12)][1];NJ=fB[(i*12+11)][2];jp=0;DX=fB[(i*12+11)][4];for(KJ=0;KJ<12;KJ++){WS=i*12+KJ;jp+=fB[WS][3]}if((i%2)==1){Yf+="<tr align=right><td>"+(i+1)}else{Yf+="<tr align=right bgcolor='#dddddd'><td>"+(i+1)}Yf+="</td><td>"+formatAsMoney(kE);Yf+="</td><td>"+formatAsMoney(lF);Yf+="</td><td>"+formatAsMoney(jp);Yf+="</td><td>"+formatAsMoney(NJ);Yf+="</td><td>"+formatAsMoney(DX)+"</td></tr>"}if((A-parseInt(A))>0.0001){QU=fB.length-1;i=parseInt(A);kE=fB[(i*12)][0];lF=fB[(i*12)][1];NJ=fB[(QU)][2];jp=0;DX=fB[(QU)][4];for(KJ=(i*12);KJ<=QU;KJ++){WS=KJ;jp+=fB[WS][3]}if((i%2)==1){Yf+="<tr align=right><td>"+(i+1)}else{Yf+="<tr align=right bgcolor='#dddddd'><td>"+(i+1)}Yf+="</td><td>"+formatAsMoney(kE);Yf+="</td><td>"+formatAsMoney(lF);Yf+="</td><td>"+formatAsMoney(jp);Yf+="</td><td>"+formatAsMoney(NJ);Yf+="</td><td>"+formatAsMoney(DX)+"</td></tr>"}Yf+="</table>";UF=fB[(tj-1)][2]*Math.pow((1+B/100),(-1*A));Wk="<hr /><h2>Summary</h2>"+Mg+"<table cellpadding='3' width='80%'>";Wk+="<tr bgcolor='#dddddd'><td>FV (future value)</td><td align=right>"+formatAsMoney(fB[(tj-1)][2])+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>PV (present value)</td><td align=right>"+formatAsMoney(UF)+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>N (number of compounding periods)</td><td align=right>"+A.toFixed(3)+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>I/Y (interest rate per compounding period)</td><td align=right>"+B.toFixed(3)+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>PMT (annuity payment, or constant periodic cash flow)</td><td align=right>"+formatAsMoney(F)+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>starting investment</td><td align=right>"+formatAsMoney(C)+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>total principal</td><td align=right>"+formatAsMoney(fB[(tj-1)][4])+"</td></tr>";Wk+="<tr bgcolor='#dddddd'><td>total interest</td><td align=right>"+formatAsMoney(fB[(tj-1)][5])+"</td></tr>";if(FC>0.0001){Wk+="<tr bgcolor='#dddddd'><td>total tax</td><td align=right>"+formatAsMoney(fB[(tj-1)][7])+"</td></tr>"}Wk+="</table>";dataArray=fB;showquickmsg(Wk,false);gObj("resulttable").innerHTML="<hr /><h2>Schedule</h2>"+Yf;if((delayShow)&&(/msie/i.test(navigator.userAgent)&&!/opera/i.test(navigator.userAgent))){delayShow=false;setTimeout("drawCurve()",2000)}else{delayShow=false;drawCurve()}}function drawCurve(){var F=[];var D=[];var C=[];if(dataArray.length<100){for(var B=0;B<dataArray.length;B++){F.push([(B+1)/12,dataArray[B][4]]);D.push([(B+1)/12,dataArray[B][2]]);C.push([(B+1)/12,dataArray[B][5]])}}else{if(dataArray.length<200){for(var B=0;B<dataArray.length;(B=B+2)){F.push([(B+1)/12,dataArray[B][4]]);D.push([(B+1)/12,dataArray[B][2]]);C.push([(B+1)/12,dataArray[B][5]])}}else{if(dataArray.length<300){for(var B=0;B<dataArray.length;(B=B+3)){F.push([(B+1)/12,dataArray[B][4]]);D.push([(B+1)/12,dataArray[B][2]]);C.push([(B+1)/12,dataArray[B][5]])}}else{for(var B=0;B<dataArray.length;(B=B+4)){F.push([(B+1)/12,dataArray[B][4]]);D.push([(B+1)/12,dataArray[B][2]]);C.push([(B+1)/12,dataArray[B][5]])}}}}function A(G){return G}var E=Flotr.draw($("container"),[{data:F,label:"principal"},{data:D,label:"balance"},{data:C,label:"interest"}],{legend:{position:"ne",labelFormatter:A,backgroundColor:"#D2E8FF"},xaxis:{ticks:[[1/12,""],formatNum(theLoanTerm*0.2),formatNum(theLoanTerm*0.4),formatNum(theLoanTerm*0.6),formatNum(theLoanTerm*0.8),theLoanTerm],max:theLoanTerm}})};
