/*
	LFSB Profit Calculator
*/

function lfsbpc_product_change() {
	var yearsToShow = 3;
	var commissionRate = .25;
	
	// get the selection
	var productSelection = $('example_products');
	//var monthlySales = $('monthly_sales');
	var oneMonthCommission = $('one_month_commission');

	var salesPerMonth = parseInt($F('salesPerMonth'));

	var baseMonthly = $F(productSelection) * salesPerMonth * commissionRate;

	//monthlySales.innerHTML = '$'+Number($F(productSelection)).toFixed(2);
	oneMonthCommission.innerHTML = '$'+addCommas(baseMonthly.toFixed(2));

	// get the container
	resultsTableContainer = $('results');
	
	// clear the container
	resultsTableContainer.innerHTML = '';
	
	resultsTable = document.createElement("table");
	resultsTableHeader = document.createElement("thead");
	resultsTableBody = document.createElement("tbody");
	resultsTableHeaderRow = document.createElement("tr");
	resultsTableDataRow = document.createElement("tr");

	// add the year headers
	for (i=1; i<=yearsToShow; i++) {
		var yearHeader = createSimpleElement("th",null,"header",ith(i)+" Year");
		yearHeader.width="33%";
		if(i % 2 == 1)
			yearHeader.style.backgroundColor = "#EEEEEE";
		resultsTableHeaderRow.appendChild(yearHeader);
	}
	// add the rows to the header
	resultsTableHeader.appendChild(resultsTableHeaderRow);


	// Build the data tables
	//
	var runningTotal = 0;
	var balanceTotal = 0;
	var totalSales = 0;

	for (year=1; year<=yearsToShow; year++) {
		var yearTotal = 0;
		// build the data table for this year.
		dataTable = document.createElement("table");
		dataTableBody = document.createElement("tbody");
		dataTableHeader = document.createElement("thead");
		
		if(year % 2 == 1)
			dataTable.style.backgroundColor = "#EEEEEE";
		
		// add the header
		dataHeaderRow = document.createElement('tr');
		dataHeaderRow.appendChild(createSimpleElement('th',null,null,'Month'));
		dataHeaderRow.appendChild(createSimpleElement('th',null,null,'Accts'));
		dataHeaderRow.appendChild(createSimpleElement('th',null,null,'Profit'));
		
		dataTableHeader.appendChild(dataHeaderRow);
		
		// add the rows for the year
		for (month=1; month<=12; month++) {
			runningTotal += baseMonthly;
			yearTotal += runningTotal;
			balanceTotal += runningTotal;
			totalSales = totalSales + salesPerMonth;
			
			dataTableRow = document.createElement('tr');
			dataTableRow.appendChild(createSimpleElement('td',null,'month',ith(month)));
			dataTableRow.appendChild(createSimpleElement('td',null,'sales',addCommas(totalSales)));
			dataTableRow.appendChild(createSimpleElement('td',null,'profit','$'+addCommas(runningTotal.toFixed(2))));
			
			// add the row to the body
			dataTableBody.appendChild(dataTableRow);
		}
		
		// add the total row.
		dataTableRow = document.createElement('tr');
		var yearTotalCell = createSimpleElement('td',null,'yearTotal','Year Total:');
		yearTotalCell.colSpan = 2;
		dataTableRow.appendChild(yearTotalCell);
		dataTableRow.appendChild(createSimpleElement('td',null,'yearTotalAmount','$'+addCommas(yearTotal.toFixed(2))));
		dataTableBody.appendChild(dataTableRow);

		// add the balance row
		/*
		dataTableRow = document.createElement('tr');
		var yearBalanceCell = createSimpleElement('td',null,'yearBalance','Balance:');
		yearBalanceCell.colSpan = 2;
		dataTableRow.appendChild(yearBalanceCell);
		dataTableRow.appendChild(createSimpleElement('td',null,'yearBalanceAmount','$'+addCommas(balanceTotal.toFixed(2))));
		dataTableBody.appendChild(dataTableRow);
		*/
		
		// update the top totals table
		($('profit'+year)).innerHTML = '$'+addCommas(yearTotal.toFixed(2));
		($('profit'+year+'Balance')).innerHTML = '$'+addCommas(balanceTotal.toFixed(2));

		// add the body and header to the table.
		dataTable.appendChild(dataTableHeader);
		dataTable.appendChild(dataTableBody);
		
		// add the cell for this year
		dataCell = document.createElement("td");
		dataCell.appendChild(dataTable);
		
		// add the cell to the row.
		resultsTableDataRow.appendChild(dataCell);
		// add the row to the body.
		resultsTableBody.appendChild(resultsTableDataRow);
	}
	
	// add the header and the body to the main table
	resultsTable.appendChild(resultsTableHeader);
	resultsTable.appendChild(resultsTableBody);
	
	// add the table to the container
	resultsTableContainer.appendChild(resultsTable);
}

function createSimpleElement (elementType, id, className, innerHTML) {
	var newElement = document.createElement(elementType);
	if (id) newElement.id = id;
	if (className) newElement.className = className;
	if (innerHTML) newElement.innerHTML = innerHTML;
	return newElement;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function ith(num) {
	if (num == 1) {
		return "1st";
	} else if (num == 2) {
		return "2nd";
	} else if (num == 3) {
		return "3rd";
	} else {
		return num+"th";
	}
}