/***************************************\ * Financial Calculator(s) * * March, 2005 by Harley H. Puthuff * * Copyright 2005, Your Showcase * \***************************************/ /* * Global object */ var Our = new Object(); // global Class/Object Our.sponsor = (typeof sponsor=='undefined') ? 'Your Showcase' : sponsor; Our.calculator = 0; // object with calculations Our.months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); Our.date = new Date(); // current date Our.month = Our.date.getMonth()+1; // current month Our.year = Our.date.getFullYear(); // current year Our.round = function(v) { // round to nearest cent return (Math.round(v*100.0)/100.0); }; Our.roundup = function(v) { // round up to nearest cent return (Math.ceil(v*100.0)/100.0); }; Our.rounddown = function(v) { // round down to nearest cent return (Math.floor(v*100.0)/100.0); }; Our.number = function(v) { // convert to number var p=new String(v),r=new String(''),i,d; for (i=0;i='0')&&(d<='9'))) r+=d; } if (r=='') r='0'; return Number(r); }; Our.currency = function(v) { // convert to currency ($) var t='',c,p; c=(this.round(this.number(v))).toString(); p=c.split('.'); if (p.length<2) p[1]='00'; if (p[1].length==1) p[1]=p[1]+'0'; if (p[1].length>2) p[1]=p[1].substring(0,2); if (p[0].length==0) p[0]='0'; for (c=p[0]; c.length>3;) { t=','+c.substr(c.length-3)+t; c=c.substr(0,(c.length-3)); } return(c+t+'.'+p[1]); }; Our.term = function(months,years) { // convert to "nn month(s) and nn year(s)" var m=this.number(months),y=this.number(years),s=''; if (m>11) {y+=Math.floor(m/12); m%=12;} if (y>0) {s+=y.toString()+' year'; if (y>1) s+='s';} if (m>0) {if (s.length>0) s+=' and '; s+=m.toString()+' month'; if (m>1) s+='s';} return s; }; Our.selectDate = function() { // returns text for date selector var m,y,t,i,j,e,yr; m=this.month; y=this.year; if (++m>12) {m=1; y++;} t=' ' + ''; return t; }; Our.selectYears = function() { // returns text for #years selector var i,yr,t; t=''; return t; }; /* * Constructor for a Calculator object: * P1 = loan amount or 0 * P2 = loan term (years) or 0 * P3 = loan rate (apr) or 0 * P4 = loan monthly payment or 0 * P5 = (optional) first payment month (1-12) * P6 = (optional) first payment year (2000-????) */ function Calculator(name,amount,term,rate,payment,month,year,years) { var x,y; this.name = new String(name); this.amount = Our.number(amount); this.term = Our.number(term); this.rate = Our.number(rate); this.payment = Our.number(payment); this.firstMonth = Our.month; this.firstYear = Our.year; if (++this.firstMonth > 12) {this.firstMonth = 1; ++this.firstYear;} x = Our.number(month); if (x > 0) this.firstMonth = x; y = Our.number(year); if (y > 0) this.firstYear = y; this.limit = Our.number(years) * 12; if (this.limit == 0) this.limit = this.term * 12; if (this.term == 0) { var mpr = this.rate / 1200; var months = (Math.log(this.payment) - Math.log(this.payment - (this.amount * mpr))) / Math.log(1 + mpr); this.term = Math.ceil(months / 12); } if (this.rate == 0) { var months = this.term * 12.0; var lastrate = 0.0; var thisrate = 80.0; var adjustment,mpr,principal; while (1) { adjustment = Math.abs(thisrate - lastrate) / 2.0; mpr = thisrate / 1200.0; principal = this.payment * ((Math.pow((1.0 + mpr), months) - 1.0) / (mpr * Math.pow((1.0 + mpr), months))); if (Math.round(principal) == this.amount) { this.rate = Math.round(thisrate * 100.0) / 100.0; break; } lastrate = thisrate; if (principal > this.amount) thisrate += adjustment; else thisrate -= adjustment; } } this.months = this.term * 12.0; this.apr = this.rate / 100.0; this.mpr = this.apr / 12.0; if (this.amount == 0) { this.amount = this.payment * ((Math.pow((1.0 + this.mpr), this.months) - 1.0) / (this.mpr * Math.pow((1.0 + this.mpr), this.months))); } if (this.payment == 0) { this.payment = (this.amount * Math.pow((1.0 + this.mpr), this.months) * this.mpr) / (Math.pow((1.0 + this.mpr), this.months) - 1.0); this.payment = Our.roundup(this.payment); } x = (this.firstYear*12) + (this.firstMonth-1) + (this.months-1); this.lastMonth = (x%12)+1; this.lastYear = Math.floor(x/12); this.numberOfPayments = this.months; this.totalOfPayments = this.payment * this.numberOfPayments; this.financeCharge = this.totalOfPayments - this.amount; } /* * Calculator method: show mortgage/loan details * returns text for a page with mortgage details */ Calculator.prototype.details = function () { return( '\n' + '\n' + '' + this.name + '\n' + '\n' + '\n' + '
\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '
Results for ' + this.name + ' calculations
Amount Financed$ ' + Our.currency(this.amount) + '
Term' + this.term.toString() + ' years
Interest Rate' + Our.currency(this.rate) + '%
Monthly Payment$ ' + Our.currency(this.payment) + '
First Payment' + Our.months[this.firstMonth-1] + ', ' + this.firstYear.toString() + '
Last Payment' + Our.months[this.lastMonth-1] + ', ' + this.lastYear.toString() + '
Number of Payments' + this.numberOfPayments.toString() + '
Finance Charge$ ' + Our.currency(this.financeCharge) + '
Total of Payments$ ' + Our.currency(this.totalOfPayments) + '
\n' + '' ); }; /* * Calculator method: show amortization * returns text for a page with amortization */ Calculator.prototype.amortization = function() { var title = this.name + ' Amortization'; var row = 'even'; var text = '\n' + '\n' + '' + title + '\n' + '\n' + '\n' + '
\n' + '

