3 class block_loancalc
extends block_base
{
6 $this->title
= get_string('loancalc','block_loancalc');
7 $this->content_type
= BLOCK_TYPE_TEXT
;
8 $this->version
= 2005022100;
11 function get_content() {
14 $calc = $CFG->pixpath
.'/i/calc.gif';
16 $this->content
->text
= '
17 <script type="text/javascript">
22 document.getElementById("vbankform").submit();
27 document.getElementById("vbankform").submit();
30 function num_format(x) { // format numbers with two digits
33 x = Math.floor((x * 100) + .5);
36 while(((i--) > 0) || (x > 0)) {
38 x = Math.floor(x / 10);
48 function comp(v) { // general entry point for all cases
50 // convert all entry fields into variables
51 x = document.getElementById("vbankform");
52 pv = parseFloat(x.LOANAMOUNT.value);
53 lpp = parseFloat(x.LOANPAYPERIOD[x.LOANPAYPERIOD.selectedIndex].value);
54 if (isNaN(pv) && (v != "pv"))
56 x.LOANAMOUNT.select();
58 alert("Numbers only to be entered");
62 yr = parseFloat(x.LOANTERM.value);
63 if (isNaN(yr) && (v != "np"))
67 alert("Numbers only to be entered");
71 pmt = -parseFloat(x.LOANREPAYMENT.value);
72 if (isNaN(pmt) && (v != "pmt"))
74 x.LOANREPAYMENT.select();
75 x.LOANREPAYMENT.focus();
76 alert("Numbers only to be entered");
79 if(x.LOANINTRATE.value == "") {
80 alert("You must enter an interest rate (ir).");
83 ir = parseFloat(x.LOANINTRATE.value);
86 x.LOANINTRATE.select();
87 x.LOANINTRATE.focus();
88 alert("Numbers only to be entered");
91 ir = ((ir / lpp) / 100);
93 // test and compute all cases
97 pv = -(fv + (pmt * np));
100 q1 = Math.pow(1 + ir,-np);
101 q2 = Math.pow(1 + ir,np);
102 pv = -(q1 * (fv * ir - pmt + q2 * pmt))/ir;
104 x.LOANAMOUNT.value = num_format(pv);
110 np = - (fv + pv)/pmt;
113 alert("Divide by zero error.");
117 np = Math.log((-fv * ir + pmt)/(pmt + ir * pv))/ Math.log(1 + ir);
120 alert("Can\'t compute Number of Periods for the present values.");
125 alert("The repayment amount is less than the interest. You must increase your repayments to pay off this loan!");
127 x.LOANTERM.value = num_format(np);
138 alert("Divide by zero error.");
142 q = Math.pow(1 + ir,np);
143 pmt = ((ir * (fv + q * pv))/(-1 + q));
145 x.LOANREPAYMENT.value = num_format(pmt);
155 <form method="post" id="vbankform" action="">
158 <td colspan="2">'.get_string('amountofloan','block_loancalc').'</td>
161 <td><input name="LOANAMOUNT" id="LOANAMOUNT" size="17" /></td>
162 <td><a href="JavaScript:comp(\'pv\');"><img src="'.$calc.'" alt="calculate" /></a></td>
165 <td colspan="2">'.get_string('repaymentamount','block_loancalc').'</td>
168 <td><input name="LOANREPAYMENT" id="LOANREPAYMENT" size="17" /></td>
169 <td><a href="JavaScript:comp(\'pmt\');"><img src="'.$calc.'" alt="calculate" /></a></td>
172 <td colspan="2">'.get_string('loanterm','block_loancalc').'</td>
175 <td><input name="LOANTERM" id="LOANTERM" size="17" /></td>
176 <td><a href="JavaScript:comp(\'np\');"><img src="'.$calc.'" alt="calculate" /></a></td>
179 <td colspan="2">'.get_string('interestrate','block_loancalc').'</td>
182 <td><input name="LOANINTRATE" id="LOANINTRATE" size="17" /></td>
186 <td colspan="2">'.get_string('repaymentfreq','block_loancalc').'</td>
190 $options[52] = get_string('weekly','block_loancalc');
191 $options[26] = get_string('fortnightly','block_loancalc');
192 $options[12] = get_string('monthly','block_loancalc');
193 $this->content
->text
.= choose_from_menu($options,'LOANPAYPERIOD','12',NULL,NULL,NULL,true);
194 $this->content
->text
.= '</td>
199 $this->content
->footer
= '';
201 return $this->content
;