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