Debt Calculator
by ataylor
HTML
<form>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<th scope="row">
<label for="balance">Total Debt:</label>
</th>
<td style="width:150px">
<input name="balance" id="balance" size="12" type="text" class="text" />
</td>
</tr>
<tr '.$bg.'>
<th scope="row">
<label for="debt_interest">Interest Rate (Annual Percentage):</label>
</th>
<td style="width:150px">
<input name="debt_interest" id="debt_interest" size="12" type="text" class="text" />
</td>
</tr>
<tr>
<th scope="row">
<label for="mnth_pay">Current Monthly Payment:</label>
</th>
<td style="width:150px">
<input name="mnth_pay" id="mnth_pay" size="12" type="text" class="text" />
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">
<input name="calculateDebt" id="calculateDebt" type="button" value="Calculate" />
</td>
</tr>
</tfoot>
</table>
<table>
<tbody>
<tr>
<th scope="row">Months It Will Take To Be Debt Free:</th>
<td>
<input name="num_months" size="12" type="text" class="text disabled">
</td>
</tr>
<tr>
<th scope="row">Years It Will Take To Be Debt Free:</th>
<td>
<input name="num_years" size="12" type="text" class="text disabled">
</td>
</tr>
<tr '.$bg.'>
<th scope="row">Total Amount Payed To Lender:</th>
<td>
<input name="total_pay" size="12"...
JavaScript
jQuery(document).ready(function ($) {
$("#calculateDebt").bind('click submit', function () {
calculate(this.form);
return false;
});
/*
This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Chris Crenshaw | http://www.creditcarddebtnegotiation.org/
Updated by: Katz Web Services, Inc. | http://www.katzwebservices.com
*/
// validation function
function isValid(entry, a, b) {
if (isNaN(entry.value) || (entry.value == null) || (entry.value == "")) {
//alert("Invalid entry. Your min payment should be between " + a + " and " + b + ".")
entry.focus()
entry.select()
return false
}
return true
}
// clear results fields when input values changed
function clearCalcs(form) {
form.num_months.value = ""
form.total_pay.value = ""
form.total_int.value = ""
}
function calculate(form) {
// send entries to validation function
// exit if not valid
if (!isValid(form.balance, 0, 100000)) {
return false
} else if (!isValid(form.debt_interest, 0, 30)) {
return false
} else {
var init_bal = eval(form.balance.value);
}
if (!isValid(form.mnth_pay, init_bal * .02, init_bal)) {
return false
} else {
// variables used in calculation
var cur_bal = init_bal; // used in loop
var interest = eval(form.debt_interest.value / 100);
var mnth_pay = eval(form.mnth_pay.value);
var fin_chg = 0; // finance charge
var num_mnths = 0;
var tot_int = 0;
}
while (cur_bal > 0) {
fin_chg = cur_bal * interest / 12;
cur_bal = cur_bal - mnth_pay + fin_chg;
num_mnths++;
if (num_mnths > 1200) {
$('.debtCalculator...