﻿
// variables for this specific page
var loanAmountMin = 1;
var loanAmountMax = 500000;

var termMonthsMin = 24;
var termMonthsMax = 120;

var lowAsRateMin = 1;
var lowAsRateMax = 20;


var highAsRateMin = 1; // highAsRateMin will actually be automatically set to the user-entered value of LowAsRate
var highAsRateMax = 20;

var scenarioRowDisposition = 0;

function GetObj(id) {
	return document.getElementById(id);
}




function AddToScenarioComparison() {
	_amount = parseFloat(GetObj('loanAmount').value);
	_termMonths = parseFloat(GetObj('termMonths').value);
	_rateLow = parseFloat(GetObj('LowAsRate').value);
	_rateHigh = parseFloat(GetObj('HighAsRate').value);
	_paymenLow = parseFloat(GetObj('PaymentLow').value);
	_paymenHigh = parseFloat(GetObj('PaymentHigh').value);

	CalcPayments(_amount, _termMonths, _rateLow, _rateHigh);
	StoreScenario(_amount, _termMonths, _rateLow, _paymenLow, _rateHigh, _paymenHigh);
	GetObj('btnClearComparisons').disabled = false;
	GetObj('btnSendToPrint').disabled = false;

	_rateLow = _rateLow + '%';
	_rateHigh = _rateHigh + '%';

	var tblBody = document.getElementById('tblScenario').tBodies[0];
	var newRow = tblBody.insertRow(-1);
	newRow.A
	var divAttribute = '<div style="text-align:right">';

	if (scenarioRowDisposition == 0) {
		scenarioRowDisposition = 1;
		//divAttribute = divAttribute + '">';
		newRow.style.backgroundColor = '';
	}
	else {
		scenarioRowDisposition = 0;
		//divAttribute = divAttribute + '; background-color:#C9FFF6; ">';
		newRow.style.backgroundColor = '#C9FFF6';
	}


	var newCell = newRow.insertCell(0);
	newCell.innerHTML = divAttribute + _amount + '</div>';

	newCell = newRow.insertCell(1);
	newCell.innerHTML = divAttribute + _termMonths + '</div>';

	newCell = newRow.insertCell(2);
	newCell.innerHTML = divAttribute + _rateLow + '</div>';

	newCell = newRow.insertCell(3);
	newCell.innerHTML = divAttribute + _rateHigh + '</div>';

	newCell = newRow.insertCell(4);
	newCell.innerHTML = divAttribute + _paymenLow + '</div>';

	newCell = newRow.insertCell(5);
	newCell.innerHTML = divAttribute + _paymenHigh + '</div>';

}



function CalcFixedTermRate(tValue) {
	if (tValue == '--')
		return;

	_amount = GetObj('loanAmount').value;

	splitValues = tValue.split('|');
	_termMonths = splitValues[0];
	_rateLow = splitValues[1];
	_rateHigh = splitValues[2];

	GetObj('termMonths').value = _termMonths;
	GetObj('LowAsRate').value = _rateLow;
	GetObj('HighAsRate').value = _rateHigh;
	CalcPayments(_amount, _termMonths, _rateLow, _rateHigh);
	ValidateAll();
}



function StoreScenario(amt, terms, lowRate, lowPay, highRate, highPay) {
	GetObj('hfPrinciple').value = GetObj('hfPrinciple').value + amt + 'W';
	GetObj('hfTerm').value = GetObj('hfTerm').value + terms + 'Y';
	GetObj('hfRateLow').value = GetObj('hfRateLow').value + lowRate + 'X';
	GetObj('hfPaymentLow').value = GetObj('hfPaymentLow').value + lowPay + 'Z';
	GetObj('hfRateHigh').value = GetObj('hfRateHigh').value + highRate + 'C';
	GetObj('hfPaymentHigh').value = GetObj('hfPaymentHigh').value + highPay + 'D';
}


function CalcPaymentsNP() {
	// NP = NoParams

	//CalcPayments(loanAmount.value, termMonths.value, LowAsRate.value, HighAsRate.value);
	CalcPayments(GetObj('loanAmount').value, GetObj('termMonths').value, GetObj('LowAsRate').value, GetObj('HighAsRate').value);
}


function CalcPayments(_amt, _term, _aprLow, _aprHigh) {
	SetPaymentsNull();
	
	_amount = parseFloat(_amt);
	_termMonths = parseFloat(_term);
	_rateLow = parseFloat(_aprLow);
	_rateHigh = parseFloat(_aprHigh);

	if (_amount < 1)
		return;
	if (_termMonths < 1)
		return;
	if (_rateLow < 1)
		return;
	if (_rateHigh < _rateLow)
		return;
	GetObj('PaymentLow').value = MonthlyPayment(_amount, _termMonths, _rateLow);
	GetObj('PaymentHigh').value = MonthlyPayment(_amount, _termMonths, _rateHigh);
	GetObj('btnAddToScenario').disabled = false;
	GetObj('btnCompareScenarios').disabled = false;
}


