// JavaScript Document

// ADJUST STYLE SHEET ACCORDING TO BROWER'S VIEWABLE SIZE
var width;
if (self.innerHeight)
	width = self.innerWidth;
else if (document.documentElement)
	width = document.documentElement.clientWidth;
else if (document.body)
	width = document.body.clientWidth;

if (width <= 1024)
	document.getElementsByTagName("link")[0].href = "includes/styles-sm.css";


function FormatCurrency(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));
			
	num = '$' + num + '.' + cents;
	return num;
}

function FormatAmount(elmnt) {
	num = elmnt.value.toString().replace(/\$|\,/g,'');

	if (num.length > 0) {
		if (isNaN(num)) {
			alert("Please enter a numeric value.");
			elmnt.style.backgroundColor = "#FCC";
			return false;
		}
		
		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));
			
		elmnt.style.backgroundColor = "";
		elmnt.value = '$' + num + '.' + cents;
	} else
		return false;
}

function FormatPercent(elmnt) {
	num = elmnt.value.toString().replace(/\%|\,/g,'');
	if (num.length > 0) {
		if (isNaN(num)) {
			alert("Please enter a numeric value.");
			elmnt.style.backgroundColor = "#FCC";
			return false;
		}
		
		elmnt.style.backgroundColor = "";
		elmnt.value = parseFloat(num).toFixed(2) + "%";
	} else
		return false;
}

function FormatInteger(elmnt) {
	num = elmnt.value.toString().replace(/\,/g,'');
	
	if (num.length > 0) {
		if (isNaN(num)) {
			alert("Please enter a numeric value.");
			elmnt.style.backgroundColor = "#FCC";
			return false;
		}
		
		elmnt.style.backgroundColor = "";
		elmnt.value = parseInt(num).toString();
	} else
		return false;
}

function CalculateMortgage() {
	var txtAmount   = document.getElementById('txtAmount');
	var txtInterest = document.getElementById('txtInterest');
	var txtMonths   = document.getElementById('txtMonths');
	
	var amt = txtAmount.value.toString().replace(/\$|\,/g,'');
	var int = txtInterest.value.toString().replace(/\%|\,/g,'');
	var trm = txtMonths.value;
	
	if (amt.length == 0) {
		alert("Please enter a Loan Amount.");
		txtAmount.style.backgroundColor = "#FCC";
		return false;
	} else if (int.length == 0) {
		alert("Please enter an Interest Amount.");
		txtInterest.style.backgroundColor = "#FCC";
		return false;
	} else if (trm.length == 0) {
		alert("Please enter a Term Length.");
		txtMonths.style.backgroundColor = "#FCC";
		return false;
	}
	
	int = int / 12 / 100;
	
	// PAYMENT FORMULA
	// P = L[c(1+c)^n] / [(1+c)^n - 1]
	// P: Monthly Payment
	// L: Loan Amount
	// c: Interest Rate
	// n: Number of Months
	
	var mthPayment = 0.00;
	mthPayment = (amt * (int * Math.pow((1 + int), trm)) / (Math.pow((1 + int), trm) - 1));

	mthPayment = Math.floor(mthPayment * 100 + 0.50000000001);
	cents = mthPayment % 100;
	mthPayment = Math.floor(mthPayment/100).toString();
		
	if (cents < 10)
		cents = "0" + cents;
			
	for (var i=0; i<Math.floor((mthPayment.length-(1+i))/3); i++)
		mthPayment = mthPayment.substring(0,mthPayment.length-(4*i+3))+','+mthPayment.substring(mthPayment.length-(4*i+3));
			
	mthPayment = '$' + mthPayment + '.' + cents;
	
	if (document.all)
		document.getElementById('txtPayment').innerText = "" + mthPayment;
	else
		document.getElementById('txtPayment').textContent = "" + mthPayment;
}

