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