';s+='';document.write(s);el=document.getElementById('can');ratio=2;el.width=w*ratio;el.height=h*ratio;el.style.width=w+"px";el.style.height=h+"px";g=el.getContext("2d");g.setTransform(ratio,0,0,ratio,0,0);el.addEventListener('touchmove',ontouchmove,false);el.addEventListener('mousemove',onmousemove,false);this.vals=[-1000,200,200,200,200,200];document.getElementById('vals').value=arr2str(this.vals);this.lt=70;this.tp=30;this.wd=420;this.ht=300;my.lblYMax=0;xstt=0;xend=6;ystt=0;yend=10;my.numsQ=true;my.clrs={expense:'#f44',income:'#88f',loan:'#f90',interest:'#c44',repay:'#0ff'}
my.i=0.07;document.getElementById('r1').value=my.i*100;document.getElementById('iLbl').innerHTML=parseInt(my.i*10000)/100+'%';my.periods=[];this.typ=0;go(0);doPop();}
function onIChg(n,v){console.log("onIChg="+n,v);v=Number(v);document.getElementById('iLbl').innerHTML=v+'%';my.i=v/100;go(0);}
function arr2str(arr){return arr.join(',');}
function str2arr(s){return s.split(',');}
function toggleNums(){my.numsQ=!my.numsQ;toggleBtn("numsBtn",my.numsQ);go(-1);}
function toggleDisp(){my.dispGraphQ=!my.dispGraphQ;toggleBtn("dispGraphBtn",my.dispGraphQ);toggleBtn("dispTableBtn",!my.dispGraphQ);go(-1);}
function toggleTyp(){my.typNPVQ=!my.typNPVQ;toggleBtn("typNPVBtn",my.typNPVQ);toggleBtn("typLoanBtn",!my.typNPVQ);go(-1);}
function toggleBtn(btn,onq){if(onq){document.getElementById(btn).classList.add("hi");document.getElementById(btn).classList.remove("lo");}else{document.getElementById(btn).classList.add("lo");document.getElementById(btn).classList.remove("hi");}}
function tbl2vals(){var s='';for(var i=0;i0)s+=',';s+=div.value;}
document.getElementById('vals').value=s;}
function go(n){this.title=document.getElementById('title').value;this.xtitl=document.getElementById('xtitl').value;this.ytitl=document.getElementById('ytitl').value;this.vals=str2arr(document.getElementById('vals').value);if(my.dispGraphQ){document.getElementById('tbl').innerHTML='';el.style.height=h+"px";g.clearRect(0,0,el.width,el.height);if(my.typNPVQ){periodCalc('npv');setScale();drawGraph('');drawBarLine('npv');}else{periodCalc('loan');setScale();drawGraph('');drawBarLine('loan');}}else{el.style.height=0+"px";if(my.typNPVQ){periodCalc('table');drawTable();}else{periodCalc('table');drawTable();}}}
function onmousemove(e){}
function ontouchmove(evt){var touch=evt.targetTouches[0];evt.clientX=touch.clientX;evt.clientY=touch.clientY;evt.touchQ=true;onmousemove(evt);evt.preventDefault();}
function setScale(){xstt=0;xend=my.periods.length+1;xspan=xend-xstt;if(xspan<=0)xspan=0.1;xscale=xspan/this.wd;var ymin=0;var ymax=0;for(var i=0;i=0&&ymax>=0)ystt=0;yspan=yend-ystt;if(Math.abs(yspan)<0.1)yspan=0.1;yscale=yspan/this.ht;}
function periodCalc(typ){my.periods=[];var curr=0;var fact=1;for(i=0;i';if(my.typNPVQ){s+=getDivStr(id,'','period');s+=getDivStr(id,'In/Out','title');s+=getDivStr(id,'PV','title');s+=getDivStr(id,'NPV','title');}else{s+=getDivStr(id,'','period');s+=getDivStr(id,'Loan/Repay','title');s+=getDivStr(id,'Interest','title');s+=getDivStr(id,'Debt','title');}
s+='';for(var i=0;i';if(my.typNPVQ){s+=getDivStr(id,i,'period');s+=getDivStr(id,per.cash,'in');s+=getDivStr(id,per.pv,'fix');s+=getDivStr(id,per.fin,'fix');}else{s+=getDivStr(id,i,'period');s+=getDivStr(id,per.cash,'in');s+=getDivStr(id,per.int,'fix');s+=getDivStr(id,per.fin,'fix');}
s+='';}
var div=document.getElementById('tbl');div.innerHTML=s;}
function getDivStr(id,val,typ){var s='';switch(typ){case 'period':s+='';s+=val;s+='';break;case 'title':s+='';s+=val;s+='';break;case 'in':s+=' ';break;case 'fix':s+='';s+=fmt2(val);s+='';break;default:}
return s;}
function fmt2(v){if(Math.round(v)==v){return v;}else{return parseFloat(Math.round(v*100)/100).toFixed(2);}}
function drawBarLine(typ){g.font='18px Verdana';g.textAlign='center';g.fillStyle='black';g.fillText(this.title,this.lt+this.wd/2,this.tp-8);g.font='16px Verdana';g.fillStyle='blue';g.fillText(this.xtitl,this.lt+this.wd/2,my.lblYMax+20);g.fillStyle='darkorange';var txt=this.ytitl;var rot=-Math.PI/2;g.rotate(rot);g.fillText(txt,-ht/2-10,15);g.rotate(-rot);g.font='14px Verdana';g.textAlign='center';TotVal=0;if(my.pctsQ){for(i=0;i",i,xp);var period=my.periods[i];var stt=period.stt;var mid=period.incInt;var fin=period.fin;barwidth=0.8*this.wd/xspan;if(barwidth>50)barwidth=50;switch(typ){case "npv":g.strokeStyle='black';g.beginPath();if(stt>=fin){g.fillStyle=my.clrs.expense;}else{g.fillStyle=my.clrs.income;}
g.rect(xp,zero-stt/yscale,barwidth/2,(stt-fin)/yscale);g.stroke();g.fill();if(my.numsQ){var yText=-3;if(mid-fin>0)yText=12;g.fillStyle='black';g.beginPath();g.fillText(fmt2(fin),xp+barwidth/4,zero-fin/yscale+yText);g.fill();}
break;case "loan":g.strokeStyle='black';g.beginPath();g.fillStyle=my.clrs.interest;g.rect(xp-barwidth/2,zero-stt/yscale,barwidth/2,(stt-mid)/yscale);g.stroke();g.fill();g.beginPath();if(stt>=fin){g.fillStyle=my.clrs.loan;}else{g.fillStyle=my.clrs.repay;}
g.rect(xp,zero-mid/yscale,barwidth/2,(mid-fin)/yscale);g.stroke();g.fill();if(my.numsQ){var yText=-3;if(mid-fin>0)yText=17;g.fillStyle='black';g.beginPath();g.fillText(fmt2(fin),xp+barwidth/4,zero-fin/yscale+yText);g.fill();}
break;default:}}}
function getPlace(angle){var places=[[0,75,[0,0,"left"]],[75,105,[0,15,"center"]],[105,255,[0,0,"right"]],[255,285,[0,0,"center"]],[285,360,[]]];places[4][2]=places[0][2];var angled=angle*180/Math.PI;angled-=Math.round(angled/360-0.5)*360;for(var i=0;i=places[i][0]&&angled<=places[i][1]){return(places[i][2]);}}
return(places[0][2]);}
function drawGraph(typ){g.beginPath();g.lineWidth=1;g.strokeStyle='black';g.fillStyle='rgba(0,0,255,0.3)';g.rect(this.lt,this.tp,this.wd,this.ht);g.stroke();my.lblYMax=0;xIntq=true;xMajorTick=1;xMinorTick=1;yIntq=false;if(yIntq){yMajorTick=1;yMinorTick=1;}else{LogSpan=Math.log(yspan*0.6)*Math.LOG10E;yMajorTick=Math.pow(10,Math.floor(LogSpan));if((LogSpan-Math.floor(LogSpan))<0.4){yMajorTick/=2;}
yMinorTick=yMajorTick/5;}
g.strokeStyle='rgba(0,0,255,0.2)';xTick=xMajorTick;yTick=yMajorTick;for(var x=Math.ceil(xstt/xTick)*xTick;x<=xend;x+=xTick){i=(x-xstt)/xscale;label=(x-1).toString();lineq=false;labelq=false;inputid=undefined;if(xIntq){if(x>xstt&&x2)txtWidth=this.wd/xend;g.font='13px Verdana';g.fillStyle='blue';g.textAlign='center';wrapText(g,label,this.lt+i,this.tp+this.ht+18,txtWidth,16);}}
for(var y=Math.round(ystt/yTick)*yTick;y<=yend;y+=yTick){j=(yend-y)/yscale;label=y;lineq=false;labelq=true;inputid=undefined;if(yIntq){if(TickLevel==0){if(y>ystt&&y");win.document.location="#";}
function canvasPrint(){var can=document.getElementById("can");var c2=document.createElement('canvas');c2.width=can.width*0.8;c2.height=can.height*0.8;g2=c2.getContext("2d");g2.drawImage(can,0,0,can.width,can.height,0,0,c2.width,c2.height);var dataUrl=c2.toDataURL('image/png');var win=window.open();win.document.write("");win.document.location="#";win.setTimeout(function(){win.focus();win.print();},500);}
function wrapText(context,text,x,y,maxWidth,lineHeight){var words=text.split(' ');var line='';for(var n=0;nmaxWidth&&n>0){context.fillText(line,x,y);line=words[n]+' ';y+=lineHeight;}else{line=testLine;}}
my.lblYMax=Math.max(my.lblYMax,y);context.fillText(line,x,y);}
function getPopHTML(){s='';s+='';s+='
';inps=[['Investment','popInv',1000],['Income','popInc',200],['Number of Periods','popn',10],['Interest Rate (%)','popInt',5]];for(var i=0;i';s+=''+inp[0]+'';s+=' ';s+='
'}
s+='
';s+='';s+='';s+='
';s+='';return s;}
function doPop(){console.log("doPop");if(my.typNPVQ){document.getElementById('popInvLbl').innerHTML='Investment';document.getElementById('popIncLbl').innerHTML='Income';}else{document.getElementById('popInvLbl').innerHTML='Loan Amount';document.getElementById('popIncLbl').innerHTML='Payment Amount';}
var pop=document.getElementById('editpop');pop.style.transitionDuration="0.3s";pop.style.opacity=1;pop.style.zIndex=12;pop.style.left='50px';}
function editYes(){var pop=document.getElementById('editpop');pop.style.opacity=0;pop.style.zIndex=1;pop.style.left='-500px';console.log("editYes",my.fn);my.i=Number(document.getElementById('popInt').value)/100;document.getElementById('r1').value=my.i*100;document.getElementById('iLbl').innerHTML=parseInt(my.i*10000)/100+'%';var inv=Number(document.getElementById('popInv').value);var inc=Number(document.getElementById('popInc').value);var n=Number(document.getElementById('popn').value);var s=(-inv).toString();for(var i=0;i