(function ($, window, document){
'use strict';
function initShippingQuoteLogic(){
const formIds=[36, 46];
formIds.forEach(function (formId){
const $form=$('#gform_' + formId);
if(!$form.length) return;
let inboundXHR=null;
let outboundXHR=null;
function safeCalculateTotalPrice(){
if(typeof window.gformCalculateTotalPrice==='function'){
window.gformCalculateTotalPrice(formId);
}}
function recalcInbound(){
const opt=$form.find('[name="input_86"]:checked').val();
const qty=$form.find('[name="input_9.3"]').val();
const postcode=$form.find('[name="input_5.5"]').val();
const country=$form.find('[name="input_5.6"]').val()||'AU';
const expressFlag=$form.find('input[name="input_110.1"]').is(':checked') ? 1:0;
const inboundInsSel=$form.find('input[name="input_111.1"]').is(':checked') ? 1:0;
const inboundInsVal=parseInt($form.find('[name="input_112"]').val(), 10)||0;
if(opt!=='Prepaid'){
if(inboundXHR) inboundXHR.abort();
$form.find('[name="input_88"]').val(0);
$('#inbound-cost-display').text('$0.00');
safeCalculateTotalPrice();
return;
}
if(!qty||!postcode) return;
if(inboundXHR) inboundXHR.abort();
inboundXHR=$.post(slbdAjax.ajax_url,
{
action: 'slbd_calc_inbound',
nonce: slbdAjax.nonce,
card_qty: qty,
postcode: postcode,
country: country,
inbound_express_flag: expressFlag,
inbound_insurance: inboundInsSel,
inbound_insurance_value: inboundInsVal
},
function (resp){
if(resp.success){
const cost=parseFloat(resp.data.cost)||0;
$form.find('[name="input_88"]').val(resp.data.cost);
$('#inbound-cost-display').text('$' + cost.toFixed(2));
safeCalculateTotalPrice();
}},
'json'
).always(function (){
inboundXHR=null;
});
}
function recalcOutbound(){
const requiresReturn=$form.find('[name="input_60"]:checked').val();
const opt=$form.find('[name="input_90"]:checked').val();
const qty=$form.find('[name="input_9.3"]').val();
const postcode=$form.find('[name="input_59.5"]').val();
const country=$form.find('[name="input_59.6"]').val()||'AU';
const insSel=$form.find('input[name="input_113.1"]').is(':checked') ? 1:0;
const insVal=parseInt($form.find('[name="input_115"]').val(), 10)||0;
const openOrd=$form.find('[name="input_95"]').val()||'';
if(requiresReturn!=='Yes'){
if(outboundXHR) outboundXHR.abort();
$form.find('[name="input_91"]').val(0);
$('#outbound-cost-display').text('$0.00');
safeCalculateTotalPrice();
return;
}
if(!opt||!qty||!postcode){
if(outboundXHR) outboundXHR.abort();
$form.find('[name="input_91"]').val('');
$('#outbound-cost-display').text('');
safeCalculateTotalPrice();
return;
}
if(outboundXHR) outboundXHR.abort();
outboundXHR=$.post(slbdAjax.ajax_url,
{
action: 'slbd_calc_outbound',
nonce: slbdAjax.nonce,
card_qty: qty,
post_code: postcode,
country: country,
return_shipping_option: opt,
outbound_insurance: insSel,
outbound_insurance_value: insVal,
open_orders: openOrd
},
function (resp){
if(resp.success){
const cost=parseFloat(resp.data.cost)||0;
$form.find('[name="input_91"]').val(resp.data.cost);
$('#outbound-cost-display').text('$' + cost.toFixed(2));
safeCalculateTotalPrice();
}},
'json'
).always(function (){
outboundXHR=null;
});
}
$form.off('.slbdShipping');
$form.on('change.slbdShipping keyup.slbdShipping blur.slbdShipping',
'[name="input_86"],[name="input_9.3"],[name="input_5.5"],[name="input_110.1"],[name="input_111.1"],[name="input_112"]',
recalcInbound
);
$form.on('change.slbdShipping keyup.slbdShipping blur.slbdShipping',
'[name="input_60"],[name="input_90"],[name="input_9.3"],[name="input_59.5"],[name="input_113.1"],[name="input_115"]',
recalcOutbound
);
function formatCurrency(val){
return (Math.round(val * 100) / 100).toFixed(2);
}
const paymentRadios=$form.find('input[name="input_27"]');
const remainingInput=$form.find('#input_46_84');
const remainingField=remainingInput[0];
const displayDiv=$form.find('#tap-surcharge-display')[0];
function updateSurchargeDisplay(){
if(!remainingField||!displayDiv) return;
const selected=$form.find('input[name="input_27"]:checked').val();
const remainingRaw=remainingField.value||'0';
const remainingValue=parseFloat(remainingRaw.replace(/[^0-9.]/g, ''))||0;
if(selected==='TAP'||selected==='CASH_TAP'){
const surcharge=formatCurrency(remainingValue * 0.015);
const adjusted=formatCurrency(remainingValue + parseFloat(surcharge));
displayDiv.innerHTML=`
Surcharge (1.5%): <strong>$${surcharge}</strong><br>
Quote total including surcharge: <strong>$${adjusted}</strong><br>
Surcharge is automatically applied when using LivePayments terminal. Enter the total below ⬇️<br>
`;
$form.find('#input_' + formId + '_85').val(surcharge);
$form.find('#input_' + formId + '_87').val(adjusted);
}else{
displayDiv.innerHTML='';
$form.find('#input_' + formId + '_85').val('0');
$form.find('#input_' + formId + '_87').val('0');
}}
if(paymentRadios.length&&remainingField&&displayDiv){
paymentRadios.off('.slbdSurcharge');
remainingInput.off('.slbdSurcharge');
paymentRadios.on('change.slbdSurcharge', updateSurchargeDisplay);
remainingInput.on('input.slbdSurcharge change.slbdSurcharge keyup.slbdSurcharge', updateSurchargeDisplay);
updateSurchargeDisplay();
}});
}
function initCancellationDateLogic(){
const cancelFormId=19;
const consignmentDateFieldId=95;
const expiryDateFieldId=96;
const feeRequiredFieldId=97;
const form=document.querySelector('#gform_' + cancelFormId);
if(!form) return;
if(form.dataset.slbdCancellationDateBound==='1'){
queuePopulateCancellationDate();
return;
}
form.dataset.slbdCancellationDateBound='1';
function parseConsignmentDate(rawDate){
if(!rawDate) return null;
rawDate=String(rawDate).trim();
const mysqlMatch=rawDate.match(/^(\d{4})-(\d{2})-(\d{2})(?:\s+\d{2}:\d{2}:\d{2})?$/);
if(mysqlMatch){
return new Date(
parseInt(mysqlMatch[1], 10),
parseInt(mysqlMatch[2], 10) - 1,
parseInt(mysqlMatch[3], 10)
);
}
const auMatch=rawDate.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
if(auMatch){
return new Date(
parseInt(auMatch[3], 10),
parseInt(auMatch[2], 10) - 1,
parseInt(auMatch[1], 10)
);
}
return null;
}
function setFieldValue(field, value, triggerChange){
if(!field) return;
const oldValue=field.value||'';
const newValue=value||'';
if(oldValue===newValue){
return;
}
field.value=newValue;
if(triggerChange){
jQuery(field).trigger('change');
}}
function populateCancellationDate(){
const consignmentDateField=document.querySelector('#input_' + cancelFormId + '_' + consignmentDateFieldId
);
const expiryDateField=document.querySelector('#input_' + cancelFormId + '_' + expiryDateFieldId
);
const feeRequiredField=document.querySelector('#input_' + cancelFormId + '_' + feeRequiredFieldId
);
if(!consignmentDateField||!expiryDateField) return;
const originalDate=parseConsignmentDate(consignmentDateField.value);
if(!originalDate||isNaN(originalDate.getTime())){
setFieldValue(expiryDateField, '', false);
setFieldValue(feeRequiredField, '', true);
return;
}
const feeAppliesUntil=new Date(originalDate);
feeAppliesUntil.setDate(feeAppliesUntil.getDate() + 90);
feeAppliesUntil.setHours(0, 0, 0, 0);
const day=String(feeAppliesUntil.getDate()).padStart(2, '0');
const month=String(feeAppliesUntil.getMonth() + 1).padStart(2, '0');
const year=feeAppliesUntil.getFullYear();
const expiryValue=`${day}/${month}/${year}`;
const today=new Date();
today.setHours(0, 0, 0, 0);
const feeRequired=today <=feeAppliesUntil ? 'yes':'no';
setFieldValue(expiryDateField, expiryValue, false);
setFieldValue(feeRequiredField, feeRequired, true);
}
function queuePopulateCancellationDate(){
clearTimeout(form.slbdCancellationDateTimerOne);
clearTimeout(form.slbdCancellationDateTimerTwo);
clearTimeout(form.slbdCancellationDateTimerThree);
form.slbdCancellationDateTimerOne=setTimeout(populateCancellationDate, 400);
form.slbdCancellationDateTimerTwo=setTimeout(populateCancellationDate, 900);
form.slbdCancellationDateTimerThree=setTimeout(populateCancellationDate, 1500);
}
form.addEventListener('change', function (event){
const target=event.target;
if(!target) return;
if(target.tagName==='SELECT' ||
target.id==='input_' + cancelFormId + '_' + consignmentDateFieldId
){
queuePopulateCancellationDate();
}});
form.addEventListener('input', function (event){
const target=event.target;
if(!target) return;
if(target.id==='input_' + cancelFormId + '_' + consignmentDateFieldId){
queuePopulateCancellationDate();
}});
setTimeout(populateCancellationDate, 1000);
}
function bootSlbdGravityFormsScripts(){
initShippingQuoteLogic();
initCancellationDateLogic();
}
$(document).ready(bootSlbdGravityFormsScripts);
$(document).on('gform_post_render', function (){
bootSlbdGravityFormsScripts();
});
document.addEventListener('gform/postRender', function (){
bootSlbdGravityFormsScripts();
});
})(jQuery, window, document);