/************************************************************
 *                                                          *
 *   calcs.js - Mortgage Calculator functions               *
 *                                                          *
 ************************************************************/


/* Monthly Payment Calculator */
/* 
Input Field ID's: 
	InterestRate	- Loan interest rate
	LoanAmount		- Loan Principal Amount
	AnnualTax		- Annual Mortgage Tax
	AnnualInsurance	- Annual Mortgage Insurance
	Years			- Length of loan) 

Output Field ID's:
	PrincipalInt	- Principal + Interest Payment
	MonthlyTax		- Monthly Tax Payment
	MonthlyInsurance- Monthly Mortgage Insurance Payment
	TotMonPayment	- Total Monthly Payments
	TotalPayment	- Total Payments over life of loan
	
*/
function monthlyPayment()
{
	var interestRate = stripNum(document.getElementById('InterestRate').value) / 100.0;
	var loanAmount = Math.floor(stripNum(document.getElementById('LoanAmount').value));
	var annualTax = stripNum(document.getElementById('AnnualTax').value);
	var annualInsurance = stripNum(document.getElementById('AnnualInsurance').value);
	var years = stripNum(document.getElementById('Years').value);
	var months = years * 12;
	
	var monthlyInterest = interestRate / 12;
	var base = 1;
	var mbase = 1 + monthlyInterest;
	for(var i = 0; i < Math.floor(months); i++)
	{
		base *= mbase;
	}
	
	var monthlyPrinInt = (loanAmount * monthlyInterest) / (1 - (1 / base));
	var monthlyTax = annualTax / 12;
	var monthlyInsurance = annualInsurance / 12;
	var totalMonthlyPayment = monthlyPrinInt + monthlyTax + monthlyInsurance;
	
	document.getElementById('PrincipalInt').value = "$" + formatNumber(monthlyPrinInt);
	document.getElementById('MonthlyTax').value = "$" + formatNumber(monthlyTax);
	document.getElementById('MonthlyInsurance').value = "$" + formatNumber(monthlyInsurance);
	document.getElementById('TotMonPayment').value = "$" + formatNumber(totalMonthlyPayment);
	document.getElementById('TotalPayment').value = "$" + formatNumber(totalMonthlyPayment * months);
}


/* Mortgage Refinancing Calculator */
/* 
Input Field ID's: 
	CurBalance			- Current mortgage balance
	CurMonthlyPmt		- Current monthly payments
	CurInterestRate		- Current interest rate
	NewInterestRate		- New interest rate
	NewTerm				- New loan term (# years)
	ClosingCosts		- Refi Closing Costs
	ClosingCostsType	- Type of Closing Costs (points or fixed dollar amount)
	FinanceClosingCosts	- Finance the closing costs in the loan?

Output Field ID's:
	NetSavings			- Net savings under new loan
	MonthlySavings		- Monthly savings if you refi
	BreakEvenTime		- Time to break even with closing costs
	TotalCurInterest	- Total interest under current loan
	TotalNewInterest	- Total interest under new loan
	NewMonthlyPmt		- New monthly payment if you refi
	
*/
function mortgageRefi()
{
	var curBalance = stripNum(document.getElementById('CurBalance').value);
	var curMonthlyPmt = stripNum(document.getElementById('CurMonthlyPmt').value);
	var curInterestRate = stripNum(document.getElementById('CurInterestRate').value) / 100;
	var newInterestRate = stripNum(document.getElementById('NewInterestRate').value) / 100;
	var newTerm = stripNum(document.getElementById('NewTerm').value);
	var closingCosts = stripNum(document.getElementById('ClosingCosts').value);
	var closingCostsType = stripNum(document.getElementById('ClosingCostsType').value);
	var financeClosingCosts = stripNum(document.getElementById('FinanceClosingCosts').value);
	
	var months = newTerm * 12;
	var curMonthlyIntRate = curInterestRate / 12;
	var newMonthlyIntRate = newInterestRate / 12;
	
	if(closingCostsType == 0) // if user selected points as cc type...
	{
		closingCosts = curBalance * (closingCosts / 100);
	}
	
	var principal = curBalance;
	var interestPart = 0;
	var principalPart = 0;
	var accumPrincipal = 0;
	var accumInterest = 0;
	var count = 0;
	
	while(principal > 0)
	{
		interestPart = principal * curMonthlyIntRate;
		principalPart = curMonthlyPmt - interestPart;
		principal -= principalPart;
		accumPrincipal += principalPart;
		accumInterest += interestPart;
		count++;
		if(count > 600)
			break;
	}
	
	var principal2 = curBalance;
	if(financeClosingCosts == 1)
	{
		principal2 = curBalance + closingCosts;
	}
	
	var pow = 1;
	for(var i = 0; i < (newTerm * 12); i++)
		pow *= (1 + newMonthlyIntRate);
		
	var newMonthlyPmt = (principal2 * pow * newMonthlyIntRate) / (pow - 1);
	var savings = curMonthlyPmt - newMonthlyPmt;
	var totalInterest = (newMonthlyPmt * newTerm * 12) - principal2;
	var interestSaved = Math.max(accumIntterest - totalInterest, 0);
	
	document.getElementById('TotalCurInterest').value = "$" + formatNumber(accumInterest);
	document.getElementById('NewMonthlyPmt').value = "$" + formatNumber(newMonthlyPmt);
	document.getElementById('TotalNewInterest').value = "$" + formatNumber(monthlyTax);
	
	
	
	document.getElementById('MonthlyInsurance').value = "$" + formatNumber(monthlyInsurance);
	document.getElementById('TotMonPayment').value = "$" + formatNumber(totalMonthlyPayment);
	document.getElementById('TotalPayment').value = "$" + formatNumber(totalMonthlyPayment * months);
}


