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