address standardization/census tract fixes
[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, 'confirm_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 var running = false;
22
23 %# state machine to deal with all the asynchronous stuff we're doing
24 %# call this after each fixup on success:
25 function submit_continue() {
26   if ( running ) {
27     window[ fixups[fixup_position++] ].call();
28   }
29 }
30
31 %# or on failure:
32 function submit_abort() {
33   running = false;
34   fixup_position = 0;
35   document.CustomerForm.submitButton.disabled = false;
36   cClick();
37 }
38
39 function bottomfixup(what) {
40   fixup_position = 0;
41   document.CustomerForm.submitButton.disabled = true;
42   running = true;
43   submit_continue();
44 }
45
46 function do_submit() {
47   document.CustomerForm.submit();
48 }
49
50 function copy_payby_fields() {
51   var layervars = new Array(
52     'payauto', 'billday',
53     'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
54     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
55     'paystart_month', 'paystart_year', 'payissue',
56     'payip',
57     'paid'
58   );
59
60   var cf = document.CustomerForm;
61   var payby = cf.payby.options[cf.payby.selectedIndex].value;
62   for ( f=0; f < layervars.length; f++ ) {
63     var field = layervars[f];
64     copyelement( cf.elements[payby + '_' + field],
65                  cf.elements[field]
66                );
67   }
68   submit_continue();
69 }
70
71 <& /elements/standardize_locations.js,
72   'callback' => 'submit_continue();',
73   'main_prefix' => 'bill_',
74   'no_company' => 1,
75 &>
76
77 function copyelement(from, to) {
78   if ( from == undefined ) {
79     to.value = '';
80   } else if ( from.type == 'select-one' ) {
81     to.value = from.options[from.selectedIndex].value;
82     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
83   } else if ( from.type == 'checkbox' ) {
84     if ( from.checked ) {
85       to.value = from.value;
86     } else {
87       to.value = '';
88     }
89   } else {
90     if ( from.value == undefined ) {
91       to.value = '';
92     } else {
93       to.value = from.value;
94     }
95   }
96   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
97 }
98
99 % # the value in pre+'censustract' is the confirmed censustract; if it's set,
100 % # do nothing here
101 function confirm_censustract() {
102   var cf = document.CustomerForm;
103   var pre = cf.elements['same'].checked ? 'bill_' : 'ship_';
104   if ( cf.elements[pre+'censustract'].value == '' ) {
105     var address_info = form_address_info();
106     address_info[pre+'latitude']  = cf.elements[pre+'latitude'].value;
107     address_info[pre+'longitude'] = cf.elements[pre+'longitude'].value;
108     OLpostAJAX(
109         '<%$p%>/misc/confirm-censustract.html',
110         'q=' + encodeURIComponent(JSON.stringify(address_info)),
111         function() {
112           overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
113             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
114             576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
115             TEXTSIZE, 3 );
116         },
117         0);
118   } else submit_continue();
119 }
120
121 %# called from confirm-censustract.html
122 function set_censustract(tract, year) {
123   var cf = document.CustomerForm;
124   var pre = 'ship_';
125   if ( cf.elements['same'].checked ) {
126     pre = 'bill_';
127   }
128   cf.elements[pre + 'censustract'].value = tract;
129   cf.elements[pre + 'censusyear'].value = year;
130   submit_continue();
131 }
132
133 function check_unique() {
134   var search_hash = new Object;
135 % foreach ($conf->config('cust_main-check_unique')) {
136   search_hash['<% $_ %>'] = document.CustomerForm.elements['<% $_ %>'].value;
137 % }
138
139 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
140   duplicates_form(JSON.stringify(search_hash), confirm_unique);
141 }
142
143 function confirm_unique(arg) {
144   if ( arg.match(/\S/) ) {
145 %# arg contains a complete form to choose an existing customer, or not
146   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
147       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
148       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
149   } else { // no duplicates
150     submit_continue();
151   }
152 }
153