function SendToPrintWindow() {
	if (GetObj('hfPrinciple').value == '') {
		alert('Please add 1 or more comparisons.');
		return;
	}

	if (confirm('Show the Comparisons in the Print Window?') == false)
		return;
	var myUrl = 'http://www.TruGrocer.com/cgi-local/printLoan.pl?princ=[principle]&term=[terms]&rate=[rateLow]&pment=[paymentLow]&hrate=[rateHigh]&hpay=[paymentHigh]';
	myUrl = myUrl.replace('[principle]', GetObj('hfPrinciple').value);
	myUrl = myUrl.replace('[terms]', GetObj('hfTerm').value);
	myUrl = myUrl.replace('[rateLow]', GetObj('hfRateLow').value);
	myUrl = myUrl.replace('[paymentLow]', GetObj('hfPaymentLow').value);
	myUrl = myUrl.replace('[rateHigh]', GetObj('hfRateHigh').value);
	myUrl = myUrl.replace('[paymentHigh]', GetObj('hfPaymentHigh').value);

	window.open(myUrl);
}


function CompareWithAlternatives() {
	if (confirm('Compare the current Scenario with Alternatives?') == false)
		return;

	myRate = parseFloat(GetObj('LowAsRate').value) + 0.5;

	myUrl = 'http://www.trugrocer.com/cgi-local/compare.pl?princ=[principle]&rate=[rate1]&pmts=[pmts1]';
	myUrl = myUrl.replace('[principle]', GetObj('loanAmount').value);
	myUrl = myUrl.replace('[rate1]', myRate);
	myUrl = myUrl.replace('[pmts1]', GetObj('termMonths').value);
	window.open(myUrl);
}


function ClearTable(msg, tableID) {
	if (confirm(msg) == false)
		return;

	var table = GetObj(tableID);
	//GetObj('btnClearComparisons').disabled = true;
	//GetObj('btnSendToPrint').disabled = true;

	for (var i = table.rows.length - 1; i > 0; i--) {
		table.deleteRow(i);
	}
	GetObj('hfPrinciple').value = '';
	GetObj('hfTerm').value = '';
	GetObj('hfRateLow').value = '';
	GetObj('hfPaymentLow').value = '';
	GetObj('hfRateHigh').value = '';
	GetObj('hfPaymentHigh').value = '';
	GetObj('btnClearComparisons').disabled = true;
	GetObj('btnSendToPrint').disabled = true;
	scenarioRowDisposition = 0;
}


function ValidateAll() {
	ValidateLoanAmount();
	ValidateTermMonths();
	ValidateLowAsRate();
	ValidateHighAsRate();
}


function ValidateThis(sender, min, max, errLabel, allowDecimal, decimalPlaces) {
	var allowNegative = false;
	
	SetPaymentsNull();
	extractNumber(sender, decimalPlaces, allowNegative);
	
	// by multiplying the sender's value by 100 as well as the min and max, we remove the necessity in dealing with decimals
	var iValue = sender.value * 100;
	min = min * 100;
	max = max * 100;
			
	var bValid = isIntegerInRange(iValue, min, max);

	if (bValid) {
		CalcPaymentsNP();
		toggleVisibility(errLabel, false);
	}
	else {
		toggleVisibility(errLabel, true);
	}
}


function ValidateLoanAmount() {
	sender = GetObj('loanAmount');
	errLabel = GetObj('lblLoanAmountError');
	ValidateThis(sender, loanAmountMin, loanAmountMax, errLabel, true, 2);
}


function ValidateTermMonths() {
	sender = GetObj('termMonths');
	errLabel = GetObj('lblTermMonthsError');
	ValidateThis(sender, termMonthsMin, termMonthsMax, errLabel, false, 0);
}


function ValidateLowAsRate() {
	sender = GetObj('LowAsRate');
	errLabel = GetObj('lblLowAsRateError');
	ValidateThis(sender, lowAsRateMin, lowAsRateMax, errLabel, true, 2);
	return;
}


function ValidateHighAsRate() {
	sender = GetObj('HighAsRate');
	errLabel = GetObj('lblHighAsRateError');
	highAsRateMin = GetObj('LowAsRate').value;
	if ((highAsRateMin * 100) < 100)
		highAsRateMin = 1;
	ValidateThis(sender, highAsRateMin, highAsRateMax, errLabel, true, 2);
	return;
}


function SetPaymentsNull() {
	GetObj('btnAddToScenario').disabled = true;
	GetObj('btnCompareScenarios').disabled = true;
	GetObj('PaymentLow').value = '--';
	GetObj('PaymentHigh').value = '--';
}
		