4f3b7da420934ce102774a8dc6df03cd431953c5
[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
12 push @fixups, 'check_unique'
13     if $conf->exists('cust_main-check_unique') and !$opt{'custnum'};
14
15 push @fixups, 'do_submit'; # always last
16 </%init>
17
18 var fixups = <% encode_json(\@fixups) %>;
19 var fixup_position;
20 var running = false;
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   if ( running ) {
26     window[ fixups[fixup_position++] ].call();
27   }
28 }
29
30 %# or on failure:
31 function submit_abort() {
32   running = false;
33   fixup_position = 0;
34   document.CustomerForm.submitButton.disabled = false;
35   cClick();
36 }
37
38 function bottomfixup(what) {
39   fixup_position = 0;
40   document.CustomerForm.submitButton.disabled = true;
41   running = true;
42   submit_continue();
43 }
44
45 function do_submit() {
46   document.CustomerForm.submit();
47 }
48
49 function copy_payby_fields() {
50   var layervars = new Array(
51     'payauto', 'billday',
52     'payinfo', 'payinfo1', 'payinfo2', 'payinfo3', 'paytype',
53     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
54     'paystart_month', 'paystart_year', 'payissue',
55     'payip',
56     'paid'
57   );
58
59   var cf = document.CustomerForm;
60   var payby = cf.payby.options[cf.payby.selectedIndex].value;
61   for ( f=0; f < layervars.length; f++ ) {
62     var field = layervars[f];
63     copyelement( cf.elements[payby + '_' + field],
64                  cf.elements[field]
65                );
66   }
67   submit_continue();
68 }
69
70 <% include( '/elements/standardize_locations.js',
71             'callback' => 'submit_continue();',
72             'main_prefix' => 'bill_',
73             'no_company' => 1,
74           )
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 'censustract' is the confirmed censustract; if it's set,
100 % # do nothing here
101 function confirm_censustract() {
102   var cf = document.CustomerForm;
103   if ( cf.elements['censustract'].value == '' ) {
104     var address_info = form_address_info();
105     address_info['ship_latitude']  = cf.elements['ship_latitude'].value;
106     address_info['ship_longitude'] = cf.elements['ship_longitude'].value;
107     OLpostAJAX(
108         '<%$p%>/misc/confirm-censustract.html',
109         'q=' + encodeURIComponent(JSON.stringify(address_info)),
110         function() {
111           overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
112             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
113             576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
114             TEXTSIZE, 3 );
115         },
116         0);
117   } else submit_continue();
118 }
119
120 %# called from confirm-censustract.html
121 function set_censustract(tract, year) {
122   var cf = document.CustomerForm;
123   cf.elements['censustract'].value = tract;
124   cf.elements['censusyear'].value = year;
125   submit_continue();
126 }
127
128 function check_unique() {
129   var search_hash = new Object;
130 % foreach ($conf->config('cust_main-check_unique')) {
131   search_hash['<% $_ %>'] = document.CustomerForm.elements['<% $_ %>'].value;
132 % }
133
134 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
135   duplicates_form(JSON.stringify(search_hash), confirm_unique);
136 }
137
138 function confirm_unique(arg) {
139   if ( arg.match(/\S/) ) {
140 %# arg contains a complete form to choose an existing customer, or not
141   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
142       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
143       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
144   } else { // no duplicates
145     submit_continue();
146   }
147 }
148