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