function toggleIntro() {
	document.getElementById("introterm").disabled=false;
	document.getElementById("introterm").focus();
	document.getElementById("fixed").checked=false;
	document.getElementById("introductory").checked=true;
}

function toggleFixed() {
	document.getElementById("introterm").disabled=true;
	document.getElementById("introterm").value="";
	document.getElementById("introductory").checked=false;
	document.getElementById("fixed").checked=true;
}

function checkPercent(obj) {
	if (obj.value == "") {
		return;
	}
	if (isNumber(obj.value.replace(" ","").replace("%",""))) {
		obj.value = obj.value.replace(" ","").replace("%","") + "%";
	}
	else {
		alert(obj.value + " is not a valid value.  Please enter a valid percentage value.");
		obj.value = "";
		obj.focus();
	}
}

function checkCurrency(obj) {
	if (obj.value == "") {
		return;
	}
	if (obj.id == "maxfee") {
		if (obj.value.replace(" ","").replace("$","") == 0) {
			obj.value = "";
			return;
		}
	}
	if (isNumber(obj.value.replace(" ","").replace("$",""))) {
		obj.value = "$" + obj.value.replace(" ","").replace("$","");
	}
	else {
		alert(obj.value + " is not a valid value.  Please enter a valid number.");
		obj.value = "";
		obj.focus();
	}
}

function checkNumber(obj) {
	if (obj.value == "") {
		return;
	}
	if (isNumber(obj.value.replace(" ","").replace("$",""))) {
		obj.value = obj.value.replace(" ","").replace("$","").replace("%","");
	}
	else {
		alert(obj.value + " is not a valid value.  Please enter a valid number.");
		obj.value = "";
		obj.focus();
	}
}

function isNumber(str) {
	if (str == parseFloat(str)) {
		return true;
	}
	else {
		return false;
	}
}

