if(typeof DC_Lightspeed_JSON == 'undefined') {
function DC_Lightspeed_JSON_func() {
var jsonCache = null;
var callbacksToCall = [];
var isLoading = false;
var forceReload = false;
function loadJson() {
// Loading the password reset page adds a 'The reset link has expired' error to the sessions.
// As a workaround we just ignore this page.
if(document.location.href.includes("/account/reset/?token=")) {
return;
}
// Don't break the reCaptcha
if(document.location.href.includes("/services/challenge/")) {
return;
}
isLoading = true;
var url = document.location.pathname + addParamToUrl(document.location.search, "format", "json");
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if(!forceReload) {
if(xhr.status == 200 || xhr.status == 404) {
try {
jsonCache = JSON.parse(xhr.responseText);
for(var i=0;i').replace(/'/g,'\'').replace(/&/g, '&');
jsonCache = JSON.parse(json);
}catch(ex) {}
}
}
if(jsonCache === null) {
callbacksToCall.push(callback);
if(!isLoading) {
loadJson();
}
}else{
if(mayCallImmediately) {
callback(jsonCache);
}else{
setTimeout(function () {
callback(jsonCache);
}, 1);
}
}
};
this.fetch = function (callback) {
if(isLoading) {
forceReload = true;
callbacksToCall.push(callback);
}else{
callbacksToCall.push(callback);
loadJson();
}
};
this.getOrFetch = function(doFetch, callback) {
if(doFetch) {
this.fetch(callback);
}else{
this.get(callback);
}
}
}
DC_Lightspeed_JSON = new DC_Lightspeed_JSON_func();
}function CmdcGiftInCartInit() {
var appBaseUrl = 'https://giftincart.cmdcbv.app';
var productElements;
var responseData;
var cartButtonSelector = '.custom\u002Dcart\u002Dpage\u0020.cart\u002Dwrap\u0020.buttons\u0020.btn.accent';
var containerSelector = '.custom\u002Dcart\u002Dpage\u0020\u0020.cart\u002Dwrap';
var customerLoginRequired = null;
function addLanguageToUrl(url) {
var urlParts = document.location.pathname.split('/');
if (urlParts[1].length == 2) {
return "/" + urlParts[1] + url;
} else {
return url;
}
}
function getView() {
var xhr = new XMLHttpRequest();
xhr.open('GET', appBaseUrl + '/public/ls/' + responseData.shop.id + '/getGifts?checkoutId=' + responseData.checkout.id + '&language=' + responseData.shop.language);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var container = $(xhr.response);
$(containerSelector).after(container);
productElements = document.getElementsByClassName('giftincart-product');
for (var i = 0; i < productElements.length; i++) {
productElements[i].addEventListener('click', function () {
setGift(this);
});
}
}
}
xhr.send();
var guiActionButtons = document.querySelectorAll(cartButtonSelector);
if (guiActionButtons !== null && guiActionButtons.length > 0) {
for (var a = 0; a < guiActionButtons.length; a++) {
guiActionButtons[a].addEventListener('click', function(event) {
event.preventDefault();
event.stopImmediatePropagation();
var giftVid = getGift();
var giftLength = document.querySelectorAll('.giftincart-product.giftincart-selectable').length;
var checkoutUrl = this.href;
if (giftVid == null && giftLength > 0) {
confirmWithoutSelectingGift(checkoutUrl);
} else if (giftLength > 0) {
var loadingSpinner = document.createElement('span');
if (document.querySelectorAll('#loadingSpinner').length === 0) {
loadingSpinner.innerHTML = '';
this.appendChild(loadingSpinner);
}
var currentPage = document.location.href;
if(currentPage.indexOf('?') == -1) {
currentPage += '?format=json';
}else{
currentPage += '&format=json';
}
var xhr = new XMLHttpRequest();
var url = appBaseUrl + '/public/addGiftToCheckout?shop=' + responseData.shop.id;
url += '"e=' + responseData.checkout.quote.id;
url += '&checkout=' + responseData.checkout.id;
url += '&variant=' + giftVid;
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if(responseData.checkout && responseData.checkout.loyalty_reward && responseData.checkout.loyalty_reward.reward_id) {
fixRewardCode(responseData.checkout.loyalty_reward.reward_id, checkoutUrl);
}else {
document.location = checkoutUrl;
}
} else {
localStorage.removeItem('giftincart_selected_product');
productElements = document.getElementsByClassName('giftincart-product');
for (var i = 0; i < productElements.length; i++) {
productElements[i].classList.remove('giftincart-selected');
}
confirmWithoutSelectingGift(checkoutUrl);
}
}
}
xhr.send();
} else {
window.location.href = checkoutUrl;
}
});
}
}
}
async function fixRewardCode(rewardId, checkoutUrl) {
const data = new FormData();
data.append("reward_id", rewardId);
await fetch(addLanguageToUrl("/cart/applyReward/"), {
method: "POST",
body: data
});
document.location = checkoutUrl;
}
function getCustomerLoginRequiredView() {
var xhr = new XMLHttpRequest();
xhr.open('GET', appBaseUrl + '/public/ls/' + lightspeedJson.shop.id + '/getCustomerLoginRequired?language=' + lightspeedJson.shop.language);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var container = $(xhr.response.trim());
$(containerSelector).after(container);
}
}
xhr.send();
}
function getGift() {
return localStorage.getItem('giftincart_selected_product');
}
function setGift(el) {
if (typeof el.dataset.variant !== 'undefined') {
for (var i = 0; i < productElements.length; i++) {
productElements[i].classList.remove('giftincart-selected');
}
if (el.dataset.variant == getGift()) {
localStorage.removeItem('giftincart_selected_product');
el.classList.remove('giftincart-selected');
} else {
localStorage.setItem('giftincart_selected_product', el.dataset.variant);
el.classList.add('giftincart-selected');
}
} else {
console.log('CMDC GiftInCart: No vid');
}
}
if (customerLoginRequired && lightspeedJson.page.account === false) {
getCustomerLoginRequiredView();
} else {
var xhr = new XMLHttpRequest();
xhr.open('GET', addLanguageToUrl('/checkout/onestep?format=json'));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
responseData = JSON.parse(xhr.response);
if (document.readyState === "complete" || document.readyState === "interactive" || document.readyState === "loaded") {
getView();
} else {
document.addEventListener("DOMContentLoaded", function () {
getView();
});
}
} else {
console.error(xhr.statusText);
}
}
}
xhr.send();
}
}
if (document.location.pathname.match(/\/([a-z]{2}\/)?cart\/.*/)) {
DC_Lightspeed_JSON.get(function (data) {
lightspeedJson = data;
if (document.readyState === 'complete' || document.readyState === 'loaded' || document.readyState === 'interactive') {
CmdcGiftInCartInit();
} else {
window.addEventListener('DOMContentLoaded', function () {
CmdcGiftInCartInit();
});
}
});
}
if (document.location.pathname.match(/\/([a-z]{2}\/)?thankyou\/.*/)) {
localStorage.removeItem('giftincart_selected_product');
}