fix dev operation @ localhost, RT#13852
[freeside.git] / httemplate / edit / cust_main / bottomfixup.js
1 <%init>
2 my %opt = @_; # custnum
3 my $conf = new FS::Conf;
4
5 my $company_latitude  = $conf->config('company_latitude');
6 my $company_longitude = $conf->config('company_longitude');
7
8 my @fixups = ('copy_payby_fields', 'standardize_locations');
9
10 push @fixups, 'fetch_censustract'
11     if $conf->exists('cust_main-require_censustract');
12
13 push @fixups, 'check_unique'
14     if $conf->exists('cust_main-check_unique') and !$opt{'custnum'};
15
16 push @fixups, 'do_submit'; # always last
17 </%init>
18
19 var fixups = <% encode_json(\@fixups) %>;
20 var fixup_position;
21
22 %# state machine to deal with all the asynchronous stuff we're doing
23 %# call this after each fixup on success:
24 function submit_continue() {
25   window[ fixups[fixup_position++] ].call();
26 }
27
28 %# or on failure:
29 function submit_abort() {
30   fixup_position = 0;
31   document.CustomerForm.submitButton.disabled = false;
32   cClick();
33 }
34
35 function bottomfixup(what) {
36   fixup_position = 0;
37   document.CustomerForm.submitButton.disabled = true;
38   submit_continue();
39 }
40
41 function do_submit() {
42   document.CustomerForm.submit();
43 }
44
45 function copy_payby_fields() {
46   var layervars = new Array(
47     'payauto', 'billday',
48     'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
49     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
50     'paystart_month', 'paystart_year', 'payissue',
51     'payip',
52     'paid'
53   );
54
55   var cf = document.CustomerForm;
56   var payby = cf.payby.options[cf.payby.selectedIndex].value;
57   for ( f=0; f < layervars.length; f++ ) {
58     var field = layervars[f];
59     copyelement( cf.elements[payby + '_' + field],
60                  cf.elements[field]
61                );
62   }
63   submit_continue();
64 }
65
66 %# call submit_continue() on completion...
67 %# otherwise not touching standardize_locations for now
68 <% include( '/elements/standardize_locations.js',
69             'callback' => 'submit_continue();'
70           )
71 %>
72
73 function fetch_censustract() {
74
75   //alert('fetch census tract data');
76   var cf = document.CustomerForm;
77   var state_el = cf.elements['ship_state'];
78   var census_data = new Array(
79     'year',     <% $conf->config('census_year') || '2012' %>,
80     'address1', cf.elements['ship_address1'].value,
81     'city',     cf.elements['ship_city'].value,
82     'state',    state_el.options[ state_el.selectedIndex ].value,
83     'zip',      cf.elements['ship_zip'].value
84   );
85
86   censustract( census_data, update_censustract );
87
88 }
89
90 var set_censustract;
91
92 function update_censustract(arg) {
93
94   var argsHash = eval('(' + arg + ')');
95
96   var cf = document.CustomerForm;
97
98 /*  var msacode    = argsHash['msacode'];
99   var statecode  = argsHash['statecode'];
100   var countycode = argsHash['countycode'];
101   var tractcode  = argsHash['tractcode'];
102   
103   var newcensus = 
104     new String(statecode)  +
105     new String(countycode) +
106     new String(tractcode).replace(/\s$/, '');  // JSON 1 workaround */
107   var error      = argsHash['error'];
108   var newcensus  = argsHash['censustract'];
109
110   set_censustract = function () {
111
112     cf.elements['censustract'].value = newcensus;
113     submit_continue();
114
115   }
116
117   if (error || cf.elements['censustract'].value != newcensus) {
118     // popup an entry dialog
119
120     if (error) { newcensus = error; }
121     newcensus.replace(/.*ndefined.*/, 'Not found');
122
123     var latitude = cf.elements['latitude' ].value || '<% $company_latitude %>';
124     var longitude= cf.elements['longitude'].value || '<% $company_longitude %>';
125
126     var choose_censustract =
127       '<CENTER><BR><B>Confirm censustract</B><BR>' +
128       '<A href="http://maps.ffiec.gov/FFIECMapper/TGMapSrv.aspx?' +
129       'census_year=<% $conf->config('census_year') || '2012' %>' +
130       '&latitude=' + latitude +
131       '&longitude=' + longitude +
132       '" target="_blank">Map service module location</A><BR>' +
133       '<A href="http://maps.ffiec.gov/FFIECMapper/TGMapSrv.aspx?' +
134       'census_year=<% $conf->config('census_year') || '2012' %>' +
135       '&zip_code=' + cf.elements['ship_zip'].value +
136       '" target="_blank">Map zip code center</A><BR><BR>' +
137       '<TABLE>';
138     
139     choose_censustract = choose_censustract + 
140       '<TR><TH style="width:50%">Entered census tract</TH>' +
141         '<TH style="width:50%">Calculated census tract</TH></TR>' +
142       '<TR><TD>' + cf.elements['censustract'].value +
143         '</TD><TD>' + newcensus + '</TD></TR>' +
144         '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
145
146     choose_censustract = choose_censustract +
147       '<TR><TD ALIGN="center">' +
148         '<BUTTON TYPE="button" onClick="submit_continue();"><IMG SRC="<%$p%>images/error.png" ALT=""> Use entered census tract </BUTTON>' + 
149       '</TD><TD ALIGN="center">' +
150         '<BUTTON TYPE="button" onClick="set_censustract();"><IMG SRC="<%$p%>images/tick.png" ALT=""> Use calculated census tract </BUTTON>' + 
151       '</TD></TR>' +
152       '<TR><TD COLSPAN=2 ALIGN="center">' +
153         '<BUTTON TYPE="button" onClick="submit_abort();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
154         
155       '</TABLE></CENTER>';
156
157     overlib( choose_censustract, CAPTION, 'Confirm censustract', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
158
159   } else {
160
161     submit_continue();
162
163   }
164
165 }
166
167 function copyelement(from, to) {
168   if ( from == undefined ) {
169     to.value = '';
170   } else if ( from.type == 'select-one' ) {
171     to.value = from.options[from.selectedIndex].value;
172     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
173   } else if ( from.type == 'checkbox' ) {
174     if ( from.checked ) {
175       to.value = from.value;
176     } else {
177       to.value = '';
178     }
179   } else {
180     if ( from.value == undefined ) {
181       to.value = '';
182     } else {
183       to.value = from.value;
184     }
185   }
186   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
187 }
188
189 function check_unique() {
190   var search_hash = new Object;
191 % foreach ($conf->config('cust_main-check_unique')) {
192   search_hash['<% $_ %>'] = document.CustomerForm.elements['<% $_ %>'].value;
193 % }
194
195 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
196   duplicates_form(JSON.stringify(search_hash), confirm_unique);
197 }
198
199 function confirm_unique(arg) {
200   if ( arg.match(/\S/) ) {
201 %# arg contains a complete form to choose an existing customer, or not
202   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
203       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
204       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
205   } else { // no duplicates
206     submit_continue();
207   }
208 }
209