alternate address standardization method (TeleAtlas), #13763
[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 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 <% include( '/elements/standardize_locations.js',
72             'callback' => 'submit_continue();'
73           )
74 %>
75
76 function copyelement(from, to) {
77   if ( from == undefined ) {
78     to.value = '';
79   } else if ( from.type == 'select-one' ) {
80     to.value = from.options[from.selectedIndex].value;
81     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
82   } else if ( from.type == 'checkbox' ) {
83     if ( from.checked ) {
84       to.value = from.value;
85     } else {
86       to.value = '';
87     }
88   } else {
89     if ( from.value == undefined ) {
90       to.value = '';
91     } else {
92       to.value = from.value;
93     }
94   }
95   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
96 }
97
98 function check_unique() {
99   var search_hash = new Object;
100 % foreach ($conf->config('cust_main-check_unique')) {
101   search_hash['<% $_ %>'] = document.CustomerForm.elements['<% $_ %>'].value;
102 % }
103
104 %# supported in IE8+, Firefox 3.5+, WebKit, Opera 10.5+
105   duplicates_form(JSON.stringify(search_hash), confirm_unique);
106 }
107
108 function confirm_unique(arg) {
109   if ( arg.match(/\S/) ) {
110 %# arg contains a complete form to choose an existing customer, or not
111   overlib( arg, CAPTION, 'Duplicate customer', STICKY, AUTOSTATUSCAP, 
112       CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 
113       268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
114   } else { // no duplicates
115     submit_continue();
116   }
117 }
118