Financial Analysis Results
        
        
        
        
        
        
          Investment Metrics
          
            
              
                Cash-on-Cash Return
                0.00%
               
             
            
           
          
            
              
                Net Worth Impact (over 10 years)
                $0
               
             
            
              
                Total Equity Return
                0.00%
               
             
           
         
        
        
          Combined Property Analysis
          
            
              
                Existing Monthly Payment
                $0
               
             
            
           
          
            
              
                Total Combined Cash Flow
                $0
               
             
            
              
                Total Property Value (Year 10)
                $0
               
             
           
          
            
              
                ROI (over 10 years)
                0.00%
               
             
            
           
         
        
          Long-term Projection
          
            
              
                
                  | Year | Property Value | ADU Cash Flow | Combined Cash Flow | Cumulative | 
              
              
                
              
            
           
         
       
      
      
;
      var annualCashFlow = monthlyCashFlow * 12;
      
      // Calculate investment metrics
      var initialInvestment = financingMethod === 'cash' ? totalCost : equityUsed;
      var cashOnCash = initialInvestment > 0 ? (annualCashFlow / initialInvestment) * 100 : 0;
      var annualNOI = (monthlyRent - monthlyExpenses) * 12;
      var capRate = (annualNOI / totalCost) * 100;
      var breakEven = monthlyCashFlow > 0 ? totalCost / monthlyCashFlow : Infinity;
      
      // Calculate projection
      var projection = [];
      var cumulativeCashFlow = 0;
      var currentPropertyValue = propertyValue + totalCost;
      
      for (var year = 1; year <= timeframe; year++) {
        currentPropertyValue *= (1 + appreciation / 100);
        var currentRent = monthlyRent * Math.pow(1 + rentGrowth / 100, year);
        var currentVacancy = currentRent * (vacancyRate / 100);
        var currentManagement = currentRent * (propertyManagement / 100);
        var currentMaintenance = currentRent * (maintenance / 100);
        var currentExpenses = currentVacancy + currentManagement + currentMaintenance + (annualExpenses / 12);
        var currentCashFlow = (currentRent - currentExpenses - monthlyPayment) * 12;
        cumulativeCashFlow += currentCashFlow;
        
        projection.push({
          year: year,
          propertyValue: currentPropertyValue,
          cashFlow: currentCashFlow,
          cumulative: cumulativeCashFlow
        });
      }
      
      // Calculate ROI
      var finalPropertyValue = projection[projection.length - 1].propertyValue;
      var equityGain = finalPropertyValue - propertyValue - mortgageBalance;
      var roi = ((cumulativeCashFlow + equityGain) / initialInvestment) * 100;
      
      // Update results
      document.getElementById('aduTotalCost').textContent = formatCurrency(totalCost);
      
      var monthlyCashFlowElement = document.getElementById('aduMonthlyCashFlow');
      monthlyCashFlowElement.textContent = formatCurrency(monthlyCashFlow);
      monthlyCashFlowElement.className = 'result-value ' + (monthlyCashFlow >= 0 ? 'positive' : 'negative');
      
      var annualCashFlowElement = document.getElementById('aduAnnualCashFlow');
      annualCashFlowElement.textContent = formatCurrency(annualCashFlow);
      annualCashFlowElement.className = 'result-value ' + (annualCashFlow >= 0 ? 'positive' : 'negative');
      
      document.getElementById('aduCashOnCash').textContent = formatPercentage(cashOnCash);
      document.getElementById('aduCapRate').textContent = formatPercentage(capRate);
      document.getElementById('aduRoi').textContent = formatPercentage(roi);
      document.getElementById('aduBreakEven').textContent = isFinite(breakEven) ? Math.ceil(breakEven).toString() : 'N/A';
      
      // Update projection table
      var tableBody = document.getElementById('aduProjectionTable');
      tableBody.innerHTML = '';
      
      projection.forEach(function(year) {
        var row = document.createElement('tr');
        
        var yearCell = document.createElement('td');
        yearCell.textContent = year.year;
        row.appendChild(yearCell);
        
        var valueCell = document.createElement('td');
        valueCell.textContent = formatCurrency(year.propertyValue);
        row.appendChild(valueCell);
        
        var cashFlowCell = document.createElement('td');
        cashFlowCell.textContent = formatCurrency(year.cashFlow);
        cashFlowCell.style.color = year.cashFlow >= 0 ? 'green' : 'red';
        row.appendChild(cashFlowCell);
        
        var cumulativeCell = document.createElement('td');
        cumulativeCell.textContent = formatCurrency(year.cumulative);
        cumulativeCell.style.color = year.cumulative >= 0 ? 'green' : 'red';
        row.appendChild(cumulativeCell);
        
        tableBody.appendChild(row);
      });
      
      // Update assessment
      var cashFlowAssessment = document.getElementById('aduCashFlowAssessment');
      if (monthlyCashFlow > 0) {
        cashFlowAssessment.textContent = `This ADU project generates positive cash flow of ${formatCurrency(monthlyCashFlow)} per month.`;
      } else {
        cashFlowAssessment.textContent = `This ADU project has negative cash flow of ${formatCurrency(Math.abs(monthlyCashFlow))} per month.`;
      }
      
      var cocAssessment = document.getElementById('aduCocAssessment');
      if (cashOnCash >= 8) {
        cocAssessment.textContent = `The cash-on-cash return of ${formatPercentage(cashOnCash)} is excellent.`;
      } else if (cashOnCash >= 5) {
        cocAssessment.textContent = `The cash-on-cash return of ${formatPercentage(cashOnCash)} is solid.`;
      } else {
        cocAssessment.textContent = `The cash-on-cash return of ${formatPercentage(cashOnCash)} is below the typical 5-8% threshold.`;
      }
      
      var breakEvenAssessment = document.getElementById('aduBreakEvenAssessment');
      if (isFinite(breakEven) && breakEven > 0) {
        var breakEvenYears = (breakEven / 12).toFixed(1);
        if (breakEven <= 120) {
          breakEvenAssessment.textContent = `You'll break even in ${Math.ceil(breakEven)} months (${breakEvenYears} years), a reasonable timeframe.`;
        } else {
          breakEvenAssessment.textContent = `Breaking even will take ${Math.ceil(breakEven)} months (${breakEvenYears} years), a longer payback period.`;
        }
      } else {
        breakEvenAssessment.textContent = `With negative cash flow, this project will not break even based on cash flow alone.`;
      }
      
      var overallAssessment = document.getElementById('aduOverallAssessment');
      if (monthlyCashFlow > 0 && cashOnCash >= 5 && (isFinite(breakEven) && breakEven <= 240)) {
        overallAssessment.textContent = "Overall Assessment: This ADU project appears financially feasible and a good investment.";
      } else if (monthlyCashFlow > 0 && (cashOnCash >= 5 || (isFinite(breakEven) && breakEven <= 240))) {
        overallAssessment.textContent = "Overall Assessment: This ADU project shows moderate financial feasibility.";
      } else {
        overallAssessment.textContent = "Overall Assessment: This ADU project faces financial feasibility challenges.";
      }
      
      console.log("ADU calculations completed successfully");
    } catch (error) {
      console.error("Error in ADU calculator:", error);
      alert("Error in calculations. Please check console for details.");
    }
  }
  
  // Format currency
  function formatCurrency(value) {
    return new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
      minimumFractionDigits: 0,
      maximumFractionDigits: 0
    }).format(value);
  }
  
  // Format percentage
  function formatPercentage(value) {
    return new Intl.NumberFormat('en-US', {
      style: 'percent',
      minimumFractionDigits: 2,
      maximumFractionDigits: 2
    }).format(value / 100);
  }
  
  // Run calculation when page loads
  window.onload = calculateADU;
  // Also run immediately
  calculateADU();