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