function calcSavings() {
	balance = parseFloat(document.getElementById("balance").value.replace("$", ""));
	minfee = parseFloat(document.getElementById("minfee").value.replace("$", ""));
	maxfee = document.getElementById("maxfee").value.replace("$", "");
	if (maxfee == "") {
		maxfee = 0;
	}
	else {
		maxfee = parseFloat(maxfee);
	}
	feepercent = parseFloat(document.getElementById("feepercent").value.replace("%", ""));
	if (feepercent * balance / 100 > maxfee && maxfee != 0) {
		fee = maxfee;
	}
	else if (feepercent * balance / 100 < minfee) {
		fee = minfee;
	}
	else {
		fee = balance * feepercent / 100;
	}

	currentrate = parseFloat(document.getElementById("currentrate").value.replace("%", ""));
	refirate = parseFloat(document.getElementById("refirate").value.replace("%", ""));
	if (refirate > currentrate) {
		alert(refirate + "," + currentrate);
		document.getElementById("results").innerHTML = "Savings not possible.  Refinance rate must be less than the current rate.";
		_IG_AdjustIFrameHeight();
		return;
	}

	introterm = document.getElementById("introterm").value;
	if (introterm == "") {
		introterm = 0;
	}
	else {
		introterm = parseFloat(introterm);
	}

	currentprincipal = balance;
	currentpayment = parseFloat(document.getElementById("currentpayment").value.replace("$", ""));
	currentcost = 0;
	currentterm = 0;
	refiprincipal = balance + fee;
	refipayment = parseFloat(document.getElementById("refipayment").value.replace("$", ""));
	reficost = 0;
	refiterm = 0;
	if (refiprincipal * refirate / 100 / 12 >= refipayment) {
		document.getElementById("results").innerHTML = "The refinance minimum payment is lower than the montly interest payment.  The loan will never be paid off.  Please change the refinance payment to be at least $" + (refiprincipal * refirate / 100 / 12 + 0.01).toFixed(2) + ".";
		_IG_AdjustIFrameHeight();
		return;
	}
	payback = 0;

	for (month=1; true; month++) {
		// Keep reducing current principal as long as the payment is lower than the balance
		if (currentpayment < currentprincipal) {
			currentcost += currentprincipal * currentrate / 100 / 12;
			currentprincipal = currentprincipal - currentpayment + currentprincipal * currentrate / 100 / 12;
		} else if (currentterm == 0) {
			// If the current payment is less than the principal then this is the last payment
			currentterm = month;
			currentprincipal = 0;
		}

		// Keep reducing refinanced principal as long as the payment is lower than the balance
		if (refipayment < refiprincipal) {
			reficost += refiprincipal * refirate / 100 / 12;
			refiprincipal = refiprincipal - refipayment + refiprincipal * refirate / 100 / 12;
		} else if (refiterm == 0) {
			// If the refi payment is less than the principal then this is the last payment
			refiterm = month;
			refiprincipal = 0;
			break;
		}

		// Check to see if we"ve recovered our financing fee
		if (currentcost - reficost > fee && payback == 0) {
			payback = month;
		}

		// Check to see if we"ve exceeded the introductory period
		if (month >= introterm && introterm != 0) {
			break;
		}
	}

	if (payback == 0) {
		if (currentcost > reficost) {
			document.getElementById("results").innerHTML = "The introductory period is not long enough to recover the financing fees.  During the introductory period, the refinance will only result in $" + (currentcost - reficost).toFixed(2) + " in total interest saved, which is not enough to recover the transfer fee of $" + fee.toFixed(2) + ".";
			_IG_AdjustIFrameHeight();
			return;
		} else {
			document.getElementById("results").innerHTML = "The current loan will be paid off too quickly to justify refinancing it under the current parameters.";
			_IG_AdjustIFrameHeight();
			return;
		}
	}

	output = "It will take " + payback + " months to recover the transfer fee of $" + fee.toFixed(2) + ".<br>"
	output += "Loan comparison after " + month + " months:<p>";
	output += "<table>";
	output += "	<tr>";
	output += "		<th></th>";
	output += "		<th>Balance</th>";
	output += "		<th>Total Interest Paid</th>";
	output += "		<th># of Months to Pay Off</th>";
	output += "	</tr>";
	output += "	<tr>";
	output += "		<td>Current Loan</td>";
	output += "		<td>$" + currentprincipal.toFixed(2) + "</td>";
	output += "		<td>$" + currentcost.toFixed(2) + "</td>";
	if (currentterm == 0) {
		output += "		<td>&gt; " + month + " months</td>";
	} else {
		output += "		<td>" + currentterm + " months</td>";
	}
	output += "	</tr>";
	output += "	<tr>";
	output += "		<td>Refinanced Loan</td>";
	output += "		<td>$" + refiprincipal.toFixed(2) + "</td>";
	output += "		<td>$" + reficost.toFixed(2) + "</td>";
	if (refiterm == 0) {
		output += "		<td>&gt; " + month + " months</td>";
	} else {
		output += "		<td>" + refiterm + " months</td>";
	}
	output += "	</tr>";
	output += "</table><p>";

	// Check to see if the loan was paid off
	if (refiterm == 0) {
		output += "<font color=\"red\">Warning:</font> If the loan is refinanced at the introductory rate, the introductory period will expire without the loan being paid off.  The loan should be refinanced once again prior to the expiration of the introductory period.<p>";
	}

	if (currentterm == 0) {
		if (refiterm == 0) {
			output += "If the current loan is not refinanced, it will not be paid off within the introductory period.<br>";
		} else {
			output += "If refinanced, the current loan could be paid off within " + refiterm + " months.<br>";
		}
		output += "The refinance will save $" + (currentcost - reficost).toFixed(2) + " in total interest paid.  ";
	} else {
		output += "The current loan will be paid off quicker if it is not refinanced.  ";
		if (currentcost > reficost) {
			output += "However, the refinance will <b>still</b> result in $" + (currentcost - reficost).toFixed(2) + " less interest paid and provide better cash flow.  ";
		} else{
			output += "If the loan is not refinanced, because of the accelerated payments it will be paid off quicker and result in $" + (reficost - currentcost).toFixed(2) + " less interest paid.  ";
			output += "Increasing the payments on the refinanced loan will pay it off quicker, and could result in less total interest paid than the current loan.  ";
		}
	}
	document.getElementById("results").innerHTML = output;
	_IG_AdjustIFrameHeight();
}
