function findObj(n, d) {
        var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
                d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
        if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
        for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
        if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function power(no) {
        return Math.round(no*Math.pow(10,2))/Math.pow(10,2);
}
function calc() {
	    var p = findObj('p').value;
    	var i = findObj('i').value;
        var y = findObj('y').value;
        var iM = i/1200;
        var b = 1;
        var bM =  1+iM;
        for (i=0; i<y*12; i++) {
                b = b * bM;
        }
        mP = p*iM/(1-(1/b));
        findObj('result').innerHTML = "Your Monthly Payment : $" + power(mP) + "<br><a href='javascript:;' onclick='genHTML(mP)'>Click for Amortization Table</a>";
}
function genHTML() {
        var p = findObj('p').value;
        var i = findObj('i').value;
        var y = findObj('y').value;
        var tHTML = '<table width="100%"><tr><th>Month</th><th>Interest Paid</th><th>Principal Paid</th><th>Balance</th></tr>';
        for (j=0; j<y*12; j++) {
                iP = p*i/1200;
                pP = mP-iP;
                bal= p-pP;
                tHTML += '<tr class="'+((j%2!=0) ? 'style1' : 'style2')+'"><td>'+eval(j+1)+'</td><td>'+power(iP)+'</td><td>'+power(pP)+'</td><td>'+power(bal)+'</td></tr>'
                p=bal;
        }
        tHTML += '<table>'
        findObj('amort').innerHTML = tHTML;
}

window.onload = function () { findObj('mess').style.display="none";findObj('p').focus(); findObj('p').select(); }