/* No, you are not allowed to copy this. I wish you could but my employer would not really like that. */
function amortize (principal, interest, duration) {
    displayArea=document.getElementById("amortization_table");
    var output="<table class=\"datatable\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <thead>  <th>Month</th>  <th>Payment</th>  <th>Interest Paid</th>  <th>Principal Paid</th>  <th>Remaining Principal</th> </thead> <tbody>";
    principal = parseFloat(principal);
    rate = parseFloat(interest);
    duration = parseInt(duration);
    monthlyRate = parseFloat( rate/(12 * 100) );
    durationMonths = parseInt( duration * 12 );
    payment = ( principal * ( monthlyRate / (1 - Math.pow((1 + monthlyRate), -durationMonths)) )  );
    month = 1;
    while ( (month <= durationMonths) ) {
	output += "<tr class=\"rowtwo\">";				// Row start
	output += "<td class=\"textright\">"+ month +"</td>";
	output += "<td class=\"textright\">"+ currency(payment) +"</td>";
	currentMonthlyInt = (principal * monthlyRate);
	output += "<td class=\"textright\">"+ currency(currentMonthlyInt) +"</td>";
	principalMonth = (payment - currentMonthlyInt);
	output += "<td class=\"textright\">"+ currency(principalMonth) +"</td>";
	principalBalance = (principal - principalMonth);
	output += "<td class=\"textright\">"+ currency(principalBalance) +"</td>";
	principal = principalBalance;
	output += "</tr>";			// Row end
	month++;
    }
    output += '</tbody></table>';
    displayArea.innerHTML = output;
}
function currency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
	cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function calcLoan(){
    this.autoloan = document.getElementById('autoloan');

    var amount = parseFloat(autoloan.amount.value.replace(',',''));
    var term = parseFloat(autoloan.term.value)*12;
    var int = parseFloat(autoloan.rate.value) /1200;

    var _payment = (int/(1-(1/(Math.pow(1+int, term)))))*amount;
    _payment = Math.round(_payment*100)/100;
    autoloan.payment.value = _payment;

    var total = eval(term * autoloan.payment.value);
    // autoloan.total.value = Math.round(total*100)/100;
}

function calculateLoan()
{
    var prince=document.frmCalc.purchasePrice.value;
    var payments=document.frmCalc.numPayments.value;
    var rate=document.frmCalc.cuRate.value;
    var rate1=0;
    var badChars=new Array('$','%',' ',',');
    var $loop;

    for ($loop=0; $loop<=badChars.length; $loop++) {
	prince=prince.replace(badChars[$loop], '')
    }

    for ($loop=0; $loop<=badChars.length; $loop++) {
	payments=payments.replace(badChars[$loop], '')
    }

    for ($loop=0; $loop<=badChars.length; $loop++) {
	rate=rate.replace(badChars[$loop], '')
    }

    var monthlyPay=0;
    var totalPay=0;

    if (isNaN(prince) == 1 || isNaN(payments) == 1 || isNaN(rate) == 1) {
	alert('Error: One or more of your fields have bad data.\r\nPlease only enter numbers (no dollar signs or percent signs).');
	document.frmCalc.monthlyPayment.value='';
	document.frmCalc.totalPaid.value='';
	return false;
    }

    if (rate >= 1) { rate=rate / 100;}//Turns percentages to decimals.
    rate = rate / 12;//Interest rate needs to be per month.

    rate1=rate+1;
    monthlyPay=(prince*Math.pow(rate1, payments)) / (((Math.pow(rate1, payments)-1))/rate)
	totalPay=monthlyPay*payments;

    monthlyPay=Math.round(monthlyPay*100)/100
	totalPay=Math.round(totalPay*100)/100;

    document.frmCalc.monthlyPayment.value=(isNaN(monthlyPay) ==1) ? 'Not a number.' : '$'+monthlyPay;
    document.frmCalc.totalPaid.value=(isNaN(totalPay) ==1) ? 'Not a number.' : '$'+totalPay;
    return true;
}


var yieldCalc = document.getElementById('yieldCalc');
var yield;

function interest(p, ar, ct, t) {
    return p * Math.pow(1 + ar / ct, t);
}



function doCalc() {

    var r, t, p, c;
    r = parseFloat(yieldCalc.apr.value);
    t = parseInt(yieldCalc.term.value);
    p = parseFloat(yieldCalc.principal.value);
    r = parseFloat(r) > 1 ? parseFloat(r) * 0.01 : parseFloat(r);
    c = parseInt(yieldCalc.compounding.value);
    if (isNaN(p)) {
	alert("Pricipal amount must be numeric.");
	yieldCalc.principal.focus();
	return;
    }
    if (isNaN(r)) {
	alert("APR must be numeric.");
	yieldCalc.apr.focus();
	return;
    }
    if (isNaN(t)) {
	alert("Term must be numeric.");
	yieldCalc.term.focus();
	return;
    }
    var termError = 0;
    var apyTerm = 0;
    switch (yieldCalc.compounding.selectedIndex) {
	case 1:
	    termError = t < 1 ? 1 : 0;
	    apyTerm = 12;
	    break;
	case 2:
	    termError = t < 3 ? 1 : 0;
	    t = t / 3;
	    apyTerm = 4;
	    break;
	case 3:
	    termError = t < 6 ? 1 : 0;
	    t = t / 6;
	    apyTerm = 2;
	    break;
	case 4:
	    termError = t < 12 ? 1 : 0;
	    t = t / 12;
	    apyTerm = 1;
	    break;
	default:
	    t = t * 30.416666666666668;
	    termError = t < 1 ? 1 : 0;
	    apyTerm = 365;
	    break;
    }
    var apy = (interest(1000, r, c, apyTerm) - 1000) / 1000;
    apy = Math.round(apy * 100000);
    var sapy = new String(apy);
    var lenapy = sapy.length;
    sapy = sapy.substr(0, lenapy - 3) + "." + sapy.substr(lenapy - 3, 3) + "%";
    if (termError) {
	alert("The term must be greater than or equal to the compounding period.");
	yieldCalc.term.focus();
    }
    yieldCalc.yield.value = (interest(p, r, c, t)).toFixed(2);
    yieldCalc.apy.value = sapy;
}


// To overide the form action
var init = function() {}; 
// If we have JavaScript we want to overide the form action
if (document.getElementById("yieldCalc")) { 
    yieldCalc.action = 'javascript:doCalc()';
}

