      // Copyright (c) 2000 Vermont Employees Credit Union
      //
      // Programmer:	Joshua W. Flood (jflood@fmlabs.net)
      // Description:	JavaScript loan calculator
      //
      // History:
      //  02-10-2000 JWF - Initial Code
      //  02-16-2000 JWF - Added Currency Formatting
      //                   Added Ability to strip characters
    
      // function to insert comma seperators in string of digits
      
      function helpText(message)
        {
          //alert(message);
          document.help.helpText.value = message;
        }
        
      function calcYears()
        {
          var months 	= stripChars(document.savingsCalculator.numberOfPayments.value);
          
          months	= parseInt(months); 
          if(months == null || isNaN(months))
            {
              months = 0;
              document.savingsCalculator.lengthInYears.value = months;
            }
            
          document.savingsCalculator.lengthInYears.value = months / 12;
        }
        
      function calcMonths()
        {
          var years 	= stripChars(document.savingsCalculator.lengthInYears.value);
          
          years		= parseFloat(years); 
          if(years == null || isNaN(years))
            {
              years = 0;
              document.savingsCalculator.numberOfPayments.value = years;
            }
            
          document.savingsCalculator.numberOfPayments.value = years * 12;
        }
      
      function insertCommas(incoming)
        {
          var outgoing = "";
          
          // reverse incoming, we have to put it together backward to get
          // the commas in the right place.
          incoming = reverse(incoming);
          
          for (var i=0; i < incoming.length; i++)
            {
              if ((i%3 == 0) && (i != 0))
                {outgoing = incoming.charAt(i) + "," + outgoing;}

              else
                {outgoing = incoming.charAt(i) + outgoing;}
            }
          
          return outgoing;
        }
      
      // strip everything except digits and decimal points  
      function stripChars(incoming)
        {
          var outgoing = "";
          for (var i=0; i < incoming.length; i++)
            {
              // use a little double negative to find out if it is a digit
              if (!isNaN(incoming.charAt(i)) || incoming.charAt(i) == ".")
                {outgoing = outgoing + incoming.charAt(i);}
            }
          return(outgoing);
        }
      
      // function to raise a number to another number
      function raise(base, power)
        {
          var result = Math.exp(power * Math.log(base));
          return result;
        }
      
      // function to reverse a string  
      function reverse(incoming)
        {
          var outgoing = "";
          
          // build the string one char at a time, backwards
          for (var i=0; i <= incoming.length; i++) 
            {outgoing = incoming.charAt(i) + outgoing;}
            
          return(outgoing)
        }
      
      // function to round a floating point to adjustable number of places  
      function roundTo(incoming, places)
        {
          var adjuster = raise(10, places);
          
          // Now round adjuster since it is a floating point.
          adjuster = Math.round(adjuster);
          
          // There is no way to specify places when rounding, so we shift the 
          // decimal over "places" number of slots and just round, then put it
          // back.
          
          var outgoing = Math.round(incoming * adjuster) / adjuster;
          
          return(outgoing);
        }
      
      // Function to format a floating point to a string that looks like currency
      function currencyFormat(incoming)
        {
          var dollars = "";
          var cents = "";
          
          // round to 2 signifigant decimal places
          var rounded = roundTo(incoming, 2);
          
          // Convert payment to a string
          var stringIncoming = rounded.toString();
          
          // Split up the dollars and cents
          var i = stringIncoming.indexOf(".");

          // see if there is a decimal place
          if (i != -1)
            {
              // there was a decimal
              dollars = stringIncoming.substring(0, i);
              cents = stringIncoming.substring(i + 1, stringIncoming.length);
              
              // see if there was only 1 decimal place, we need two for money
              if (cents.length == 1)
                {cents = cents + "0";}
            }
          else
            {
              // there was no decimal
              dollars = stringIncoming;
              cents = "00";
            }
          
          // Add commas to the result as needed
          dollars = insertCommas(dollars);
          
          // rejoin the dollars and cents
          var outgoing = ("$" + dollars + "." + cents);
          
          return(outgoing);
        }
    
      // function to calculate loan payment
      function calculateLoanPayment()
        {          
          // strip characters from the input data
          var loanAmmount 	= stripChars(document.loanCalculator.loanAmmount.value);
          var interestRate 	= stripChars(document.loanCalculator.interestRate.value);
          var numberOfPayments 	= stripChars(document.loanCalculator.numberOfPayments.value);
          
          // you can have fractional ammounts and rates
          loanAmmount		= parseFloat(loanAmmount);
          interestRate		= parseFloat(interestRate);
          
          // You can't have a partial payment...
          numberOfPayments	= parseInt(numberOfPayments);
          
          // Check that the input data is valid, then fill it back to the form
          // so that the user sees what numbers we actually used for the calculation
          if(loanAmmount == null || isNaN(loanAmmount))
            {
              loanAmmount = 0;
              document.loanCalculator.loanAmmount.value = loanAmmount;
            }
          else
            {
              // Make it look like currency
              document.loanCalculator.loanAmmount.value = currencyFormat(loanAmmount);
            }
            
          if(interestRate == null || isNaN(interestRate))
            {
              interestRate = 0;
              document.loanCalculator.interestRate.value = interestRate;
            }
          else
            {
              document.loanCalculator.interestRate.value = interestRate;
              interestRate = interestRate / 100;
            }
            
          if(numberOfPayments == null || isNaN(numberOfPayments))
            {
              numberOfPayments = 0;
              document.loanCalculator.numberOfPayments.value = numberOfPayments;
            }
          else
            {
              document.loanCalculator.numberOfPayments.value = numberOfPayments;
            }
            
          // Calculate the monthly payment
          if(interestRate == 0)
            {
              var payment = loanAmmount / numberOfPayments;
            }
          else
            {
              var compoundRate = 365;
              var years = numberOfPayments / 12;
          
              var partOne	= 1 + (interestRate / compoundRate);
              var partTwo	= raise(partOne, compoundRate / 12) - 1;
              var partThree	= 1 - raise(partOne, years * compoundRate * -1);
          
              var payment = loanAmmount * (partTwo / partThree);
            }
            
          // Check to make sure we have a valid result
          if(payment == null || isNaN(payment))
            {payment = 0;}
          
          // Make it look like money
          var finalPayment = currencyFormat(payment);
          
          // Display the result
          document.loanCalculator.monthlyPayment.value = finalPayment;
        }
          
      // function to calculate future value of savings (compounded monthly)
      function calculateSavingsPayment()
        {          
          // strip characters from the input data
          var startingAmmount 	= stripChars(document.savingsCalculator.startingAmmount.value);
          var finalAmmount 	= stripChars(document.savingsCalculator.finalAmmount.value);
          var interestRate 	= stripChars(document.savingsCalculator.interestRate.value);
          var numberOfPayments 	= stripChars(document.savingsCalculator.numberOfPayments.value);
          
          // you can have fractional ammounts and rates
          startingAmmount	= parseFloat(startingAmmount);
          finalAmmount		= parseFloat(finalAmmount);
          interestRate		= parseFloat(interestRate);
          
          // You can't have a partial payment...
          numberOfPayments	= parseInt(numberOfPayments);
          
          // Check that the input data is valid, then fill it back to the form
          // so that the user sees what numbers we actually used for the calculation
          if(startingAmmount == null || isNaN(startingAmmount))
            {
              startingAmmount = 0;
              document.savingsCalculator.startingAmmount.value = startingAmmount;
            }
          else
            {
              // Make it look like currency
              document.savingsCalculator.startingAmmount.value = currencyFormat(startingAmmount);
            }
            
          if(finalAmmount == null || isNaN(finalAmmount))
            {
              finalAmmount = 0;
              document.savingsCalculator.finalAmmount.value = finalAmmount;
            }
          else
            {
              // Make it look like currency
              document.savingsCalculator.finalAmmount.value = currencyFormat(finalAmmount);
            }
            
          if(interestRate == null || isNaN(interestRate))
            {
              interestRate = 0;
              document.savingsCalculator.interestRate.value = interestRate;
            }
          else
            {
              document.savingsCalculator.interestRate.value = interestRate;
              interestRate = interestRate / 100;
            }
            
          if(numberOfPayments == null || isNaN(numberOfPayments))
            {
              numberOfPayments = 0;
              document.savingsCalculator.numberOfPayments.value = numberOfPayments;
            }
          else
            {
              document.savingsCalculator.numberOfPayments.value = numberOfPayments;
            }
            
          // Calculate the final value
          if(interestRate == 0)
            {
              var payment = (finalAmmount - startingAmmount) / numberOfPayments;
            }
          else
            {
              var compoundRate = 365;
              var years = numberOfPayments / 12;
              
              interestRate = interestRate / 12;
              
              startingAmmount = startingAmmount * -1;
          
              //var partOne	= raise(1 + interestRate, numberOfPayments);
              //var partTwo	= interestRate * (finalAmmount + (partOne * startingAmmount));
              //var partThree	= -1 + partOne;
          
              //var payment = -1 * (partTwo / partThree);
              
              var partOne	= raise(1 + interestRate, numberOfPayments);
              var partTwo	= interestRate * (finalAmmount + (partOne * startingAmmount));
              var partThree	= -1 + partOne;
          
              var payment = (partTwo / partThree);
            }
            
          // Check to make sure we have a valid result
          if(payment == null || isNaN(payment))
            {payment = 0;}
          
          // Make it look like money
          var finalPayment = currencyFormat(payment);
          
          // Display the result
          document.savingsCalculator.payment.value = finalPayment;
        }
