-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaddress.js
198 lines (158 loc) · 5.54 KB
/
address.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use strict";
var templates = {};
var order = null;
var addressType = '';
// ---------------------------------------------------------
// Security (well, not really security, but a helper to
// redirect the user if they're not logged in. Security
// is handled by the REST API.
// ---------------------------------------------------------
var redirectToLogin = function () {
var location_href = "index.html";
if (location.hash && location.hash.length > 0) {
location_href += "?hash=" + location.hash.substring(1);
}
location.href = location_href;
};
var theDocument = jQuery(document);
theDocument.ajaxSend(function (event, xhr) {
var cartId = getCookie('UltraCartShoppingCartID');
var merchantId = window.merchantId || getCookie('UltraCartMerchantID');
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("X-UC-Merchant-Id", merchantId);
xhr.setRequestHeader("X-UC-Shopping-Cart-Id", cartId);
});
theDocument.ajaxError(function (event, xhr) {
if (xhr.status === 401)
redirectToLogin();
});
jQuery.ajaxSetup({cache: false});
function initialize() {
// find the address type and address id (if edit).
var params = uc.commonFunctions.parseHttpParameters();
if (params['type'] && params['type'].length) {
addressType = params['type'][0];
} else {
addressType = 'shipping'; // not ideal to default like this. but for a new page it will work out.
}
var id = null;
if (params['id'] && params['id'].length) {
id = params['id'][0];
}
loadAddress(id);
}
function loadAddress(id) {
var html = '';
if (id) {
var functionName = addressType === 'shipping' ? 'getShippingAddress' : 'getBillingAddress';
//noinspection JSUnusedLocalSymbols
ultracart.myAccount[functionName](id, {
success: function (address) {
html = 'This address could not be found at this time.';
if (address) {
address.isBilling = addressType === 'billing';
html = templates.address(address);
// console.log(html);
}
jQuery('#address').html(html);
bindFields();
}
});
} else {
html = templates.address({isBilling: addressType === 'billing'});
jQuery('#address').html(html);
bindFields();
}
}
function updateAddress() {
clearAllMessages();
// validate the fields
var id = jQuery.trim(jQuery('#id').val());
var company = jQuery.trim(jQuery('#company').val());
var firstName = jQuery.trim(jQuery('#firstName').val());
var lastName = jQuery.trim(jQuery('#lastName').val());
var address1 = jQuery.trim(jQuery('#address1').val());
var address2 = jQuery.trim(jQuery('#address2').val());
var city = jQuery.trim(jQuery('#city').val());
var state = jQuery.trim(jQuery('#state').val());
var postalCode = jQuery.trim(jQuery('#postalCode').val());
var country = jQuery.trim(jQuery('#country').val());
var title = jQuery.trim(jQuery('#title').val());
var dayPhone = jQuery.trim(jQuery('#dayPhone').val());
var eveningPhone = jQuery.trim(jQuery('#eveningPhone').val());
if (!firstName) {
showError("First Name is a required field.");
return;
}
if (!lastName) {
showError("Last Name is a required field.");
return;
}
if (!address1) {
showError("Address 1 is a required field.");
return;
}
if (!city) {
showError("City is a required field.");
return;
}
if (!state) {
showError("State is a required field.");
return;
}
if (!postalCode) {
showError("Postal Code is a required field.");
return;
}
if (!country) {
showError("Country is a required field.");
return;
}
var functionName = addressType === 'shipping' ? 'insertShippingAddress' : 'insertBillingAddress';
if (id) {
functionName = addressType === 'shipping' ? 'updateShippingAddress' : 'updateBillingAddress';
}
var account = {
id: id === '' ? 0 : parseInt(id),
company: company,
firstName: firstName,
lastName: lastName,
address1: address1,
address2: address2,
city: city,
state: state,
postalCode: postalCode,
country: country,
title: title,
dayPhone: dayPhone,
eveningPhone: eveningPhone
};
ultracart.myAccount[functionName](account, {
success: function (address) {
showSuccess("Your changes were saved. Press back on your browser to return to your address book.");
jQuery('#id').val(address.id); // set the id so subsequent saves are updates.
},
failure: function (jqXHR) {
var errorMsg = null;
if (jqXHR && jqXHR.getResponseHeader) {
errorMsg = jqXHR.getResponseHeader('UC-REST-ERROR');
}
if (errorMsg) {
showError("Save failed with this error: " + errorMsg);
} else {
showError("Your address could not be saved at this time. Please try again later.");
}
}
});
}
function bindFields() {
jQuery('#cancelButton').unbind().bind('click', function () {
window.history.back();
});
jQuery('#saveButton').unbind().bind('click', updateAddress);
}
jQuery(document).ready(function () {
enablePleaseWaitMessage();
templates.address = Handlebars.compile(jQuery('#address-template').html());
initialize();
});