Some more fixes to selects
[fa-stable.git] / includes / ui / ui_lists.inc
1 <?php
2
3 include_once($path_to_root . "/includes/banking.inc");
4 include_once($path_to_root . "/includes/reserved.inc");
5 include_once($path_to_root . "/includes/types.inc");
6 include_once($path_to_root . "/includes/current_user.inc");
7
8 $_search_button = "<input type='submit' class='combo_submit' style='border:0;background:url($path_to_root/themes/"
9         ."%s/images/locate.png) no-repeat;%s' name='%s' value=' ' title='"._("Set filter")."'> ";
10
11 $_select_button = "<input type='submit' class='combo_select' style='border:0;background:url($path_to_root/themes/"
12         ."%s/images/button_ok.png) no-repeat;%s' name='%s' value=' ' title='"._("Select")."'> ";
13
14 $all_items = reserved_words::get_all();
15
16 //----------------------------------------------------------------------------
17 //      Universal combo generator
18 //      $sql must return selector values and selector texts in columns 0 & 1
19 //      Options are merged with defaults.
20
21 function combo_input($name, $selected_id, $sql, $valfield, $namefield,
22         $options=null)
23 {
24 global $Ajax;
25
26 $opts = array(          // default options
27         'where'=> array(),              // additional constraints
28         'order' => $namefield,  // list sort order
29                 // special option parameters
30         'spec_option'=>false,   // option text or false
31         'spec_id' => 0,         // option id
32                 // submit on select parameters
33         'select_submit' => false, //submit on select: true/false
34         'async' => true,        // select update via ajax (true) vs _page_body reload
35                 // search box parameters
36         'sel_hint' => null,
37         'search_box' => false,  // name or true/false
38         'type' => 0,    // type of extended selector:
39                 // 0 - with (optional) visible search box, search by id
40                 // 1 - with hidden search box, search by option text
41                 // 2 - TODO reverse: box with hidden selector available via enter; this
42                 // would be convenient for optional ad hoc adding of new item
43         'search_submit' => true, //search submit button: true/false
44         'size' => 8,    // size and max of box tag
45         'max' => 50,
46         'cells' => false,       // combo displayed as 2 <td></td> cells
47         'search' => array(), // sql field names to search
48         'format' => null,        // format functions for regular options
49         'disabled' => false, // FIX todo
50         'box_hint' => null // box/selectors hints; null = std see below
51 );
52 // ------ merge options with defaults ----------
53         $opts = array_merge($opts, $options);
54         if (!is_array($opts['where']))  $opts['where'] = array($opts['where']);
55
56         $search_box = $opts['search_box']===true ? '_'.$name.'_edit' : $opts['search_box'];
57         $search_submit = $opts['search_submit']===true ? '_'.$name.'_button' : $opts['search_submit'];
58         $select_submit =  $opts['select_submit']===true ? '_'.$name.'_update' : $opts['select_submit'];
59         $spec_id = $opts['spec_id'];
60         $spec_option = $opts['spec_option'];
61         $by_id = ($opts['type'] == 0);
62         $class = $by_id ? 'combo':'combo2';
63
64         if(!count($opts['search'])) {
65                 $opts['search'] = array($by_id ? $valfield : $namefield);
66         }
67         if ($opts['sel_hint'] === null)
68                 $opts['sel_hint'] = $by_id || $search_box==false ?
69                 '' : _('Press Space tab for search pattern entry');
70
71         if ($opts['box_hint'] === null)
72                 $opts['box_hint'] = $search_box ?
73                         ($by_id ? _('Enter code fragment to search or * for all')
74                         : _('Enter description fragment to search or * for all')) :'';
75
76         if ($selected_id == null) {
77                 $selected_id = get_post($name);
78         }
79         $txt = get_post($search_box);
80         $rel = '';
81         $limit = '';
82
83         if (isset($_POST[$select_submit])) {
84                 if (!$opts['async'])
85                         $Ajax->activate('_page_body');
86                 else
87                         $Ajax->activate($name);
88         }
89         if ($search_box) {
90                 // search related sql modifications
91
92         $rel = "rel='$search_box'"; // set relation to list
93    if ($opts['search_submit']) {
94         // if selected from list - set focus on next field
95         if (isset($_POST[$search_submit])) {
96                 $Ajax->activate($name);
97         }
98         if ($txt == '') {
99                 if ($spec_option === false)
100                 $limit = ' LIMIT 1';
101                 else
102                 $opts['where'][] = $valfield . "='". get_post($name, $spec_id)."'";
103         }
104         else
105                 if ($txt != '*') {
106
107                 foreach($opts['search'] as $i=> $s)
108                         $opts['search'][$i] = $s . " LIKE '%{$txt}%'";
109                 $opts['where'][] = '('. implode($opts['search'], ' OR ') . ')';
110                 }
111         }
112    }
113         // sql completion
114         if (count($opts['where'])) {
115                 $sql .= strpos($sql, 'WHERE')==false ? ' WHERE ':' AND ';
116                 $sql .= '('. implode($opts['where'], ' AND ') . ')';
117         }
118         if ($opts['order'] != false) {
119                 $sql .= ' ORDER BY '.$opts['order'];
120         }
121
122         $sql .= $limit;
123         // ------ make selector ----------
124         $selector = $first_opt = '';
125         $first_id = false;
126         $found = false;
127 //if($name=='code_id')    display_error($sql);
128         if($result = db_query($sql)) {
129                 while ($contact_row = db_fetch($result)) {
130                         $value = $contact_row[0];
131                         $descr = $opts['format']==null ?  $contact_row[1] :
132                         call_user_func($opts['format'], $contact_row);
133                         $sel = '';
134                         if (isset($_POST[$search_submit])) {
135                                 if (($by_id ? $value : $contact_row[1]) === $txt)
136                                 $selected_id = $value;
137                         }
138                         // if no initial selection - set the first item
139                         if ($selected_id === "")        {
140                                 $selected_id = $value;
141                         }
142                         if ($selected_id == $value) {
143                                 $sel = 'selected';
144                                 $found = $value;
145                                 $_POST[$name] = $selected_id;
146                         }
147                         if ($first_opt == false) {
148                                 $first_id = $value;
149                                 $first_opt = $descr;
150                                 continue;
151                         }
152                         $selector .= "<option $sel value='$value'>$descr</option>\n";
153                 }
154                 db_free_result($result);
155         }
156         if ($first_id !== false) {
157                 // add first option - selected also if no match for selected_id was found
158                 // and no special option defined
159                 $sel = $spec_option===false || $found === $first_id ? 'selected' : '';
160                 $selector = "<option $sel value='$first_id'>$first_opt</option>\n"
161                         . $selector;
162         }
163         // Prepend special option.
164         if ($spec_option !== false) { // if special option used - add it
165                 $first_id = $spec_id;
166                 $first_opt = $spec_option;
167                 if (isset($_POST[$search_submit])) {
168                         if ($txt == '')
169                                 $selected_id = $spec_id;
170                 }
171                 $sel = $found===false ? 'selected' : '';
172                 $selector = "<option $sel value='$spec_id'>$spec_option</option>\n"
173                         . $selector;
174                 $first_id = $spec_id;
175         }
176
177         if ($found === false) {
178                 $_POST[$name] = $first_id;
179         }
180
181
182         if ($by_id) {
183                 $txt = $_POST[$name];
184                 if ($search_box)
185                         $Ajax->addUpdate($name, $search_box, $txt);
186         }
187
188         $selector = "<select name='$name' class='$class' title='"
189                 . $opts['sel_hint']."' $rel>".$selector."</select>\n";
190
191         $Ajax->addUpdate($name, "_{$name}_sel", $selector);
192
193         // because of bug which M$ cannot fix since IE 5.0
194         // we must embed whole selector in span tags to enable proper ajax update
195         $selector = "<span id='_{$name}_sel'>".$selector."</span>\n";
196
197         if ($select_submit != false) { // if submit on change is used - add select button
198                 global $_select_button;
199         // button class selects form reload/ajax selector update
200                 $selector .= sprintf($_select_button, user_theme(),
201                         (in_ajax() ? 'display:none;':''),
202                         $select_submit)."\n";
203         }
204 // ------ make combo ----------
205
206 $edit_entry = '';
207         if ($search_box != false) {
208                 $edit_entry = "<input type='text' name='$search_box' id='$search_box' size='".
209                         $opts['size']."' maxlength='".$opts['max'].
210                         "' value='$txt' class='$class' rel='$name' autocomplete='off' title='"
211                         .$opts['box_hint']."'"
212                         .(in_ajax() && !$by_id ? " style=display:none;":'')
213                         .">\n";
214                 if ($search_submit != false) {
215                         global $_search_button;
216                         $edit_entry .= sprintf($_search_button, user_theme(),
217                                 (in_ajax() ? 'display:none;':''),
218                                 $search_submit)."\n";
219                 }
220         }
221         default_focus($name);
222         $str = $selector;
223         if ($search_box && $opts['cells'])
224                 echo ($edit_entry!='' ? "<td>$edit_entry</td>" : '')."<td>$selector</td>";
225         else
226                 echo $edit_entry.$selector;
227
228         return $str;
229 }
230
231 function supplier_list($name, $selected_id, $spec_option=false, $submit_on_change=false)
232 {
233         global $all_items;
234
235         $sql = "SELECT supplier_id, supp_name, curr_code FROM ".TB_PREF."suppliers ";
236
237         $mode = get_company_pref('no_supplier_list');
238
239         return combo_input($name, $selected_id, $sql, 'supplier_id', 'supp_name',
240         array(
241                 'format' => '_format_add_curr',
242                 'search_box' => $mode!=0,
243                 'type' => 1,
244                 'spec_option' => $spec_option === true ? _("All Suppliers") : $spec_option,
245                 'spec_id' => $all_items,
246                 'select_submit'=> $submit_on_change,
247                 'async' => false,
248                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment') :
249                 _('Select supplier')
250                 ));
251 }
252
253 function _format_add_curr($row)
254 {
255 static $company_currency;
256
257 if ($company_currency == null)
258 {
259         $company_currency = get_company_currency();
260 }
261 return $row[1] . ($row[2] == $company_currency ?
262         '' : ("&nbsp;-&nbsp;" . $row[2]));
263 }
264
265 function supplier_list_cells($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
266 {
267         if ($label != null)
268                 echo "<td>$label</td><td>\n";
269         $str = supplier_list($name, $selected_id, $all_option, $submit_on_change);
270                 echo "</td>\n";
271         return $str;
272 }
273
274 function supplier_list_row($label, $name, $selected_id, $all_option = false, $submit_on_change=false)
275 {
276 echo "<tr><td>$label</td><td>";
277         $str = supplier_list($name, $selected_id, $all_option, $submit_on_change );
278 echo "</td></tr>\n";
279 return $str;
280 }
281 //----------------------------------------------------------------------------------------------
282
283 function customer_list($name, $selected_id, $spec_option=false, $submit_on_change=false)
284 {
285         global $all_items;
286
287         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
288
289         $mode = get_company_pref('no_customer_list');
290
291 return combo_input($name, $selected_id, $sql, 'debtor_no', 'name',
292         array(
293             'format' => '_format_add_curr',
294                 'search_box' => $mode!=0,
295                 'type' => 1,
296                 'size' => 20,
297                 'spec_option' => $spec_option === true ? _("All Customers") : $spec_option,
298                 'spec_id' => $all_items,
299                 'select_submit'=> $submit_on_change,
300                 'async' => false,
301                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment') :
302                 _('Select customer')
303         ) );
304 }
305
306 function customer_list_cells($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
307 {
308         if ($label != null)
309                 echo "<td>$label</td>\n";
310         echo "<td nowrap>";
311         $str = customer_list($name, $selected_id, $all_option, $submit_on_change);
312         echo "</td>\n";
313         return $str;
314 }
315
316 function customer_list_row($label, $name, $selected_id, $all_option = false, $submit_on_change=false)
317 {
318 echo "<tr><td>$label</td><td nowrap>";
319 $str = customer_list($name, $selected_id, $all_option, $submit_on_change);
320 echo "</td>\n</tr>\n";
321 return $str;
322 }
323
324 //------------------------------------------------------------------------------------------------
325
326 function customer_branches_list($customer_id, $name, $selected_id,
327         $spec_option = true, $enabled=true, $submit_on_change=false)
328 {
329         global $all_items;
330
331         $sql = "SELECT branch_code, br_name FROM ".TB_PREF."cust_branch
332                 WHERE debtor_no='" . $customer_id . "' ";
333
334         $where = $enabled ? array("disable_trans = 0") : array();
335 return  combo_input($name, $selected_id, $sql, 'branch_code', 'br_name',
336         array(
337                 'where' => $where,
338                 'spec_option' => $spec_option === true ? _('All branches') : $spec_option,
339                 'spec_id' => $all_items,
340                 'select_submit'=> $submit_on_change,
341                 'sel_hint' => _('Select customer branch')
342 //                'async' => false
343         ) );
344 }
345 //------------------------------------------------------------------------------------------------
346
347 function customer_branches_list_cells($label,$customer_id, $name, $selected_id, $all_option = true, $enabled=true, $submit_on_change=false)
348 {
349         if ($label != null)
350                 echo "<td>$label</td>\n";
351         echo "<td>";
352         $ret = customer_branches_list($customer_id, $name, $selected_id, $all_option, $enabled, $submit_on_change);
353         echo "</td>\n";
354         return $ret;
355 }
356
357 function customer_branches_list_row($label,$customer_id, $name, $selected_id, $all_option = true, $enabled=true, $submit_on_change=false)
358 {
359         echo "<tr>";
360         $ret = customer_branches_list_cells($label, $customer_id, $name, $selected_id, $all_option, $enabled, $submit_on_change);
361         echo "</tr>";
362         return $ret;
363 }
364
365 //------------------------------------------------------------------------------------------------
366
367 function locations_list($name, $selected_id, $all_option=false, $submit_on_change=false)
368 {
369         global $all_items;
370
371         $sql = "SELECT loc_code, location_name FROM ".TB_PREF."locations";
372
373 return combo_input($name, $selected_id, $sql, 'loc_code', 'location_name',
374         array(
375                 'spec_option' => $all_option === true ? _("All Locations") : $all_option,
376                 'spec_id' => $all_items,
377                 'select_submit'=> $submit_on_change,
378                 'async' => false
379         ) );
380 }
381
382 function locations_list_cells($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
383 {
384         if ($label != null)
385                 echo "<td>$label</td>\n";
386         echo "<td>";
387         $str = locations_list($name, $selected_id, $all_option, $submit_on_change);
388         echo "</td>\n";
389         return $str;
390 }
391
392 function locations_list_row($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
393 {
394         echo "<tr>";
395         $str = locations_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
396         echo "</tr>\n";
397         return $str;
398 }
399
400 //-----------------------------------------------------------------------------------------------
401
402 function currencies_list($name, $selected_id, $submit_on_change=false)
403 {
404         $sql = "SELECT curr_abrev, currency FROM ".TB_PREF."currencies";
405
406 // default to the company currency
407         if ($selected_id == "")
408         {
409                         $selected_id = get_company_currency();
410                         $_POST[$name] = $selected_id;
411         }
412
413 return combo_input($name, $selected_id, $sql, 'curr_abrev', 'currency',
414         array(
415                 'select_submit'=> $submit_on_change,
416                 'async' => false
417         ) );
418 }
419
420 function currencies_list_cells($label, $name, $selected_id)
421 {
422         if ($label != null)
423                 echo "<td>$label</td>\n";
424         echo "<td>";
425         $str = currencies_list($name, $selected_id);
426         echo "</td>\n";
427         return $str;
428 }
429
430 function currencies_list_row($label, $name, $selected_id)
431 {
432         echo "<tr>\n";
433         $str = currencies_list_cells($label, $name, $selected_id);
434         echo "</tr>\n";
435         return $str;
436 }
437
438 //---------------------------------------------------------------------------------------------------
439
440 function fiscalyears_list($name, $selected_id, $submit_on_change=false)
441 {
442
443         $sql = "SELECT * FROM ".TB_PREF."fiscal_year";
444
445 // default to the company current fiscal year
446         if ($selected_id == "")
447         {
448                         $selected_id = get_company_pref('f_year');
449                         $_POST[$name] = $selected_id;
450         }
451
452 return combo_input($name, $selected_id, $sql, 'id', '',
453         array(
454                 'order' => 'begin',
455                 'format' => '_format_fiscalyears',
456                 'select_submit'=> $submit_on_change,
457                 'async' => false
458         ) );
459 }
460
461 function _format_fiscalyears($row)
462 {
463         return sql2date($row[1]) . "&nbsp;-&nbsp;" . sql2date($row[2])
464         . "&nbsp;&nbsp;" . ($row[3] ? _('Closed') : _('Active')) . "</option>\n";
465 }
466
467 function fiscalyears_list_cells($label, $name, $selected_id)
468 {
469         if ($label != null)
470                 echo "<td>$label</td>\n";
471         echo "<td>";
472         $str = fiscalyears_list($name, $selected_id);
473         echo "</td>\n";
474         return $str;
475 }
476
477 function fiscalyears_list_row($label, $name, $selected_id)
478 {
479         echo "<tr>\n";
480         $str = fiscalyears_list_cells($label, $name, $selected_id);
481         echo "</tr>\n";
482         return $str;
483 }
484 //------------------------------------------------------------------------------------
485
486 function dimensions_list($name, $selected_id, $no_option=false, $showname=' ',
487         $submit_on_change=false, $showclosed=false, $showtype=1)
488 {
489 $sql = "SELECT id, CONCAT(reference,'  ',name) as ref FROM ".TB_PREF."dimensions";
490
491 $options = array(
492         'order' => 'reference',
493         'spec_option'=>$no_option ? $showname : false,
494         'spec_id' => 0,
495         'select_submit'=> $submit_on_change,
496         'async' => false,
497         );
498
499         if ($showclosed)
500         $options['where'][] = "closed=0";
501         if($showtype)
502         $options['where'][] = "type_=$showtype";
503
504         return combo_input($name, $selected_id, $sql, 'id', 'ref', $options);
505 }
506
507 function dimensions_list_cells($label, $name, $selected_id, $no_option=false, $showname=null,
508         $showclosed=false, $showtype=0)
509 {
510         if ($label != null)
511                 echo "<td>$label</td>\n";
512         echo "<td>";
513         $str = dimensions_list($name, $selected_id, $no_option, $showname, false, $showclosed, $showtype);
514         echo "</td>\n";
515         return $str;
516 }
517
518 function dimensions_list_row($label, $name, $selected_id, $no_option=false, $showname=null,
519         $showclosed=false, $showtype=0)
520 {
521         echo "<tr>\n";
522         $str = dimensions_list_cells($label, $name, $selected_id, $no_option, $showname,
523                 $showclosed, $showtype);
524         echo "</tr>\n";
525         return $str;
526 }
527
528 //---------------------------------------------------------------------------------------------------
529
530 function stock_items_list($name, $selected_id, $all_option=false, $submit_on_change=false, $opts=array())
531 {
532         global $all_items;
533
534         $sql = "SELECT stock_id, s.description, c.description
535                         FROM ".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE s.category_id=c.category_id";
536
537         return combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
538         array_merge(
539           array(
540                 'format' => '_format_stock_items',
541                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
542                 'spec_id' => $all_items,
543                 'search_box' => true,
544                 'search' => array("stock_id", "c.description","s.description"),
545                 'search_submit' => get_company_pref('no_item_list')!=0,
546                 'size'=>10,
547                 'select_submit'=> $submit_on_change
548           ), $opts) );
549 }
550
551 function _format_stock_items($row)
552 {
553         return (user_show_codes() ?  ($row[0] . "&nbsp;-&nbsp;") : "")
554                 . $row[2] . "&nbsp;-&nbsp;" . $row[1];
555 }
556
557 function stock_items_list_cells($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
558 {
559         if ($label != null)
560                 echo "<td>$label</td>\n";
561         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
562         array('cells'=>true));
563         return $str;
564 }
565 /*
566 function stock_items_list_row($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
567 {
568         echo "<tr>\n";
569         $str = stock_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
570         echo "</tr>\n";
571         return $str;
572 }
573 */
574 //------------------------------------------------------------------------------------
575
576 function base_stock_items_list($where, $name, $selected_id,
577         $all_option=false, $submit_on_change=false)
578 {
579         global $all_items;
580
581         $sql = "SELECT stock_id, s.description, c.description
582                 FROM ".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE
583                 s.category_id=c.category_id";
584
585         return combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
586         array(
587                 'format' => '_format_stock_items',
588                 'spec_option' => $all_option==true ?  _("All Items") : $all_option,
589                 'spec_id' => $all_items,
590                 'select_submit'=> $submit_on_change,
591                 'where' => $where
592         ) );
593 }
594 //------------------------------------------------------------------------------------
595
596 function stock_bom_items_list($name, $selected_id, $all_option=false, $submit_on_change=false)
597 {
598
599         return base_stock_items_list("(s.mb_flag='M' OR s.mb_flag='K')",
600                 $name, $selected_id, $all_option, $submit_on_change);
601 }
602 /*
603 function stock_bom_items_list_cells($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
604 {
605         if ($label != null)
606                 echo "<td>$label</td>\n";
607         echo "<td>";
608         $str = stock_bom_items_list($name, $selected_id, $all_option, $submit_on_change);
609         echo "</td>\n";
610         return $str;
611 }
612
613 function stock_bom_items_list_row($label, $name, $selected_id, $all_option=false, $submit_on_change=false)
614 {
615         echo "<tr>\n";
616         $str = stock_bom_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
617         echo "</tr>\n";
618         return $str;
619 }
620 */
621 //------------------------------------------------------------------------------------
622
623 function stock_manufactured_items_list($name, $selected_id,
624         $all_option=false, $submit_on_change=false)
625 {
626         return base_stock_items_list("s.mb_flag='M'",
627                 $name, $selected_id, $all_option, $submit_on_change);
628 }
629
630 function stock_manufactured_items_list_cells($label, $name, $selected_id,
631                                 $all_option=false, $submit_on_change=false)
632 {
633         if ($label != null)
634                 echo "<td>$label</td>\n";
635         echo "<td>";
636         $str = stock_manufactured_items_list($name, $selected_id, $all_option,
637                                 $submit_on_change);
638         echo "</td>\n";
639         return $str;
640 }
641
642 function stock_manufactured_items_list_row($label, $name, $selected_id,
643                 $all_option=false, $submit_on_change=false)
644 {
645         echo "<tr>\n";
646         $str = stock_manufactured_items_list_cells($label, $name, $selected_id,
647                         $all_option, $submit_on_change);
648         echo "</tr>\n";
649         return $str;
650 }
651 //------------------------------------------------------------------------------------
652
653 function stock_component_items_list($name, $parent_stock_id, $selected_id,
654         $all_option=false, $submit_on_change=false)
655 {
656         return base_stock_items_list("stock_id != '$parent_stock_id'", $name, $selected_id,
657                 $all_option, $submit_on_change);
658 }
659 //------------------------------------------------------------------------------------
660
661 function stock_costable_items_list($name, $selected_id,
662         $all_option=false, $submit_on_change=false)
663 {
664         return base_stock_items_list("mb_flag!='D'", $name, $selected_id,
665                 $all_option, $submit_on_change);
666 }
667
668 //------------------------------------------------------------------------------------
669
670 function stock_purchasable_items_list($name, $selected_id,      $all_option=false,
671                 $submit_on_change=false, $opts=array())
672 {
673
674         $sql = "SELECT stock_id, s.description, c.description
675                         FROM ".TB_PREF."stock_master s,".TB_PREF."stock_category c
676                         WHERE s.category_id=c.category_id
677                                 AND mb_flag !='M' ";
678         return combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
679           array_merge( array(
680                 'format' => '_format_stock_items',
681                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
682                 'spec_id' => $all_items,
683                 'search_box' => true,
684                 'search' => array("stock_id", "c.description","s.description"),
685                 'search_submit' => get_company_pref('no_item_list')!=0,
686                 'size'=>10,
687                 'select_submit'=> $submit_on_change
688           ), $opts) );
689
690 }
691
692 function stock_purchasable_items_list_cells($label, $name, $selected_id,
693                         $all_option=false, $submit_on_change=false)
694 {
695         if ($label != null)
696                 echo "<td>$label</td>\n";
697         $ret = stock_purchasable_items_list($name, $selected_id, $all_option,
698                 $submit_on_change, array('cells'=>true) );
699         return $ret;
700 }
701
702 function stock_purchasable_items_list_row($label, $name, $selected_id,
703                         $all_option=false, $submit_on_change=false)
704 {
705         echo "<tr>\n";
706         $ret = stock_purchasable_items_list_cells($label, $name, $selected_id,
707                 $all_option, $submit_on_change);
708         echo "</tr>\n";
709         return $ret;
710 }
711
712 //------------------------------------------------------------------------------------
713
714 function stock_item_types_list_row($label, $name, $selected_id, $enabled=true)
715 {
716         default_focus($name);
717         echo "<tr>";
718         if ($label != NULL)
719                 echo "<td>$label</td>\n";
720         echo "<td>";
721         if ($enabled)
722                 $sel = "<select name='$name' onchange='this.form.submit();'>\n";
723         else
724                 $sel =  "<select disabled name='$name'>\n";
725         if ($selected_id == null)
726                 $selected_id = (!isset($_POST[$name]) ? "" : $_POST[$name]);
727         if ($selected_id == "")
728                 $_POST[$name] = $selected_id = "B";
729         $sel .= "<option " . ($selected_id == 'M'?" selected ":"") . " value='M'>" . _("Manufactured"). "</option>\n";
730         $sel .= "<option " . ($selected_id == 'B'?" selected ":"") . " value='B'>" . _("Purchased"). "</option>\n";
731         $sel .= "<option " . ($selected_id == 'D'?" selected ":"") . " value='D'>" . _("Service"). "</option>\n";
732         $sel .= "</select>";
733         echo $sel;
734         echo "</td></tr>\n";
735         return $sel;
736 }
737
738 function stock_units_list_row($label, $name, $value, $enabled=true)
739 {
740         global $stock_units;
741
742         default_focus($name);
743         $result = get_all_item_units();
744         echo "<tr><td>$label</td>\n";
745         if ($enabled)
746                 echo "<td><select name='$name'>";
747         else
748                 echo "<td><select disabled name='$name'>";
749
750         if ($value == null)
751                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
752
753         while($unit = db_fetch($result))
754         {
755                 if ($value == "")
756                 {
757                         $_POST[$name] = $unit['abbr'];
758                 }
759                 $descr = $unit['name'];
760                 if ($value==$unit['abbr'])
761                 {
762                         echo "<option selected value='".$unit['abbr']."'>$descr</option>\n";
763                 }
764                 else
765                 {
766                         echo "<option value='".$unit['abbr']."'>$descr</option>\n";
767                 }
768         }
769         echo "</select></td></tr>\n";
770 }
771
772 //------------------------------------------------------------------------------------
773
774 function tax_types_list($name, $selected_id, $none_option=false, $submit_on_change=false)
775 {
776         global $all_items;
777
778         $sql = "SELECT id, CONCAT(name, ' (',rate,'%)') as name FROM ".TB_PREF."tax_types";
779
780         return combo_input($name, $selected_id, $sql, 'id', 'name',
781         array(
782                 'spec_option' => $none_option,
783                 'spec_id' => $all_items,
784                 'select_submit'=> $submit_on_change,
785                 'async' => false,
786         ) );
787 }
788
789 function tax_types_list_cells($label, $name, $selected_id, $none_option=false,
790         $submit_on_change=false)
791 {
792         if ($label != null)
793                 echo "<td>$label</td>\n";
794         echo "<td>";
795         $str = tax_types_list($name, $selected_id, $none_option, $submit_on_change);
796         echo "</td>\n";
797         return $str;
798 }
799
800 function tax_types_list_row($label, $name, $selected_id, $none_option=false,
801         $submit_on_change=false)
802 {
803         echo "<tr>\n";
804         $str = tax_types_list_cells($label, $name, $selected_id, $none_option, $submit_on_change);
805         echo "</tr>\n";
806         return $str;
807 }
808
809 //------------------------------------------------------------------------------------
810
811 function tax_groups_list($name, $selected_id,
812         $none_option=false, $submit_on_change=false)
813 {
814         global $all_items;
815
816         $sql = "SELECT id, name FROM ".TB_PREF."tax_groups";
817
818         return combo_input($name, $selected_id, $sql, 'id', 'name',
819         array(
820                 'order' => 'id',
821                 'spec_option' => $none_option,
822                 'spec_id' => $all_items,
823                 'select_submit'=> $submit_on_change,
824                 'async' => false,
825         ) );
826 }
827
828 function tax_groups_list_cells($label, $name, $selected_id, $submit_on_change=false)
829 {
830         if ($label != null)
831                 echo "<td>$label</td>\n";
832         echo "<td>";
833         $str = tax_groups_list($name, $selected_id, false, $submit_on_change);
834         echo "</td>\n";
835         return $str;
836 }
837
838 function tax_groups_list_row($label, $name, $selected_id, $submit_on_change=false)
839 {
840         echo "<tr>\n";
841         $str = tax_groups_list_cells($label, $name, $selected_id, false, $submit_on_change);
842         echo "</tr>\n";
843         return $str;
844 }
845
846 //------------------------------------------------------------------------------------
847
848 function item_tax_types_list($name, $selected_id)
849 {
850         $sql ="SELECT id, name FROM ".TB_PREF."item_tax_types";
851         combo_input($name, $selected_id, $sql, 'id', 'name', array('order' => 'id') );
852 }
853
854 function item_tax_types_list_cells($label, $name, $selected_id)
855 {
856         if ($label != null)
857                 echo "<td>$label</td>\n";
858         echo "<td>";
859         item_tax_types_list($name, $selected_id);
860         echo "</td>\n";
861 }
862
863 function item_tax_types_list_row($label, $name, $selected_id)
864 {
865         echo "<tr>\n";
866         item_tax_types_list_cells($label, $name, $selected_id);
867         echo "</tr>\n";
868 }
869
870 //------------------------------------------------------------------------------------
871
872 function shippers_list($name, $selected_id)
873 {
874         $sql = "SELECT shipper_id, shipper_name FROM ".TB_PREF."shippers";
875         combo_input($name, $selected_id, $sql, 'shipper_id', 'shipper_name', array());
876 }
877
878 function shippers_list_cells($label, $name, $selected_id)
879 {
880         if ($label != null)
881                 echo "<td>$label</td>\n";
882         echo "<td>";
883         shippers_list($name, $selected_id);
884         echo "</td>\n";
885 }
886
887 function shippers_list_row($label, $name, $selected_id)
888 {
889         echo "<tr>\n";
890         shippers_list_cells($label, $name, $selected_id);
891         echo "</tr>\n";
892 }
893
894 //-------------------------------------------------------------------------------------
895
896 function sales_persons_list($name, $selected_id)
897 {
898         $sql = "SELECT salesman_code, salesman_name FROM ".TB_PREF."salesman";
899         combo_input($name, $selected_id, $sql, 'salesman_code', 'salesman_name', array());
900 }
901
902 function sales_persons_list_cells($label, $name, $selected_id)
903 {
904         if ($label != null)
905                 echo "<td>$label</td>\n";
906         echo "<td>\n";
907         sales_persons_list($name, $selected_id);
908         echo "</td>\n";
909 }
910
911 function sales_persons_list_row($label, $name, $selected_id, $submit_on_change=false)
912 {
913         echo "<tr>\n";
914         sales_persons_list_cells($label, $name, $selected_id, $submit_on_change=false);
915         echo "</tr>\n";
916 }
917
918 //------------------------------------------------------------------------------------
919
920 function sales_areas_list($name, $selected_id)
921 {
922         $sql = "SELECT area_code, description FROM ".TB_PREF."areas";
923         combo_input($name, $selected_id, $sql, 'area_code', 'description', array());
924 }
925
926 function sales_areas_list_cells($label, $name, $selected_id)
927 {
928         if ($label != null)
929                 echo "<td>$label</td>\n";
930         echo "<td>";
931         sales_areas_list($name, $selected_id);
932         echo "</td>\n";
933 }
934
935 function sales_areas_list_row($label, $name, $selected_id)
936 {
937         echo "<tr>\n";
938         sales_areas_list_cells($label, $name, $selected_id);
939         echo "</tr>\n";
940 }
941
942 //------------------------------------------------------------------------------------
943
944 function workorders_list($name, $selected_id)
945 {
946         $sql = "SELECT id, wo_ref FROM ".TB_PREF."workorders WHERE closed=0";
947         combo_input($name, $selected_id, $sql, 'id', 'wo_ref', array());
948 }
949
950 function workorders_list_cells($label, $name, $selected_id)
951 {
952         if ($label != null)
953                 echo "<td>$label</td>\n";
954         echo "<td>";
955         workorders_list($name, $selected_id);
956         echo "</td>\n";
957 }
958
959 function workorders_list_row($label, $name, $selected_id)
960 {
961         echo "<tr>\n";
962         workorders_list_cells($label, $name, $selected_id);
963         echo "</tr>\n";
964 }
965
966 //------------------------------------------------------------------------------------
967
968 function payment_terms_list($name, $selected_id)
969 {
970         $sql = "SELECT terms_indicator, terms FROM ".TB_PREF."payment_terms";
971         combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
972 }
973
974 function payment_terms_list_cells($label, $name, $selected_id)
975 {
976         if ($label != null)
977                 echo "<td>$label</td>\n";
978         echo "<td>";
979         payment_terms_list($name, $selected_id);
980         echo "</td>\n";
981 }
982
983 function payment_terms_list_row($label, $name, $selected_id)
984 {
985         echo "<tr>\n";
986         payment_terms_list_cells($label, $name, $selected_id);
987         echo "</tr>\n";
988 }
989
990 //------------------------------------------------------------------------------------
991
992 function credit_status_list($name, $selected_id)
993 {
994         $sql ="SELECT id, reason_description FROM ".TB_PREF."credit_status";
995         combo_input($name, $selected_id, $sql, 'id', 'reason_description', array());
996 }
997
998 function credit_status_list_cells($label, $name, $selected_id)
999 {
1000         if ($label != null)
1001                 echo "<td>$label</td>\n";
1002         echo "<td>";
1003         credit_status_list($name, $selected_id);
1004         echo "</td>\n";
1005 }
1006
1007 function credit_status_list_row($label, $name, $selected_id)
1008 {
1009         echo "<tr>\n";
1010         credit_status_list_cells($label, $name, $selected_id);
1011         echo "</tr>\n";
1012 }
1013
1014 //-----------------------------------------------------------------------------------------------
1015
1016 function sales_types_list($name, $selected_id, $submit_on_change=false, $special_option=false)
1017 {
1018         global $all_items;
1019
1020         $sql = "SELECT id, sales_type FROM ".TB_PREF."sales_types";
1021
1022         return combo_input($name, $selected_id, $sql, 'id', 'sales_type',
1023         array(
1024                 'spec_option' => $special_option===true ? _("All Sales Types") : $special_option,
1025                 'spec_id' => 0,
1026                 'select_submit'=> $submit_on_change,
1027         //        'async' => false,
1028         ) );
1029 }
1030
1031 function sales_types_list_cells($label, $name, $selected_id, $submit_on_change=false, $special_option=false)
1032 {
1033         if ($label != null)
1034                 echo "<td>$label</td>\n";
1035         echo "<td>";
1036         $str = sales_types_list($name, $selected_id, $submit_on_change, $special_option);
1037         echo "</td>\n";
1038         return $str;
1039 }
1040
1041 function sales_types_list_row($label, $name, $selected_id, $submit_on_change=false, $special_option=false)
1042 {
1043         echo "<tr>\n";
1044         $str = sales_types_list_cells($label, $name, $selected_id, $submit_on_change, $special_option);
1045         echo "</tr>\n";
1046         return $str;
1047 }
1048
1049 //-----------------------------------------------------------------------------------------------
1050
1051 function movement_types_list($name, $selected_id)
1052 {
1053         $sql = "SELECT id, name FROM ".TB_PREF."movement_types";
1054         combo_input($name, $selected_id, $sql, 'id', 'name', array());
1055 }
1056
1057 function movement_types_list_cells($label, $name, $selected_id)
1058 {
1059         if ($label != null)
1060                 echo "<td>$label</td>\n";
1061         echo "<td>";
1062         movement_types_list($name, $selected_id);
1063         echo "</td>\n";
1064 }
1065
1066 function movement_types_list_row($label, $name, $selected_id)
1067 {
1068         echo "<tr>\n";
1069         movement_types_list_cells($label, $name, $selected_id);
1070         echo "</tr>\n";
1071 }
1072
1073 //-----------------------------------------------------------------------------------------------
1074
1075 function bank_trans_types_list($name, $selected_id)
1076 {
1077         $sql = "SELECT id, name FROM ".TB_PREF."bank_trans_types";
1078         combo_input($name, $selected_id, $sql, 'id', 'name', array());
1079 }
1080
1081 function bank_trans_types_list_cells($label, $name, $selected_id)
1082 {
1083         if ($label != null)
1084                 echo "<td>$label</td>\n";
1085         echo "<td>";
1086         bank_trans_types_list($name, $selected_id);
1087         echo "</td>\n";
1088 }
1089
1090 function bank_trans_types_list_row($label, $name, $selected_id)
1091 {
1092         echo "<tr>\n";
1093         bank_trans_types_list_cells($label, $name, $selected_id);
1094         echo "</tr>\n";
1095 }
1096
1097 //-----------------------------------------------------------------------------------------------
1098
1099 function workcenter_list($name, $selected_id, $all_option=false)
1100 {
1101         global $all_items;
1102
1103         $sql = "SELECT id, name FROM ".TB_PREF."workcentres";
1104
1105         return combo_input($name, $selected_id, $sql, 'id', 'name',
1106         array(
1107                 'spec_option' =>$all_option===true ? _("All Suppliers") : $all_option,
1108                 'spec_id' => $all_items,
1109         ) );
1110 }
1111
1112 function workcenter_list_cells($label, $name, $selected_id, $all_option=false)
1113 {
1114         default_focus($name);
1115         if ($label != null)
1116                 echo "<td>$label</td>\n";
1117         echo "<td>";
1118         workcenter_list($name, $selected_id, $all_option);
1119         echo "</td>\n";
1120 }
1121
1122 function workcenter_list_row($label, $name, $selected_id, $all_option=false)
1123 {
1124         echo "<tr>\n";
1125         workcenter_list_cells($label, $name, $selected_id, $all_option);
1126         echo "</tr>\n";
1127 }
1128
1129 //-----------------------------------------------------------------------------------------------
1130
1131 function bank_accounts_list($name, $selected_id, $submit_on_change=false)
1132 {
1133         $sql = "SELECT ".TB_PREF."bank_accounts.account_code, bank_account_name, bank_curr_code
1134                 FROM ".TB_PREF."bank_accounts, ".TB_PREF."chart_master
1135                 WHERE ".TB_PREF."bank_accounts.account_code=".TB_PREF."chart_master.account_code";
1136
1137         return combo_input($name, $selected_id, $sql, 'account_code', 'bank_account_name',
1138         array(
1139                 'format' => '_format_add_curr',
1140                 'select_submit'=> $submit_on_change,
1141                 'async' => false
1142         ) );
1143 }
1144
1145 function bank_accounts_list_cells($label, $name, $selected_id, $submit_on_change=false)
1146 {
1147         if ($label != null)
1148                 echo "<td>$label</td>\n";
1149         echo "<td>";
1150         $str = bank_accounts_list($name, $selected_id, $submit_on_change);
1151         echo "</td>\n";
1152         return $str;
1153 }
1154
1155 function bank_accounts_list_row($label, $name, $selected_id, $submit_on_change=false)
1156 {
1157         echo "<tr>\n";
1158         $str = bank_accounts_list_cells($label, $name, $selected_id, $submit_on_change);
1159         echo "</tr>\n";
1160         return $str;
1161 }
1162
1163 //-----------------------------------------------------------------------------------------------
1164
1165 function class_list($name, $selected_id, $submit_on_change=false)
1166 {
1167         $sql = "SELECT cid, class_name FROM ".TB_PREF."chart_class";
1168
1169         return combo_input($name, $selected_id, $sql, 'cid', 'class_name',
1170         array(
1171                 'select_submit'=> $submit_on_change,
1172                 'async' => false
1173         ) );
1174
1175 }
1176
1177 function class_list_cells($label, $name, $selected_id, $submit_on_change=false)
1178 {
1179         if ($label != null)
1180                 echo "<td>$label</td>\n";
1181         echo "<td>";
1182         $str = class_list($name, $selected_id, $submit_on_change);
1183         echo "</td>\n";
1184         return $str;
1185 }
1186
1187 function class_list_row($label, $name, $selected_id, $submit_on_change=false)
1188 {
1189         echo "<tr>\n";
1190         $str = class_list_cells($label, $name, $selected_id, $submit_on_change);
1191         echo "</tr>\n";
1192         return $str;
1193 }
1194
1195 //-----------------------------------------------------------------------------------------------
1196
1197 function stock_categories_list($name, $selected_id)
1198 {
1199         $sql = "SELECT category_id, description FROM ".TB_PREF."stock_category";
1200         combo_input($name, $selected_id, $sql, 'category_id', 'description',
1201         array('order'=>'category_id'));
1202 }
1203
1204 function stock_categories_list_cells($label, $name, $selected_id)
1205 {
1206         if ($label != null)
1207                 echo "<td>$label</td>\n";
1208         echo "<td>";
1209         stock_categories_list($name, $selected_id);
1210         echo "</td>\n";
1211 }
1212
1213 function stock_categories_list_row($label, $name, $selected_id)
1214 {
1215         echo "<tr>\n";
1216         stock_categories_list_cells($label, $name, $selected_id);
1217         echo "</tr>\n";
1218 }
1219
1220 //-----------------------------------------------------------------------------------------------
1221
1222 function gl_account_types_list($name, $selected_id, $all_option, $all_option_numeric)
1223 {
1224         global $all_items;
1225
1226         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
1227
1228         combo_input($name, $selected_id, $sql, 'id', 'name',
1229         array(
1230                 'order' => 'id',
1231                 'spec_option' =>$all_option,
1232                 'spec_id' => $all_option_numeric ? 0 : $all_items
1233         ) );
1234 }
1235
1236 function gl_account_types_list_cells($label, $name, $selected_id, $all_option=false,
1237         $all_option_numeric=false)
1238 {
1239         if ($label != null)
1240                 echo "<td>$label</td>\n";
1241         echo "<td>";
1242         gl_account_types_list($name, $selected_id, $all_option, $all_option_numeric);
1243         echo "</td>\n";
1244 }
1245
1246 function gl_account_types_list_row($label, $name, $selected_id, $all_option=false,
1247         $all_option_numeric=false)
1248 {
1249         echo "<tr>\n";
1250         gl_account_types_list_cells($label, $name, $selected_id, $all_option,
1251                 $all_option_numeric);
1252         echo "</tr>\n";
1253 }
1254
1255 //-----------------------------------------------------------------------------------------------
1256 function gl_all_accounts_list($name, $selected_id, $skip_bank_accounts=false,
1257         $show_group=false, $cells=false)
1258 {
1259         if ($skip_bank_accounts)
1260                 $sql = "SELECT chart.account_code, chart.account_name, type.name
1261                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
1262                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
1263                         ."ON chart.account_code=acc.account_code
1264                                 WHERE acc.account_code IS NULL
1265                         AND chart.account_type=type.id";
1266         else
1267                 $sql = "SELECT chart.account_code, chart.account_name, type.name
1268                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type
1269                         WHERE chart.account_type=type.id";
1270
1271         combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1272         array(
1273                 'format' => '_format_account' .  ($show_group ? '2' : ''),
1274                 'order' => 'account_code',
1275                 'search_box' => $cells,
1276                         'search_submit' => false,
1277                         'size' => 12,
1278                         'max' => 10,
1279                         'cells' => true
1280         ) );
1281
1282 }
1283
1284 function _format_account($row)
1285 {
1286                 return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1287 }
1288
1289 function _format_account2($row)
1290 {
1291                 return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[2] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1292 }
1293
1294 function gl_all_accounts_list_cells($label, $name, $selected_id, $skip_bank_accounts=false,
1295         $show_group=false, $cells=false)
1296 {
1297         if ($label != null)
1298                 echo "<td>$label</td>\n";
1299         echo "<td>";
1300         gl_all_accounts_list($name, $selected_id, $skip_bank_accounts, $show_group, $cells);
1301         echo "</td>\n";
1302 }
1303
1304 function gl_all_accounts_list_row($label, $name, $selected_id, $skip_bank_accounts=false,
1305         $show_group=false, $cells=false)
1306 {
1307         echo "<tr>\n";
1308         gl_all_accounts_list_cells($label, $name, $selected_id, $skip_bank_accounts,
1309                 $show_group, $cells);
1310         echo "</tr>\n";
1311 }
1312
1313 function yesno_list($name, $selected_id, $name_yes="", $name_no="", $submit_on_change=false)
1314 {
1315         default_focus($name);
1316         if ($submit_on_change == true)
1317                 echo "<select name='$name' onchange='this.form.submit();'>"; // FIX ajax
1318         else
1319                 echo "<select name='$name'>";
1320
1321         if (strlen($name_yes) == 0)
1322         {
1323                 unset($name_yes);
1324         }
1325         if (strlen($name_no) == 0)
1326         {
1327                 unset($name_no);
1328         }
1329
1330         if ($selected_id == null)
1331                 $selected_id = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
1332         if ($selected_id == 0)
1333                 echo "<option value=1>";
1334         else
1335                 echo "<option selected value=1>";
1336         if (!isset($name_yes))
1337                 echo _("Yes") . "</option>\n";
1338         else
1339                 echo $name_yes . "</option>\n";
1340         if ($selected_id == 0)
1341                 echo "<option selected value=0>";
1342         else
1343                 echo "<option value=0>";
1344         if (!isset($name_no))
1345                 echo _("No") . "</option>\n";
1346         else
1347                 echo $name_no . "</option>\n";
1348         echo "</select>";
1349 }
1350
1351 function yesno_list_cells($label, $name, $selected_id, $name_yes="", $name_no="", $submit_on_change=false)
1352 {
1353         if ($label != null)
1354                 echo "<td>$label</td>\n";
1355         echo "<td>";
1356         $str = yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1357         echo "</td>\n";
1358         return $str;
1359 }
1360
1361 function yesno_list_row($label, $name, $selected_id, $name_yes="", $name_no="", $submit_on_change=false)
1362 {
1363         echo "<tr>\n";
1364         $str = yesno_list_cells($label, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1365         echo "</tr>\n";
1366         return $str;
1367 }
1368
1369 //------------------------------------------------------------------------------------------------
1370
1371 function languages_list($name, $selected_id)
1372 {
1373         global $installed_languages;
1374
1375         default_focus($name);
1376         echo "<select name='$name'>";
1377
1378         if ($selected_id == null)
1379                 $selected_id = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1380         foreach ($installed_languages as $lang)
1381         {
1382                 echo "<option ";
1383                 if ($selected_id == $lang['code'])
1384                         echo "selected ";
1385                 echo "value='" . $lang['code'] . "'>" . $lang['name'] . "</option>\n";
1386         }
1387 }
1388
1389 function languages_list_cells($label, $name, $selected_id)
1390 {
1391         if ($label != null)
1392                 echo "<td>$label</td>\n";
1393         echo "<td>";
1394         languages_list($name, $selected_id);
1395         echo "</td>\n";
1396 }
1397
1398 function languages_list_row($label, $name, $selected_id)
1399 {
1400         echo "<tr>\n";
1401         languages_list_cells($label, $name, $selected_id);
1402         echo "</tr>\n";
1403 }
1404
1405 //------------------------------------------------------------------------------------------------
1406
1407 function bank_account_types_list($name, $selected_id)
1408 {
1409         $bank_account_types = bank_account_types::get_all();
1410
1411         default_focus($name);
1412         echo "<select name='$name'>";
1413
1414         if ($selected_id == null)
1415                 $selected_id = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1416         foreach ($bank_account_types as $type)
1417         {
1418                 echo "<option ";
1419                 if ($selected_id == "" || $selected_id == $type['id'])
1420                         echo "selected ";
1421                 echo "value='" . $type['id'] . "'>" . $type['name'] . "</option>\n";
1422         }
1423 }
1424
1425 function bank_account_types_list_cells($label, $name, $selected_id)
1426 {
1427         if ($label != null)
1428                 echo "<td>$label</td>\n";
1429         echo "<td>";
1430         bank_account_types_list($name, $selected_id);
1431         echo "</td>\n";
1432 }
1433
1434 function bank_account_types_list_row($label, $name, $selected_id)
1435 {
1436         echo "<tr>\n";
1437         bank_account_types_list_cells($label, $name, $selected_id);
1438         echo "</tr>\n";
1439 }
1440
1441 //------------------------------------------------------------------------------------------------
1442
1443 function payment_person_types_list($name, $selected_id, $related=null)
1444 {
1445         $types = payment_person_types::get_all();
1446
1447         default_focus($name);
1448         echo "<select name='$name'";
1449         if ($related)   // FIX ajax
1450                 echo " onchange='this.form.$related.value=\"\"; this.form.submit();' ";
1451         echo ">";
1452
1453         if ($selected_id == null)
1454                 $selected_id = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1455         foreach ($types as $type)
1456         {
1457                 if (payment_person_types::has_items($type['id']))
1458                 {
1459                         if ($selected_id == "")
1460                                 $_POST[$name] = $selected_id = $type['id'];
1461                         echo "<option ";
1462                         if ($selected_id == $type['id'])
1463                                 echo "selected ";
1464                         echo "value='" . $type['id'] . "'>" . $type['name'] . "</option>\n";
1465                 }
1466         }
1467 }
1468
1469 function payment_person_types_list_cells($label, $name, $selected_id, $related=null)
1470 {
1471         if ($label != null)
1472                 echo "<td>$label</td>\n";
1473         echo "<td>";
1474         $str = payment_person_types_list($name, $selected_id, $related);
1475         echo "</td>\n";
1476         return $str;
1477 }
1478
1479 function payment_person_types_list_row($label, $name, $selected_id, $related=null)
1480 {
1481         echo "<tr>\n";
1482         $str = payment_person_types_list_cells($label, $name, $selected_id, $related);
1483         echo "</tr>\n";
1484         return $str;
1485 }
1486
1487 //------------------------------------------------------------------------------------------------
1488
1489 function wo_types_list($name, $selected_id)
1490 {
1491         $types = wo_types::get_all();
1492
1493         default_focus($name);   // FIX ajax
1494         echo "<select name='$name' onchange='this.form.submit();'>";
1495
1496         if ($selected_id == null)
1497                 $selected_id = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1498         foreach ($types as $type)
1499         {
1500                 echo "<option ";
1501                 if ($selected_id == $type['id'])
1502                         echo "selected ";
1503                 echo "value='" . $type['id'] . "'>" . $type['name'] . "</option>\n";
1504         }
1505         echo "</select>";
1506 }
1507
1508 function wo_types_list_row($label, $name, $selected_id)
1509 {
1510         echo "<tr><td>$label</td><td>\n";
1511         $str = wo_types_list($name, $selected_id);
1512         echo "</td></tr>\n";
1513         return $str;
1514 }
1515
1516 //------------------------------------------------------------------------------------------------
1517
1518 function dateformats_list_row($label, $name, $value)
1519 {
1520         global $dateformats;
1521
1522         default_focus($name);
1523         echo "<tr><td>$label</td>\n";
1524         echo "<td><select name='$name'>";
1525
1526         if ($value == null)
1527                 $value = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
1528         $counter = 0;
1529         foreach ($dateformats as $df)
1530         {
1531
1532                 if ($value==$counter)
1533                 {
1534                         echo "<option selected value='$counter'>$df</option>\n";
1535                 }
1536                 else
1537                 {
1538                         echo "<option value='$counter'>$df</option>\n";
1539                 }
1540                 $counter++;
1541         }
1542         echo "</select></td></tr>\n";
1543 }
1544
1545 function dateseps_list_row($label, $name, $value)
1546 {
1547         global $dateseps;
1548
1549         default_focus($name);
1550         echo "<tr><td>$label</td>\n";
1551         echo "<td><select name='$name'>";
1552
1553         if ($value == null)
1554                 $value = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
1555         $counter = 0;
1556         foreach ($dateseps as $ds)
1557         {
1558
1559                 if ($value==$counter)
1560                 {
1561                         echo "<option selected value='$counter'>$ds</option>\n";
1562                 }
1563                 else
1564                 {
1565                         echo "<option value='$counter'>$ds</option>\n";
1566                 }
1567                 $counter++;
1568         }
1569         echo "</select></td></tr>\n";
1570 }
1571
1572 function thoseps_list_row($label, $name, $value)
1573 {
1574         global $thoseps;
1575
1576         default_focus($name);
1577         echo "<tr><td>$label</td>\n";
1578         echo "<td><select name='$name'>";
1579
1580         if ($value == null)
1581                 $value = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
1582         $counter = 0;
1583         foreach ($thoseps as $ts)
1584         {
1585
1586                 if ($value==$counter)
1587                 {
1588                         echo "<option selected value='$counter'>$ts</option>\n";
1589                 }
1590                 else
1591                 {
1592                         echo "<option value='$counter'>$ts</option>\n";
1593                 }
1594                 $counter++;
1595         }
1596         echo "</select></td></tr>\n";
1597 }
1598
1599 function decseps_list_row($label, $name, $value)
1600 {
1601         global $decseps;
1602
1603         default_focus($name);
1604         echo "<tr><td>$label</td>\n";
1605         echo "<td><select name='$name'>";
1606
1607         if ($value == null)
1608                 $value = (!isset($_POST[$name]) ? 0 : $_POST[$name]);
1609         $counter = 0;
1610         foreach ($decseps as $ds)
1611         {
1612
1613                 if ($value==$counter)
1614                 {
1615                         echo "<option selected value='$counter'>$ds</option>\n";
1616                 }
1617                 else
1618                 {
1619                         echo "<option value='$counter'>$ds</option>\n";
1620                 }
1621                 $counter++;
1622         }
1623         echo "</select></td></tr>\n";
1624 }
1625
1626 function themes_list_row($label, $name, $value)
1627 {
1628         global $path_to_root;
1629
1630         default_focus($name);
1631         $path = $path_to_root.'/themes/';
1632         $themes = array();
1633         $themedir = opendir($path);
1634         while(false !== ($fname = readdir($themedir)))
1635         {
1636                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
1637                 {
1638                         $themes[] =  $fname;
1639                 }
1640         }
1641         sort($themes);
1642         echo "<tr><td>$label</td>\n";
1643         echo "<td><select name='$name'>";
1644
1645         if ($value == null)
1646                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1647         foreach ($themes as $th)
1648         {
1649
1650                 if ($value==$th)
1651                 {
1652                         echo "<option selected value='$th'>$th</option>\n";
1653                 }
1654                 else
1655                 {
1656                         echo "<option value='$th'>$th</option>\n";
1657                 }
1658         }
1659         echo "</select></td></tr>\n";
1660 }
1661
1662 function pagesizes_list_row($label, $name, $value)
1663 {
1664         global $pagesizes;
1665
1666         default_focus($name);
1667         echo "<tr><td>$label</td>\n";
1668         echo "<td><select name='$name'>";
1669
1670         if ($value == null)
1671                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1672         foreach ($pagesizes as $pz)
1673         {
1674
1675                 if ($value==$pz)
1676                 {
1677                         echo "<option selected value='$pz'>$pz</option>\n";
1678                 }
1679                 else
1680                 {
1681                         echo "<option value='$pz'>$pz</option>\n";
1682                 }
1683         }
1684         echo "</select></td></tr>\n";
1685 }
1686
1687 function security_headings_list_row($label, $name, $value)
1688 {
1689         global $security_headings;
1690
1691         default_focus($name);
1692         echo "<tr><td>$label</td>\n";
1693         echo "<td><select name='$name'>";
1694
1695         if ($value == null)
1696                 $value = (!isset($_POST[$name]) ? 0 : (int)$_POST[$name]);
1697         $counter=0;
1698         foreach ($security_headings as $sh)
1699         {
1700
1701                 if ($value==$counter)
1702                 {
1703                         echo "<option selected value='$counter'>$sh</option>\n";
1704                 }
1705                 else
1706                 {
1707                         echo "<option value='$counter'>$sh</option>\n";
1708                 }
1709                 $counter++;
1710         }
1711         echo "</select></td></tr>\n";
1712 }
1713
1714 function systypes_list_cells($label, $name, $value, $submit_on_change=false)
1715 {
1716         global $systypes_array;
1717
1718         default_focus($name);
1719         if ($label != null)
1720                 echo "<td>$label</td>\n";
1721         echo "<td><select name='$name'";
1722         if ($submit_on_change)
1723                 echo " onchange='this.form.submit();'>";        // FIX ajax
1724         else
1725                 echo ">";
1726         if ($value == null)
1727                 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1728         foreach ($systypes_array as $key=>$type)
1729         {
1730
1731                 if ($value==$key)
1732                 {
1733                         echo "<option selected value='$key'>".$type['name']."</option>\n";
1734                 }
1735                 else
1736                 {
1737                         echo "<option value='$key'>".$type['name']."</option>\n";
1738                 }
1739         }
1740         echo "</select></td>\n";
1741 }
1742
1743 function systypes_list_row($label, $name, $value, $submit_on_change=false)
1744 {
1745         echo "<tr>\n";
1746         $str = systypes_list_cells($label, $name, $value, $submit_on_change);
1747         echo "</tr>\n";
1748         return $str;
1749 }
1750
1751 function cust_allocations_list_cells($label, $name, $selected)
1752 {
1753         global $all_items;
1754         default_focus($name);
1755         if ($label != null)
1756                 label_cell($label);
1757         if ($selected == null)
1758                 $selected = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1759         echo "<td><select name='$name'>";
1760         echo "<option " . ($selected == $all_items ? " selected " : "") . " value='$all_items'>" . _("All Types"). "</option>\n";
1761         echo "<option " . ($selected == '1'?" selected ":"") . " value='1'>" . _("Sales Invoices"). "</option>\n";
1762         echo "<option " . ($selected == '2'?" selected ":"") . " value='2'>" . _("Overdue Invoices"). "</option>\n";
1763         echo "<option " . ($selected == '3'?" selected ":"") . " value='3'>" . _("Payments"). "</option>\n";
1764         echo "<option " . ($selected == '4'?" selected ":"") . " value='4'>" . _("Credit Notes"). "</option>\n";
1765         echo "<option " . ($selected == '5'?" selected ":"") . " value='5'>" . _("Delivery Notes"). "</option>\n";
1766         echo "</select></td>\n";
1767 }
1768
1769 function supp_allocations_list_cells($name, $selected)
1770 {
1771         global $all_items;
1772
1773         default_focus($name);
1774         if ($selected == null)
1775                 $selected = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1776         echo "<td><select name='$name'>";
1777         echo "<option " . ($selected == $all_items ? " selected " : "") . " value='$all_items'>" . _("All Types"). "</option>\n";
1778         echo "<option " . ($selected == '1'?" selected ":"") . " value='1'>" . _("Invoices"). "</option>\n";
1779         echo "<option " . ($selected == '2'?" selected ":"") . " value='2'>" . _("Overdue Invoices"). "</option>\n";
1780         echo "<option " . ($selected == '3'?" selected ":"") . " value='3'>" . _("Payments"). "</option>\n";
1781         echo "<option " . ($selected == '4'?" selected ":"") . " value='4'>" . _("Credit Notes"). "</option>\n";
1782         echo "<option " . ($selected == '5'?" selected ":"") . " value='5'>" . _("Overdue Credit Notes"). "</option>\n";
1783         echo "</select></td>\n";
1784 }
1785
1786 function policy_list_cells($label, $name, $selected)
1787 {
1788         default_focus($name);
1789         if ($selected == null)
1790         {
1791                 $selected = (!isset($_POST[$name]) ? "" : $_POST[$name]);
1792                 if ($selected == "")
1793                         $_POST[$name] = $selected;
1794         }
1795         if ($label != null)
1796                 label_cell($label);
1797         echo "<td><select name='$name'>";
1798         echo "<option " . ($selected == ''?" selected ":"") . " value=''>" . _("Automatically put balance on back order"). "</option>\n";
1799         echo "<option " . ($selected == 'CAN'?" selected ":"") . " value='CAN'>" . _("Cancel any quantites not delivered"). "</option>\n";
1800         echo "</select></td>\n";
1801 }
1802
1803 function policy_list_row($label, $name, $selected)
1804 {
1805         echo "<tr>\n";
1806         policy_list_cells($label, $name, $selected);
1807         echo "</tr>\n";
1808 }
1809
1810 function credit_type_list_cells($label, $name, $selected, $submit_on_change=false)
1811 {
1812         default_focus($name);
1813         if ($selected == null)
1814         {
1815                 $selected = (!isset($_POST[$name]) ? "Return" : $_POST[$name]);
1816                 if ($selected == "Return")
1817                         $_POST[$name] = $selected;
1818         }
1819         if ($label != null)
1820                 label_cell($label);
1821         echo "<td><select name='$name'";
1822         if ($submit_on_change)
1823                 echo " onchange='this.form.submit();'>";        // FIX ajax
1824         else
1825                 echo ">";
1826         echo "<option " . ($selected == 'Return'?" selected ":"") . " value='Return'>" . _("Items Returned to Inventory Location"). "</option>\n";
1827         echo "<option " . ($selected == 'WriteOff'?" selected ":"") . " value='WriteOff'>" . _("Items Written Off"). "</option>\n";
1828         echo "</select></td>\n";
1829 }
1830
1831 function credit_type_list_row($label, $name, $selected, $submit_on_change=false)
1832 {
1833         echo "<tr>\n";
1834         $str = credit_type_list_cells($label, $name, $selected, $submit_on_change);
1835         echo "</tr>\n";
1836         return $str;
1837 }
1838
1839 function number_list($name, $selected, $from, $to, $firstlabel="")
1840 {
1841         default_focus($name);
1842         if ($selected == null)
1843         {
1844                 $selected = (!isset($_POST[$name]) ? $from : $_POST[$name]);
1845                 if ($selected == $from)
1846                         $_POST[$name] = $selected;
1847         }
1848         echo "<select name='$name'>";
1849         for ($i = $from; $i <= $to; $i++)
1850         {
1851                 if ($i == 0 && $firstlabel != "")
1852                         $label = $firstlabel;
1853                 else
1854                         $label = $i;
1855                 if ($selected == $i)
1856                 {
1857                         echo "<option selected value='$i'>$label</option>\n";
1858                 }
1859                 else
1860                 {
1861                         echo "<option value='$i'>$label</option>\n";
1862                 }
1863         }
1864         echo "</select>\n";
1865 }
1866
1867 function number_list_cells($label, $name, $selected, $from, $to)
1868 {
1869         if ($label != null)
1870                 label_cell($label);
1871         echo "<td>\n";
1872         number_list($name, $selected, $from, $to);
1873         echo "</td>\n";
1874 }
1875
1876 function number_list_row($label, $name, $selected, $from, $to)
1877 {
1878         echo "<tr>\n";
1879         number_list_cells($label, $name, $selected, $from, $to);
1880         echo "</tr>\n";
1881 }
1882 ?>