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