/
settings.js
182 lines (144 loc) · 4.83 KB
/
settings.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
"use strict";
var templates = {};
// ---------------------------------------------------------
// 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() {
loadSettings();
}
function loadSettings() {
var html = '';
//noinspection JSUnusedLocalSymbols
ultracart.myAccount.getSettings({
success: function (settings) {
html = 'We are sorry. Your settings could not be loaded at this time.';
if (settings) {
html = templates.settings(settings);
// console.log(html);
}
jQuery('#settings').html(html);
bindFields();
}
});
}
function updateSettings() {
clearAllMessages();
// validate the fields
var email = jQuery.trim(jQuery('#email').val());
var merchantId = jQuery.trim(jQuery('#merchantId').val());
var customerProfileOid = jQuery.trim(jQuery('#customerProfileOid').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());
var fax = jQuery.trim(jQuery('#fax').val());
var taxId = jQuery.trim(jQuery('#taxId').val());
if (!email) {
showError("Your email is a required field.");
return;
}
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 changes = {
merchantId: merchantId,
customerProfileOid: customerProfileOid === '' ? -1 : parseInt(customerProfileOid),
email: email,
company: company,
firstName: firstName,
lastName: lastName,
address1: address1,
address2: address2,
city: city,
state: state,
postalCode: postalCode,
country: country,
title: title,
dayPhone: dayPhone,
eveningPhone: eveningPhone,
taxId: taxId,
fax: fax
};
ultracart.myAccount.updateSettings(changes, {
success: function () {
showSuccess("Your changes were saved.");
},
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 settings 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', updateSettings);
}
jQuery(document).ready(function () {
enablePleaseWaitMessage();
templates.settings = Handlebars.compile(jQuery('#settings-template').html());
initialize();
});