//constants
var DEFAULT_INVESTMENT_RATE = new Number(0.08);


/**
 * This calculator determines the savings resulting from buying a property
 * versus Renting.
 */
function RentVsBuyCalculator() {
	var returnObject = new Object()

	returnObject.rentingCost = new Number();
	returnObject.ownershipCost = new Number();
	returnObject.taxSavings = new Number();
	returnObject.closingCosts = new Number();
	returnObject.equityEarned = new Number();
	returnObject.grossCosts = new Number();

	returnObject.className = "RentVsBuyCalculator";
	
	returnObject.getOwnershipBenefit = getOwnershipBenefit;
	returnObject.getRentingCost = getRentingCost;
	returnObject.getOwnershipCost = getOwnershipCost;
	returnObject.getTaxSavings = getTaxSavings;
	returnObject.getClosingCosts = getClosingCosts;
	returnObject.getGrossCosts = getGrossCosts;
	returnObject.getEquityEarned = getEquityEarned;
	returnObject.getPotentialSavings = getPotentialSavings;
	returnObject.calculate = RentVsBuyCalculatorCalculate;
	

	return returnObject;

}

/**
 * Returns the difference between renting cost and ownership cost
 * if retning cost &gt; ownership costs, 0 otherwise.
 * @return the difference between renting cost and ownership cost
 * if retning cost &gt; ownership costs, 0 otherwise.
 */
function getOwnershipBenefit() {
	// Determine savings if owning is cheaper than renting
	if (this.rentingCost - this.ownershipCost > 0) {
		return (this.rentingCost - this.ownershipCost);
	}
	else {
		return 0;
	}
}

/**
 * Returns the total cost of renting in dollars
 * @return the total cost of renting in dollars
 */
function getRentingCost() {
	return this.rentingCost;
}


/**
 * Returns the total cost of ownership in dollars
 * @return the total cost of ownership in dollars
 */
function getOwnershipCost() {
	return this.ownershipCost;
}

/**
 * Returns the tax savings of home ownership in dollars
 * @return the tax savings of home ownership in dollars
 */
function getTaxSavings() {
	return this.taxSavings;
}


/**
 * Returns the closing costs of a home purchase in dollars
 * @return the closing costs of a home purchase in dollars
 */
function getClosingCosts() {
	return this.closingCosts;
}

/**
 * Returns the total costs of buying
 * @return the total costs of buying
 */
function getGrossCosts() {
	return this.grossCosts;
}

/**
 * Returns the equity earned over the life of the loan
 * @return the equity earned over the life of the loan
 */
function getEquityEarned() {
	return this.equityEarned;
}

/**
 * Returns the potential savings of renting.  This is the
 * difference between renting cost and ownership cost multiplied by
 * the default investment rate.
 *
 * @return the potential savings of renting
 */
function getPotentialSavings() {
	return ((this.rentingCost - this.ownershipCost) * DEFAULT_INVESTMENT_RATE);
}

/**
 * The calculate() method takes the loan parameters and rent parameters
 * and returns information about the cost/benefit of renting versus buying.
 *
 * @param salesPrice sales price
 * @param appreciationRate annual appreciate rate of home
 * @param interestRate annual interest rate of loan
 * @param discountPoints discount points
 * @param loanAmt loan amount in dollars
 * @param loanTerm loan term in months
 * @param expectedYearsToOwn expected years to own the home
 * @param federalTaxRate federal tax rate
 * @param monthlyRent monthly rent amount in dollars
 * @param annualRentIncrease annual rent increase percentage
 * @param annualPropertyTax annual property tax amount in dollars
 * @param annualMiscOwnershipFees annual misc fees of ownership in
 * dollars
 * @param annualMiscRentalFees annual misc fees of renting in dollars
 *
 * @return a calculator instance populated with the rent vs. buy
 * information based on the loan parameters
 *
 * @exception MortgageFamilyException if an exception occurs loading
 * default values or if the loan parameter values are not valid
 */
function RentVsBuyCalculatorCalculate(salesPrice, interestRate, loanAmt, loanTerm, monthlyMI, federalTaxRate, appreciationRate, discountPoints, expectedYearsToOwn, monthlyRent, annualRentIncrease, annualPropertyTax, annualMiscOwnershipFees, annualMiscRentalFees) {

	var totalAppreciation = new Number(0);
	var totalPrincipalPaid = new Number(0);
	var totalInterestPaid = new Number(0);
	var totalPMI = new Number(0);
	var rvb = new RentVsBuyCalculator();

	// Need to get principal, interest, and tax savings from the
	// amort schedule
	var amort = new AmortScheduleCalculator();
	amort = amort.calculate(loanAmt,salesPrice, interestRate,0,0,1,expectedYearsToOwn*12,0,0,0,0,federalTaxRate,monthlyMI,false,loanTerm,loanTerm,0,0,0,0,0,0);

	totalPrincipalPaid = amort.getTotalNormalPrincipal();
	totalInterestPaid = amort.getTotalNormalInterest();
	totalPMI = amort.getTotalPMI();
	var annualRent = monthlyRent * 12;

	// Forcast appreciation/increases
	for (year = 0; year < expectedYearsToOwn; year++) {
		// Calculate all of the rental costs for the term expected
		rvb.rentingCost += annualRent;
		annualRent += annualRent * annualRentIncrease;

		// Calculate the annual appreciation in equity
		if (year > 0) {
			totalAppreciation += salesPrice * appreciationRate;
		}
	}

	rvb.rentingCost += (annualMiscRentalFees * expectedYearsToOwn);


	salesPrice = new Number(salesPrice);
	totalAppreciation = new Number(totalAppreciation);
	totalPrincipalPaid = new Number(totalPrincipalPaid);
	loanAmt = new Number(loanAmt);
	
	rvb.equityEarned = salesPrice + totalAppreciation + totalPrincipalPaid - loanAmt;

	var totalPropertyTax = annualPropertyTax * expectedYearsToOwn;

	rvb.taxSavings = amort.getTotalTaxSavings() + (totalPropertyTax * federalTaxRate) + (discountPoints * loanAmt * federalTaxRate);

	// Get closing costs
	var tempEst = new EstimatedClosingCosts();
	rvb.closingCosts = tempEst.calculate(loanAmt,loanTerm,interestRate,discountPoints,salesPrice - loanAmt);

	rvb.ownershipCost= (totalInterestPaid + totalPrincipalPaid + totalPMI + totalPropertyTax + rvb.closingCosts + (annualMiscOwnershipFees * expectedYearsToOwn)) - (rvb.taxSavings + rvb.equityEarned);

	rvb.grossCosts = (totalInterestPaid + totalPrincipalPaid + totalPMI + totalPropertyTax + rvb.closingCosts + (annualMiscOwnershipFees * expectedYearsToOwn));

	return rvb;
}