Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / elements / customer-table.html
1 <%doc>
2
3 Example:
4
5   include( '/elements/customer-table.html',
6
7              ###
8              # required
9              ###
10
11              #listrefs...
12              'header'        => [ '#', 'Item' ],
13              'fields'        => [
14                                   'column',
15                                   sub { my ($row,$param) = @_;
16                                         $param->{"column$row"};
17                                       },
18                                 ],
19
20              ###
21              # optional
22              ###
23
24              'name_singular' => 'customer', #label
25              'custnum_update_callback' => 'name_of_js_callback' #passed a rownum
26
27              #listrefs
28              'types'         => ['immutable', ''], # immutable or ''/text
29              'align'         => [ 'c', 'l', 'r', '' ],
30              'size'          => [],                # sizes ignored for immutable
31              'color'         => [],
32              'footer'        => ['string', '_TOTAL'], # strings or the special
33                                                       #value _TOTAL
34              'footer_align'  => [ 'c', 'l', 'r', '' ],
35
36              'param'         => { column0 => 1 },  # preset column of row 0 to 1
37
38          )
39
40 </%doc>
41
42 <SCRIPT TYPE="text/javascript">
43
44   var num_open_invoices = new Array;
45
46   function clearhint_invnum() {
47
48     if ( this.value == 'Not found' || this.value == 'Multiple' ) {
49       this.value = '';
50       this.style.color = '#000000';
51     }
52
53   }
54
55   function clearhint_custnum() {
56
57     if ( this.value == 'Not found' || this.value == 'Multiple' ) {
58       this.value = '';
59       this.style.color = '#000000';
60     }
61
62   }
63
64   function clearhint_customer() {
65
66     this.style.color = '#000000';
67
68     if ( this.value == '(last name or company)' || this.value == 'Not found' )
69       this.value = '';
70
71   }
72
73   function update_customer(searchrow, customerArray) {
74       
75       var custnum_obj = document.getElementById('custnum'+searchrow);
76       var customer = document.getElementById('customer'+searchrow);
77       var customer_select = document.getElementById('cust_select'+searchrow);
78
79       custnum_obj.disabled = false;
80       custnum_obj.style.backgroundColor = '#ffffff';
81       customer.disabled = false;
82       customer.style.backgroundColor = '#ffffff';
83
84       if ( customerArray.length == 0 ) {
85
86           custnum_obj.value = 'Not found';
87           customer.value = 'Not found';
88           custnum_obj.style.color = '#ff0000';
89           customer.style.color = '#ff0000';
90
91           customer.style.display = '';
92           customer_select.style.display = 'none';
93           return false;
94
95       } else if ( customerArray.length == 6 ) {
96
97           custnum_obj.value = customerArray[0];
98           custnum_obj.style.color = '#000000';
99           customer.value = customerArray[1];
100
101           update_balance_text(searchrow, customerArray[2]);
102           update_status_text( searchrow, customerArray[3]);
103           update_status_color(searchrow, '#'+customerArray[4]);
104           update_num_open(searchrow, customerArray[5]);
105
106           customer.style.display = '';
107           customer_select.style.display = 'none';
108           return true;
109       }
110   }
111
112   function <% $opt{prefix} %>search_invnum() {
113
114     this.style.color = '#000000'
115
116     var invnum_obj = this;
117     var searchrow = this.getAttribute('rownum');
118     var invnum = this.value;
119
120     if ( invnum == 'searching...' || invnum == 'Not found' || invnum == '' )
121       return;
122
123     if ( this.getAttribute('magic') == 'nosearch' ) {
124       this.setAttribute('magic', '');
125       return;
126     }
127
128     if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
129       <% $opt{prefix} %>addRow();
130     }
131     var customer = document.getElementById('customer'+searchrow);
132     customer.value = 'searching...';
133     customer.disabled = true;
134     customer.style.color = '#000000';
135     customer.style.backgroundColor = '#dddddd';
136
137     var customer_select = document.getElementById('cust_select'+searchrow);
138
139     customer.style.display = '';
140     customer_select.style.display = 'none';
141     
142     var custnum_obj = document.getElementById('custnum'+searchrow);
143     update_balance_text(searchrow, '');
144     update_status_text(searchrow, '');
145     update_status_color(searchrow, '#000000');
146     update_num_open(searchrow, 0);
147
148     function search_invnum_update(customers) {
149       
150       var customerArray = eval('(' + customers + ')');
151       update_customer(searchrow, customerArray);
152
153 % if ( $opt{invnum_update_callback} ) {
154         <% $opt{invnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
155 % }
156
157     }
158
159     invnum_search( invnum, search_invnum_update );
160
161   }
162
163   function <% $opt{prefix} %>search_custnum() {
164
165     this.style.color = '#000000'
166
167     var custnum_obj = this;
168     var searchrow = this.getAttribute('rownum');
169     var custnum = this.value;
170
171     if ( custnum == 'searching...' || custnum == 'Not found' || custnum == '' )
172       return;
173
174     if ( this.getAttribute('magic') == 'nosearch' ) {
175       this.setAttribute('magic', '');
176       return;
177     }
178
179     if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
180       <% $opt{prefix} %>addRow();
181     }
182
183     var customer_obj = document.getElementById('customer'+searchrow);
184     customer_obj.value = 'searching...';
185     customer_obj.disabled = true;
186     customer_obj.style.color = '#000000';
187     customer_obj.style.backgroundColor = '#dddddd';
188
189     var customer_select = document.getElementById('cust_select'+searchrow);
190
191     customer_obj.style.display = '';
192     customer_select.style.display = 'none';
193
194     var invnum = document.getElementById('invnum'+searchrow);
195     invnum.value = '';
196
197     update_balance_text(searchrow, '');
198     update_status_text( searchrow, '');
199     update_status_color(searchrow, '#000000');    
200     update_num_open(searchrow, 0);
201
202     function search_custnum_update(customers) {
203
204       var customerArrayArray = eval('(' + customers + ')') || [];
205
206       if ( customerArrayArray.length == 1 ) {
207
208         update_customer(searchrow, customerArrayArray[0]);
209 % if ( $opt{custnum_update_callback} ) {
210           <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
211 % }
212
213       } else {
214
215         custnum_obj.value = 'Multiple'; // or something
216         custnum_obj.style.color = '#ff0000';
217
218         //blank the current list
219         customer_select.options.length = 0;
220
221         opt(customer_select, '', 'Multiple customers match "' + custnum + '" - select one', '#ff0000');
222         //add the multiple customers
223         for ( var s = 0; s < customerArrayArray.length; s++ ) {
224           opt(customer_select,
225               JSON.stringify(customerArrayArray[s]),
226               customerArrayArray[s][1],
227               '#000000');
228         }
229
230         opt(customer_select, 'cancel', '(Edit search string)', '#000000');
231
232         customer_obj.style.display = 'none';
233
234         customer_select.style.display = '';
235
236       }
237
238     }
239
240     custnum_search(custnum, search_custnum_update );
241
242   }
243
244   function <% $opt{prefix} %>search_customer() {
245
246     var customer_obj = this;
247     var searchrow = this.getAttribute('rownum');
248     var customer = this.value;
249
250     if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
251       return;
252
253     if ( this.getAttribute('magic') == 'nosearch' ) {
254       this.setAttribute('magic', '');
255       return;
256     }
257
258     if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
259       <% $opt{prefix} %>addRow();
260     }
261     
262     var invnum = document.getElementById('invnum'+searchrow);
263     invnum.value = '';
264
265     var custnum_obj = document.getElementById('custnum'+searchrow);
266     custnum_obj.value = 'searching...';
267     custnum_obj.disabled = true;
268     custnum_obj.style.color = '#000000';
269     custnum_obj.style.backgroundColor = '#dddddd';
270
271     var customer_select = document.getElementById('cust_select'+searchrow);
272     
273     function search_customer_update(customers) {
274
275       var customerArrayArray = eval('(' + customers + ')') || [ [] ];
276
277       custnum_obj.disabled = false;
278       custnum_obj.style.backgroundColor = '#ffffff';
279
280       if ( customerArrayArray.length == 1 ) {
281
282         update_customer(searchrow, customerArrayArray[0]);
283 % if ( $opt{custnum_update_callback} ) {
284         <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
285 % }
286
287       } else {
288
289         custnum_obj.value = 'Multiple'; // or something
290         custnum_obj.style.color = '#ff0000';
291
292         //blank the current list
293         customer_select.options.length = 0;
294
295         opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
296         //add the multiple customers
297         for ( var s = 0; s < customerArrayArray.length; s++ ) {
298           opt(customer_select,
299               JSON.stringify(customerArrayArray[s]),
300               customerArrayArray[s][1],
301               '#000000');
302         }
303
304         opt(customer_select, 'cancel', '(Edit search string)', '#000000');
305
306         customer_obj.style.display = 'none';
307
308         customer_select.style.display = '';
309
310       }
311
312     }
313
314     smart_search( customer, search_customer_update );
315
316   }
317
318   function select_customer() {
319
320     var custnum_balance_status = this.options[this.selectedIndex].value;
321     var customer = this.options[this.selectedIndex].text;
322
323     var searchrow = this.getAttribute('rownum');
324     var custnum_obj = document.getElementById('custnum'+searchrow);
325     var customer_obj = document.getElementById('customer'+searchrow);
326     var balance_obj = document.getElementById('balance'+searchrow);
327     var status_obj = document.getElementById('status'+searchrow);
328
329     if ( custnum_balance_status == '' ) {
330
331     } else if ( custnum_balance_status == 'cancel' ) {
332
333       custnum_obj.value = '';
334       custnum_obj.style.color = '#000000';
335
336       this.style.display = 'none';
337       customer_obj.style.display = '';
338       customer_obj.focus();
339
340     } else {
341     
342       update_customer(searchrow, JSON.parse(custnum_balance_status));
343
344 % if ( $opt{custnum_update_callback} ) {
345       <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
346 % }
347
348     }
349
350   }
351
352   function opt(what,value,text,color) {
353     var optionName = new Option(text, value, false, false);
354     optionName.style.color = color;
355     var length = what.length;
356     what.options[length] = optionName;
357   }
358
359   function update_status_text(rownum, newval) {
360     document.getElementById('status'+rownum).value = newval;
361     document.getElementById('status'+rownum+'_text').innerHTML = newval;
362   }
363
364   function update_status_color(rownum, newval) {
365     document.getElementById('statuscolor'+rownum).value = newval;
366     document.getElementById('status'+rownum+'_text').style.color = newval;
367   }
368
369   function update_balance_text(rownum, newval) {
370     document.getElementById('balance'+rownum).value = newval;
371     document.getElementById('balance'+rownum+'_text').innerHTML = newval;
372   }
373
374   function update_num_open(rownum, newval) {
375     num_open_invoices[rownum] = newval;
376   }
377
378
379 </SCRIPT>
380
381 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
382
383 <TR>
384   <TH>Inv #</TH>
385   <TH>Cust #</TH>
386   <TH>Status</TH>
387   <TH>Customer</TH>
388   <TH>Balance</TH>
389 % foreach my $header ( @{$opt{header}} ) {
390     <TH><% $header %></TH>
391 % }
392 </TR>
393 % my $row = 0;
394 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) { 
395
396     <TR id="row<%$row%>" rownum="<%$row%>">
397       <TD>
398         <INPUT TYPE      = "text"
399                NAME      = "invnum<% $row %>"
400                ID        = "invnum<% $row %>"
401                SIZE      = 8
402                MAXLENGTH = 12
403                STYLE     = "text-align:right;"
404                VALUE     = "<% $param->{"invnum$row"} %>"
405                rownum    = "<% $row %>"
406         >
407         <SCRIPT TYPE="text/javascript">
408           var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
409           invnum_input<% $row %>.onfocus = clearhint_invnum;
410           invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
411         </SCRIPT>
412       </TD>
413
414       <TD>
415         <INPUT TYPE      = "text"
416                NAME      = "custnum<% $row %>"
417                ID        = "custnum<% $row %>"
418                SIZE      = 8
419                MAXLENGTH = 12
420                STYLE     = "text-align:right;"
421                VALUE     = "<% $param->{"custnum$row"} %>"
422                rownum    = "<% $row %>"
423         >
424         <SCRIPT TYPE="text/javascript">
425           var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
426           custnum_input<% $row %>.onfocus = clearhint_custnum;
427           custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
428         </SCRIPT>
429       </TD>
430       
431       <TD STYLE="text-align: center">
432         <SPAN
433                ID        = "status<% $row %>_text"
434                rownum    = "<% $row %>"
435                STYLE     = "font-weight: bold;
436                             color: <%$param->{"statuscolor$row"} || '#000000'%>"
437
438         ><% $param->{"status$row"} %></SPAN>
439         <INPUT TYPE      = "hidden"
440                NAME      = "status<% $row %>"
441                ID        = "status<% $row %>"
442                VALUE     = "<% $param->{"status$row"} %>"
443                rownum    = "<% $row %>"
444         >
445         <INPUT TYPE      = "hidden"
446                NAME      = "statuscolor<% $row %>"
447                ID        = "statuscolor<% $row %>"
448                VALUE     = "<% $param->{"statuscolor$row"} %>"
449                rownum    = "<% $row %>"
450         >
451       </TD>
452
453       <TD>
454         <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
455           <SCRIPT TYPE="text/javascript">
456             var customer_input<% $row %> = document.getElementById("customer<% $row %>");
457             customer_input<% $row %>.onfocus = clearhint_customer;
458             customer_input<% $row %>.onclick = clearhint_customer;
459             customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
460           </SCRIPT>
461         <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
462         </SELECT>
463           <SCRIPT TYPE="text/javascript">
464             var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
465             customer_select<% $row %>.onchange = select_customer;
466           </SCRIPT>
467       </TD>
468
469       <TD STYLE="text-align:right">
470         <% $money_char %>
471         <SPAN 
472                ID        = "balance<% $row %>_text"
473                rownum    = "<% $row %>"
474         ><% $param->{"balance$row"} %></SPAN>
475         &nbsp;
476         <INPUT TYPE      = "hidden"
477                NAME      = "balance<% $row %>"
478                ID        = "balance<% $row %>"
479                VALUE     = "<% $param->{"balance$row"} %>"
480                rownum    = "<% $row %>"
481         >
482       </TD>
483
484 %   my $col = 0;
485 %   foreach my $field ( @{$opt{fields}} ) {
486 %     my $value;
487 %     if ( ref($field) eq 'CODE' ) {
488 %       $value = &{$field}($row,$param);
489 %     } else {
490 %       $value = $param->{"$field$row"}; 
491 %     }
492 %     my $name  = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
493 %     my $align = $align{ $opt{align}->[$col] || 'l' };
494 %     my $size  = $sizes->[$col]  || 10;
495 %     my $color = $opt{color}->[$col];
496 %     my $font = $color ? qq(<FONT COLOR="$color">) : '';
497 %     my $onchange = '';
498 %     if ( $opt{onchange}->[$col] ) {
499 %       $onchange = 'onchange="'.$opt{onchange}->[$col].'"';
500 %     }
501 %     elsif ( $opt{footer}->[$col] eq '_TOTAL' ) {
502 %       $total[$col] += $value;
503 %       $onchange = $opt{prefix}. "calc_total$col();";
504 %       $onchange = qq(onchange="$onchange" onkeyup="$onchange");
505 %     }
506       <TD ALIGN="<% $align %>">
507 %     my $type = $types->[$col] || 'text';
508 %     if ($type eq 'text' or $type eq 'checkbox') {
509         <INPUT TYPE  = "<% $type %>"
510                NAME  = "<% $name %>"
511                ID    = "<% $name %>"
512                SIZE  = "<% $size %>"
513                STYLE = "text-align: <% $align %>;"
514                VALUE = "<% $value %>"
515                rownum    = "<% $row %>"
516                <% $onchange %>
517         >
518 %     } elsif ($types->[$col] eq 'immutable') {
519         <% $font %><% $value %><% $font ? '</FONT>' : '' %>
520         <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
521 %     } else {
522         Cannot represent unknown type: <% $types->[$col] %>
523 %     }
524       </TD>
525 %     $col++;
526 %   }
527     </TR>
528 % } 
529
530 <TR id="row_total">
531   <TH COLSPAN=5 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
532     Total <% $row ? $row-1 : 0 %>
533     <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
534   </TH>
535 % my $col = 0;
536 % foreach my $footer ( @{$opt{footer}} ) {
537 %   my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
538 %   if ($footer eq '_TOTAL' ) {
539 %     my $id = $opt{'fields'}->[$col];
540 %     $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
541       <TH ALIGN="<% $align %>" ID="<% $id %>">&nbsp;<% sprintf('%.2f', $total[$col] ) %></TH>
542 %   } else {
543       <TH ALIGN="<% $align %>"><% $footer %></TH>
544 %   }
545 %   $col++;
546 % }
547 </TR>
548
549 </TABLE>
550
551 <SCRIPT TYPE="text/javascript">
552 % my $col = 0;
553 % foreach my $footer ( @{$opt{footer}} ) {
554 %   if ($footer eq '_TOTAL' ) {
555 %     my $name = $opt{fields}->[$col];
556 %     $name = ref($name) ? "column$col" : $name;
557       var <% $opt{prefix}.$name %>_CACHE = new Array ();
558       var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
559       function <% $opt{prefix} %>calc_total<% $col %>() {
560         var row = 0;
561         var total = 0;
562         for ( var row = 0;
563               
564               ( <% $opt{prefix}.$name%>_CACHE[row] =
565                                         <% $opt{prefix}.$name%>_CACHE[row]
566                                      || document.getElementById("<%$name%>"+row)
567               ) != null;
568               
569               row++
570             )
571         {
572           var value = <%$name%>_CACHE[row].value;
573           value = parseFloat(value);
574           if ( ! isNaN(value) ) {
575             total = total + value;
576           }
577         }
578         <% $opt{prefix} %>th_el.innerHTML = '&nbsp;' + total.toFixed(2);
579
580       }
581 %   }
582 %   $col++;
583 % }
584 </SCRIPT>
585
586 <% include('/elements/xmlhttp.html',
587               'url'  => $p. 'misc/xmlhttp-cust_main-search.cgi',
588               'subs' => [qw( custnum_search smart_search invnum_search )],
589            )
590 %>
591
592 <SCRIPT TYPE="text/javascript">
593
594   var <% $opt{prefix} %>total_el =
595     document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
596
597   var <% $opt{prefix} %>rownum = <% $row %>;
598
599   function <% $opt{prefix} %>addRow() {
600
601     var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
602     var tablebody = table.getElementsByTagName('tbody').item(0);
603
604     var row = table.insertRow(table.rows.length - 1);
605     row.setAttribute('id', 'row'+rownum);
606     
607     var invnum_cell = document.createElement('TD');
608
609       var invnum_input = document.createElement('INPUT');
610       invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
611       invnum_input.setAttribute('id',   'invnum'+<% $opt{prefix} %>rownum);
612       invnum_input.style.textAlign = 'right';
613       invnum_input.setAttribute('size', 8);
614       invnum_input.setAttribute('maxlength', 12);
615       invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
616       invnum_input.onfocus = clearhint_invnum;
617       invnum_input.onchange = <% $opt{prefix} %>search_invnum;
618       invnum_cell.appendChild(invnum_input);
619
620     row.appendChild(invnum_cell);
621
622     var custnum_cell = document.createElement('TD');
623
624       var custnum_input = document.createElement('INPUT');
625       custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
626       custnum_input.setAttribute('id',   'custnum'+<% $opt{prefix} %>rownum);
627       custnum_input.style.textAlign = 'right';
628       custnum_input.setAttribute('size', 8);
629       custnum_input.setAttribute('maxlength', 12);
630       custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
631       custnum_input.onfocus = clearhint_custnum;
632       custnum_input.onchange = <% $opt{prefix} %>search_custnum;
633       custnum_cell.appendChild(custnum_input);
634
635     row.appendChild(custnum_cell);
636     
637     var status_cell = document.createElement('TD');
638       status_cell.style.textAlign = 'center';
639         
640       var status_span = document.createElement('SPAN');
641       status_span.setAttribute('id', 'status'+<% $opt{prefix} %>rownum+'_text');
642       status_span.style.fontWeight = 'bold';
643       status_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
644       status_cell.appendChild(status_span);
645         
646       var status_input = document.createElement('INPUT');
647       status_input.setAttribute('type', 'hidden');
648       status_input.setAttribute('name', 'status'+<% $opt{prefix} %>rownum);
649       status_input.setAttribute('id',   'status'+<% $opt{prefix} %>rownum);
650       status_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
651       status_cell.appendChild(status_input);
652
653       var statuscolor_input = document.createElement('INPUT');
654       statuscolor_input.setAttribute('type', 'hidden');
655       statuscolor_input.setAttribute('name', 'statuscolor'+<% $opt{prefix} %>rownum);
656       statuscolor_input.setAttribute('id',   'statuscolor'+<% $opt{prefix} %>rownum);
657       statuscolor_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
658       status_cell.appendChild(statuscolor_input);
659
660     row.appendChild(status_cell);
661
662     var customer_cell = document.createElement('TD');
663
664       var customer_input = document.createElement('INPUT');
665       customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
666       customer_input.setAttribute('id',   'customer'+<% $opt{prefix} %>rownum);
667       customer_input.setAttribute('size', 64);
668       customer_input.setAttribute('value', '(last name or company)' );
669       customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
670       customer_input.onfocus = clearhint_customer;
671       customer_input.onclick = clearhint_customer;
672       customer_input.onchange = <% $opt{prefix} %>search_customer;
673       customer_cell.appendChild(customer_input);
674
675       var customer_select = document.createElement('SELECT');
676       customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
677       customer_select.setAttribute('id',   'cust_select'+<% $opt{prefix} %>rownum);
678       customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
679       customer_select.style.color = '#ff0000';
680       customer_select.style.display = 'none';
681       customer_select.onchange = select_customer;
682       customer_cell.appendChild(customer_select);
683
684     row.appendChild(customer_cell);
685     
686     var balance_cell = document.createElement('TD');
687
688       balance_cell.style.textAlign = 'right';
689       balance_cell.appendChild(document.createTextNode('<%$money_char%>'));
690
691       var balance_span = document.createElement('SPAN');
692       balance_span.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum+'_text');
693       balance_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
694       balance_cell.appendChild(balance_span);
695
696       balance_cell.appendChild(
697         document.createTextNode(String.fromCharCode(160)) //&nbsp;
698       );
699
700       var balance_input = document.createElement('INPUT');
701       balance_input.setAttribute('type', 'hidden');
702       balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
703       balance_input.setAttribute('id',   'balance'+<% $opt{prefix} %>rownum);
704       balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
705       balance_cell.appendChild(balance_input);
706
707     row.appendChild(balance_cell);
708
709 %   my $col = 0;
710 %   foreach my $field ( @{$opt{fields}} ) {
711
712       var my_cell = document.createElement('TD');
713       my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
714
715 %     if ($types->[$col] eq 'immutable') {
716 %       my $value;
717 %       if ( ref($field) eq 'CODE' ) {
718 %         $value = &{$field}($row,$param);
719 %       } else {
720 %         $value = $param->{"$field$row"}; 
721 %       }
722         var my_text = document.createTextNode(<% $value |js_string %>);
723         my_cell.appendChild(my_text);
724 %     }
725
726 %     my $name  = (ref($field) eq 'CODE') ? "column${col}_" : $field;
727       var my_input = document.createElement('INPUT');
728       my_input.setAttribute('name', '<% $name %>'+<% $opt{prefix} %>rownum);
729       my_input.setAttribute('id',   '<% $name %>'+<% $opt{prefix} %>rownum);
730       my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
731       my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
732       my_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
733 %     if ( $types->[$col] eq 'immutable' ) {
734         my_input.setAttribute('type', 'hidden');
735 %     }
736 %     elsif ( $types->[$col] eq 'checkbox' ) {
737         my_input.setAttribute('type', 'checkbox');
738 %     }
739 %     if ( $opt{onchange}->[$col] ) {
740         my_input.onchange   = <% $opt{onchange}->[$col] %>;
741 %     }
742 %     elsif ( $opt{footer}->[$col] eq '_TOTAL' ) {
743         my_input.onchange   = <% $opt{prefix} %>calc_total<%$col%>;
744         my_input.onkeyup    = <% $opt{prefix} %>calc_total<%$col%>;
745 %     }
746       my_cell.appendChild(my_input);
747
748     row.appendChild(my_cell);
749
750 %     $col++;
751 %   }
752
753     //update the total # of rows display
754     if ( <% $opt{prefix} %>rownum == 1 ) {
755       <% $opt{prefix} %>total_el.innerHTML =
756         'Total '
757           + <% $opt{prefix} %>rownum
758           + ' <% $opt{name_singular} || 'customer' %>';
759     } else {
760       <% $opt{prefix} %>total_el.innerHTML =
761         'Total '
762           + <% $opt{prefix} %>rownum
763           + ' <% PL($opt{name_singular} || 'customer') %>';
764     }
765
766 % if ( $opt{add_row_callback} ) {
767     <% $opt{add_row_callback} %>(<% $opt{prefix} %>rownum,
768                                  '<% $opt{prefix} %>');
769 % }
770
771     <% $opt{prefix} %>rownum++;
772
773   }
774
775 % unless ($cgi->param('error')) {
776   <% $opt{prefix} %>addRow();
777 % }
778 </SCRIPT>
779
780 <%init>
781
782 my(%opt) = @_;
783 my $conf = new FS::Conf;
784
785 $opt{prefix} = '' unless defined $opt{prefix};
786 $opt{prefix} .= '_' if $opt{prefix};
787
788 my $types = $opt{'type'} ? [ @{$opt{'type'}} ] : [];
789 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
790
791 my $param = $opt{param};
792 $param = $cgi->Vars if $cgi->param('error');
793
794 $opt{$_} ||= [] foreach qw(align color footer footer_align);
795
796 my @total = map 0, @{$opt{footer}};
797
798 my %align = (
799   'l' => 'left',
800   'r' => 'right',
801   'c' => 'center',
802 );
803
804 my $money_char = $conf->config('money_char') || '$';
805 </%init>