default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / elements / select-areacode.html
1 <% include('/elements/xmlhttp.html',
2               'url'  => $p.'misc/areacodes.cgi',
3               'subs' => [ $opt{'prefix'}. 'get_areacodes' ],
4           )
5 %>
6
7 <SCRIPT TYPE="text/javascript">
8
9   function jopt(what,value,text) {
10     var optionName = new Option(text, value, false, false);
11     what.append(optionName);
12   }
13
14   function <% $opt{'state_prefix'} %>state_changed(what, callback) {
15
16     what.form.<% $opt{'prefix'} %>areacode.disabled = 'disabled';
17     what.form.<% $opt{'prefix'} %>areacode.style.display = 'none';
18     var areacodewait = document.getElementById('<% $opt{'prefix'} %>areacodewait');
19     areacodewait.style.display = 'inline';
20     var areacodeerror = document.getElementById('<% $opt{'prefix'} %>areacodeerror');
21     areacodeerror.style.display = 'none';
22
23     what.form.<% $opt{'prefix'} %>exchange.disabled = 'disabled';
24     $('#<% $opt{'prefix'} %>phonenum').prop('disabled', true);
25
26     var state = what.options[what.selectedIndex].value;
27
28     function <% $opt{'prefix'} %>update_areacodes(areacodes) {
29
30       var reply = JSON.parse(areacodes);
31       // blank the current areacode
32       $('#<% $opt{prefix} %>areacode').empty();
33
34       // blank the current exchange too
35       $('#<% $opt{prefix} %>exchange').empty();
36       jopt( $('#<% $opt{prefix} %>exchange'), '', 'Select city / exchange' );
37
38       // blank the current phonenum too
39       $('#<% $opt{prefix} %>phonenum').empty();
40       if ( what.form.<% $opt{'prefix'} %>phonenum.type != 'select-multiple' ) {
41         jopt($('#<% $opt{'prefix'} %>phonenum'), '', 'Select phone number');
42       }
43
44 %     if ($opt{empty}) {
45         jopt($('#<% $opt{'prefix'} %>areacode'), '', '<% $opt{empty} %>');
46 %     }
47
48       // add the new areacodes
49       var areacodeArray = reply.areacodes;
50       for ( var s = 0; s < areacodeArray.length; s++ ) {
51           var areacodeLabel = areacodeArray[s];
52           if ( areacodeLabel == "" )
53               areacodeLabel = '(n/a)';
54           jopt($('#<% $opt{'prefix'} %>areacode'), areacodeArray[s], areacodeLabel);
55       }
56
57       areacodewait.style.display = 'none';
58       if ( areacodeArray.length >= 1 ) {
59         what.form.<% $opt{'prefix'} %>areacode.disabled = '';
60         what.form.<% $opt{'prefix'} %>areacode.style.display = '';
61       } else {
62         var areacodeerror = document.getElementById('<% $opt{'prefix'} %>areacodeerror');
63         areacodeerror.style.display = 'inline';
64         if ( reply.error ) {
65           areacodeerror.textContent = reply.error;
66         } else {
67           areacodeerror.textContent = 'Select a different state';
68         }
69       }
70
71       //run the callback
72       if ( callback != null ) 
73         callback();
74     }
75
76     // go get the new areacodes
77     <% $opt{'prefix'} %>get_areacodes( state, <% $opt{'svcpart'} %>, <% $opt{'prefix'} %>update_areacodes );
78
79   }
80
81 </SCRIPT>
82
83 <DIV ID="areacodewait" STYLE="display:none"><IMG SRC="<%$fsurl%>images/wait-orange.gif"> <B>Finding area codes</B></DIV>
84
85 <DIV ID="areacodeerror" STYLE="display:none; font-weight: bold"><IMG SRC="<%$fsurl%>images/cross.png"></DIV>
86
87 <SELECT
88   ID       = "<% $opt{prefix} %>areacode"
89   NAME     = "<% $opt{prefix} %>areacode"
90   onChange = "<% $opt{prefix} %>areacode_changed(this); <% $opt{onchange} %>"
91   <% $opt{disabled} %>
92 >
93   <OPTION VALUE="">Select area code</OPTION>
94 </SELECT>
95
96 <%init>
97
98 my %opt = @_;
99
100 $opt{disabled}     = 'disabled'   unless exists $opt{disabled};
101 $opt{state_prefix} = $opt{prefix} unless exists $opt{state_prefix};
102
103 </%init>