
/*
  turn floating number into formatted currency string
*/

var tableData;     // array used to hold monthly table


/*
  called by the user, grabs input from fields, makes calculations,
  builds table, and then calls results functions
*/
function calc(){
	// Get form field values
	// --------------------------------
	numerator    = parseInt(get("numerator").value);
	divisor      = parseInt(get("divisor").value);

	// do to completion
	// --------------------------------
	while(remPrince>0.01){
		var mIntPay = remPrince * mRate;                   // interest this month
		var mPrincePay = (mPayment - mIntPay);             // principal payed this month
		totalPayed += mIntPay + mPrincePay;

    if(curMonths <= actualMonths){
      mPrincePay += extraPay;
      totalPayed += extraPay;
  		var rowData = new Array();
  		rowData[0] = actualMonths;			      // month
  		rowData[1] = money(remPrince);	      // remaining principal
  		rowData[2] = money(mIntPay);		      // interest payed
  		rowData[3] = money(mPrincePay);	      // principal payed
  		rowData[4] = money(totalPayed);	      // total payed
  		tableData.push(rowData);
  	}

		remPrince -= mPrincePay;
		totalInterest += mIntPay;
		actualMonths++;
	}

	// Show results
	// --------------------------------
  show("main");
  clearTag(get("drawTable"));

	drawResultText();

	if(!summOnly){
		show("drawingMsg");
		window.setTimeout("drawTable();",100);
	}
}


/*
  drawTable and makeRow are called by calc() to display the data
  stored in the tableData array
*/
function drawTable(){
	var drawTable = get("drawTable");
	var newTable = newFromTemplate("tableTemplate");
	clearTag(drawTable);
	drawTable.appendChild(newTable);
	var tableBody = newTable.appendChild(document.createElement("tbody"));
	for(var i=0; i<tableData.length; i++){
		var row = makeRow(tableData[i]);
		tableBody.appendChild(row);
	}
	showObject(newTable);
	show("tableHeading");
	hide("drawingMsg");
}

function makeRow(dataArray){
	var row = newFromTemplate("rowTemplate");
	for(var i=0; i<dataArray.length; i++){
		var cell = getTag(row, "td", i);
    cell.appendChild(makeText(dataArray[i]));
	}
	return row;
}
