Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / blocks / loancalc / block_loancalc.php
blob1c26ab9a0081fadbb7cbbaf282d39308caa1dd33
1 <?php // $Id$
3 class block_loancalc extends block_base {
5 function init() {
6 $this->title = get_string('loancalc','block_loancalc');
7 $this->content_type = BLOCK_TYPE_TEXT;
8 $this->version = 2005022100;
11 function get_content() {
12 global $CFG;
14 $calc = $CFG->pixpath.'/i/calc.gif';
16 $this->content->text = '
17 <script type="text/javascript">
18 <![CDATA[
19 function Next()
21 submitScreen("Next");
22 document.getElementById("vbankform").submit();
24 function Back()
26 submitScreen("Back");
27 document.getElementById("vbankform").submit();
30 function num_format(x) { // format numbers with two digits
31 sgn = (x < 0);
32 x = Math.abs(x);
33 x = Math.floor((x * 100) + .5);
34 i = 3;
35 y = "";
36 while(((i--) > 0) || (x > 0)) {
37 y = (x % 10) + y;
38 x = Math.floor(x / 10);
39 if(i == 1) {
40 y = "." + y;
43 if(sgn) {
44 y = "-" + y;
46 return(y);
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();
57 x.LOANAMOUNT.focus();
58 alert("Numbers only to be entered");
59 return;
61 fv = parseFloat("0");
62 yr = parseFloat(x.LOANTERM.value);
63 if (isNaN(yr) && (v != "np"))
65 x.LOANTERM.select();
66 x.LOANTERM.focus();
67 alert("Numbers only to be entered");
68 return;
70 np = lpp * yr;
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");
77 return;
79 if(x.LOANINTRATE.value == "") {
80 alert("You must enter an interest rate (ir).");
82 else {
83 ir = parseFloat(x.LOANINTRATE.value);
84 if (isNaN(ir))
86 x.LOANINTRATE.select();
87 x.LOANINTRATE.focus();
88 alert("Numbers only to be entered");
89 return;
91 ir = ((ir / lpp) / 100);
93 // test and compute all cases
95 if (v == "pv") {
96 if(ir == 0) {
97 pv = -(fv + (pmt * np));
99 else {
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);
107 if (v == "np") {
108 if(ir == 0) {
109 if(pmt != 0) {
110 np = - (fv + pv)/pmt;
112 else {
113 alert("Divide by zero error.");
116 else {
117 np = Math.log((-fv * ir + pmt)/(pmt + ir * pv))/ Math.log(1 + ir);
119 if(np == 0) {
120 alert("Can\'t compute Number of Periods for the present values.");
122 else {
123 np = (np / lpp)
124 if (isNaN(np)) {
125 alert("The repayment amount is less than the interest. You must increase your repayments to pay off this loan!");
126 } else {
127 x.LOANTERM.value = num_format(np);
132 if (v == "pmt") {
133 if(ir == 0.0) {
134 if(np != 0) {
135 pmt = (fv + pv)/np;
137 else {
138 alert("Divide by zero error.");
141 else {
142 q = Math.pow(1 + ir,np);
143 pmt = ((ir * (fv + q * pv))/(-1 + q));
145 x.LOANREPAYMENT.value = num_format(pmt);
150 } // function comp
152 </script>
154 <h1></h1>
155 <form method="post" id="vbankform" action="">
156 <table>
157 <tr>
158 <td colspan="2">'.get_string('amountofloan','block_loancalc').'</td>
159 </tr>
160 <tr>
161 <td><input name="LOANAMOUNT" id="LOANAMOUNT" size="17" /></td>
162 <td><a href="JavaScript:comp(\'pv\');"><img src="'.$calc.'" alt="calculate" /></a></td>
163 </tr>
164 <tr>
165 <td colspan="2">'.get_string('repaymentamount','block_loancalc').'</td>
166 </tr>
167 <tr>
168 <td><input name="LOANREPAYMENT" id="LOANREPAYMENT" size="17" /></td>
169 <td><a href="JavaScript:comp(\'pmt\');"><img src="'.$calc.'" alt="calculate" /></a></td>
170 </tr>
171 <tr>
172 <td colspan="2">'.get_string('loanterm','block_loancalc').'</td>
173 </tr>
174 <tr>
175 <td><input name="LOANTERM" id="LOANTERM" size="17" /></td>
176 <td><a href="JavaScript:comp(\'np\');"><img src="'.$calc.'" alt="calculate" /></a></td>
177 </tr>
178 <tr>
179 <td colspan="2">'.get_string('interestrate','block_loancalc').'</td>
180 </tr>
181 <tr>
182 <td><input name="LOANINTRATE" id="LOANINTRATE" size="17" /></td>
183 <td></td>
184 </tr>
185 <tr>
186 <td colspan="2">'.get_string('repaymentfreq','block_loancalc').'</td>
187 </tr>
188 <tr>
189 <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>
195 <td></td>
196 </tr>
197 </table>
198 </form>';
199 $this->content->footer = '';
201 return $this->content;