'use strict';

function Month(balance, payment, interestRate) {
  this.balance      = balance;
  this.interestRate = interestRate;
  this.interest     = this.balance * this.interestRate / 12;
  this.amortization = Math.min(payment - this.interest, this.balance);
  this.payment      = this.interest + this.amortization;
  this.remainder    = this.balance - this.amortization;
}

function InterestCtrl($scope, $locale) {
  // setup defaults
  $scope.inputs = {
    price:            450000,
    brokerage:        6.25,
    tax:              5.0,
    notary:           1.5,
    addOns:           0,
    capital:          100000,
    interestRate:     3.0,
    amortizationRate: 1.0
  };
  $scope.calculated = {};

  $scope.calculate = function() {
    if ($scope.processing) {
      clearTimeout($scope.processing.timeout);
    }
    var inputs      = $scope.inputs,
        calculated  = $scope.calculated;
    calculated.brokerageCost  = inputs.price * inputs.brokerage / 100.0;
    calculated.taxCost        = inputs.price * inputs.tax / 100.0;
    calculated.notaryCost     = inputs.price * inputs.notary / 100.0;
    calculated.cost           = inputs.price + calculated.brokerageCost + calculated.taxCost + calculated.notaryCost + inputs.addOns;
    calculated.credit         = calculated.cost - inputs.capital;
    calculated.payment        = calculated.credit * (inputs.interestRate + inputs.amortizationRate) / 100.0 / 12;
    $scope.processing = {
      months: [],
      month: null,
      balance: calculated.credit,
      timeout: setTimeout(processNextMonth, 500)
    };
  };

  function processNextMonth() {
    var data = $scope.processing;
    if (data.balance > 0) {
      var month = new Month(data.balance, $scope.calculated.payment, $scope.inputs.interestRate / 100.0);
      data.balance = month.remainder;
      data.months.push(month);
      data.timeout = setTimeout(processNextMonth, 0);
    } else {
      $scope.calculated.totalPayments = 0;
      for (var month in data.months) {
       $scope.calculated.totalPayments += data.months[month].payment;
      };
      $scope.months = data.months;
      $scope.processing = null;
      $scope.$apply();
    }
  }

  $scope.calculate();
}

@@ -413,16 +413,104 @@
 cale) %7B%0A
+  $scope.inputs = JSON.parse(localStorage.getItem(%22inputs%22));%0A  if (!$scope.inputs) %7B%0A  
   // set
@@ -521,16 +521,18 @@
 efaults%0A
+  
   $scope
@@ -547,16 +547,18 @@
 = %7B%0A    
+  
 price:  
@@ -579,16 +579,18 @@
 00,%0A    
+  
 brokerag
@@ -605,16 +605,18 @@
   6.25,%0A
+  
     tax:
@@ -638,16 +638,18 @@
 .0,%0A    
+  
 notary: 
@@ -667,16 +667,18 @@
 .5,%0A    
+  
 addOns: 
@@ -690,16 +690,18 @@
      0,%0A
+  
     capi
@@ -726,16 +726,18 @@
 00,%0A    
+  
 interest
@@ -755,16 +755,18 @@
 .0,%0A    
+ 
+ 
 amortiza
@@ -779,21 +779,27 @@
 te: 1.0%0A
+  
   %7D;%0A
+  %7D%0A
   $scope
@@ -847,24 +847,91 @@
 unction() %7B%0A
+    localStorage.setItem(%22inputs%22, JSON.stringify($scope.inputs));%0A
     if ($sco

Store inputs in localStorage