5 include('/elements/tr-select-cust_location.html',
7 'cust_main' => $cust_main,
12 <% include('/elements/xmlhttp.html',
13 'url' => $p.'misc/location.cgi',
14 'subs' => [ 'get_location' ],
18 <SCRIPT TYPE="text/javascript">
20 function locationnum_changed(what) {
21 var locationnum = what.options[what.selectedIndex].value;
22 if ( locationnum == -1 ) {
24 % for (@location_fields, 'city_select') {
25 what.form.<%$_%>.disabled = false;
26 what.form.<%$_%>.style.backgroundColor = '#ffffff';
29 what.form.address1.value = '';
30 what.form.address2.value = '';
31 what.form.city.value = '';
32 what.form.zip.value = '';
34 changeSelect(what.form.country, <% $countrydefault |js_string %>);
36 country_changed( what.form.country,
37 fix_state_factory( <% $statedefault |js_string %>,
44 if ( locationnum == 0 ) {
45 what.form.address1.value = <% $cust_main->get($prefix.'address1') |js_string %>;
46 what.form.address2.value = <% $cust_main->get($prefix.'address2') |js_string %>;
47 what.form.city.value = <% $cust_main->get($prefix.'city') |js_string %>;
48 what.form.zip.value = <% $cust_main->get($prefix.'zip') |js_string %>;
50 changeSelect(what.form.country, <% $cust_main->get($prefix.'country') | js_string %> );
52 country_changed( what.form.country,
53 fix_state_factory( <% $cust_main->get($prefix.'state') | js_string %>,
54 <% $cust_main->get($prefix.'county') | js_string %>
59 get_location( locationnum, update_location );
62 %#sleep/wait until dropdowns are updated?
63 % for (@location_fields, 'city_select') {
64 what.form.<%$_%>.disabled = true;
65 what.form.<%$_%>.style.backgroundColor = '#dddddd';
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) );
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 );
86 county_el.selectedIndex = 0;
88 county_changed(county_el);
93 function changeSelect(what, value) {
94 for ( var i=0; i<what.length; i++) {
95 if ( what.options[i].value == value ) {
96 what.selectedIndex = i;
101 function update_location( string ) {
102 var hash = eval('('+string+')');
103 document.getElementById('address1').value = hash['address1'];
104 document.getElementById('address2').value = hash['address2'];
105 document.getElementById('city').value = hash['city'];
106 document.getElementById('zip').value = hash['zip'];
108 var country_el = document.getElementById('country');
110 changeSelect( country_el, hash['country'] );
112 country_changed( country_el,
113 fix_state_factory( hash['state'],
122 <TH ALIGN="right">Service location</TH>
124 <SELECT NAME="locationnum" onChange="locationnum_changed(this);">
125 <OPTION VALUE="">(default service address)
126 % foreach my $loc ( $cust_main->cust_location ) {
127 <OPTION VALUE="<% $loc->locationnum %>"
128 <% $locationnum == $loc->locationnum ? 'SELECTED' : '' %>
132 <% $locationnum == -1 ? 'SELECTED' : '' %>
138 <% include('/elements/location.html',
139 'object' => $cust_location,
140 #'onchange' ? probably not
141 'disabled' => ( $locationnum == -1 ? '' : 'DISABLED' ),
148 my @location_fields = qw( address1 address2 city county state zip country );
153 my $conf = new FS::Conf;
154 my $countrydefault = $conf->config('countrydefault') || 'US';
155 my $statedefault = $conf->config('statedefault')
156 || ($countrydefault eq 'US' ? 'CA' : '');
159 my $cgi = $opt{'cgi'};
160 my $cust_main = $opt{'cust_main'};
162 my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
164 $cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum";
165 my $locationnum = $1;
167 if ( $locationnum && $locationnum != -1 ) {
168 $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } )
169 or die "unknown locationnum";
171 $cust_location = new FS::cust_location;
172 if ( $locationnum == -1 ) {
173 $cust_location->$_( $cgi->param($_) ) foreach @location_fields;
175 $cust_location->$_( $cust_main->get($prefix.$_) ) foreach @location_fields;