/*****************************************
(C) http://www.calculator.net all right reserved.  
*****************************************/
var dataArray=new Array();var theLoanTerm=0;totalCalcCycle=0;cutoffCalcCycle=1000;cutoffPrecision=0.000001;var delayShow=true;function popMenu(A){dY="";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"){dY=dY+'<li><a href="#" onclick="popMenu(\'contributeamount\');">Contribute Amount</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'endamount\');">End Amount</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">Return Rate</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">Starting Amount</a></li><li><a href="#" onclick="popMenu(\'investlength\');">Invest Length</a></li>';gObj("cendamount").style.display="none"}else{if(A=="investlength"){dY=dY+'<li><a href="#" onclick="popMenu(\'contributeamount\');">Contribute Amount</a></li><li><a href="#" onclick="popMenu(\'endamount\');">End Amount</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">Return Rate</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">Starting Amount</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'investlength\');">Invest Length</a></li>';gObj("cinvestlength").style.display="none"}else{if(A=="startingamount"){dY=dY+'<li><a href="#" onclick="popMenu(\'contributeamount\');">Contribute Amount</a></li><li><a href="#" onclick="popMenu(\'endamount\');">End Amount</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">Return Rate</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'startingamount\');">Starting Amount</a></li><li><a href="#" onclick="popMenu(\'investlength\');">Invest Length</a></li>';gObj("cstartingamount").style.display="none"}else{if(A=="returnrate"){dY=dY+'<li><a href="#" onclick="popMenu(\'contributeamount\');">Contribute Amount</a></li><li><a href="#" onclick="popMenu(\'endamount\');">End Amount</a></li><li id=\'menuon\'><a href="#" onclick="popMenu(\'returnrate\');">Return Rate</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">Starting Amount</a></li><li><a href="#" onclick="popMenu(\'investlength\');">Invest Length</a></li>';gObj("creturnrate").style.display="none"}else{if(A=="contributeamount"){dY=dY+'<li id=\'menuon\'><a href="#" onclick="popMenu(\'contributeamount\');">Contribute Amount</a></li><li><a href="#" onclick="popMenu(\'endamount\');">End Amount</a></li><li><a href="#" onclick="popMenu(\'returnrate\');">Return Rate</a></li><li><a href="#" onclick="popMenu(\'startingamount\');">Starting Amount</a></li><li><a href="#" onclick="popMenu(\'investlength\');">Invest Length</a></li>';gObj("ccontributeamount").style.display="none"}}}}}dY="<ul>"+dY+"</ul>";gObj("topmenu").innerHTML=dY;calc()}function calcEndVal(C,E,B,D,A){aL=C*Math.pow((1+E),D);if(A){E=Math.pow((E+1),1/12)-1;D=D*12}for(i=1;i<=D;i++){aL+=B*Math.pow((1+E),(D-i))}return aL}function getInvLength(F,G,B,E,D,C,A){lj=calcEndVal(F,G,E,C,D);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){if(lj>B){return C}}if((lj!=0)&&(B!=0)){if(Math.abs((lj-B)/B)<cutoffPrecision){return C}}else{if(Math.abs(lj-B)<cutoffPrecision){return C}}if(lj>B){return getInvLength(F,G,B,E,D,(C-0.7*A),(0.7*A))}else{return getInvLength(F,G,B,E,D,(C+0.7*A),(0.7*A))}}function getStartingAmount(E,G,B,D,C,F,A){lj=calcEndVal(F,G,D,E,C);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){return"too long"}if((lj!=0)&&(B!=0)){if(Math.abs((lj-B)/B)<cutoffPrecision){return F}}else{if(Math.abs(lj-B)<cutoffPrecision){return F}}if(lj>B){return getStartingAmount(E,G,B,D,C,(F-0.7*A),(0.7*A))}else{return getStartingAmount(E,G,B,D,C,(F+0.7*A),(0.7*A))}}function getReturnRate(G,F,B,E,D,C,A){lj=calcEndVal(F,C,E,G,D);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){return"too long"}if((lj!=0)&&(B!=0)){if(Math.abs((lj-B)/B)<cutoffPrecision){return C}}else{if(Math.abs(lj-B)<cutoffPrecision){return C}}if(lj>B){return getReturnRate(G,F,B,E,D,(C-0.7*A),(0.7*A))}else{return getReturnRate(G,F,B,E,D,(C+0.7*A),(0.7*A))}}function getContributeAmount(F,E,C,G,D,B,A){lj=calcEndVal(E,G,B,F,D);totalCalcCycle++;if(totalCalcCycle>cutoffCalcCycle){return"too long"}if((lj!=0)&&(C!=0)){if(Math.abs((lj-C)/C)<cutoffPrecision){return B}}else{if(Math.abs(lj-C)<cutoffPrecision){return B}}if(lj>C){return getContributeAmount(F,E,C,G,D,(B-0.7*A),(0.7*A))}else{return getContributeAmount(F,E,C,G,D,(B+0.7*A),(0.7*A))}}function calc(){showquickmsg("calculating...",true);gObj("resulttable").innerHTML="";setTimeout("process()",2)}function process(){var C=trimAll(gObj("cstartingprinciplev").value+"");var F=trimAll(gObj("cinterestratev").value+"");var D=trimAll(gObj("cyearsv").value+"");var A=trimAll(gObj("ctargetamountv").value+"");var B=trimAll(gObj("ccontributeamountv").value+"");var E=trimAll(gObj("ctype").value+"");FO=true;if(gObj("ciadditionat2").checked){FO=false}IV=false;if((!isNumber(C))&&(E!="startingamount")){showquickmsg("starting principle need to be numeric",true);return }else{if((!isNumber(F))&&(E!="returnrate")){showquickmsg("average investment return need to be numeric",true);return }else{if(((F<0)||(F>200))&&(E!="returnrate")){showquickmsg("average investment return needs to be between 0 and 200",true);return }else{if((!isNumber(A))&&(E!="endamount")){showquickmsg("your target need to be numeric",true);return }else{if((!isNumber(B))&&(E!="contributeamount")){showquickmsg("contribute amount need to be numeric",true);return }else{if((!isNumber(D))&&(E!="investlength")){showquickmsg("investment years need to be numeric",true);return }}}}}}ao=0;RG=0;if(typeof (C)!="undefined"){C=parseFloat(C)}if(typeof (F)!="undefined"){F=parseFloat(F)}if(typeof (D)!="undefined"){D=parseFloat(D)}if(typeof (A)!="undefined"){A=parseFloat(A)}if(typeof (B)!="undefined"){B=parseFloat(B)}Gt=parseInt(D*12);theLoanTerm=D;totalCalcCycle=0;AR="";if(E=="endamount"){}else{if(E=="investlength"){ki=getInvLength(C,F/100,A,B,FO,100,100);D=ki;theLoanTerm=D.toFixed(2);AR="<p>You will need to invest <font color='green'><b>"+ki.toFixed(3)+"</b></font> years to reach the target of "+formatAsMoney(A)+".</p>";Gt=parseInt(D*12)}else{if(E=="startingamount"){ki=getStartingAmount(D,F/100,A,B,FO,A,A);if(ki=="too long"){showquickmsg("please use reasonable numbers.",true);return }C=ki;AR="<p>You will need to invest <font color='green'><b>"+formatAsMoney(ki)+"</b></font> at the beginning to reach the target of "+formatAsMoney(A)+".</p>"}else{if(E=="returnrate"){ki=getReturnRate(D,C,A,B,FO,1,1);if(ki=="too long"){showquickmsg("please use reasonable numbers.",true);return }ki=ki*100;F=ki;AR="<p>You will need an annual return rate of <font color='green'><b>"+ki.toFixed(3)+"%</b></font> to reach the target of "+formatAsMoney(A)+".</p>"}else{if(E=="contributeamount"){ki=getContributeAmount(D,C,A,F/100,FO,A,A);if(ki=="too long"){showquickmsg("please use reasonable numbers.",true);return }B=ki;if(FO){AR="<p>You will need to contribute <font color='green'><b>"+formatAsMoney(ki)+"</b></font> at the end of each month to reach the target of "+formatAsMoney(A)+".</p>"}else{AR="<p>You will need to contribute <font color='green'><b>"+formatAsMoney(ki)+"</b></font> at the end of each year to reach the target of "+formatAsMoney(A)+".</p>"}}}}}}if(FO){Vf=0;AU=B}else{Vf=B;AU=0}il=Math.log(F/100+1);oQ=il;gC=new Array();cx=0;oo=0;for(i=1;i<=(D*12);i++){gC[(i-1)]=new Array();if(i==1){gC[(i-1)][0]=C;gC[(i-1)][1]=gC[(i-1)][0];if(IV){gC[(i-1)][0]+=AU;gC[(i-1)][1]+=AU;if(((i-1)%12)==0){gC[(i-1)][0]+=Vf;gC[(i-1)][1]+=Vf}}}else{gC[(i-1)][0]=gC[(i-2)][4];gC[(i-1)][1]=gC[(i-2)][2];if(IV){gC[(i-1)][0]+=AU;gC[(i-1)][1]+=AU;if(((i-1)%12)==0){gC[(i-1)][0]+=Vf;gC[(i-1)][1]+=Vf}}}gC[(i-1)][2]=gC[(i-1)][1]*Math.pow(Math.E,(il/12));gC[(i-1)][3]=gC[(i-1)][2]-gC[(i-1)][1];gC[(i-1)][6]=gC[(i-1)][3]*ao/100;gC[(i-1)][2]=gC[(i-1)][2]-gC[(i-1)][6];if(!(IV)){gC[(i-1)][4]=gC[(i-1)][0]+AU;gC[(i-1)][2]=gC[(i-1)][2]+AU;if((i%12)==0){gC[(i-1)][4]=gC[(i-1)][0]+Vf;gC[(i-1)][2]=gC[(i-1)][2]+Vf}}else{gC[(i-1)][4]=gC[(i-1)][0]}cx+=gC[(i-1)][3];oo+=gC[(i-1)][6];gC[(i-1)][5]=cx;gC[(i-1)][7]=oo}gM=parseInt(D*12);jQ=D*12-gM;if(jQ>0.0001){Gt++;gC[gM]=new Array();gC[gM][0]=gC[(gM-1)][4];gC[gM][1]=gC[(gM-1)][2];if(IV){gC[gM][0]+=AU;gC[gM][1]+=AU;if((gM%12)==0){gC[gM][0]+=Vf;gC[gM][1]+=Vf}}gC[gM][2]=gC[gM][1]*Math.pow(Math.E,(il*jQ/12));gC[gM][3]=gC[gM][2]-gC[gM][1];gC[gM][6]=gC[gM][3]*ao/100;gC[gM][2]=gC[gM][2]-gC[gM][6];gC[gM][4]=gC[gM][0];cx+=gC[gM][3];oo+=gC[gM][6];gC[gM][5]=cx;gC[gM][7]=oo}ej="<table border=0 cellpadding=3 width='100%'>";ej+="<tr align=right bgcolor='#E0F0FE'><td>&nbsp;</td><td><b>start principle</b></td><td><b>start balance</b></td><td><b>interest</b></td><td><b>end balance</b></td><td><b>end principle</b></td></tr>";for(i=1;i<=(D*12);i++){if((i%2)==1){ej+="<tr align=right bgcolor='#dddddd'><td>"+i}else{ej+="<tr align=right><td>"+i}ej+="</td><td>"+formatAsMoney(gC[(i-1)][0])+"</td><td>"+formatAsMoney(gC[(i-1)][1])+"</td><td>"+formatAsMoney(gC[(i-1)][3])+"</td><td>"+formatAsMoney(gC[(i-1)][2])+"</td><td>"+formatAsMoney(gC[(i-1)][4])+"</td></tr>";if((i%12)==0){pN=parseInt(i/12);ej+="<tr bgcolor='#E0F0FE' align='center'><td colspan='6'><b>year "+pN+" end</b></td></tr>"}}if(jQ>0.0001){Bk=(gM+jQ).toFixed(3);ej+="<tr align=right><td>"+Bk+"</td><td>"+formatAsMoney(gC[gM][0])+"</td><td>"+formatAsMoney(gC[gM][1])+"</td><td>"+formatAsMoney(gC[gM][3])+"</td><td>"+formatAsMoney(gC[gM][2])+"</td><td>"+formatAsMoney(gC[gM][4])+"</td></tr>"}ej+="</table>";Gn="<hr /><h2>Summary</h2>"+AR+"<table cellpadding='3' width='60%'>";Gn+="<tr bgcolor='#dddddd'><td><b>end balance</b></td><td align=right><b>"+formatAsMoney(gC[(Gt-1)][2])+"</b></td></tr>";if(RG!=0){pL=gC[(Gt-1)][2]/Math.pow((1+RG/100),D);Gn+="<tr bgcolor='#dddddd'><td>after inflation adjustment</td><td align=right>"+formatAsMoney(pL)+"</td></tr>"}Gn+="<tr bgcolor='#dddddd'><td>total principle</td><td align=right>"+formatAsMoney(gC[(Gt-1)][4])+"</td></tr>";Gn+="<tr bgcolor='#dddddd'><td>total interest</td><td align=right>"+formatAsMoney(gC[(Gt-1)][5])+"</td></tr>";if(ao>0.0001){Gn+="<tr bgcolor='#dddddd'><td>total tax</td><td align=right>"+formatAsMoney(gC[(Gt-1)][7])+"</td></tr>"}Gn+="</table>";dataArray=gC;showquickmsg(Gn,false);gObj("resulttable").innerHTML="<hr /><h2>Monthly Schedule</h2>"+ej;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:"principle"},{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}})};