PostNL = { /* * Init * * Initialize the PostNL checkout. * */ data: {}, currentLocation: {}, DELIVERY_MORNING: 'morning', DELIVERY_NORMAL: 'standard', DELIVERY_EVENING: 'avond', DELIVERY_PICKUP: 'retail', DELIVERY_PICKUP_EXPRESS: 'retailexpress', DELIVERY_SIGNATURE: 0, DELIVERY_ONLY_RECIPIENT: 0, SPLIT_STREET_REGEX: /(.*?)\s?(\d{1,4})[/\s\-]{0,2}([a-zA-Z]{1}\d{1,3}|-\d{1,4}|\d{2}\w{1,2}|[a-zA-Z]{1}[a-zA-Z\s]{0,3})?$/g, init: function() { this.data = JSON.parse(wcmp_config); isMobile = jQuery(window).width() < 980; /* Titles of the options*/ if (PostNL.data.textToTranslate.deliveryTitle) { jQuery('#post-delivery-title').html(PostNL.data.textToTranslate.deliveryTitle); } if (PostNL.data.textToTranslate.headerDeliveryOptions) { jQuery('#post-delivery-options-title').html(PostNL.data.textToTranslate.headerDeliveryOptions); jQuery('#header-delivery-options-title').show(); } if (PostNL.data.textToTranslate.onlyRecipientTitle) { jQuery('#post-only-recipient-title').html(PostNL.data.textToTranslate.onlyRecipientTitle); } if (PostNL.data.textToTranslate.signatureTitle) { jQuery('#post-signature-title').html(PostNL.data.textToTranslate.signatureTitle); } if (PostNL.data.textToTranslate.pickupTitle) { jQuery('#post-pickup-title').html(PostNL.data.textToTranslate.pickupTitle); jQuery('.post-pickup-delivery-titel').html(PostNL.data.textToTranslate.pickUpFrom); } /* Prices */ var prices = { 'morning': this.data.config.priceMorningDelivery, 'evening': this.data.config.priceEveningDelivery, 'normal': this.data.config.priceNormalDelivery, 'signature': this.data.config.priceSignature, 'only-recipient': this.data.config.priceOnlyRecipient, 'pickup': this.data.config.pricePickup }; PostNL.showPrices(prices); PostNL.callDeliveryOptions(); /* Engage defaults */ PostNL.hideDelivery(); jQuery('#method-postnl-normal').click(); PostNL.bind(); }, showPrices: function(prices) { jQuery.each(prices, function(selectName, price) { jQuery('#post-' + selectName + '-delivery, #post-' + selectName + '-price').html(PostNL.getPriceHtml(price)); }); }, getPriceHtml: function(priceOfDeliveryOption) { var price; if (!priceOfDeliveryOption) { price = ""; } if (parseFloat(priceOfDeliveryOption) >= 0) { price = '+ € ' + Number(priceOfDeliveryOption).toFixed(2).replace(".", ","); } if (parseFloat(priceOfDeliveryOption) < 0) { price = "

"+'- € ' + Number(priceOfDeliveryOption).toFixed(2).replace(/-|\./g,function(match) {return (match==".")?",":""})+"

"; } if (priceOfDeliveryOption && isNaN(parseFloat(priceOfDeliveryOption))) { price = priceOfDeliveryOption; } return price; }, setCurrentDeliveryOptions: function() { if (typeof PostNL.storeDeliveryOptions === 'undefined') { console.error('setCurrentDeliveryOptions() PostNL.storeDeliveryOptions === undefined'); return; } var selectedDate = jQuery('#post-select-date').val(); var selectDateKey = PostNL.storeDeliveryOptions.data.delivery[selectedDate]['time']; PostNL.hideMorningDelivery(); PostNL.hideEveningDelivery(); jQuery.each(selectDateKey, function(key, value) { if (value['price_comment'] == 'morning' && PostNL.data.config.allowMorningDelivery) { var morningTitle = PostNL.data.textToTranslate.deliveryMorningTitle; PostNL.getDeliveryTime(morningTitle, 'morning', value['start'], value['end']); PostNL.showMorningDelivery(); } if (value['price_comment'] == 'standard') { var standardTitle = PostNL.data.textToTranslate.deliveryStandardTitle; if (PostNL.data.address.cc === 'BE') { standardTitle = PostNL.data.textToTranslate.BEdeliveryStandardTitle; } PostNL.getDeliveryTime(standardTitle, 'standard', value['start'], value['end']); } if (value['price_comment'] == 'avond' && PostNL.data.config.allowEveningDelivery) { var eveningTitle = PostNL.data.textToTranslate.deliveryEveningTitle; PostNL.getDeliveryTime(eveningTitle, 'evening', value['start'], value['end']); PostNL.showEveningDelivery(); } }); }, getDeliveryTime: function(configDeliveryTitle, deliveryMoment, startTime, endTime) { startTime = startTime.replace(/(.*)\D\d+/, '$1'); endTime = endTime.replace(/(.*)\D\d+/, '$1'); jQuery('#post-' + deliveryMoment + '-title').html(configDeliveryTitle); if (!configDeliveryTitle) { jQuery('#post-' + deliveryMoment + '-title').html(startTime + ' - ' + endTime); } }, setCurrentLocation: function() { var locationId = jQuery('#post-pickup-location').val(); this.currentLocation = this.getPickupByLocationId(PostNL.storeDeliveryOptions.data.pickup, locationId); }, /* * Bind * * Bind actions to selectors. * */ bind: function() { jQuery('#post-submit').on('click', function(e) { e.preventDefault(); PostNL.exportDeliveryOptionToWebshop(); }); /* show default delivery options and hide PostNL options */ jQuery('#post-select-delivery').on('click', function() { PostNL.setCurrentDeliveryOptions(); PostNL.showDelivery(); PostNL.showShippingAddress(); PostNL.hidePickUpLocations(); }); /* hide default delivery options and show PostNL options */ jQuery('#post-pickup-delivery').on('click', function() { PostNL.hideDelivery(); PostNL.hideShippingAddress(); PostNL.showPickUpLocations(); }); jQuery('#method-postnl-delivery-morning, #method-postnl-delivery-evening').on('click', function() { PostNL.defaultCheckCheckbox('post-only-recipient'); }); /* Mobile specific triggers */ if (isMobile) { jQuery('#post-show-location-details').on('click', function() { PostNL.setCurrentLocation(); PostNL.showLocationDetails(); PostNL.hideDelivery(); }); } /* Desktop specific triggers */ else { jQuery('#post-show-location-details').on('click', function() { PostNL.setCurrentLocation(); PostNL.showLocationDetails(); }); } jQuery('#post-location-details').on('click', function() { PostNL.hideLocationDetails(); }); jQuery('#method-postnl-normal').on('click', function() { PostNL.defaultCheckCheckbox('method-postnl-normal'); }); // jQuery('#post-pickup-express').hide(); /* todo: move */ jQuery('#post-pickup-delivery, #post-pickup-location').on('change', function(e) { PostNL.setCurrentLocation(); PostNL.toggleDeliveryOptions(); PostNL.mapExternalWebshopTriggers(); }); jQuery('#post-select-date').on('change', function(e) { PostNL.setCurrentDeliveryOptions(); PostNL.mapExternalWebshopTriggers(); }); /* External webshop triggers */ jQuery('#post-load input, #post-load select').on('click', function() { PostNL.mapExternalWebshopTriggers() }); fields = window.postnl_is_using_split_address_fields ? '#billing_house_number, #shipping_house_number' : '#billing_address_1, #shipping_address_1'; jQuery('#billing_country, #shipping_country, #billing_postcode, #shipping_postcode, ' + fields).on('change', function() { PostNL.callDeliveryOptions(); }); }, mapExternalWebshopTriggers: function() { PostNL.DELIVERY_SIGNATURE = 0; PostNL.DELIVERY_ONLY_RECIPIENT = 0; PostNL.removeStyleFromPrice(); /** * Morning delivery * */ if (jQuery('#post-pickup-delivery').prop('checked') === false && jQuery('#method-postnl-delivery-morning').prop('checked')) { jQuery('#s_method_postnl_morning').click(); PostNL.DELIVERY_ONLY_RECIPIENT = 1; PostNL.addStyleToPrice('#post-morning-delivery, #post-only-recipient-price'); /** * Signature */ if (jQuery('#post-signature-selector').prop('checked')) { jQuery('#s_method_postnl_morning_signature').click(); PostNL.DELIVERY_SIGNATURE = 1; PostNL.addStyleToPrice('#post-signature-price'); } PostNL.addDeliveryToExternalInput(PostNL.DELIVERY_MORNING); return; } /** * Normal delivery * */ if (jQuery('#post-pickup-delivery').prop('checked') === false && jQuery('#method-postnl-normal').prop('checked')) { PostNL.addStyleToPrice('#post-normal-delivery'); /** * Signature and only recipient */ if (jQuery('#post-signature-selector').prop('checked') && jQuery('#post-only-recipient-selector').prop('checked')) { jQuery('#s_method_postnl_delivery_signature_and_only_recipient_fee').click(); PostNL.DELIVERY_SIGNATURE = 1; PostNL.DELIVERY_ONLY_RECIPIENT = 1; PostNL.addStyleToPrice('#post-signature-price, #post-only-recipient-price'); } else /** * Signature */ if (jQuery('#post-signature-selector').prop('checked')) { jQuery('#s_method_postnl_delivery_signature').click(); PostNL.DELIVERY_SIGNATURE = 1; PostNL.addStyleToPrice('#post-signature-price'); } else /** * Only recipient */ if (jQuery('#post-only-recipient-selector').prop('checked')) { jQuery('#s_method_postnl_delivery_only_recipient').click(); PostNL.DELIVERY_ONLY_RECIPIENT = 1; PostNL.addStyleToPrice('#post-only-recipient-price'); } else { jQuery('#s_method_postnl_flatrate, #s_method_postnl_tablerate').click(); } PostNL.addDeliveryToExternalInput(PostNL.DELIVERY_NORMAL); return; } /** * Evening delivery * */ if (jQuery('#post-pickup-delivery').prop('checked') === false && jQuery('#method-postnl-delivery-evening').prop('checked')) { jQuery('#s_method_postnl_evening').click(); PostNL.DELIVERY_ONLY_RECIPIENT = 1; PostNL.addStyleToPrice('#post-evening-delivery, #post-only-recipient-price'); /** * Signature */ if (jQuery('#post-signature-selector').prop('checked')) { jQuery('#s_method_postnl_evening_signature').click(); PostNL.DELIVERY_SIGNATURE = 1; PostNL.addStyleToPrice('#post-signature-price'); } PostNL.addDeliveryToExternalInput(PostNL.DELIVERY_EVENING); return; } /** * Pickup * */ if (jQuery('#post-pickup-delivery').prop('checked') || jQuery('#post-pickup-selector').prop('checked')) { /** * Early morning pickup */ if (jQuery('#post-pickup-express-selector').prop('checked')) { jQuery('#s_method_postnl_pickup_express').click(); PostNL.addPickupToExternalInput(PostNL.DELIVERY_PICKUP_EXPRESS); PostNL.addStyleToPrice('#post-pickup-express-price'); return; } else { PostNL.addStyleToPrice('#post-pickup-price'); } jQuery('#s_method_postnl_pickup').click(); PostNL.addPickupToExternalInput(PostNL.DELIVERY_PICKUP); } }, addPickupToExternalInput: function(selectedPriceComment) { var locationId = jQuery('#post-pickup-location').val(); var currentLocation = PostNL.getPickupByLocationId(PostNL.storeDeliveryOptions.data.pickup, locationId); var result = jQuery.extend({}, currentLocation); /* If pickup; convert pickup express to pickup */ if (selectedPriceComment === PostNL.DELIVERY_PICKUP) { result.price_comment = PostNL.DELIVERY_PICKUP; } jQuery('body').trigger('update_checkout'); jQuery('#post-input').val(JSON.stringify(result)); }, addDeliveryToExternalInput: function(deliveryMomentOfDay) { var deliveryDateId = jQuery('#post-select-date').val(); var currentDeliveryData = PostNL.triggerDefaultOptionDelivery(deliveryDateId, deliveryMomentOfDay); if (currentDeliveryData !== null) { currentDeliveryData.signature = PostNL.DELIVERY_SIGNATURE; currentDeliveryData.only_recipient = PostNL.DELIVERY_ONLY_RECIPIENT; jQuery('#post-input').val(JSON.stringify(currentDeliveryData)); } jQuery('body').trigger('update_checkout'); }, addStyleToPrice: function(chosenDelivery) { jQuery(chosenDelivery).addClass('post-bold-price'); }, removeStyleFromPrice: function() { jQuery('.post-delivery-option-table').find("span").removeClass('post-bold-price'); }, triggerDefaultOptionDelivery: function(deliveryDateId, deliveryMomentOfDay) { var dateArray = PostNL.data.deliveryOptions.data.delivery[deliveryDateId]; var currentDeliveryData = null; jQuery.each(dateArray['time'], function(key, value) { if (value.price_comment === deliveryMomentOfDay) { currentDeliveryData = jQuery.extend({}, dateArray); currentDeliveryData['time'] = [value]; } }); if (currentDeliveryData === null) { jQuery('#post-only-recipient-selector').prop('disabled', false).prop('checked', false); jQuery('#method-postnl-normal').prop('checked', true); PostNL.mapExternalWebshopTriggers(); } return currentDeliveryData; }, /* * defaultCheckCheckbox * * Check the additional options that are required for certain delivery options * */ defaultCheckCheckbox: function(selectedOption) { if (selectedOption === 'post-only-recipient') { jQuery('#post-only-recipient-selector').prop('checked', true).prop({disabled: true}); jQuery('#post-only-recipient-price').html('Inclusief'); } else { jQuery('#post-only-recipient-selector').prop('checked', false).removeAttr("disabled"); jQuery('#post-only-recipient-price').html(PostNL.getPriceHtml(this.data.config.priceOnlyRecipient)); } }, /* * toggleDeliveryOptions * * Shows and hides the display options that are valid for the recipient only and signature required pre-selectors * */ toggleDeliveryOptions: function() { var isPickup = jQuery('#post-pickup-delivery').is(':checked'); jQuery('#post-pickup-selector').prop('checked', true); if (isPickup && this.currentLocation.price_comment === PostNL.DELIVERY_PICKUP_EXPRESS && this.data.config.allowPickupExpress) { jQuery('#post-pickup-express-price').html(PostNL.getPriceHtml(this.data.config.pricePickupExpress)); jQuery('#post-pickup-express').show(); } else { jQuery('#post-pickup-express-selector').attr("checked", false); jQuery('#post-pickup-express').hide(); } }, /* * exportDeliverOptionToWebshop * * Exports the selected delivery option to the webshop. * */ exportDeliveryOptionToWebshop: function() { var deliveryOption = ""; var selected = jQuery("#post-delivery-option-form").find("input[type='radio']:checked"); if (selected.length > 0) { deliveryOption = selected.val(); } }, /* * hideMessage * * Hides pop-up message. * */ hideMessage: function() { jQuery('.post-message-model').hide(); jQuery('#post-delivery-option-form').show(); }, /* * hideMessage * * Hides pop-up message. * */ showMessage: function(message) { jQuery('.post-message-model').show(); jQuery('#post-message').html(message).show(); jQuery('#post-delivery-option-form').hide(); }, /* * hideDelivery * * Hides interface part for delivery. * */ hideDelivery: function() { jQuery('#post-delivery-date-text,.post-extra-delivery-options').hide(); jQuery('#post-select-date').parent().parent().hide(); jQuery('#post-delivery').parent().parent().hide(); PostNL.hideSignature(); PostNL.hideOnlyRecipient(); PostNL.hideMorningDelivery(); PostNL.hideEveningDelivery(); }, /* * showDelivery * * Shows interface part for delivery. * */ showDelivery: function() { jQuery('#post-delivery').parent().parent().show(); if (PostNL.data.address.cc === "NL") { jQuery('#post-delivery-selectors-' + this.data.address.cc.toLowerCase()).show(); jQuery('.post-extra-delivery-options').show(); if (this.data.config.deliverydaysWindow >= 2) { jQuery('#post-delivery-date-select').show(); } PostNL.hideSignature(); if (this.data.config.allowSignature) { PostNL.showSignature(); } PostNL.hideOnlyRecipient(); if (this.data.config.allowOnlyRecipient) { PostNL.showOnlyRecipient(); } } if (PostNL.data.address.cc === 'BE') { jQuery('#post-delivery-title').html(PostNL.data.config.BEdeliveryTitle); jQuery('#post-delivery-date-text').hide(); } }, hideAllDeliveryOptions: function() { jQuery('#post-load').hide(); }, showAllDeliveryOptions: function() { jQuery('#post-load').show(); }, /* * showSpinner * * Shows the PostNL spinner. * */ showSpinner: function() { jQuery('#post-delivery-option-form').hide(); jQuery('.post-message-model').hide(); jQuery('#post-spinner-model').show(); }, /* * hideSpinner * * Hides the PostNL spinner. * */ hideSpinner: function() { jQuery('#post-spinner-model').hide(); }, showMorningDelivery: function() { jQuery('#method-postnl-delivery-morning-div').show(); }, hideMorningDelivery: function() { jQuery('#method-postnl-delivery-morning-div').hide(); }, showEveningDelivery: function() { jQuery('#method-postnl-delivery-evening-div').show(); }, hideEveningDelivery: function() { jQuery('#method-postnl-delivery-evening-div').hide(); }, showSignature: function() { jQuery('.post-extra-delivery-option-signature, #post-signature-price').show(); }, hideSignature: function() { jQuery('.post-extra-delivery-option-signature, #post-signature-price').hide(); }, showOnlyRecipient: function() { jQuery('#post-only-recipient, #post-only-recipient-price').parent().show(); }, hideOnlyRecipient: function() { jQuery('#post-only-recipient, #post-only-recipient-price').parent().hide(); }, /* * dateToString * * Convert api date string format to human readable string format * */ dateToString: function(apiDate) { var deliveryDate = apiDate; var dateArr = deliveryDate.split('-'); var dateObj = new Date(dateArr[0], dateArr[1] - 1, dateArr[2]); var day = ("0" + (dateObj.getDate())).slice(-2); var month = ("0" + (dateObj.getMonth() + 1)).slice(-2); return this.data.txtWeekDays[dateObj.getDay()] + " " + day + "-" + month + "-" + dateObj.getFullYear(); }, /* * showDeliveryDates * * Show possible delivery dates. * */ showDeliveryDates: function() { var html = ""; var deliveryWindow = parseInt(PostNL.data.config.deliverydaysWindow); jQuery.each(PostNL.data.deliveryOptions.data.delivery, function(key, value) { html += '\n'; }); /* Hide the day selector when the value of the deliverydaysWindow is 0*/ if (deliveryWindow === 0) { jQuery('#post-delivery-date-select').hide(); } /* When deliverydaysWindow is 1, hide the day selector and show a div to show the date */ if (deliveryWindow === 1) { jQuery('#post-select-date').hide(); jQuery('#post-delivery-date-text').show(); } /* When deliverydaysWindow > 1, show the day selector */ if (deliveryWindow > 1) { jQuery('#post-select-date').show(); } jQuery('#post-select-date, #post-date').html(html); }, hideDeliveryDates: function() { jQuery('#post-delivery-date-text').parent().hide(); }, /* * clearPickupLocations * * Clear pickup locations and show a non-value option. * */ clearPickUpLocations: function() { var html = ''; jQuery('#post-pickup-location').html(html); }, /* * hidePickupLocations * * Hide the pickup location option. * */ hidePickUpLocations: function() { if (!PostNL.data.config.allowPickupPoints) { jQuery('#post-pickup-location-selector').hide(); } jQuery('#post-pickup-options, #post-pickup, #post-pickup-express').hide(); }, /* * showPickupLocations * * Shows possible pickup locations, from closest to further. * */ showPickUpLocations: function() { if (false === PostNL.data.config.allowPickupPoints) { return; } var html = ""; jQuery.each(PostNL.data.deliveryOptions.data.pickup, function(key, value) { var distance = parseFloat(Math.round(value.distance) / 1000).toFixed(1); html += '\n"; }); jQuery('#post-pickup-location').html(html).prop("checked", true); jQuery('#post-pickup-location-selector, #post-pickup-options, #post-pickup').show(); }, /* * hideLocationDetails * * Hide the detailed information pop-up for selected location. * */ hideLocationDetails: function() { jQuery('#post-delivery-option-form').show(); jQuery('#post-location-details').hide(); }, /* * showLocationDetails * * Shows the detailed information pop-up for the selected pick-up location. */ showLocationDetails: function() { var html = ""; var locationId = jQuery('#post-pickup-location').val(); var currentLocation = PostNL.getPickupByLocationId(PostNL.storeDeliveryOptions.data.pickup, locationId); var startTime = currentLocation.start_time; /* Strip seconds if present */ if (startTime.length > 5) { startTime = startTime.slice(0, -3); } html += '' html += '

' + currentLocation.location + '

'; html += ''; html += '' + currentLocation.street + ' ' + this.currentLocation.number + ''; html += '' + currentLocation.postal_code + ' ' + currentLocation.city + ''; if (currentLocation.phone_number) { html += '' + currentLocation.phone_number + ''; } html += '' + PostNL.data.textToTranslate.pickUpFrom + ': ' + startTime + ''; html += '

' + PostNL.data.textToTranslate.openingHours + '

'; jQuery.each( currentLocation.opening_hours, function(weekday, value) { html += '' + PostNL.data.textToTranslate[weekday] + ""; if (value[0] === undefined) { html += '' + PostNL.data.textToTranslate.closed + ''; } jQuery.each(value, function(key2, times) { html += '' + times + ""; }); html += "
"; }); jQuery('#post-delivery-option-form').hide(); jQuery('#post-location-details').html(html).css('display', 'inline-block'); }, /* * hideShippingAddress * * Hide the "ship to different address" section. * */ hideShippingAddress: function() { jQuery('.woocommerce-shipping-fields').hide(); }, /* * showShippingAddress * * Show the "ship to different address" section. * */ showShippingAddress: function() { jQuery('.woocommerce-shipping-fields').show(); }, /* * getPickupByLocationId * * Find the location by id and return the object. * */ getPickupByLocationId: function(obj, locationId) { var object; jQuery.each(obj, function(key, info) { if (info.location_code === locationId) { object = info; return false; } }); return object; }, /* * retryPostalCodeHouseNumber * * After detecting an unrecognised postal code / house number combination the user can try again. * This function copies the newly entered data back into the webshop forms. * */ retryPostalCodeHouseNumber: function() { var retryPostalCode = jQuery('#post-error-postcode').val(); var retryNumber = jQuery('#post-error-number').val(); if (window.postnl_is_using_split_address_fields) { jQuery('#billing_house_number').val(retryNumber); } else { address = PostNL.data.address.street + ' ' + retryNumber; if (typeof PostNL.data.address.numberSuffix !== 'undefined') { address += PostNL.data.address.numberSuffix } jQuery('#billing_address_1').val(address); } jQuery('#billing_postcode').val(retryPostalCode); PostNL.callDeliveryOptions(); jQuery('#post-select-delivery').click(); }, /* * showFallBackDelivery * * If the API call fails and we have no data about delivery or pick up options * show the customer an "As soon as possible" option. */ showFallBackDelivery: function() { PostNL.hideSpinner(); PostNL.hideDelivery(); jQuery('#post-select-date, #method-postnl-normal-div, .post-is-pickup-element').hide(); jQuery('#post-select-delivery-title').html('Zo snel mogelijk bezorgen'); }, /* * showRetry * * If a customer enters an unrecognised postal code & house number combination show a * pop-up so they can try again. */ showRetry: function() { PostNL.showMessage( '

' + PostNL.data.textToTranslate.wrongHouseNumberPostcode + '

' + '
' + '' + '' + '
' + '' + '' + '
' + PostNL.data.textToTranslate.again + '
' + '
' ); /* remove trigger that closes message */ jQuery('#post-message').off('click'); /* bind trigger to new button */ jQuery('#post-error-try-again').on('click', function() { PostNL.retryPostalCodeHouseNumber(); }); }, setAddressFromInputFields: function() { addressType = jQuery('#ship-to-different-address-checkbox').prop('checked') ? 'shipping' : 'billing'; address = PostNL.getAddressInputValues(addressType); if (!PostNL.getAddressInputValues('billing').postalCode) { return; } this.data.address.street = address.streetName; this.data.address.number = address.houseNumber; this.data.address.numberSuffix = address.houseNumberSuffix; this.data.address.cc = address.country; this.data.address.postalCode = address.postalCode; this.data.address.city = address.city; }, getAddressInputValues: function(type) { streetParts = {}; input = { 'fullStreet': jQuery('#' + type + '_address_1').val(), 'postalCode': jQuery('#' + type + '_postcode').val(), 'city': jQuery('#' + type + '_city').val(), 'country': jQuery('#' + type + '_country').val(), }; if (window.postnl_is_using_split_address_fields) { input.streetName = jQuery('#' + type + '_street_name').val(); input.houseNumber = jQuery('#' + type + '_house_number').val(); input.houseNumberSuffix = jQuery('#' + type + '_house_number_suffix').val(); } else { streetParts = PostNL.splitFullStreetFromInput(input.fullStreet); input.streetName = streetParts.streetName; input.houseNumber = streetParts.houseNumber; input.houseNumberSuffix = streetParts.houseNumberSuffix; } return input; }, /* * splitFullStreetFromInput * * Split full street into parts and returning empty array if there's no street entered */ splitFullStreetFromInput: function(fullStreet) { result = { streetName: '', houseNumber: '', houseNumberSuffix: '', }; if (fullStreet.length) { streetParts = new RegExp(PostNL.SPLIT_STREET_REGEX).exec(fullStreet); result.streetName = streetParts[1]; result.houseNumber = streetParts[2]; result.houseNumberSuffix = streetParts[3]; } return result; }, /* * callDeliveryOptions * * Calls the PostNL API to retrieve the pickup and delivery options for given house number and * Postal Code. * */ callDeliveryOptions: function() { PostNL.showSpinner(); PostNL.clearPickUpLocations(); PostNL.hideDelivery(); PostNL.setAddressFromInputFields(); // Hide PostNL field if there is no address entered if (PostNL.data.address.postalCode == '' || PostNL.data.address.number == '') { PostNL.hideSpinner(); PostNL.showMessage( '

' + PostNL.data.textToTranslate.addressNotFound + '

' ); return; } /* Check if the deliverydaysWindow == 0 and hide the select input*/ this.deliveryDaysWindow = this.data.config.deliverydaysWindow; if (this.deliveryDaysWindow === 0) { this.deliveryDaysWindow = 1; } /* Make the api request */ jQuery.get(this.data.config.apiBaseUrl + "delivery_options", { cc: this.data.address.cc, postal_code: this.data.address.postalCode.trim(), number: this.data.address.number.trim(), city: this.data.address.city, carrier: this.data.config.carrier, dropoff_days: this.data.config.dropOffDays, monday_delivery: this.data.config.allowMondayDelivery, deliverydays_window: this.deliveryDaysWindow, cutoff_time: this.data.config.cutoffTime, dropoff_delay: this.data.config.dropoffDelay }) .done(function(response) { PostNL.data.deliveryOptions = response; if (response.errors) { jQuery.each(response.errors, function(key, value) { /* Postal code & house number combination not found or not recognised. */ if (value.code == '3212' || value.code == '3505') { PostNL.showRetry(); } /* Any other error */ else { PostNL.showFallBackDelivery(); } }); } /* No errors */ else { PostNL.hideMessage(); PostNL.showPickUpLocations(); PostNL.showDeliveryDates(); if (PostNL.data.deliveryOptions.data.delivery.length <= 0) { PostNL.hideDeliveryDates(); } PostNL.storeDeliveryOptions = response; } PostNL.hideSpinner(); }) .fail(function() { PostNL.showFallBackDelivery(); }) .always(function() { jQuery('#post-select-delivery').click(); }); } }; // timeout because postcode api might take too long to respond setTimeout(function() { PostNL.init(); }, 2000);