/* Home Affordability Calculator */
function homeAffordability()
{
	var monthlyIncome = stripNum(document.getElementById('MonthlyIncome').value);
	var monthlyObligations = stripNum(document.getElementById('MonthlyObligations').value);
	var downPayment = stripNum(document.getElementById('DownPayment').value);
	var closingCosts = stripNum(document.getElementById('ClosingCosts').value);
	var savingsCash = stripNum(document.getElementById('SavingsCash').value);
	var annualPropTax = stripNum(document.getElementById('AnnualPropTax').value);
	var annualHazardIns = stripNum(document.getElementById('AnnualHazardIns').value);
	var annualCondoFee = stripNum(document.getElementById('AnnualCondoFee').value);
	var annualInterestRate = stripNum(document.getElementById('AnnualInterestRate').value);
	var loanTerm = stripNum(document.getElementById('LoanTerm').value);
	
	// to be continued...
}


/* Mortgage Term Comparison Calculator */
// Inputs:
//     Principal		- Mortgage Principal ($)
//     AnnualInterest	- Expected Annual Interest Rate (%)
//
// Outputs: (5 versions of each field, ## = 10, 15, 20, 25, and 30)
//     MonthlyPmt##		- Monthly Payment
//     TotalPrincipal##	- Total Principal
//     TotalInterest##	- Total Interest
//     TotalPmts##		- Total Payments
//
function mortgageTermComparison()
{
	var principal = stripNum(document.getElementById('Principal').value);
	var interest = stripNum(document.getElementById('AnnualInterest').value) / 100;
	var monthlyInterest = interest / 12;
	
	for(var years = 10; years <= 30; years += 5)
	{
		var months = years * 12;
		
		var monthlyPayment = principal / months;
		
		if(interest > 0)
		{
			// Compute monthly payments
			var tmp = 1;
			for(var i = 0; i < months; i++)
				tmp *= (1 + monthlyInterest);
			monthlyPayment = (principal * tmp * monthlyInterest) / (tmp - 1);
		}
		document.getElementById('MonthlyPmt' + years).value = "" + formatNumber(monthlyPayment);
		
		// Total Principal doesn't change
		document.getElementById('TotalPrincipal' + years).value = "" + formatNumber(principal);
		
		var tmpPrin = principal;
		var tmpIntPart = 0;
		var tmpPrnPart = 0;
		var totalInterest = 0;
		var i = 0;
		while(tmpPrin > 0)
		{
			if(monthlyPayment > (tmpPrin * (1 + monthlyInterest)))
			{
				// we are on last payment
				tmpIntPart = tmpPrin * monthlyInterest;
				tmpPrnPart = tmpPrin;
				totalInterest += tmpIntPart;
				tmpPrin = 0;
			}
			else
			{
				// normal monthly payment
				tmpIntPart = tmpPrin * monthlyInterest;
				totalInterest += tmpIntPart;
				tmpPrnPart = monthlyPayment - tmpIntPart;
				tmpPrin -= tmpPrnPart;
			}
			
			// make sure it doesnt keep on going indefinitely
			i++;
			if(i > 1000)
				tmpPrin = 0;
		}
		document.getElementById('TotalInterest' + years).value = "" + formatNumber(totalInterest);
		
		document.getElementById('TotalPmts' + years).value = "" + formatNumber(principal + totalInterest);
	}
	
}

/* Interest Only Calculator */
function interestOnly()
{
	var principal = stripNum(document.getElementById('Principal').value);
	var interestRate = stripNum(document.getElementById('InterestRate').value) / 100;
	
	interestRate /= 12;
	
	document.getElementById('InterestPayment').value = "$" + formatNumber(principal * interestRate);
}