// Payment Calculator
/*

author: scott lenger
last updated: February 2008

instructions:
1. add link to <head>
2. add setCalculator to onload script
3. ensure input name attributes match those in the calculate values function
*/

var costElement;

// set the onkeyup and onchange functions and prepare the DOM
function setCalculator(calculatorFormId,paymentId) {
	if (!document.getElementById(calculatorFormId)) {return;} // if ID does not exist stop script
	var inputs = document.getElementById(calculatorFormId);
	inputs.getElementsByTagName("input");
	
	// call calculateValues from inputs
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].type == "text") { // skips hidden fields
			inputs[i].onkeyup = function () {filterNumeric(this); calculateValues(calculatorFormId);}
		}
		if (inputs[i].type == "submit") { // removes submit button
			inputs[i].parentNode.removeChild(inputs[i]);
		}
	}
	
	// call calculateValues from select

	var dropdown = document.getElementById(calculatorFormId);
	dropdown.getElementsByTagName("option"); // we won't bother with a loop since there's only one
		for (i=0; i<dropdown.length; i++) {
			dropdown[i].onchange = function() {calculateValues(calculatorFormId);}
		}
	
	// get the cost wrapper id
	costElement = paymentId;
	return costElement;
}


// filteres non-numeric characters from input fields
function filterNumeric(usrtxt) {
	var filter = usrtxt.value.replace(/[^\d\.]/,'');
	usrtxt.value = filter;
}
	

// gets values from fields and calculates payment
function calculateValues(calculatorFormId,costId) {

	var inputs = document.getElementById(calculatorFormId);
	inputs.getElementsByTagName("input");

	for (i=0; i<inputs.length; i++) { // assign inputs to proper variable
		if (inputs[i].name == "price") {
			var price = inputs[i].value;
		} else if (inputs[i].name == "cash_trade") {
			var tradein = inputs[i].value;
		} else if (inputs[i].name == "rate") {
			var rate = inputs[i].value;
		} 
	}
	
	// get term dropdown values
	var selectterm = document.getElementById(calculatorFormId);
	selectterm.getElementsByTagName("option");
	for (j=0; j<selectterm.length; j++) {
		var optionterm = selectterm[j].getElementsByTagName("option");
		
		for (k=0; k<optionterm.length; k++) { // get the select dropdown value
			if (optionterm[k].selected == "1") {
				var term = optionterm[k].value;
			}
		}
	}
	
	// run values through the equation
	price = price - tradein;
	
	var interest = (rate / 100) /12;
	
	var pow = 1;  // for pow see: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Math:pow
	for (var k=0; k<term; k++) {
		pow = pow * (1 + interest);
	}
	
	if ( rate != 0 ) { // Total when there IS interest	
		var total = (price * pow * interest) / (pow -1);
	} else { // Total when there IS NOT interest
		var total = price / term;
	}	
	
	total = Math.round(total);
	showPayment(total);
}
	

// write payment value to the DOM
function showPayment(payment) {
	var cost = document.getElementById(costElement);
	
	while (cost.firstChild) { // remove previous payment text
  		cost.removeChild(cost.firstChild);
	}

	if (!(/\d/.test(payment)) || (payment < 0)) {payment = 0;} // just in case, if payment is NOT a number, or less than 0, payment = 0	
	var text = document.createTextNode("$"+payment);
	
	cost.appendChild(text);
}
