more work on package service addresses: hide locations when they're all the default...
[freeside.git] / httemplate / elements / tr-select-cust_location.html
1 <%doc>
2
3 Example:
4
5   include('/elements/tr-select-cust_location.html',
6             'cgi'       => $cgi,
7             'cust_main' => $cust_main,
8          )
9
10 </%doc>
11
12 <% include('/elements/xmlhttp.html',
13               'url'  => $p.'misc/location.cgi',
14               'subs' => [ 'get_location' ],
15            )
16 %>
17
18 <SCRIPT TYPE="text/javascript">
19
20   function locationnum_changed(what) {
21     var locationnum = what.options[what.selectedIndex].value;
22     if ( locationnum == -1 ) {
23
24 %     for (@location_fields) { 
25         what.form.<%$_%>.disabled = false;
26         what.form.<%$_%>.style.backgroundColor = '#ffffff';
27 %     } 
28
29       what.form.address1.value = '';
30       what.form.address2.value = '';
31       what.form.city.value = '';
32       what.form.zip.value = '';
33
34       changeSelect(what.form.country, <% $countrydefault |js_string %>);
35
36       country_changed( what.form.country,
37                        fix_state_factory( <% $statedefault |js_string %>,
38                                           ''
39                                         )
40                      );
41
42     } else {
43
44       if ( locationnum == 0 ) {
45         what.form.address1.value = <% $cust_main->address1 |js_string %>;
46         what.form.address2.value = <% $cust_main->address2 |js_string %>;
47         what.form.city.value = <% $cust_main->city |js_string %>;
48         what.form.zip.value = <% $cust_main->zip |js_string %>;
49
50         changeSelect(what.form.country, <% $cust_main->country | js_string %> );
51
52         country_changed( what.form.country,
53                          fix_state_factory( <% $cust_main->state | js_string %>,
54                                             <% $cust_main->county | js_string %>
55                                           )
56                        );
57
58       } else {
59         get_location( locationnum, update_location );
60       } 
61
62 %#sleep/wait until dropdowns are updated?
63 %     for (@location_fields) { 
64         what.form.<%$_%>.disabled = true;
65         what.form.<%$_%>.style.backgroundColor = '#dddddd';
66 %     } 
67
68     }
69   }
70
71   function fix_state_factory (state, county) {
72     function fix_state() {
73       var state_el = document.getElementById('state');
74       changeSelect(state_el, state);
75       state_changed(state_el, fix_county_factory(county) );
76     }
77     return fix_state;
78   }
79
80   function fix_county_factory(county) {
81     function fix_county() {
82       var county_el = document.getElementById('county');
83       if ( county.length > 0 ) {
84         changeSelect(county_el, county );
85       } else {
86         county_el.selectedIndex = 0;
87       }
88     }
89     return fix_county;
90   }
91
92   function changeSelect(what, value) {
93     for ( var i=0; i<what.length; i++) {
94       if ( what.options[i].value == value ) {
95         what.selectedIndex = i;
96       }
97     }
98   }
99
100   function update_location( string ) {
101     var hash = eval('('+string+')');
102     document.getElementById('address1').value = hash['address1'];
103     document.getElementById('address2').value = hash['address2'];
104     document.getElementById('city').value     = hash['city'];
105     document.getElementById('zip').value      = hash['zip'];
106
107     var country_el = document.getElementById('country');
108
109     changeSelect( country_el, hash['country'] );
110
111     country_changed( country_el,
112                      fix_state_factory( hash['state'],
113                                         hash['county']
114                                       )
115                    );
116   }
117
118 </SCRIPT>
119
120 <TR>
121   <TH ALIGN="right">Service location</TH>
122   <TD COLSPAN=7>
123     <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
124       <OPTION VALUE="">(default service address)
125 %     foreach my $loc ( $cust_main->cust_location ) {
126         <OPTION VALUE="<% $loc->locationnum %>"
127                 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
128         ><% $loc->line |h %>
129 %     }
130       <OPTION VALUE="-1"
131               <% $locationnum == -1 ? 'SELECTED' : '' %>
132       >Add new location
133     </SELECT>
134   </TD>
135 </TR>
136
137 <% include('/elements/location.html',
138              'object'       => $cust_location,
139              #'onchange' ?  probably not
140              'disabled'     => ( $locationnum == -1 ? '' : 'DISABLED' ),
141              'no_asterisks' => 1,
142           )
143 %>
144
145 <%once>
146
147 my @location_fields = qw( address1 address2 city county state zip country );
148
149 </%once>
150 <%init>
151
152 my $conf = new FS::Conf;
153 my $countrydefault = $conf->config('countrydefault') || 'US';
154 my $statedefault = $conf->config('statedefault')
155                    || ($countrydefault eq 'US' ? 'CA' : '');
156
157 my %opt = @_;
158 my $cgi       = $opt{'cgi'};
159 my $cust_main = $opt{'cust_main'};
160
161 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
162 my $locationnum = $1;
163 my $cust_location;
164 if ( $locationnum && $locationnum != -1 ) {
165   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
166     or die "unknown locationnum";
167 } else {
168   $cust_location = new FS::cust_location;
169   if ( $cgi->param('error') && $locationnum == -1 ) {
170     $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
171   } else {
172     $cust_location->$_( $cust_main->$_() ) foreach @location_fields;
173   }
174 }
175
176 </%init>