' + title + '

\n'; text += '\n' + '' + '
\n' + '\n' + '\n' + '\n' + '\n' + '
Mortgage amount' + Our.currency(this.amount) + '
Term' + this.term.toString() + ' years
First payment' + Our.months[this.firstMonth-1] + ' ' + this.firstYear.toString() + '
Last payment' + Our.months[this.lastMonth-1] + ' ' + this.lastYear.toString() + '
\n' + '\n' + '\n' + '\n' + '\n' + '
Interest Rate' + this.rate.toString() + '%
Monthly payment' + Our.currency(this.payment) + '
Total interest' + Our.currency(this.financeCharge) + '
Total of payments' + Our.currency(this.totalOfPayments) + '
\n'; this.current = new Object(); this.current.number = 1; this.current.date = ''; this.current.balance = this.amount; this.current.month = this.firstMonth-1; this.current.year = this.firstYear; this.current.payment = this.payment; this.current.interest = 0.00; this.current.principal = 0.00; this.current.ytdInterest = 0.00; this.current.ytdPrincipal = 0.00; this.current.totalInterest = 0.00; this.current.totalPrincipal = 0.00; this.current.remaining = 0.00; text += '

\n'; while (this.current.balance > 0.00) { if ((this.current.number == 1) || (this.current.month == 0)) { text += '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n'; } this.current.date = Our.months[this.current.month] + ' ' + this.current.year.toString(); this.current.interest = Our.rounddown(this.current.balance * this.mpr); this.current.principal = this.current.payment - this.current.interest; if (this.current.principal > this.current.balance) this.current.principal = this.current.balance; this.current.ytdInterest += this.current.interest; this.current.ytdPrincipal += this.current.principal; this.current.totalInterest += this.current.interest; this.current.totalPrincipal += this.current.principal; this.current.remaining = this.current.balance - this.current.principal; row = (row == 'odd') ? 'even' : 'odd'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; text += '\n'; if (++this.current.number > this.limit) break; this.current.balance = this.current.remaining; if (++this.current.month > 11) { this.current.month = 0; ++this.current.year; this.current.ytdInterest = 0.00; this.current.ytdPrincipal = 0.00; } } text += '
#DateInterestPrincipalInt-YTDPrin-YTDTotal IntTotal PrinRemaining
' + this.current.number.toString() + '' + this.current.date + '' + Our.currency(this.current.interest) + '' + Our.currency(this.current.principal) + '' + Our.currency(this.current.ytdInterest) + '' + Our.currency(this.current.ytdPrincipal) + '' + Our.currency(this.current.totalInterest) + '' + Our.currency(this.current.totalPrincipal) + '' + Our.currency(this.current.remaining) + '

\n' + '

\n' + '
\n' + '\n'; return text; }; /* * Evaluate the input fields and perform a calculation (if necessary) */ function calculate() { var f = document.parameters,values=0; var Amount = Our.number(f.Amount.value); if (Amount>0) ++values; var Rate = Our.number(f.Rate.value); if (Rate>0) ++values; var Term = Our.number(f.Term.value); if (Term>0) ++values; var Payment = Our.number(f.Payment.value); if (Payment>0) ++values; var Month = f.Month.selectedIndex+1; var Year = f.Year.options[f.Year.selectedIndex].value; var Years = f.Years.options[f.Years.selectedIndex].value; if (Years == 0) Years = Term; if (values < 3) { alert('I need at least three of the four values!'); f.Amount.focus(); return; } if ((Amount>0) && (Rate>0) && (Term>0)) Payment=0; Our.calculator = new Calculator('Your Mortgage',Amount,Term,Rate,Payment,Month,Year,Years); f.Amount.value = Our.currency(Our.calculator.amount); f.Rate.value = Our.currency(Our.calculator.rate) + '%'; f.Term.value = Math.floor(Our.calculator.term).toString() + ' years'; f.Payment.value = Our.currency(Our.calculator.payment); results.location = "javascript:parent.Our.calculator.details()"; } /* * Show splash page in the results frame */ function splash() { return( '\n' + '\n' + '\n' + '\n' + '\n' + Our.sponsor + '
presents\n' + '

\n' + '

Your
Mortgage
Calculator

\n' + '\n' + '' ); } /* * Clear all values and restart */ function restart() { var f = document.parameters,values=0; f.Amount.value = f.Rate.value = f.Term.value = f.Payment.value = ''; Our.calculator = 0; results.location = "javascript:parent.splash()"; f.Amount.focus(); } /* * insert the calculator html */ var d=window.document; d.writeln('
'); d.writeln(''); d.writeln(''); d.writeln(''); d.writeln(''); d.writeln('
'); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln(' '); d.writeln('
Mortgage Calculator
'); d.writeln(' Fill in any three of the values shown below then click the [Calculate...] button and the'); d.writeln(' fourth value will be automatically calculated for you. You may then show the amortization,'); d.writeln(' print the results, or perform another calculation.'); d.writeln('
Mortgage Amount
Interest Rate
Term (years)
Payments
Starting Date'+Our.selectDate()+'
Amortize for:'+Our.selectYears()+'
Copyright ©2005
'); d.writeln(' '); d.writeln('
'); d.writeln('
'); results.location='javascript:parent.splash()';