if (Calculators == undefined) {
	var Calculators = {};
}
if (Calculators.report == undefined) {
	Calculators.report = {};
}
Calculators.report.RefinanceReport = function RefinanceReport(refinance, monthly) {
	this.refinance = refinance;
	this.monthly = monthly;
	
	this.getBody = function() {
		var title = "Breakeven Measures";
		
		var body = new Array();
		// Summary values
        body.push("    <table width=\"95%\">\n");
		// Balance
        body.push("      <tr>\n");
        body.push("        <td><b>Original Loan Amount</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.originalAmount.formatted() + "</td>\n");
        body.push("        <td><b>Refinanced Loan Amount</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.refinancedBalance.formatted() + "</td>\n");
        body.push("      </tr>\n");
		
		// Appraised value
        body.push("      <tr>\n");
        body.push("        <td><b>Original Appraised Value</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.originalAppraisedValue.formatted() + "</td>\n");
        body.push("        <td><b>Refinanced Appraised Valuet</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.refinancedAppraisedValue.formatted() + "</td>\n");
        body.push("      </tr>\n");
		
		// Interest rate
        body.push("      <tr>\n");
        body.push("        <td><b>Original Interest Rate</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.originalRate.formatted() + "</td>\n");
        body.push("        <td><b>Refinanced Interest Rate</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.refinancedRate.formatted() + "</td>\n");
        body.push("      </tr>\n");
		
		// Years
        body.push("      <tr>\n");
        body.push("        <td><b>Original Years Remaining </b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.getOriginalYearsRemaining() + "</td>\n");
        body.push("        <td><b>Refinanced Years Remaining</b></td>\n");
        body.push("        <td width=\"30%\">" + this.refinance.getRefinancedTermYears() + "</td>\n");
        body.push("      </tr>\n");

		var oStmt = this.refinance.getOriginalLoanMonthlyStatement(1);
		var rStmt = this.refinance.getRefinancedLoanMonthlyStatement(1);

		// Principal + Interest (PI)
        body.push("      <tr>\n");
        body.push("        <td><b>Original Principal + Interest</b></td>\n");
        body.push("        <td width=\"30%\">" + oStmt.getPrincipal().add(oStmt.getInterest()).formatted() + "</td>\n");
        body.push("        <td><b>Refinanced Principal + Interest</b></td>\n");
        body.push("        <td width=\"30%\">" + rStmt.getPrincipal().add(rStmt.getInterest()).formatted() + "</td>\n");
        body.push("      </tr>\n");

		// PMI
        body.push("      <tr>\n");
        body.push("        <td><b>Original PMI</b></td>\n");
        body.push("        <td width=\"30%\">" + oStmt.getPMI().formatted() + "</td>\n");
        body.push("        <td><b>Refinanced PMI</b></td>\n");
        body.push("        <td width=\"30%\">" + rStmt.getPMI().formatted() + "</td>\n");
        body.push("      </tr>\n");

		var periods = refinance.getRefinancedTermYears();
		var oldInterest = 0.00;
		var newInterest = 0.00;
		for (var i = 1; i <= periods; ++i) {
		    var oldPeriod = refinance.getOriginalLoanYearlyStatement(i);
		    var newPeriod = refinance.getRefinancedLoanYearlyStatement(i);
			oldInterest += oldPeriod.getInterest().getAmount();
			newInterest += newPeriod.getInterest().getAmount();
		}

		// Total interest
        body.push("      <tr>\n");
        body.push("        <td><b>Original Loan Interest</b></td>\n");
        body.push("        <td width=\"30%\">" + new Calculators.type.Money(oldInterest).formatted() + "</td>\n");
        body.push("        <td><b>Refinanced Loan Interest</b></td>\n");
        body.push("        <td width=\"30%\">" + new Calculators.type.Money(newInterest).formatted() + "</td>\n");
        body.push("      </tr>\n");

        body.push("    </table>\n");

		if (this.monthly) {
			var units = "Month";
		}
		else {
			var units = "Year";
		}
		// Main table
        body.push("    <hr size=\"1\" />\n");
        body.push("    <table width=\"100%\" cellspacing=\"0\">\n");
        body.push("      <tr>\n");
        body.push("        <td colspan=\"5\" align=\"center\"><b>Original Loan</b></td>\n");
        body.push("        <td colspan=\"4\" align=\"center\"><b>Refinanced Loan</b></td>\n");
        body.push("      </tr>\n");
        body.push("      <tr>\n");
        body.push("        <td width=\"10%\"><b>" + units + "</b></td>\n");
        body.push("        <td width=\"10%\"><b>Equity</b></td>\n");
        body.push("        <td width=\"10%\"><b>Interest</b></td>\n");
        body.push("        <td width=\"10%\"><b>PMI</b></td>\n");
        body.push("        <td width=\"20%\"><b>Balance</b></td>\n");
        body.push("        <td width=\"10%\"><b>Equity</b></td>\n");
        body.push("        <td width=\"10%\"><b>Interest</b></td>\n");
        body.push("        <td width=\"10%\"><b>PMI</b></td>\n");
        body.push("        <td width=\"10%\"><b>Balance</b></td>\n");
        body.push("      </tr>\n");

		if (this.monthly) {
			periods = periods * 12;
		}
        for (var i = 1; i <= periods; ++i) {
			if (this.monthly) {
	            var oldPeriod = refinance.getOriginalLoanMonthlyStatement(i);
	            var newPeriod = refinance.getRefinancedLoanMonthlyStatement(i);
			}
			else {
	            var oldPeriod = refinance.getOriginalLoanYearlyStatement(i);
	            var newPeriod = refinance.getRefinancedLoanYearlyStatement(i);
			}

            if (i % 2 == 0) {
                body.push("      <tr>\n");
            } else {
                body.push("      <tr class=\"highlightStripe\">\n");
            }
            body.push("        <td>");
			body.push(i);
			
			// Original loan
			body.push("</td>\n");
            body.push("        <td>");
			body.push((oldPeriod) ? oldPeriod.getPrincipal().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("        <td>");
			body.push((oldPeriod) ? oldPeriod.getInterest().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("        <td>");
			body.push((oldPeriod) ? oldPeriod.getPMI().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("        <td>");
			body.push((oldPeriod) ? oldPeriod.getBalance().formatted() : "$0.00");
			body.push("</td>\n");
			
			// Refinanced loan
            body.push("        <td>");
			body.push((newPeriod) ? newPeriod.getPrincipal().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("        <td>");
			body.push((newPeriod) ? newPeriod.getInterest().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("        <td>");
			body.push((newPeriod) ? newPeriod.getPMI().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("        <td>");
			body.push((newPeriod) ? newPeriod.getBalance().formatted() : "$0.00");
			body.push("</td>\n");
            body.push("      </tr>\n");
        }

        body.push("    </table>\n");
		
        return body.join("");
	}
	
	this.getTitle = function() {
		return "Report";
	}
}
Calculators.report.RefinanceReport.prototype = new Calculators.report.Report();

