function SpreeDeliveryOptions() { var that = this; this.initializeDeliveryTimeSelect = function() { if ($('#order_delivery_date').length > 0) { this.update_delivery_time_options(); $('#order_delivery_date').change(function(event){ that.update_delivery_time_options(); }); } }; this.parseDeliveryOptions = function(deliveryDate) { var deliveryTimeGroups = $.parseJSON($('.delivery-time-options').attr("data")); var result; var baselineTime = "00:01"; $.each(deliveryTimeGroups[0], function(index, value) { if (baselineTime < index) { result = value; return false; } }); return result; }; this.update_delivery_time_options = function() { var dateParts = [] if ($('#order_delivery_date').val().indexOf('/') !== -1) { dateParts = $('#order_delivery_date').val().split('/') } else { dateParts = $('#order_delivery_date').val().split('-') } var deliveryDate = this.convertDateFormat(dateParts); var deliveryTimeOptions = this.parseDeliveryOptions(deliveryDate); if (deliveryTimeOptions){ weekdays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; var dayOptions = []; if (deliveryTimeOptions[deliveryDate]) { dayOptions = deliveryTimeOptions[deliveryDate]; } else { dayIndex = new Date(dateParts[0], dateParts[1]-1, dateParts[2]).getDay(); weekday = weekdays[dayIndex]; dayOptions = deliveryTimeOptions[weekday]; } this.populate_delivery_time(dayOptions); } }; this.convertDateFormat = function(dateParts) { var day = dateParts[2]; var month = parseInt(dateParts[1]); if (month < 10) { month = '0' + month; } var year = dateParts[0]; return '' + day + '/' + month + '/' + year; }; this.populate_delivery_time = function(options) { if (options) { var selected_delivery_time = $('.selected-delivery-time').attr("data"); var arLen = options.length; var newList = ""; for ( var i=0, len=arLen; i' + options[i]+''; } else { newList = newList + ''; } } $('#order_delivery_time').html(newList); } else { $('#order_delivery_time').html(""); } }; } $(document).ready(function() { var deliveryOptions = new SpreeDeliveryOptions(); deliveryOptions.initializeDeliveryTimeSelect(); });