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