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