function calculatorTable() {
	if (!document.getElementById) {return false;}
	if (!document.getElementsByTagName) {return false;}
	if (!document.createElement) {return false;}
	if (!document.appendChild) {return false;}
	if (!document.removeChild) {return false;}
	if (!document.createTextNode) {return false;}

	var table = document.getElementById('calculator');

	var table_thead = table.getElementsByTagName('thead')[0];
	var table_thead_rows = table_thead.getElementsByTagName('tr');

	var table_tbody = table.getElementsByTagName('tbody')[0];
	var table_tbody_rows = table_tbody.getElementsByTagName('tr');

	var table_tfoot = table.getElementsByTagName('tfoot')[0];
	var table_tfoot_rows = table_tfoot.getElementsByTagName('tr');

	// Make select for cash-flow rate
	var cash_flow_select = document.createElement('select');
	cash_flow_select.setAttribute('id', 'cash-flow');
	cash_flow_select.setAttribute('name', 'cash-flow');

	var cash_flow_headers = table_thead_rows[0].getElementsByTagName('th');
	var cash_flow_values = Array();

	for (var h = 0; h < cash_flow_headers.length; h++) {
		var header = cash_flow_headers[h];

		if (header.getAttribute('scope') == 'col') {
			var header_value = header.firstChild.nodeValue;

			var option = document.createElement('option');
			var option_text_node = document.createTextNode(header_value);
			var option_value = header_value.replace('%', '');

			option.appendChild(option_text_node);
			option.setAttribute('value', option_value);

			cash_flow_select.appendChild(option);

			cash_flow_values.push(option_value);
		}
	}

	var cash_flow_footer_cells = table_tfoot_rows[0].getElementsByTagName('td');
	var years_payoff_values = Array();

	for (var c = 0; c < cash_flow_footer_cells.length; c++) {
		var cell = cash_flow_footer_cells[c];

		var cash_flow_value = cash_flow_values[c];
		var footer_value = cell.firstChild.nodeValue;

		years_payoff_values[cash_flow_value] = footer_value;
	}

	// Loan balance select
	var loan_balance_select = document.createElement('select');
	loan_balance_select.setAttribute('id', 'loan-balance');
	loan_balance_select.setAttribute('name', 'loan-balance');

	var loan_balance_values = Array();

	for (var r = 0; r < table_tbody_rows.length; r++) {
		var row = table_tbody_rows[r];
		var row_th_value = row.getElementsByTagName('th')[0].firstChild.nodeValue;

		var option = document.createElement('option');
		var option_text_node = document.createTextNode(row_th_value);
		var option_value = row_th_value.replace('$', '').replaceAll(',', '');

		option.appendChild(option_text_node);
		option.setAttribute('value', option_value);

		loan_balance_select.appendChild(option);

		loan_balance_values.push(option_value);
	}

	// Combination array
	var monthly_savings_values = Array();
	for (var r = 0; r < table_tbody_rows.length; r++) {
		var row = table_tbody_rows[r];
		var row_cells = row.getElementsByTagName('td');

		var loan_balance_value = loan_balance_values[r];

		monthly_savings_values[loan_balance_value] = Array();

		for (var c = 0; c < row_cells.length; c++) {
			var cell = row_cells[c];

			var cell_value = cell.firstChild.nodeValue;
			var cash_flow_value = cash_flow_values[c];

			monthly_savings_values[loan_balance_value][cash_flow_value] = cell_value;
		}
	}

	// Output elements
	var monthly_savings_output = document.createElement('output');
	monthly_savings_output.setAttribute('for', 'loan-balance cash-flow');
	monthly_savings_output.setAttribute('name', 'monthly-savings');
	monthly_savings_output.appendChild(document.createTextNode(''));

	var years_payoff_output = document.createElement('output');
	years_payoff_output.setAttribute('for', 'cash-flow');
	years_payoff_output.setAttribute('name', 'years-payoff');
	years_payoff_output.appendChild(document.createTextNode(''));

	// Build new table header
	var new_thead_tr = document.createElement('tr');

	var loan_balance_th = document.createElement('th');
	var cash_flow_th = document.createElement('th');
	var monthly_savings_th = document.createElement('th');
	var years_payoff_th = document.createElement('th');

	loan_balance_th.setAttribute('scope', 'col');
	cash_flow_th.setAttribute('scope', 'col');
	monthly_savings_th.setAttribute('scope', 'col');
	years_payoff_th.setAttribute('scope', 'col');

	loan_balance_th.appendChild(document.createTextNode("Loan balance"));
	cash_flow_th.appendChild(document.createTextNode("Cash-flow rate"));
	monthly_savings_th.appendChild(document.createTextNode("Monthly savings"));
	years_payoff_th.appendChild(document.createTextNode("Years payoff"));

	new_thead_tr.appendChild(loan_balance_th);
	new_thead_tr.appendChild(cash_flow_th);
	new_thead_tr.appendChild(monthly_savings_th);
	new_thead_tr.appendChild(years_payoff_th);

	// Build new table body
	var new_tbody_tr = document.createElement('tr');

	var loan_balance_td = document.createElement('td');
	var cash_flow_td = document.createElement('td');
	var monthly_savings_td = document.createElement('td');
	var years_payoff_td = document.createElement('td');

	loan_balance_td.appendChild(loan_balance_select);
	cash_flow_td.appendChild(cash_flow_select);
	monthly_savings_td.appendChild(monthly_savings_output);
	years_payoff_td.appendChild(years_payoff_output);

	new_tbody_tr.appendChild(loan_balance_td);
	new_tbody_tr.appendChild(cash_flow_td);
	new_tbody_tr.appendChild(monthly_savings_td);
	new_tbody_tr.appendChild(years_payoff_td);

	var new_thead = document.createElement('thead');
	new_thead.appendChild(new_thead_tr);

	var new_tbody = document.createElement('tbody');
	new_tbody.appendChild(new_tbody_tr);

	table.appendChild(new_thead);
	table.appendChild(new_tbody);

	table.removeChild(table_thead);
	table.removeChild(table_tbody);
	table.removeChild(table_tfoot);

	function updateMonthlySavings() {monthly_savings_output.firstChild.nodeValue = monthly_savings_values[loan_balance_select.getElementsByTagName('option')[loan_balance_select.selectedIndex].getAttribute('value')][cash_flow_select.getElementsByTagName('option')[cash_flow_select.selectedIndex].getAttribute('value')];}
	function updateYearsPayoff() {years_payoff_output.firstChild.nodeValue = years_payoff_values[cash_flow_select.getElementsByTagName('option')[cash_flow_select.selectedIndex].getAttribute('value')];}

	cash_flow_select.onchange = function() {
		updateMonthlySavings();
		updateYearsPayoff();
	}
	loan_balance_select.onchange = function() {updateMonthlySavings();}

	updateMonthlySavings();
	updateYearsPayoff();
}
addLoadEvent(calculatorTable);