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