function MortgageSchedule() {
	var pymt;
	if (document.all)
		pymt = document.getElementById('txtPayment').innerText.toString().replace(/\$|\,/g,'');
	else
		pymt = document.getElementById('txtPayment').textContent.toString().replace(/\$|\,/g,'');

	if (pymt.length == 0) {
		alert("Please compute the Monthly Payment before generating the schedule.");
		return false;
	}
	
	// Display Loan Date
	var startMonth = document.getElementById('optMonth')[document.getElementById('optMonth').selectedIndex].value;
	var startDay   = document.getElementById('optDay')[document.getElementById('optDay').selectedIndex].value;
	var startYear  = document.getElementById('optYear')[document.getElementById('optYear').selectedIndex].value;

	if (document.all)
		document.getElementById('AmortLoanDate').innerText = startMonth + " / " + startDay + " / " + startYear;
	else
		document.getElementById('AmortLoanDate').textContent = startMonth + " / " + startDay + " / " + startYear;

	// Display Principal
	var principal = document.getElementById('txtAmount').value;
	if (document.all)
		document.getElementById('AmortPrincipal').innerText = "" + principal;
	else
		document.getElementById('AmortPrincipal').textContent = "" + principal;

	// Display Number of Payments
	var numPayments = document.getElementById('txtMonths').value;
	if (document.all)
		document.getElementById('AmortPayments').innerText = "" + numPayments;
	else
		document.getElementById('AmortPayments').textContent = "" + numPayments;

	// Display Interest Rate
	var interest = document.getElementById('txtInterest').value;
	if (document.all)
		document.getElementById('AmortInterest').innerText = "" + interest;
	else
		document.getElementById('AmortInterest').textContent = "" + interest;

	// Display Monthly Payment
	if (document.all)
		document.getElementById('AmortPayment').innerText = "" + document.getElementById('txtPayment').innerText;
	else
		document.getElementById('AmortPayment').textContent = "" + document.getElementById('txtPayment').textContent;

	// Create Schedule
	var tBody = document.getElementById('AmortBody');
	var curPayment, curDate, curPrincipal, curInterest, curBalance;
	var totPrincipal, totInterest;
	var startDate = new Date();
	startDate.setFullYear(startYear, (startMonth - 1), startDay);
	
	for (var i=tBody.rows.length; i>0; i--) {
		tBody.deleteRow(i-1);
	}

	var amt = document.getElementById('txtAmount').value.toString().replace(/\$|\,/g,'');
	var int = document.getElementById('txtInterest').value.toString().replace(/\%|\,/g,'');
	var trm = document.getElementById('txtMonths').value;
	
	int = int / 12 / 100;
	totPrincipal = 0.00;
	totInterest  = 0.00;
	
	for (var i=1; i<=trm; i++) {
		curPayment = i;
		curDate = new Date();
		curDate.setFullYear(startDate.getFullYear(), (startDate.getMonth() + i), startDate.getDate());
		
		if (i == 1)
			curInterest = amt * int;
		else
			curInterest = curBalance * int;
		
		curPrincipal = pymt - curInterest;
		
		totPrincipal += curPrincipal;
		totInterest += curInterest;
		
		// BALANCE FORMULA
		// B = L[(1+c)^n - (1+c)^p] / [(1+c)^n - 1]
		// B: Balance
		// L: Loan Amount
		// c: Interest Rate
		// n: Number of Terms
		// p: Number of Months Passed
		curBalance = amt * (Math.pow((1+int), trm) - Math.pow((1+int), i)) / (Math.pow((1+int), trm) - 1);

		var tr  = document.createElement('tr');
		var td1 = document.createElement('td');
		var td2 = document.createElement('td');
		var td3 = document.createElement('td');
		var td4 = document.createElement('td');
		var td5 = document.createElement('td');
		
		td1.appendChild(document.createTextNode('' + curPayment));
		td2.appendChild(document.createTextNode((curDate.getMonth()+1) + "/" + curDate.getDate() + "/" + curDate.getFullYear()));
		td3.appendChild(document.createTextNode('' + FormatCurrency(curPrincipal)));
		td4.appendChild(document.createTextNode('' + FormatCurrency(curInterest)));
		td5.appendChild(document.createTextNode('' + FormatCurrency(curBalance)));

		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);
		tr.appendChild(td5);
		
		if (i % 2)
			tr.className = "RowDark";
		else
			tr.className = "RowLight";
		
		tBody.appendChild(tr);
	}

	var tr  = document.createElement('tr');
	var td1 = document.createElement('td');
	var td2 = document.createElement('td');
	var td3 = document.createElement('td');
	var td4 = document.createElement('td');
	var td5 = document.createElement('td');
	
	td1.appendChild(document.createTextNode('Totals'));
	td2.appendChild(document.createTextNode(''));
	td3.appendChild(document.createTextNode('' + FormatCurrency(totPrincipal)));
	td4.appendChild(document.createTextNode('' + FormatCurrency(totInterest)));
	td5.appendChild(document.createTextNode(''));

	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	tr.appendChild(td5);
	
	tr.className = "Totals";
	
	tBody.appendChild(tr);

	document.getElementById('AmortSchedule').style.display = "";
}

