Display bank balance in payment forms.
[fa-stable.git] / includes / ui / ui_input.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //
13 // Sets local POST value and adds Value to ajax posting if needed
14 //
15 /*function set_post($name, $value, $ajax_trigger=true) {
16     global $Ajax;
17
18     $_POST[$name] = $value;
19     if ($ajax_trigger) $Ajax->activate($name);
20 }
21 */
22 //------------------------------------------------------------------------------
23 //    Seek for _POST variable with $prefix.
24 //    If var is found returns variable name with prefix stripped,
25 //    and null or -1 otherwise.
26 //
27 function find_submit($prefix, $numeric=true)
28 {
29
30     foreach($_POST as $postkey=>$postval )
31     {
32                 if (strpos($postkey, $prefix) === 0)
33                 {
34                         $id = substr($postkey, strlen($prefix));
35                         return $numeric ? (int)$id : $id;
36                 }
37     }
38     return $numeric ? -1 : null;
39 }
40 //------------------------------------------------------------------------------
41 //
42 // Helper function for simple db table editor pages
43 //
44 function simple_page_mode($numeric_id = true)
45 {
46         global $Ajax, $Mode, $selected_id;
47
48         $default = $numeric_id ? -1 : '';
49         $selected_id = get_post('selected_id', $default);
50         foreach (array('ADD_ITEM', 'UPDATE_ITEM', 'RESET', 'CLONE') as $m) {
51                 if (isset($_POST[$m])) {
52                         $Ajax->activate('_page_body');
53                         if ($m == 'RESET'  || $m == 'CLONE') 
54                                 $selected_id = $default;
55                         unset($_POST['_focus']);
56                         $Mode = $m; return;
57                 }
58         }
59         foreach (array('Edit', 'Delete') as $m) {
60                 foreach ($_POST as $p => $pvar) {
61                         if (strpos($p, $m) === 0) {
62 //                              $selected_id = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
63                                 unset($_POST['_focus']); // focus on first form entry
64                                 $selected_id = quoted_printable_decode(substr($p, strlen($m)));
65                                 $Ajax->activate('_page_body');
66                                 $Mode = $m;
67                                 return;
68                         }
69                 }
70         }
71         $Mode = '';
72 }
73
74 //------------------------------------------------------------------------------
75 //
76 //      Read numeric value from user formatted input
77 //
78 function input_num($postname=null, $dflt=0)
79 {
80         if (!isset($_POST[$postname]) || $_POST[$postname] == "")
81                 return $dflt;
82
83     return user_numeric($_POST[$postname]);
84 }
85
86 //---------------------------------------------------------------------------------
87
88 function hidden($name, $value=null, $echo=true)
89 {
90         global $Ajax;
91         
92         if ($value === null) 
93                 $value = get_post($name);
94         
95         $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
96         $Ajax->addUpdate($name, $name, $value);
97         if ($echo)
98                 echo $ret."\n";
99         else
100                 return $ret;
101 }
102 /*
103         Universal submit form button.
104         $atype - type of submit:
105          Normal submit:
106                 false - normal button; optional icon
107                 null  - button visible only in fallback mode; optional icon
108          Ajax submit:
109                 true      - standard button; optional icon
110
111                 'default' - default form submit on Ctrl-Enter press; dflt ICON_OK icon
112                 'selector' - ditto with closing current popup editor window
113                 'cancel'  - cancel form entry on Escape press; dflt ICON_CANCEL
114                 'process' - displays progress bar during call; optional icon
115
116         $atype can contain also multiply type selectors separated by space, 
117         however make sense only combination of 'process' and one of defualt/selector/cancel
118 */
119 function submit($name, $value, $echo=true, $title=false, $atype=false, $icon=false)
120 {
121         global $path_to_root;
122
123         $aspect='';
124         if ($atype === null) {
125                 $aspect = fallback_mode() ? " aspect='fallback'" : " style='display:none;'";
126
127         } elseif (!is_bool($atype)) { // necessary: switch uses '=='
128
129                 $aspect = "aspect='$atype' ";
130                 $types = explode(' ', $atype);
131
132                 foreach ($types as $type) {
133                         switch($type) {
134                                 case 'selector':
135                                         $aspect = " aspect='selector' rel = '$value'"; 
136                                         $value = _("Select");
137                                         if ($icon===false) $icon=ICON_SUBMIT; break;
138
139                                 case 'default':
140                                         if ($icon===false) $icon=ICON_SUBMIT; break;
141
142                                 case 'cancel':
143                                         if ($icon===false) $icon=ICON_ESCAPE; break;
144                         }
145                 }
146         }
147         $submit_str = "<button class=\""
148             .($atype ? 'ajaxsubmit' : 'inputsubmit')
149                 ."\" type=\"submit\""
150                 .$aspect
151             ." name=\"$name\"  id=\"$name\" value=\"$value\""
152             .($title ? " title='$title'" : '')
153             .">"
154                 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon' height='12'>" : '')
155                 ."<span>$value</span>"
156                 ."</button>\n";
157         if ($echo)
158                 echo $submit_str;
159         else
160                 return $submit_str;
161 }
162
163 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
164 {
165         if ($echo) echo "<center>";
166         submit($name, $value, $echo, $title, $async, $icon);
167         if ($echo) echo "</center>";
168 }
169
170 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
171 {
172         echo "<center>";
173         submit($name, $value, true, $title, $async, $icon);
174         echo "&nbsp;";
175 }
176
177 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
178 {
179         echo "&nbsp;";
180         submit($name, $value, true, $title, $async, $icon);
181         echo "</center>";
182 }
183 /*
184         For following controls:
185         'both' - use both Ctrl-Enter and Escape hotkeys 
186         'cancel' - apply to 'RESET' button
187 */
188 function submit_add_or_update($add=true, $title=false, $async=false, $clone=false)
189 {
190         $cancel = $async;
191
192         if ($async === 'both') {
193                 $async = 'default'; $cancel = 'cancel';
194         } 
195         else if ($async === 'default')
196                 $cancel = true;
197         else if ($async === 'cancel')
198                 $async = true;
199         
200         if ($add)
201                 submit('ADD_ITEM', _("Add new"), true, $title, $async);
202         else {
203                 submit('UPDATE_ITEM', _("Update"), true, _('Submit changes'), $async);
204                 if ($clone) submit('CLONE', _("Clone"), true, 
205                         _('Edit new record with current data'), $async);
206                 submit('RESET', _("Cancel"), true, _('Cancel edition'), $cancel);
207         }
208 }
209
210 function submit_add_or_update_center($add=true, $title=false, $async=false, $clone=false)
211 {
212         echo "<center>";
213         submit_add_or_update($add, $title, $async, $clone);
214         echo "</center>";
215 }
216
217 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false, $clone = false)
218 {
219         echo "<tr>";
220         if ($right)
221                 echo "<td>&nbsp;</td>\n";
222         echo "<td $extra>";
223         submit_add_or_update($add, $title, $async, $clone);
224         echo "</td></tr>\n";
225 }
226
227 function submit_cells($name, $value, $extra="", $title=false, $async=false)
228 {
229         echo "<td $extra>";
230         submit($name, $value, true, $title, $async);
231         echo "</td>\n";
232 }
233
234 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
235 {
236         echo "<tr>";
237         if ($right)
238                 echo "<td>&nbsp;</td>\n";
239         submit_cells($name, $value, $extra, $title, $async);
240         echo "</tr>\n";
241 }
242
243 function submit_return($name, $value, $title=false)
244 {
245         if (@$_REQUEST['popup']) {
246                 submit($name, $value, true, $title, 'selector');
247         }
248 }
249
250 function submit_js_confirm($name, $msg, $set = true) {
251         global $Ajax;
252         $js = "_validate.$name=".($set ? "function(){ return confirm('"
253                                 . strtr($msg, array("\n"=>'\\n')) . "');};"
254                                 : 'null;');
255         if (in_ajax()) {
256                 $Ajax->addScript(true, $js);
257         } else
258                 add_js_source($js);
259 }
260 //-----------------------------------------------------------------------------------
261
262 function set_icon($icon, $title=false)
263 {
264         global $path_to_root;
265         if (basename($icon) === $icon) // standard icons does not contain path separator
266                 $icon = "$path_to_root/themes/".user_theme()."/images/$icon";
267         return "<img src='$icon' width='12' height='12' border='0'".($title ? " title='$title'" : "")." />\n";  
268 }
269
270 function button($name, $value, $title=false, $icon=false,  $aspect='')
271 {
272         // php silently changes dots,spaces,'[' and characters 128-159
273         // to underscore in POST names, to maintain compatibility with register_globals
274         $rel = '';
275         if ($aspect == 'selector') {
276                 $rel = " rel='$value'";
277                 $value = _("Select");
278         }
279         if (user_graphic_links() && $icon)
280         {
281                 if ($value == _("Delete")) // Helper during implementation
282                         $icon = ICON_DELETE;
283                 return "<button type='submit' class='editbutton' name='"
284                         .htmlentities(strtr($name, array('.'=>'=2E', '='=>'=3D',// ' '=>'=20','['=>'=5B'
285                         )))
286                         ."' value='1'" . ($title ? " title='$title'":" title='$value'")
287                         . ($aspect ? " aspect='$aspect'" : '')
288                         . $rel
289                         ." />".set_icon($icon)."</button>\n";
290         }
291         else
292                 return "<input type='submit' class='editbutton' name='"
293                         .htmlentities(strtr($name, array('.'=>'=2E', '='=>'=3D',// ' '=>'=20','['=>'=5B'
294                         )))
295                         ."' value='$value'"
296                         .($title ? " title='$title'":'')
297                         . ($aspect ? " aspect='$aspect'" : '')
298                         . $rel
299                         ." />\n";
300 }
301
302 function button_cell($name, $value, $title=false, $icon=false, $aspect='')
303 {
304         echo "<td align='center'>";
305         echo button($name, $value, $title, $icon, $aspect);
306         echo "</td>";
307 }
308
309 function delete_button_cell($name, $value, $title=false)
310 {
311         button_cell($name, $value, $title, ICON_DELETE);
312 }
313
314 function edit_button_cell($name, $value, $title=false)
315 {
316         button_cell($name, $value, $title, ICON_EDIT);
317 }
318
319 function select_button_cell($name, $value, $title=false)
320 {
321         button_cell($name, $value, $title, ICON_ADD, 'selector');
322 }
323 //-----------------------------------------------------------------------------------
324
325 function check_value($name)
326 {
327         if (!isset($_POST[$name]))
328                 return 0;
329         return 1;
330 }
331
332 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
333 {
334         global $Ajax;
335
336         $str = '';      
337
338         if ($label)
339                 $str .= $label . "  ";
340         if ($submit_on_change !== false) {
341                 if ($submit_on_change === true)
342                         $submit_on_change = 
343                                 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
344         }
345         if ($value === null)
346                 $value = get_post($name,0);
347
348         $str .= "<input"
349             .($value == 1 ? ' checked':'')
350             ." type='checkbox' name='$name' value='1'"
351             .($submit_on_change ? " onclick='$submit_on_change'" : '')
352             .($title ? " title='$title'" : '')
353             ." >\n";
354
355         $Ajax->addUpdate($name, $name, $value);
356         return $str;
357 }
358
359 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
360 {
361         echo checkbox($label, $name, $value, $submit_on_change, $title);
362 }
363
364 function check_cells($label, $name, $value=null, $submit_on_change=false, $title=false,
365         $params='')
366 {
367         if ($label != null)
368                 echo "<td>$label</td>\n";
369         echo "<td $params>";
370         echo check(null, $name, $value, $submit_on_change, $title);
371         echo "</td>";
372 }
373
374 function check_row($label, $name, $value=null, $submit_on_change=false, $title=false)
375 {
376         echo "<tr><td class='label'>$label</td>";
377         echo check_cells(NULL, $name, $value, $submit_on_change, $title);
378         echo "</tr>\n";
379 }
380
381 //-----------------------------------------------------------------------------------
382 function radio($label, $name, $value, $selected, $submit_on_change=false) {
383
384         if ($submit_on_change === true)
385                 $submit_on_change = 
386                         "JsHttpRequest.request(\"_{$name}_update\", this.form);";
387
388         return "<input type='radio' name=$name value='$value' ".($selected ? "checked":'')
389             .($submit_on_change ? " onclick='$submit_on_change'" : '')
390                 .">".($label ? $label : '');
391 }
392
393 //-----------------------------------------------------------------------------------
394 function labelheader_cell($label, $params="")
395 {
396         echo "<td class='tableheader' $params>$label</td>\n";
397 }
398
399 function label_cell($label, $params="", $id=null)
400 {
401     global $Ajax;
402
403         if(isset($id))
404         {
405             $params .= " id='$id'";
406             $Ajax->addUpdate($id, $id, $label);
407         }
408         echo "<td $params>$label</td>\n";
409
410         return $label;
411 }
412
413 function email_cell($label, $params="", $id=null)
414 {
415         label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
416 }
417
418 function amount_decimal_cell($label, $params="", $id=null)
419 {
420         $dec = 0;
421         label_cell(price_decimal_format($label, $dec), "nowrap align=right ".$params, $id);
422 }
423
424 function amount_cell($label, $bold=false, $params="", $id=null)
425 {
426         if ($bold)
427                 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
428         else
429                 label_cell(price_format($label), "nowrap align=right ".$params, $id);
430 }
431
432 //JAM  Allow entered unit prices to be fractional
433 function unit_amount_cell($label, $bold=false, $params="", $id=null)
434 {
435         if ($bold)
436                 label_cell("<b>".unit_price_format($label)."</b>", "nowrap align=right ".$params, $id);
437         else
438                 label_cell(unit_price_format($label), "nowrap align=right ".$params, $id);
439 }
440
441
442 function percent_cell($label, $bold=false, $id=null)
443 {
444         if ($bold)
445                 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
446         else
447                 label_cell(percent_format($label), "nowrap align=right", $id);
448 }
449 // 2008-06-15. Changed
450 function qty_cell($label, $bold=false, $dec=null, $id=null)
451 {
452         if (!isset($dec))
453                 $dec = get_qty_dec();
454         if ($bold)
455                 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
456         else
457                 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
458 }
459
460 function label_cells($label, $value, $params="", $params2="", $id='')
461 {
462         if ($label != null)
463                 echo "<td $params>$label</td>\n";
464         label_cell($value, $params2, $id);
465 }
466
467 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
468 {
469         echo "<tr>";
470         if ($params == "")
471         {
472                 echo "<td class='label'>$label</td>";
473                 $label = null;
474         }       
475         label_cells($label, $value, $params, $params2, $id);
476         if ($leftfill!=0)
477                 echo "<td colspan=$leftfill></td>";
478         echo "</tr>\n";
479 }
480
481 //-----------------------------------------------------------------------------------
482
483 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, 
484         $labparams="", $post_label="", $inparams="")
485 {
486         global $Ajax;
487
488         default_focus($name);
489         if ($label != null)
490                 label_cell($label, $labparams);
491         echo "<td>";
492
493         if ($value === null)
494                 $value = get_post($name);
495         echo "<input $inparams type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
496             .($title ? " title='$title'" : '')
497             .">";
498
499         if ($post_label != "")
500                 echo " " . $post_label;
501
502         echo "</td>\n";
503         $Ajax->addUpdate($name, $name, $value);
504 }
505
506 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null,
507         $labparams=null, $post_label=null, $submit_on_change=false)
508 {
509         global $Ajax;
510
511         default_focus($name);
512         if (!isset($_POST[$name]) || $_POST[$name] == "")
513         {
514                 if ($init)
515                         $_POST[$name] = $init;
516                 else
517                         $_POST[$name] = "";
518         }
519         if ($label != null)
520                 label_cell($label, $labparams);
521
522         if (!isset($max))
523                 $max = $size;
524
525         echo "<td>";
526         $class = $submit_on_change ? 'class="searchbox"' : '';
527         echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
528          .($title ? " title='$title'": '')." >";
529
530         if ($post_label)
531                 echo " " . $post_label;
532
533         echo "</td>\n";
534         $Ajax->addUpdate($name, $name, $_POST[$name]);
535 }
536
537 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
538 {
539         echo "<tr><td class='label'>$label</td>";
540         text_cells(null, $name, $value, $size, $max, $title, $params, $post_label);
541
542         echo "</tr>\n";
543 }
544
545 //-----------------------------------------------------------------------------------
546
547 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
548 {
549         echo "<tr><td class='label'>$label</td>";
550         text_cells_ex(null, $name, $size, $max, $value, $title, $params, $post_label);
551
552         echo "</tr>\n";
553 }
554
555 //-----------------------------------------------------------------------------------
556 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
557 {
558         if (get_post($name)) 
559                 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
560         text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
561 }
562
563 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
564 {
565         if (get_post($name)) 
566                 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
567         text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
568 }
569
570 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
571 {
572         $val = get_post($name);
573         if ($val) {
574                 if (strpos($val,'http://')===false)
575                         $val = 'http://'.$val;
576                 $label = "<a href='$val' target='_blank'>$label</a>";
577         }
578         text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
579 }
580
581 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
582 {
583         $val = get_post($name);
584         if ($val) {
585                 if (strpos($val,'http://')===false)
586                         $val = 'http://'.$val;
587                 $label = "<a href='$val' target='_blank'>$label</a>";
588         }
589         text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
590 }
591
592 //-----------------------------------------------------------------------------------
593 //
594 //      Since FA 2.2  $init parameter is superseded by $check. 
595 //  When $check!=null current date is displayed in red when set to other 
596 //      than current date.
597 //      
598 function date_cells($label, $name, $title = null, $check=null, $inc_days=0, 
599         $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
600 {
601         global $use_date_picker, $path_to_root, $Ajax;
602
603         if (!isset($_POST[$name]) || $_POST[$name] == "")
604         {
605                 if ($inc_years == 1001)
606                         $_POST[$name] = null;
607                 else
608                 {
609                         $dd = Today();
610                         if ($inc_days != 0)
611                                 $dd = add_days($dd, $inc_days);
612                         if ($inc_months != 0)
613                                 $dd = add_months($dd, $inc_months);
614                         if ($inc_years != 0)
615                                 $dd = add_years($dd, $inc_years);
616                         $_POST[$name] = $dd;
617                 }
618         }
619         if ($use_date_picker)
620                 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
621                 . "     <img src='$path_to_root/themes/default/images/cal.gif' width='16' height='16' border='0' alt='"._('Click Here to Pick up the date')."'></a>\n";
622         else
623                 $post_label = "";
624
625         if ($label != null)
626                 label_cell($label, $params);
627
628         echo "<td>";
629         
630         $class = $submit_on_change ? 'class="searchbox"' : '';
631
632         $aspect = $check ? 'aspect="cdate"' : '';
633         if ($check && (get_post($name) != Today()))
634                 $aspect .= ' style="color:#FF0000"';
635
636         default_focus($name);
637
638         echo "<input type=\"text\" name=\"$name\" $class $aspect size=\"9\" maxlength=\"12\" value=\"" 
639          . $_POST[$name]. "\""
640          .($title ? " title='$title'": '')." > $post_label";
641         echo "</td>\n";
642         $Ajax->addUpdate($name, $name, $_POST[$name]);
643 }
644
645 function date_row($label, $name, $title=null, $check=null, $inc_days=0, $inc_months=0, 
646         $inc_years=0, $params=null, $submit_on_change=false)
647 {
648         echo "<tr><td class='label'>$label</td>";
649         date_cells(null, $name, $title, $check, $inc_days, $inc_months, 
650                 $inc_years, $params, $submit_on_change);
651         echo "</tr>\n";
652 }
653
654 //-----------------------------------------------------------------------------------
655 function password_row($label, $name, $value)
656 {
657         echo "<tr><td class='label'>$label</td>";
658         label_cell("<input type='password' name='$name' size=20 maxlength=20 value='$value' />");
659         echo "</tr>\n";
660 }       
661
662 //-----------------------------------------------------------------------------------
663 function file_cells($label, $name, $id="")
664 {
665         if ($id != "")
666                 $id = "id='$id'";
667         label_cells($label, "<input type='file' name='$name' $id />");
668 }               
669 function file_row($label, $name, $id = "")
670 {
671         echo "<tr><td class='label'>$label</td>";
672         file_cells(null, $name, $id);
673         echo "</tr>\n";
674 }       
675
676 //-----------------------------------------------------------------------------------
677
678 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
679 {
680         text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
681 }
682
683 //-----------------------------------------------------------------------------------
684
685 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
686 {
687         echo "<tr><td class='label'>$label</td>";
688         ref_cells(null, $name, $title, $init, null, $submit_on_change);
689         echo "</tr>\n";
690 }
691
692 //-----------------------------------------------------------------------------------
693
694 function percent_row($label, $name, $init=null)
695 {
696
697         if (!isset($_POST[$name]) || $_POST[$name]=="")
698         {
699                 $_POST[$name] = $init == null ? '' : $init;
700         }
701
702         small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
703 }
704
705 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
706 {
707         global $Ajax;
708
709         if (!isset($dec))
710                 $dec = user_price_dec();
711         if (!isset($_POST[$name]) || $_POST[$name] == "")
712         {
713                 if ($init !== null)
714                         $_POST[$name] = $init;
715                 else
716                         $_POST[$name] = '';
717         }
718         if ($label != null)
719         {
720                 if ($params == null)
721                         $params = "class='label'";
722                 label_cell($label, $params);
723         }
724         if (!isset($max))
725                 $max = $size;
726
727         if ($label != null)
728                 echo "<td>";
729         else
730                 echo "<td align='right'>";
731
732         echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
733
734         if ($post_label) {
735                 echo "<span id='_{$name}_label'> $post_label</span>";
736                 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
737         }
738         echo "</td>\n";
739         $Ajax->addUpdate($name, $name, $_POST[$name]);
740         $Ajax->addAssign($name, $name, 'dec', $dec);
741 }
742
743
744 //-----------------------------------------------------------------------------------
745
746 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
747 {
748         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
749 }
750
751 //JAM  Allow entered unit prices to be fractional
752 function unit_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
753 {
754         if (!isset($dec))
755                 $dec = user_price_dec()+2;
756
757         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec+2);
758 }
759
760 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
761 {
762         echo "<tr>";
763         amount_cells($label, $name, $init, $params, $post_label, $dec);
764         echo "</tr>\n";
765 }
766
767 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
768 {
769         echo "<tr>";
770         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
771         echo "</tr>\n";
772 }
773
774 //-----------------------------------------------------------------------------------
775
776 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
777 {
778         if (!isset($dec))
779                 $dec = user_qty_dec();
780
781         amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
782 }
783
784 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
785 {
786         if (!isset($dec))
787                 $dec = user_qty_dec();
788
789         echo "<tr>";
790         amount_cells($label, $name, $init, $params, $post_label, $dec);
791         echo "</tr>\n";
792 }
793
794 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
795 {
796         if (!isset($dec))
797                 $dec = user_qty_dec();
798
799         echo "<tr>";
800         small_amount_cells($label, $name, $init, $params, $post_label, $dec);
801         echo "</tr>\n";
802 }
803
804 //-----------------------------------------------------------------------------------
805
806 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
807 {
808         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
809 }
810
811 //-----------------------------------------------------------------------------------
812
813 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
814 {
815         if (!isset($dec))
816                 $dec = user_qty_dec();
817         amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
818 }
819
820 //-----------------------------------------------------------------------------------
821
822 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
823 {
824         global $Ajax;
825
826         default_focus($name);
827         if ($label != null)
828                 echo "<td $params>$label</td>\n";
829         if ($value == null)
830                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
831         echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
832         .($title ? " title='$title'" : '')
833         .">$value</textarea></td>\n";
834         $Ajax->addUpdate($name, $name, $value);
835 }
836
837 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
838 {
839         echo "<tr><td class='label'>$label</td>";
840         textarea_cells(null, $name, $value, $cols, $rows, $title, $params);
841         echo "</tr>\n";
842 }
843
844 //-----------------------------------------------------------------------------------
845 /*
846 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
847 {
848         global $Ajax;
849
850         default_focus($name);
851         echo "<tr><td>$label</td>\n";
852         echo "<td>";
853
854         if ($value == null)
855                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
856         echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\">   ";
857
858         submit($input_name, $input_value);
859
860         echo "</td></tr>\n";
861         $Ajax->addUpdate($name, $name, $value);
862 }
863 */
864 //-----------------------------------------------------------------------------------
865 //
866 //      When show_inactive page option is set 
867 //  displays value of inactive field as checkbox cell.
868 //  Also updates database record after status change.
869 //
870 function inactive_control_cell($id, $value, $table, $key)
871 {
872         global  $Ajax;
873
874         $name = "Inactive". $id;
875         $value = $value ? 1:0;
876
877         if (check_value('show_inactive')) {
878                 if (isset($_POST['LInact'][$id]) && (get_post('_Inactive'.$id.'_update') || 
879                         get_post('Update')) && (check_value('Inactive'.$id) != $value)) {
880                         update_record_status($id, !$value, $table, $key);
881                 }
882                 echo '<td align="center">'. checkbox(null, $name, $value, true, '', "align='center'")
883                         . hidden("LInact[$id]", $value, false) . '</td>';       
884         }
885 }
886 //
887 //      Displays controls for optional display of inactive records
888 //
889 function inactive_control_row($th) {
890         echo  "<tr><td colspan=".(count($th)).">"
891                 ."<div style='float:left;'>"
892                 . checkbox(null, 'show_inactive', null, true). _("Show also Inactive")
893                 ."</div><div style='float:right;'>"
894                 . submit('Update', _('Update'), false, '', null)
895                 ."</div></td></tr>";
896 }
897 //
898 //      Inserts additional column header when display of inactive records is on.
899 //
900 function inactive_control_column(&$th) {
901         global $Ajax;
902         
903         if (check_value('show_inactive')) 
904                 array_insert($th, count($th)-2 , _("Inactive"));
905         if (get_post('_show_inactive_update')) {
906                 $Ajax->activate('_page_body');
907         }
908 }
909
910 function customer_credit_row($customer, $credit, $parms='')
911 {
912         global $path_to_root;
913         
914         label_row( _("Current Credit:"),
915                 "<a target='_blank' " . ($credit<0 ? 'class="redfg"' : '')
916                 ."href='$path_to_root/sales/inquiry/customer_inquiry.php?customer_id=".$customer."'"
917                 ." onclick=\"javascript:openWindow(this.href,this.target); return false;\" >"
918                 . price_format($credit)
919                 ."</a>", $parms);
920 }
921
922 function supplier_credit_row($supplier, $credit, $parms='')
923 {
924         global $path_to_root;
925         
926         label_row( _("Current Credit:"),
927                 "<a target='_blank' " . ($credit<0 ? 'class="redfg"' : '')
928                 ."href='$path_to_root/purchasing/inquiry/supplier_inquiry.php?supplier_id=".$supplier."'"
929                 ." onclick=\"javascript:openWindow(this.href,this.target); return false;\" >"
930                 . price_format($credit)
931                 ."</a>", $parms);
932 }
933
934 function bank_balance_row($bank_acc, $parms='')
935 {
936         global $path_to_root;
937         
938         $to = add_days(Today(), 1);
939         $bal = get_balance_before_for_bank_account($bank_acc, $to);
940         label_row( _("Bank Balance:"),
941                 "<a target='_blank' " . ($bal<0 ? 'class="redfg"' : '')
942                 ."href='$path_to_root/gl/inquiry/bank_inquiry.php?bank_account=".$bank_acc."'"
943                 ." onclick=\"javascript:openWindow(this.href,this.target); return false;\" >&nbsp;"
944                 . price_format($bal)
945                 ."</a>", $parms);
946 }
947
948 ?>