home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 November / pcwk_11_98a.iso / Wtestowe / Money99 / money99.1 / savapp / logic.js < prev    next >
Text File  |  1998-07-25  |  7KB  |  249 lines

  1. // This function calculates annual interest rate income on monthly
  2. // compound basis.
  3. function annualDeposit(balance, interest)
  4. {
  5.     var    numMon = 12
  6.     var    retBal = balance
  7.     var    monthlyInt = interest / numMon
  8.  
  9.     while(numMon > 0) {            // do the monthly compounding
  10.         retBal = retBal * (1 + monthlyInt)
  11.         numMon = numMon - 1
  12.     }
  13.     retBal = retBal - balance    // get net interest rate income
  14.     return retBal
  15. }
  16.  
  17. // This function calculates annual interest rate income on monthly
  18. // compound and quarterly deposit basis.
  19. function quarterDeposit(balance, interest, amount)
  20. {
  21.     var    retBal = balance
  22.     var    monthlyInt = interest / 12    // monthly interest
  23.     var    i, j
  24.     
  25.     i = 0
  26.     while(i < 4) {            // quarterly loop
  27.         j = 0
  28.         while(j < 3) {        // monthly loop
  29.             retBal = retBal * (1 + monthlyInt)
  30.             j = j + 1
  31.         }
  32.         i = i + 1
  33.         retBal = retBal + amount    // add the quarterly deposit
  34.     }
  35.     retBal = retBal - (balance + amount * 4)    // net interest rate income
  36.     return retBal
  37. }
  38.  
  39. // This function calculates annual interest rate income on monthly
  40. // compound and monthly deposit basis.
  41. function monthDeposit(balance, interest, amount)
  42. {
  43.     var retBal = balance
  44.     var    monthlyInt = interest / 12    // monthly interest
  45.     var i
  46.  
  47.     i = 0
  48.     while(i < 12) {
  49.         retBal = retBal * (1 + monthlyInt)
  50.         retBal = retBal + amount
  51.         i = i + 1
  52.     }
  53.     retBal = retBal - (balance + amount * 12)
  54.     return retBal
  55. }
  56.  
  57. // This function calculates all the values for the result page.
  58. function getResultSet1()
  59. {
  60.     var sto = parent.data
  61.     var    numYear
  62.     var    prevBal, totalTax, totalInterest, yearlyRet, yearlyDep
  63.  
  64.     prevBal = sto.deposit_balance
  65.     totalTax = 0
  66.     totalInterest = 0
  67.  
  68.     numYear = sto.deposit_term
  69.  
  70.     if(sto.deposit_freq == 0)        // yearly deposit
  71.         sto.result_deposit = sto.deposit_regular * numYear
  72.     else if(sto.deposit_freq == 1)    // quarterly deposit
  73.         sto.result_deposit = sto.deposit_regular * numYear * 4
  74.     else if(sto.deposit_freq == 2)    // monthly deposit
  75.         sto.result_deposit = sto.deposit_regular * numYear * 12
  76.     sto.result_deposit = Math.round(sto.result_deposit)
  77.  
  78.     for(i=0; i<numYear; i++) {
  79.         if(sto.deposit_freq == 0) {            // deposit annually
  80.             yearlyRet = annualDeposit(prevBal, sto.interest_rate)
  81.             yearlyDep = sto.deposit_regular
  82.         }
  83.         else if(sto.deposit_freq == 1) {    // deposit quarterly
  84.             yearlyRet = quarterDeposit(prevBal, sto.interest_rate, sto.deposit_regular)
  85.             yearlyDep = sto.deposit_regular * 4
  86.         }
  87.         else if(sto.deposit_freq == 2) {    // deposit monthly
  88.             yearlyRet = monthDeposit(prevBal, sto.interest_rate, sto.deposit_regular)
  89.             yearlyDep = sto.deposit_regular * 12
  90.         }
  91.  
  92.         // interest earning is pre-taxed.
  93.         totalInterest = totalInterest + yearlyRet
  94.  
  95.         // net saving is either taxed or not
  96.         if(sto.taxrate_istaxed == 1) {
  97.             totalTax = totalTax + yearlyRet * sto.taxrate_taxrate
  98.             prevBal = prevBal + yearlyRet * ( 1 - sto.taxrate_taxrate)
  99.         }
  100.         else
  101.             prevBal = prevBal + yearlyRet
  102.         
  103.         prevBal += yearlyDep
  104.     }
  105.     sto.result_saved = Math.round(prevBal)
  106.     sto.result_interest = Math.round(totalInterest)
  107.     sto.result_tax = Math.round(totalTax)
  108.     sto.result_term = sto.deposit_term * 12
  109.     sto.result_regular = sto.deposit_regular
  110. }
  111.  
  112. function getResultSet2(goal)
  113. {
  114.     var sto = parent.data
  115.     var    numMonth, numYear, numQuarter
  116.     var    prevBal, totalTax, totalInterest
  117.     var monthlyRet, monthlyDep, cumMonthlyRet
  118.  
  119.     prevBal = sto.deposit_balance
  120.     totalTax = totalInterest = numMonth = cumMonthlyRet = 0
  121.  
  122.     while(1) {
  123.         monthlyRet = prevBal * sto.interest_rate / 12
  124.  
  125.         if(((sto.deposit_freq==0) && (numMonth%12==0) && (numMonth>0)) ||
  126.             ((sto.deposit_freq==1) && (numMonth%3==0) && (numMonth>0)) ||
  127.             ((sto.deposit_freq==2) && (numMonth>0)))
  128.             monthlyDep = sto.deposit_regular
  129.         else
  130.             monthlyDep = 0
  131.  
  132.         cumMonthlyRet += monthlyRet            // do the cumulative monthly return
  133.  
  134.         // interest earning is pre-taxed.
  135.         totalInterest = totalInterest + monthlyRet
  136.  
  137.         // net saving is either taxed or not
  138.         if((sto.taxrate_istaxed == 1) && (numMonth%12 == 0) && (numMonth>0)) {
  139.             totalTax = totalTax + cumMonthlyRet * sto.taxrate_taxrate
  140.             prevBal = (prevBal + monthlyDep) -
  141.                 cumMonthlyRet * sto.taxrate_taxrate
  142.             cumMonthlyRet = 0
  143.         }
  144.         else
  145.             prevBal = (prevBal + monthlyDep) + monthlyRet
  146.  
  147.         if(prevBal >= goal)         // stop looping if goal has been reached
  148.             break;
  149.  
  150.         // move to next deposit
  151.         numMonth = numMonth + 1
  152.         if(numMonth > 1200)
  153.             break;
  154.     }
  155.  
  156.     if(numMonth < 1200) {
  157.         sto.result_saved = Math.round(prevBal)
  158.         sto.result_interest = Math.round(totalInterest)
  159.         sto.result_tax = Math.round(totalTax)
  160.         sto.result_term = numMonth
  161.         sto.result_regular = sto.deposit_regular
  162.     }
  163.     else {
  164.         sto.result_saved = 0
  165.         sto.result_interest = 0
  166.         sto.result_tax = 0
  167.         sto.result_term = 0
  168.         sto.result_regular = 0
  169.     }
  170.     numYear = Math.round((sto.result_term/12) - (sto.result_term%12)/12)
  171.     numQuarter = Math.round((sto.result_term/3) - (sto.result_term%3)/3)
  172.     if(sto.deposit_freq == 0)        // yearly deposit
  173.         sto.result_deposit = sto.deposit_regular * numYear
  174.     else if(sto.deposit_freq == 1)    // quarterly deposit
  175.         sto.result_deposit = sto.deposit_regular * numQuarter
  176.     else if(sto.deposit_freq == 2)    // monthly deposit
  177.         sto.result_deposit = sto.deposit_regular * numMonth
  178.     sto.result_deposit = Math.round(sto.result_deposit)
  179. }
  180.  
  181. <%
  182. // This function returns a annual deposit amount for a given intial
  183. // balance, term, and goal.  The calculation is a rather invovled one.
  184. // Here is the formula that calculates it.
  185. //
  186. //    X = (S_n - a_0 * b_0^n) / (b_0^(n-1) + b_0^(n-2) + ... + b_0)
  187. //
  188. //    where a_0 is the initial balance, the n is the number of years to
  189. //    save, the b_0 is the (1 - tax_bracket) + (1 + interest/12)^12 * tax_bracket.  
  190. %>
  191. //    The X gives the annual deposit amount.
  192. //
  193. function getResultSet3()
  194. {
  195.     var    sto = parent.data
  196.     var    monthlyInt = sto.interest_rate / 12
  197.     var b0, bn, i, regular
  198.     var totalInterest, yearlyRet, prevBal, totalTax
  199.  
  200.     // calculate the b0 for monthly compounding
  201.     b0 = 1 + monthlyInt
  202.     b0 = Math.pow(b0, 12)
  203.     if(sto.taxrate_istaxed == 1) {
  204.         b0 = sto.taxrate_taxrate + b0 * (1 - sto.taxrate_taxrate)
  205.     }
  206.  
  207.     // calculate the denominator
  208.     bn = b0
  209.     if(monthlyInt != 0) {
  210.         bn = (1 - Math.pow(b0, sto.deposit_term)) / (1 - b0)
  211.         if (bn==0)
  212.             bn = 1
  213.     }
  214.     else
  215.         bn = sto.deposit_term
  216.  
  217.     // calculate annual deposit amount
  218.     regular = (sto.deposit_goal - 
  219.         sto.deposit_balance * Math.pow(b0, sto.deposit_term)) / bn
  220.  
  221.     // calculate total interest earned
  222.     prevBal = sto.deposit_balance
  223.     totalInterest = 0
  224.     totalTax = 0
  225.     for(i=0; i<sto.deposit_term; i++) {
  226.         yearlyRet = annualDeposit(prevBal, sto.interest_rate)
  227.         totalInterest = totalInterest + yearlyRet
  228.  
  229.         if(sto.taxrate_istaxed == 1) {
  230.             totalTax = totalTax + yearlyRet * sto.taxrate_taxrate
  231.             prevBal = (prevBal + regular) + 
  232.                 yearlyRet * ( 1 - sto.taxrate_taxrate)
  233.         }
  234.         else
  235.             prevBal = (prevBal + regular) + yearlyRet
  236.     }
  237.  
  238.     sto.deposit_freq = 0
  239.     if(regular < 0)
  240.         sto.result_regular = 0
  241.     else
  242.         sto.result_regular = Math.round(regular)
  243.     sto.result_saved = sto.deposit_goal
  244.     sto.result_term = sto.deposit_term * 12
  245.     sto.result_deposit = sto.result_regular * sto.deposit_term
  246.     sto.result_interest = Math.round(totalInterest)
  247.     sto.result_tax = Math.round(totalTax)
  248. }
  249.