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