RT#33410: Package GB add-ons
[freeside.git] / httemplate / misc / cust_pkg_usageprice.html
1 <%doc>
2 Sets up the xmlhttp, javascript and initial (empty) table for selecting cust_pkg_usageprice.
3 Available values are based on pkgpart, and can be updated when changing pkgpart
4 by passing the new pkgpart to the following javascript:
5
6   usageprice_pkg_changed( pkgpart, pkgnum )
7
8 The pkgnum input is optional, and will be used to set initial selected values.
9
10 If pkgpart is passed as an option to this element, will run usageprice_pkg_changed
11 once to initialize table;  pkgnum can be passed as an option along with this.
12
13 You can disable usageprice selection temporarily (remove the fields from the form)
14 with the javascript usageprice_disable(1), and restore it with usageprice_disable(0,pkgnum).
15 While disabled, calling usageprice_pkg_changed will have no effect.
16 </%doc>
17
18 <& /elements/xmlhttp.html,
19               'url'  => $p.'misc/xmlhttp-part_pkg_usageprice.html',
20               'subs' => [ 'get_part_pkg_usageprice' ],
21 &>
22
23 <FONT CLASS = "fsinnerbox-title" 
24       ID    = "cust_pkg_usageprice_title"
25       STYLE = "display:none"
26 ><% mt('Usage add-ons') |h %></FONT>
27 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0 ID="cust_pkg_usageprice_table">
28
29 </TABLE>
30
31 <SCRIPT>
32
33 var usagepriceCache = {};
34 var usagepriceDisabled = 0;
35
36 function usageprice_disable (disabled, pkgpart) {
37   if (disabled) {
38     usageprice_pkg_changed(0);
39     usagepriceDisabled = 1;
40   } else {
41     usagepriceDisabled = 0;
42     usageprice_pkg_changed(pkgpart);
43   }
44 }
45
46 // main function to invoke when pkgpart changes
47 function usageprice_pkg_changed (pkgpart, pkgnum) {
48   if (usagepriceDisabled) return;
49   clear_part_pkg_usageprice();
50
51   if (pkgpart) {
52     if (usagepriceCache[pkgpart]) {
53       update_part_pkg_usageprice(pkgpart);
54     } else {
55       get_part_pkg_usageprice( pkgpart || 0, pkgnum || 0, download_part_pkg_usageprice );
56     }
57   }
58 }
59
60 // removes table rows & hides table title
61 function clear_part_pkg_usageprice () {
62   var table = document.getElementById('cust_pkg_usageprice_table');
63   for ( var r = table.rows.length - 1; r >= 0; r-- ) {
64     table.deleteRow(r);
65   }
66   document.getElementById('cust_pkg_usageprice_title').style.display = 'none';
67 }
68
69 // catches response from xmlhttp request, updates cache & calls update function
70 function download_part_pkg_usageprice (part_pkg_usageprice) {
71   var usagepriceArray = JSON.parse(part_pkg_usageprice);
72   var pkgpart = usagepriceArray[0];
73   usagepriceCache[pkgpart] = usagepriceArray;
74   update_part_pkg_usageprice(pkgpart);
75 }
76
77 // updates from cache
78 function update_part_pkg_usageprice (pkgpart) {
79   if (usagepriceDisabled) return;
80   clear_part_pkg_usageprice();
81
82   var usagepriceArray = usagepriceCache[pkgpart];
83   var table = document.getElementById('cust_pkg_usageprice_table');
84
85   // add the new usage price rows
86   var rownum = 0;
87   for ( var s = 1; s < usagepriceArray.length; s=s+2 ) {
88     var html = usagepriceArray[s];
89     var javascript = usagepriceArray[s+1];
90
91     var row = table.insertRow(rownum++);
92
93     var widget_cell = document.createElement('TD');
94     widget_cell.style.paddingTop = "3px";
95     widget_cell.colSpan = "2";
96     widget_cell.innerHTML = html;
97     row.appendChild(widget_cell);
98
99   }
100
101   if ( rownum > 0 ) {
102     document.getElementById('cust_pkg_usageprice_title').style.display = '';
103   } else {
104     document.getElementById('cust_pkg_usageprice_title').style.display = 'none';
105   }
106
107 }
108
109 % if ($opt{'pkgpart'}) {
110 <&| /elements/onload.js &>
111 usageprice_pkg_changed(<% $opt{'pkgpart'} %>, <% $opt{'pkgnum'} %>);
112 </&>
113 % }
114
115 </SCRIPT>
116
117 <%init>
118 my %opt = @_;
119 </%init>
120
121