/*
adds sherpa location checker to cart page
(c) Min Liu 2020
*/
var SherpaApp = (function() {
var sherpaLang = {
"widget-selectshipping-title": "Select shipping method",
"widget-selectshipping-desc": "Costs will be calculated at checkout",
"widget-selectshipping-button-sherpa": "Local delivery by Sherpa",
"widget-selectshipping-button-sherpa-desc": "Delivery at a specified date & time. Checks items are available. Other shipping options may also be shown at checkout",
"widget-selectshipping-button-shipping": "Other shipping methods",
"widget-selectshipping-button-shipping-desc": "Displays all other shipping options at checkout",
"widget-checkpostcode-title": "Check delivery eligibility",
"widget-checkpostcode-desc": "Enter your postcode to see if all items in your cart are available for local delivery.",
"widget-input-placeholder": "Enter your postcode to check if we can deliver",
"widget-invalid-postcode": "Please double check you have entered a valid postcode.",
"widget-all-fulfillable": "Great! Items in your cart are available for local delivery.",
"widget-none-fulfillable": "Local delivery not available to your location. Other shipping options, if available, will be shown at checkout.",
"widget-no-rates": "Local delivery not available to your location. Other shipping options, if available, will be shown at checkout.",
"widget-removed-offending": "Items not available for local delivery to your location have been removed from your cart.",
"widget-calendar-placeholder": "Select a date",
"widget-delivery-notes-title": "Note for delivery driver",
"widget-delivery-notes-placeholder": "Add note",
"modal-title": "Local delivery is not available for some items",
"modal-desc": "Please remove these items to proceed",
"modal-button": "Remove items and continue",
"modal-skip-button": "Skip this step - other shipping options will be shown at checkout",
};
var widgetLoading = false;
var widgetLoaded = false;
var checkoutItems = {};
var itemsToRemove = [];
var checkoutSelector = "[name='checkout']";
var insertionSelector = "[name='checkout']";
var sherpaIdVar = '_sherpaid';
var showLocationWidget = true;
var showCalendarWidget = false;
var showDeliveryNotes = false;
var calendarBlockoutDates = {"60983115822":["2023-06-12","2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-12-25","2025-01-01"],"61800022062":["2023-06-12","2023-10-01","2023-10-02","2023-11-25","2023-11-26","2023-12-24","2023-12-25","2023-12-26","2023-12-31","2024-01-01","2024-01-26","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-04-22","2024-04-23","2024-04-24","2024-04-25","2024-06-10","2024-10-07","2024-12-25","2024-12-26","2024-12-27","2024-12-28","2024-12-29","2024-12-30","2024-12-31","2025-01-01","2025-01-02"],"60957458478":["2023-06-12","2023-09-29","2023-09-30","2023-10-01","2023-10-02","2023-12-25","2023-12-26","2024-01-01","2024-01-02","2024-01-03","2024-01-04","2024-01-05","2024-01-06","2024-01-07","2024-01-08","2024-01-09","2024-01-10","2024-01-11","2024-01-12","2024-01-13","2024-01-14","2024-01-26","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-04-25","2024-06-10","2024-07-19","2024-07-20","2024-07-21","2024-07-22","2024-10-04","2024-10-05","2024-10-06","2024-10-07","2024-12-25","2024-12-26","2025-01-01"],"60983083054":["2023-06-12","2023-08-14","2023-08-15","2023-08-16","2023-11-26","2023-12-25","2023-12-26","2024-01-01","2024-03-29","2024-03-31","2024-12-25","2024-12-26","2024-12-27","2025-01-01"],"60983246894":["2023-06-12","2023-11-26","2023-12-25","2023-12-26","2024-01-01","2024-01-26","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-10-07","2024-12-25","2025-01-01"],"60974858286":["2023-06-12","2023-11-26","2023-12-25","2023-12-26","2024-01-01","2024-12-25","2024-12-26","2025-01-01"],"36622860334":["2023-06-12","2024-06-10"],"61323378734":"","60983050286":["2023-06-12","2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-31"],"36570726446":["2023-06-12","2023-11-26","2023-12-25","2024-01-01"],"61799399470":["2023-06-12","2023-11-26","2023-12-25","2024-01-01"],"60957491246":["2023-06-12","2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-31"],"60982984750":["2023-06-12","2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-30"],"61799366702":["2023-06-12","2023-11-26","2023-12-25","2023-12-26","2024-01-01","2024-03-29","2024-03-31","2024-04-01"],"60957425710":["2023-06-12","2023-10-02","2023-11-26","2023-12-24","2023-12-25","2023-12-31","2024-01-01","2024-01-26","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-06-10","2024-10-07","2024-12-25","2024-12-26","2024-12-27","2024-12-28","2025-01-01"],"60983214126":["2023-06-12","2023-10-02","2023-11-26"],"60973383726":["2023-06-12","2023-10-19","2023-10-20","2023-10-21","2023-10-22","2023-10-23","2023-10-24","2023-10-25","2023-10-26","2023-10-27","2023-11-07","2023-11-26","2023-12-24","2023-12-25","2023-12-26","2023-12-31","2024-01-01","2024-01-26","2024-03-29","2024-04-01","2024-06-10"],"61822107694":["2023-06-12","2023-11-26","2023-12-25","2024-01-01","2024-02-14","2024-12-25","2025-01-01"],"61822238766":["2023-06-12","2023-11-26","2023-12-25","2023-12-26","2024-01-01","2024-03-31","2024-04-01"],"61822173230":["2023-06-12","2023-11-26","2023-12-25","2024-01-01","2024-03-29"],"61822205998":["2023-06-12","2023-11-07","2023-11-26","2023-12-24","2023-12-25","2023-12-26","2024-01-01","2024-01-26"],"61832036398":["2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-31","2024-12-25","2025-01-01"],"61831970862":["2023-11-26","2023-12-25","2024-01-01","2024-01-26","2024-03-29","2024-03-31","2024-12-25","2025-01-01"],"61832003630":["2023-11-26"],"61832069166":["2023-11-26","2023-12-25","2024-01-01"],"61832134702":["2023-11-26","2023-12-25","2024-01-01"],"61832101934":["2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-31"],"61833314350":["2023-11-26","2023-12-25","2023-12-26","2023-12-31","2024-01-01","2024-01-26","2024-03-29","2024-03-31","2024-12-25","2024-12-26","2025-01-01"],"61833347118":["2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-30","2024-12-25","2025-01-01"],"61837017134":["2023-11-26","2023-12-25","2024-01-01","2024-12-25","2025-01-01"],"61836656686":["2023-11-26","2023-12-25","2024-01-01","2024-12-25","2025-01-01"],"61836984366":["2023-09-30","2023-10-01","2023-10-02","2023-11-26","2023-12-23","2023-12-24","2023-12-25","2023-12-26","2023-12-30","2023-12-31","2024-01-01","2024-01-26","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-10-05","2024-10-06","2024-10-07","2024-12-25","2024-12-26","2024-12-27","2024-12-28","2024-12-29","2025-01-01"],"61901832238":["2023-11-26","2023-12-25","2024-01-01","2024-12-25","2025-01-01"],"61901766702":["2023-11-07","2023-11-26","2023-12-24","2023-12-25","2023-12-26","2023-12-31","2024-01-01","2024-01-26","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-06-10","2024-12-25","2025-01-01"],"61901799470":["2023-11-26","2023-12-25","2024-01-01","2024-03-29"],"61901701166":["2023-10-24","2023-11-07","2023-11-26","2023-12-23","2023-12-24","2023-12-25","2023-12-26","2023-12-27","2023-12-28","2023-12-29","2023-12-30","2023-12-31","2024-01-01","2024-01-02","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-06-10"],"62006034478":["2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-12-25","2025-01-01"],"62006067246":["2023-11-26"],"62006100014":["2023-11-26","2023-12-24","2023-12-25","2023-12-26","2024-01-01","2024-03-29"],"62006165550":["2023-11-26","2023-12-25","2024-01-01","2024-01-26","2024-03-29","2024-03-31","2024-04-01"],"62006132782":["2023-11-26","2023-12-25","2024-01-01","2024-03-29","2024-03-30","2024-03-31","2024-04-01","2024-04-02"],"62006001710":["2023-11-26","2023-12-25","2024-01-01","2024-03-29"],"62113546286":["2023-11-26","2023-12-25","2024-01-01","2024-03-29"],"62114168878":["2023-11-26","2023-12-25","2024-01-01"],"62553358382":"","62577082414":["2024-03-29","2024-03-31","2024-04-01","2024-06-10","2024-10-07"],"62577246254":["2024-03-29","2024-03-31","2024-04-01","2024-06-10"],"63778455598":["2024-12-25"]};
var allowPopupSkip = false;
var selectedDate = null;
var selectedLocation = null;
var shipWithSherpa = false;
var isWidgetLoaded = function() {
return widgetLoaded;
};
var destroy = function($) {
console.log('sherpa destroy');
$('.sherpa-container').remove();
$(document).off('click','.sherpa_btnSkip');
$(document).off('click','.sherpa-modal-close');
$(document).off('click','.sherpa_btnUseSherpa');
$(document).off('click','.sherpa_btnUseShipping');
$(document).off('keydown','.sherpa_txtPostcode');
$(document).off('click','.sherpa_btnCheckPostcode');
$(document).off('click','.sherpa_btnRemoveItems');
$(document).off('click','.sherpa_btnCalendar');
$(document).off('keydown','#sherpa-deliverynotes');
};
var checkCartTags = function($) {
console.log('checkCartTags');
var inflight = 0;
var hasExcludeTag = false;
$.ajax({
type: "GET",
url: "/cart.js",
processData: false,
dataType: 'json',
async: false,
success: function(cart) {
$(cart.items).each(function(k,v) {
inflight += 1;
$.ajax({
type: "GET",
url: "/products/"+v.handle+".js",
processData: false,
dataType: 'json',
async: true,
success: function(product) {
inflight -= 1;
if (product.tags.indexOf('sherpa-exclude') > -1) {
hasExcludeTag = true;
}
if (inflight == 0) {
if (hasExcludeTag) {
console.log('skip UI: has sherpa-exclude');
//re-enable checkout button
$(checkoutSelector).prop('disabled',false).removeClass('disabled');
} else {
initUIElements($);
}
}
}
});
});
}
});
};
var addCartToken = function($,forceUpdate) {
console.log('addCartToken');
if (typeof forceUpdate == 'undefined') force = false;
var cartToken = null;
var cartData = {};
//get current cart
$.ajax({
type: "GET",
url: "/cart.js",
processData: false,
dataType: 'json',
async: false,
success: function(cart) {
//set on all items in the cart that requires shipping
cartToken = cart.token;
console.log('cart token: '+cartToken);
var sherpaID = 'T='+cartToken;
if (showCalendarWidget) {
sherpaID = sherpaID+'&D='+selectedDate;
}
sherpaID = sherpaID+'&TS='+Date.now();
for (idx=0; idx < cart.items.length; idx++) {
var item = cart.items[idx];
if (item.requires_shipping && (!(item.properties && item.properties[sherpaIdVar] && item.properties[sherpaIdVar] != '') || forceUpdate)) {
cartData.line = idx+1;
cartData.quantity = item.quantity;
cartData.properties = item.properties;
if (cartData.properties === null) {
//no existing properties
cartData.properties = {};
cartData.properties[sherpaIdVar] = sherpaID;
} else {
cartData.properties[sherpaIdVar] = sherpaID;
}
$.ajax({
url: '/cart/sherpachange.js',
type: 'POST',
dataType: 'json',
data: cartData,
async: false,
complete: function(data) {
console.log('line item updated');
}
});
}
}
}
});
};
var removeCartToken = function($) {
console.log('removeCartToken');
//get current cart
$.ajax({
type: "GET",
url: "/cart.js",
processData: false,
dataType: 'json',
async: false,
success: function(cart) {
for (idx=0; idx < cart.items.length; idx++) {
var item = cart.items[idx];
if (item.properties && item.properties[sherpaIdVar]) {
console.log('property requires removing: '+(idx+1));
var cartData = {};
cartData.line = idx+1;
cartData.quantity = item.quantity;
cartData.properties = item.properties;
cartData.properties[sherpaIdVar] = '';
$.ajax({
url: '/cart/sherpachange.js',
type: 'POST',
dataType: 'json',
data: cartData,
async: false,
success: function(data) {
console.log('removed property line: '+cartData.line);
}
});
}
}
}
});
};
var showNextStep = function($,currentStep) {
if (currentStep == 'SHOWEND') {
$(checkoutSelector).prop('disabled',false).removeClass('disabled');
}
if (currentStep == 'LOCATION') {
if (showCalendarWidget && shipWithSherpa) {
$('.sherpa-wrapper-calendar').show();
} else {
if (shipWithSherpa) $('.sherpa-wrapper-deliverynotes').show();
$(checkoutSelector).prop('disabled',false).removeClass('disabled');
}
}
if (currentStep == 'CALENDAR') {
$(checkoutSelector).prop('disabled',false).removeClass('disabled');
$('.sherpa-wrapper-deliverynotes').show();
}
};
var getDateStr = function(date) {
var _day = date.getDate()+'';
var _month = (date.getMonth()+1)+'';
if (_day.length == 1) _day = '0'+_day;
if (_month.length == 1) _month = '0'+_month;
return date.getFullYear()+'-'+_month+'-'+_day;
};
var dateFormatddmmyyyy = function(date) {
var day = date.getDate()+"";
var month = (date.getMonth()+1)+"";
var year = date.getFullYear();
if (day.length == 1) day = '0'+day;
if (month.length == 1) month = '0'+month;
return day+'/'+month+'/'+year;
};
var calendarAvailDOW = [];
var isDateValid = function($,date,cutoffTimeDOW,_readyTS) {
var dow = new Array(7);
dow[0] = "sun";
dow[1] = "mon";
dow[2] = "tue";
dow[3] = "wed";
dow[4] = "thu";
dow[5] = "fri";
dow[6] = "sat";
// console.log('selected location: '+selectedLocation+', date: '+getDateStr(date));
cutoffTimeLoc = selectedLocation;
if (cutoffTimeLoc === null || typeof cutoffTimeDOW[cutoffTimeLoc] === 'undefined') cutoffTimeLoc = 'DEFAULT';
blockoutLoc = selectedLocation;
if (blockoutLoc === null || typeof calendarBlockoutDates[blockoutLoc] === 'undefined') blockoutLoc = 'DEFAULT';
var mydow = dow[date.getDay()];
var mydateStr = getDateStr(date);
var _date = new Date(date.getTime());
//check business hour dow
if (calendarAvailDOW.length > 0) {
if (!calendarAvailDOW[mydow]) {
return false;
}
}
//check blockout date
if (typeof calendarBlockoutDates[blockoutLoc] !== 'undefined' && calendarBlockoutDates[blockoutLoc].includes(mydateStr)) {
return false;
}
if (cutoffTimeDOW[cutoffTimeLoc][mydow] === false) {
return false;
} else {
var matches = cutoffTimeDOW[cutoffTimeLoc][mydow].match(/(\d+)(\d\d)$/);
var windowEndHr = parseInt(matches[1]);
var windowEndMin = parseInt(matches[2]);
_date.setHours(windowEndHr,windowEndMin);
if ((_date.getTime()/1000) < _readyTS) {
//is today and after cutoff time
return false;
} else {
return true;
}
}
};
var _postcodeAutoSubmitTimer = null;
var initUI = function($) {
if ($(checkoutSelector).length > 0) {
//disable checkout button
$(checkoutSelector).prop('disabled',true).addClass('disabled');
checkCartTags($);
}
};
var initUIElements = function($) {
console.log('cart page found');
addAjaxHooks($);
//add css
var css = document.createElement('link');
css.rel = 'stylesheet';
css.type = 'text/css';
css.href = '//plugin-app.sherpa.net.au/css/widget.css?20211124';
$('head').append(css)
//disable checkout button
$(checkoutSelector).prop('disabled',true).addClass('disabled');
var css = document.createElement('link');
css.rel = 'stylesheet';
css.type = 'text/css';
css.href = '//plugin-app.sherpa.net.au/css/jquery-ui.min.css?20211124';
$('head').append(css)
var html = $("
");
var insertionPoint = $(insertionSelector);
//check to see if there is a cart__actions
if ($(insertionPoint).closest('.cart__actions').length > 0) {
insertionPoint = $(insertionPoint).closest('.cart__actions');
}
$(insertionPoint).before(html);
//show the postcode checker widget
if (showLocationWidget) {
//add widget
console.log('load multi-location widget');
var html = $("