function CalculateLoan() {
	var txtAmount   = document.getElementById('txtAmount');
	var txtInterest = document.getElementById('txtInterest');
	var txtMonths   = document.getElementById('txtTerms');
	
	var amt = txtAmount.value.toString().replace(/\$|\,/g,'');
	var int = txtInterest.value.toString().replace(/\%|\,/g,'');
	var trm = txtMonths.value;
	
	if (amt.length == 0) {
		alert("Please enter a Loan Amount.");
		txtAmount.style.backgroundColor = "#FCC";
		return false;
	} else if (int.length == 0) {
		alert("Please enter an Interest Amount.");
		txtInterest.style.backgroundColor = "#FCC";
		return false;
	} else if (trm.length == 0) {
		alert("Please enter a Term Length.");
		txtMonths.style.backgroundColor = "#FCC";
		return false;
	}
	
	int = int / document.getElementById('optFrequency').value / 100;
	
	// PAYMENT FORMULA
	// P = L[c(1+c)^n] / [(1+c)^n - 1]
	// P: Monthly Payment
	// L: Loan Amount
	// c: Interest Rate
	// n: Number of Months
	
	var payment = 0.00;
	payment = (amt * (int * Math.pow((1 + int), trm)) / (Math.pow((1 + int), trm) - 1));
  finance = (payment * trm) - amt;
  payback = payment * trm;

	if (document.all) {
		document.getElementById('txtPayment').innerText = "" + FormatCurrency(payment);
		document.getElementById('txtFinance').innerText = "" + FormatCurrency(finance);
		document.getElementById('txtPayback').innerText = "" + FormatCurrency(payback);
	} else {
		document.getElementById('txtPayment').textContent = "" + FormatCurrency(payment);
		document.getElementById('txtFinance').textContent = "" + FormatCurrency(finance);
		document.getElementById('txtPayback').textContent = "" + FormatCurrency(payback);
	}
}

function CalculateVehicle() {
	var txtAmount   = document.getElementById('txtAmount');
	var txtTax      = document.getElementById('txtTax');
	var txtDownPymt = document.getElementById('txtAmountDown');
	var txtInterest = document.getElementById('txtInterest');
	var txtTerms    = document.getElementById('txtTerms');
	
	var amt = txtAmount.value.toString().replace(/\$|\,/g,'');
	var tax = txtTax.value.toString().replace(/\%|\,/g,'');
	var dwn = txtDownPymt.value.toString().replace(/\$|\,/g,'');
	var int = txtInterest.value.toString().replace(/\%|\,/g,'');
	var trm = txtTerms.value;
	
	if (amt.length == 0) {
		alert("Please enter a Purchase Price.");
		txtAmount.style.backgroundColor = "#FCC";
		return false;
	} else if (tax.length == 0) {
		alert("Please enter an Sales Tax Amount.");
		txtInterest.style.backgroundColor = "#FCC";
		return false;
	} else if (int.length == 0) {
		alert("Please enter an Interest Amount.");
		txtInterest.style.backgroundColor = "#FCC";
		return false;
	} else if (trm.length == 0) {
		alert("Please enter a Term Length.");
		txtMonths.style.backgroundColor = "#FCC";
		return false;
	}
	
	if (dwn.length == 0)
		dwn = 0.00;
	
	var price = (amt * (1 + (tax / 100)));
	var dmvFees = 0.0125 * price;
	price = price - dwn;
	
	int = int / 12 / 100;
	
	// PAYMENT FORMULA
	// P = L[c(1+c)^n] / [(1+c)^n - 1]
	// P: Monthly Payment
	// L: Loan Amount
	// c: Interest Rate
	// n: Number of Months
	
	var mthPayment = 0.00;
	mthPayment = (price * (int * Math.pow((1 + int), trm)) / (Math.pow((1 + int), trm) - 1));

	if (document.all) {
		document.getElementById('txtDMV').innerText = "" + FormatCurrency(dmvFees);
		document.getElementById('txtLoan').innerText = "" + FormatCurrency(price);
		document.getElementById('txtPayment').innerText = "" + FormatCurrency(mthPayment);
	} else {
		document.getElementById('txtDMV').textContent = "" + FormatCurrency(dmvFees);
		document.getElementById('txtLoan').textContent = "" + FormatCurrency(price);
		document.getElementById('txtPayment').textContent = "" + FormatCurrency(mthPayment);
	}
}

function toggle(id) {
	elmnt = document.getElementById(id);
	if (elmnt.style.display == 'none')
		elmnt.style.display = 'block';
	else
		elmnt.style.display = 'none';
}