﻿//BuildGrid();
var startPercentage = 4;
var startLoanMonths = 12;
var startPctIncrement = .50;
var rows = 16;
var yearsToShow = 8;

var _paymentAmount = document.getElementById("_hiddenPrice").value;

document.getElementById("_loanAmt").value = _paymentAmount;

ShowPayment();


function getQS(QValue) {
	var search = window.location.search.substring(1);
	var gv = search.split("&");
	for (i = 0; i < gv.length; i++) {
		ft = gv[i].split("=");
		if (ft[0] == QValue)
			return ft[1];
	}
}

function ShowPayment() {
	var loanAmt = document.getElementById("_loanAmt").value;
	BuildGrid(loanAmt);
}

function BuildGrid(loanAmount) {
	var sthtml = "";
	var md = document.getElementById("moneyDiv");
	var loanAmt = loanAmount;

	var startPct = startPercentage;
	var loanMonths = startLoanMonths;

	sthtml += '<center><table border="0" cellpadding="3" cellspacing="0">';
	sthtml += "<tr>";
	sthtml += "<td rowspan='" + rows + 1 + "' class='_pmt_labels' align='center'>Interest<br/>Rates<\/td>";
	sthtml += "<td colspan='" + yearsToShow + 1 + "' class='_pmt_labels' align='center'>Months<\/td>";
	sthtml += "<\/tr>";
	/* Months accross the top */
	sthtml += "<tr>";
	sthtml += "<td class='_pmt_rates'>&nbsp;<\/td>";
	/* SHOW THE YEARS / MONTHS */
	for (var x = 1; x <= yearsToShow; x++) {
		sthtml += "<td class='_pmt_terms'>" + loanMonths + "<\/td>";
		loanMonths = loanMonths + 12;
	}
	sthtml += "<\/tr>";

	/* Rates down the side */
	loanMonths = 12;
	startPct = startPercentage;
	for (var i = 1; i <= rows; i++) {
		loanMonths = startLoanMonths;
		sthtml += "<tr>";
		sthtml += "<td class='_pmt_rates'>" + startPct.toFixed(3) + "%<\/td>";
		/*  Goind across the table */
		for (var x = 1; x <= yearsToShow; x++) {
			pmt = compute(loanAmt, loanMonths, startPct)
			sthtml += "<td class='_pmt_payment'>" + formatCurrency(pmt) + "<\/td>";
			loanMonths = loanMonths + 12;
		}
		sthtml += "<\/tr>";
		startPct = startPct + startPctIncrement;
	}
	sthtml += '<\/table><\/center>';
	md.innerHTML = sthtml;
}

function compute(Price, months, interest) {
	var down = document.getElementById("_downPmt").value;
	Price = Price - down;
	interest = (interest / 100.0) / 12;


	var pow = 1;
	for (var j = 0; j < months; j++)
		pow = pow * (1 + interest);

	var pmt = (Price * pow * interest) / (pow - 1);

	return myRound(pmt);
}

function myRound(val) {
	val = val * 100;
	val = Math.round(val);
	return val / 100;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

