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