var Calculators = Calculators || {};
Calculators.report = Calculators.report || {};

Calculators.report.AutoLoanReport = function AutoLoanReport(autoLoanPayment){
	this.MONTHS = 12;
	this.autoLoanPayment = autoLoanPayment;
	
	this.getBody = function() {
		var body = new Array();
        body.push("    <hr size=\"1\" />\n");
        body.push("    <table width=\"100%\" cellspacing=\"0\">\n");
        body.push("      <tr>\n");
        body.push("        <td width=\"20%\"><b>Payment</b></td>\n");
        body.push("        <td width=\"20%\"><b>Each Payment</b></td>\n");
        body.push("        <td width=\"20%\"><b>Interest</b></td>\n");
        body.push("        <td width=\"20%\"><b>Principal</b></td>\n");
        body.push("        <td width=\"20%\"><b>Balance</b></td>\n");
        body.push("      </tr>\n");

        var end = this.autoLoanPayment.getMonthsOfPayments();
				var statements = this.autoLoanPayment.getStatements();
		// Beginning balance row
		body.push("      <tr>\n");
        body.push("        <td>&nbsp;</td>\n");
        body.push("        <td>&nbsp;</td>\n");
        body.push("        <td>&nbsp;</td>\n");
        body.push("        <td>&nbsp;</td>\n");
        body.push("        <td>" + statements[0].getBalance().formatted() + "</td>\n");
        body.push("      </tr>\n");
		
		// Create running total variables for years and overall
		var yearlyPaymentTotal = 0.00;
		var yearlyInterestTotal = 0.00;
		var yearlyPrincipalTotal = 0.00;
		var grandPaymentTotal = 0.00;
		var grandInterestTotal = 0.00;
		var grandPrincipalTotal = 0.00;
		
        for (var i = 1; i <= end; ++i) {
            var monthly = statements[i];
			// Update the yearly totals
			yearlyPaymentTotal += monthly.payment.getAmount();
			yearlyInterestTotal += monthly.getInterest().getAmount();
			yearlyPrincipalTotal += monthly.getPrincipal().getAmount();
			
            if (i % 2 == 0) {
                body.push("      <tr>\n");
            } else {
                body.push("      <tr class=\"highlightStripe\">\n");
            }
            body.push("        <td>");
			body.push(i);
			body.push("</td>\n");
            body.push("        <td>");
			body.push(monthly.payment.formatted());
			body.push("</td>\n");
            body.push("        <td>");
			body.push(monthly.getInterest().formatted());
			body.push("</td>\n");
            body.push("        <td>");
			body.push(monthly.getPrincipal().formatted());
			body.push("</td>\n");
            body.push("        <td>");
			body.push(monthly.getBalance().formatted());
			body.push("</td>\n");
            body.push("      </tr>\n");
            if ((i % this.MONTHS) == 0) {
                var year = i / this.MONTHS;
                body.push("      <tr>\n");
                body.push("        <td style=\"border-top:1px solid black;\">Year ");
				body.push(year);
				body.push(" totals</td>\n");
                body.push("        <td style=\"border-top:1px solid black;\">");
				body.push(new Calculators.type.Money(yearlyPaymentTotal).formatted());
				body.push("</td>\n");
                body.push("        <td style=\"border-top:1px solid black;\">");
				body.push(new Calculators.type.Money(yearlyInterestTotal).formatted());
				body.push("</td>\n");
                body.push("        <td style=\"border-top:1px solid black;\">");
				body.push(new Calculators.type.Money(yearlyPrincipalTotal).formatted());
				body.push("</td>\n");
                body.push("        <td style=\"border-top:1px solid black;\">&nbsp;</td>\n");
                body.push("      </tr>\n");
                body.push("      <tr>\n");
                body.push("        <td colspan=\"5\">&nbsp;</td>\n");
                body.push("      </tr>\n");
				
				// Update the grand totals
				grandPaymentTotal += yearlyPaymentTotal;
				grandInterestTotal += yearlyInterestTotal;
				grandPrincipalTotal += yearlyPrincipalTotal;
				
				// Reset the yearly totals
				yearlyPaymentTotal = 0.00;
				yearlyInterestTotal = 0.00;
				yearlyPrincipalTotal = 0.00;
            }
        }

        body.push("      <tr>\n");
        body.push("        <td>Grand total</td>\n");
        body.push("        <td>");
		body.push(new Calculators.type.Money(grandPaymentTotal).formatted());
		body.push("</td>\n");
        body.push("        <td>");
		body.push(new Calculators.type.Money(grandInterestTotal).formatted());
		body.push("</td>\n");
        body.push("        <td>");
		body.push(new Calculators.type.Money(grandPrincipalTotal).formatted());
		body.push("</td>\n");
        body.push("        <td>");
		body.push("$0.00");
		body.push("</td>\n");
        body.push("        <td>&nbsp;</td>\n");
        body.push("      </tr>\n");
        body.push("    </table>\n");
		
        return body.join("");
	}

	this.getTitle = function() {
		return "Auto Loan Payment Report";
	}
}
Calculators.report.AutoLoanReport.prototype = new Calculators.report.Report();

