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