GLS = function() { this.prefix = "gls-"; this.methodcode = "gls_bestway"; this.checkDroppointShipping = function() { var show = false; var showid = false; $$("input[name='shipping_method']").each(function(v) { if (v.checked) { if (v.value.search(gls.methodcode) >= 0) { id = v.value.split("_"); id = id[id.length - 1]; var i = 0; while (i < gls.idArrayDroppoints.length) { if (id == gls.idArrayDroppoints[i]) { show = true; showid = i; break; } i++; } } } var addEvent = true; try { if (Element && typeof Element.getStorage == 'function') { var elementEventRegistry = Element.getStorage($(v.id)).get('prototype_event_registry'); } if (typeof elementEventRegistry != 'undefined') { var elementChangeEvents = elementEventRegistry.get('change'); if (typeof elementChangeEvents != 'undefined' && elementChangeEvents[0] != 'undefined') { var i = 0; while (i < elementChangeEvents.length) { var elementChangeEventName = elementChangeEvents[i].handler.name; if (elementChangeEventName == 'gls_droppoint_form_change') { addEvent = false; break; } i++; } } } } catch (err) { var addEvent = true; } if (addEvent) { Event.observe($(v.id), 'change', function gls_droppoint_form_change(event) { if (this.value.search(gls.methodcode) >= 0) { var id = v.value.split("_"); var id = id[id.length - 1]; if (this.checked && gls.idArrayDroppoints.indexOf(id) >= 0) { if (this.parentNode.className == 'no-display') { this.parentNode.parentNode.appendChild($(gls.prefix + 'droppoint-form')); } else { this.parentNode.appendChild($(gls.prefix + 'droppoint-form')); } $(gls.prefix + 'droppoint-form').style.display = "block"; } else { $(gls.prefix + 'droppoint-form').style.display = "none"; } } else { $(gls.prefix + 'droppoint-form').style.display = "none"; } }); } }); if (show && showid !== false) { var input = $('s_method_' + gls.methodcode + '_' + gls.idArrayDroppoints[showid]); if (input.parentNode.className == 'no-display') { input.parentNode.parentNode.appendChild($(gls.prefix + 'droppoint-form')); } else { input.parentNode.appendChild($(gls.prefix + 'droppoint-form')); } $(gls.prefix + 'droppoint-form').style.display = "block"; gls.setDroppointFromLocalStorage(); } else { $(gls.prefix + 'droppoint-form').style.display = "none"; } }; this.setDroppointFromLocalStorage = function() { if (typeof localStorage != 'undefined') { if (localStorage.getItem(gls.prefix + 'servicepoint') != null) { var servicePoint = JSON.parse(localStorage.getItem(gls.prefix + 'servicepoint')); var servicePointId = servicePoint.servicePointId; var radio = { value: servicePointId }; servicePointsLocal = [servicePoint]; gls.setAddress(radio, servicePointsLocal); if (!lastUpdatedDroppointId || lastUpdatedDroppointId != servicePointId) { gls.updateShippingAddress(); } } } }; this.saveFlexmessageInLocalStorage = function() { var flexElement = document.getElementById(gls.prefix + "flexmessage"); if (typeof localStorage != 'undefined' && typeof flexElement != 'undefined') { try { localStorage.setItem(gls.prefix + 'flexmessage', flexElement.value); } catch (e) {} } }; this.checkFlexShipping = function() { var show = false; var showid = false; $$("input[name='shipping_method']").each(function(v) { if (v.checked) { if (v.value.search(gls.methodcode) >= 0) { id = v.value.split("_"); id = id[id.length - 1]; var i = 0; while (i < gls.idArrayFlexs.length) { if (id == gls.idArrayFlexs[i]) { show = true; showid = i; break; } i++; } } } var addEvent = true; try { if (Element && typeof Element.getStorage == 'function') { var elementEventRegistry = Element.getStorage($(v.id)).get('prototype_event_registry'); } if (typeof elementEventRegistry != 'undefined') { var elementChangeEvents = elementEventRegistry.get('change'); if (typeof elementChangeEvents != 'undefined' && elementChangeEvents[0] != 'undefined') { var i = 0; while (i < elementChangeEvents.length) { var elementChangeEventName = elementChangeEvents[i].handler.name; if (elementChangeEventName == 'gls_flex_form_change') { addEvent = false; break; } i++; } } } } catch (err) { var addEvent = true; } if (addEvent) { Event.observe($(v.id), 'change', function gls_flex_form_change(event) { if (this.value.search(gls.methodcode) >= 0) { var id = v.value.split("_"); var id = id[id.length - 1]; if (this.checked && gls.idArrayFlexs.indexOf(id) >= 0) { if (this.parentNode.className == 'no-display') { this.parentNode.parentNode.appendChild($(gls.prefix + 'flex-form')); } else { this.parentNode.appendChild($(gls.prefix + 'flex-form')); } $(gls.prefix + 'flex-form').style.display = "block"; } else { $(gls.prefix + 'flex-form').style.display = "none"; } } else { $(gls.prefix + 'flex-form').style.display = "none"; } }); } }); if (show && showid !== false) { var input = $('s_method_' + gls.methodcode + '_' + gls.idArrayFlexs[showid]); if (input.parentNode.className == 'no-display') { input.parentNode.parentNode.appendChild($(gls.prefix + 'flex-form')); } else { input.parentNode.appendChild($(gls.prefix + 'flex-form')); } $(gls.prefix + 'flex-form').style.display = "block"; gls.setFlexFromLocalStorage(); } else { $(gls.prefix + 'flex-form').style.display = "none"; } }; this.setFlexFromLocalStorage = function() { if (typeof localStorage != 'undefined') { if (localStorage.getItem(gls.prefix + 'flexmessage') != null) { var flexmessage = localStorage.getItem(gls.prefix + 'flexmessage'); document.getElementById(gls.prefix + 'flexmessage').value = flexmessage; } } }; this.loadDroppoints = function() { postalCode = document.getElementById(gls.prefix + 'postalCode'); countryCode = document.getElementById(gls.prefix + 'countryCode'); gls.getJson(postalCode.value, countryCode.value); }; this.setMobileHeight = function() { return true; // deprecated currentScrollPosition = document.viewport.getScrollOffsets()[1]; $(document.body).setStyle({ position: 'fixed', top: -currentScrollPosition + 'px', width: '100%' }); }; this.onEnterGetJson = function(e) { if (e.keyCode == 13) { gls.loadDroppoints(); return false; } }; this.setDroppointSizes = function() { var viewHeight = document.viewport.getHeight(); var wrapperHeight = $(gls.prefix + 'droppoint-wrapper').getHeight(); var headerHeight = $(gls.prefix + 'droppoint-header').getHeight(); var buttonsHeight = $(gls.prefix + 'droppoint-buttons-set').getHeight(); var containerHeight = wrapperHeight - headerHeight - buttonsHeight; var containerWidthHalf = $(gls.prefix + 'address-container').getWidth(); $(gls.prefix + 'droppoint-wrapper').setStyle({ top: ((viewHeight - wrapperHeight) / 2) + 'px', }); $(gls.prefix + 'droppoint-result').setStyle({ height: containerHeight + 'px' }); $(gls.prefix + 'droppoint-result').down('.map').setStyle({ height: containerHeight + 'px' }); $(gls.prefix + 'address-container').setStyle({ height: containerHeight + 'px' }); $(gls.prefix + 'opening-container').setStyle({ height: containerHeight + 'px', }); } this.getJson = function(postalCode, countryCode) { $(gls.prefix + 'loading').style.display = "inline"; if (typeof baseUrl == 'undefined') { baseUrl = '/'; } new Ajax.Request(baseUrl + 'gls/droppoint/getDroppointsFromPostalCode', { method: 'get', parameters: { 'postalCode': postalCode, 'countryCode': countryCode }, onComplete: function(transport) { $(gls.prefix + 'loading').style.display = "none"; jsonResponse = transport.responseText.evalJSON(); if (jsonResponse && typeof jsonResponse.servicePointInformationResponse.compositeFault == "undefined") { if($(gls.prefix+'droppoint-select').getStyle('display') == 'none') { $(gls.prefix+'droppoint-overlay').style.display = 'block'; } else { $(gls.prefix+'droppoint-overlay').style.display = 'none'; } gls.setDroppointSizes(); var addEvent = true; try { if (Element && typeof Element.getStorage == 'function') { var elementEventRegistry = Element.getStorage($(gls.prefix + 'close-droppoint-selector-save')).get('prototype_event_registry'); } if (typeof elementEventRegistry != 'undefined') { var elementChangeEvents = elementEventRegistry.get('click'); if (typeof elementChangeEvents != 'undefined' && elementChangeEvents[0] != 'undefined') { var i = 0; while (i < elementChangeEvents.length) { var elementChangeEventName = elementChangeEvents[i].handler.name; if (elementChangeEventName == 'gls_move_overlay_click') { addEvent = false; break; } i++; } } } } catch (err) { var addEvent = true; } if (addEvent) { Event.observe($(gls.prefix + 'close-droppoint-selector-save'), 'click', function gls_move_overlay_click(event) { $(gls.prefix + 'droppoint-overlay').style.display = "none"; $(gls.prefix + 'droppoint-form').style.display = "block"; gls.moveOverlayIn(); $(document.body).setStyle({ position: 'inherit' }); window.scrollTo(0, currentScrollPosition); }); Event.observe($(gls.prefix + 'close-droppoint-selector-remove'), 'click', function(event) { $(gls.prefix + 'droppoint-overlay').style.display = "none"; $(gls.prefix + 'droppoint-form').style.display = "block"; if ($$("input:checked[id^='" + gls.prefix + "servicePointId'][type='radio']").length) { $$("input:checked[id^='" + gls.prefix + "servicePointId'][type='radio']")[0].checked = false; } gls.selectedServicePoint = null; gls.setAddress(false, gls.servicePoints); gls.moveOverlayIn(); $(document.body).setStyle({ position: 'inherit' }); window.scrollTo(0, currentScrollPosition); }); Event.observe($(gls.prefix+'droppoint-select'), 'change', function select_update_droppoint(event) { gls.setAddress(this,gls.servicePoints); gls.updateShippingAddress(); }); } gls.moveOverlayOut(); gls.handleJson(jsonResponse); gls.setMobileHeight(); } else { alert('Ingen afhentningssteder fundet for det indtastede postnummer, prøv venligst igen...'); $(gls.prefix + 'postalcode').focus(); } } }); }; this.updateShippingAddress = function() { if (typeof localStorage != 'undefined' && typeof gls.selectedServicePoint != 'undefined' && gls.selectedServicePoint != null) { try { localStorage.setItem(gls.prefix + 'servicepoint', JSON.stringify(gls.selectedServicePoint)); } catch (e) {}; gls.selectedServicePoint = null; } if (typeof baseUrl == 'undefined') { baseUrl = '/'; } new Ajax.Request(baseUrl + 'gls/droppoint/update', { method: 'post', parameters: { 'droppoint': $(gls.prefix + "droppoint-id").value, 'droppoint-name': $(gls.prefix + "droppoint-name").value, "droppoint-city": $(gls.prefix + "droppoint-city").value, "droppoint-postalcode": $(gls.prefix + "droppoint-postalcode").value, "droppoint-streetname": $(gls.prefix + "droppoint-streetname").value, "droppoint-streetnumber": $(gls.prefix + "droppoint-streetnumber").value }, onComplete: function(transport) { $(gls.prefix + 'loading').style.display = "none"; lastUpdatedDroppointId = $(gls.prefix + "droppoint-id").value; } }); }; this.moveOverlayOut = function() { $(document.body).appendChild($(gls.prefix + 'droppoint-overlay')); }; this.moveOverlayIn = function() { $(gls.prefix + 'droppoint-form').appendChild($(gls.prefix + 'droppoint-overlay')); gls.updateShippingAddress(); }; this.handleJson = function(jsonResponse) { mapCenterCoords = gls.getMapBound(jsonResponse.servicePointInformationResponse.servicePoints); sw = mapCenterCoords[0]; ne = mapCenterCoords[1]; mapOptions = { zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP, draggable: false, zoomControl: true, scrollwheel: false, disableDoubleClickZoom: true, keyboardShortcuts: false, streetViewControl: false }; map = new google.maps.Map(document.getElementById(gls.prefix + "map-canvas"), mapOptions); map.fitBounds(new google.maps.LatLngBounds(sw, ne)); gls.servicePoints = jsonResponse.servicePointInformationResponse.servicePoints; gls.markers = gls.addDroppointMarkers(jsonResponse.servicePointInformationResponse.servicePoints, map); }; this.addDroppointMarkers = function(servicePoints, map) { var markers = []; $(gls.prefix + 'addresses').update(""); $(gls.prefix+'droppoint-select').update(''); var x = 0; while (x < servicePoints.length) { var classname = ""; if (x % 5 == 0) { classname = " first"; } else if ((x + 1) % 5 == 0) { classname = " last"; } var marker = gls.addMarker(servicePoints[x], map); gls.addAddress(servicePoints[x], classname); markers.push(marker); x++; } return markers; }; this.addAddress = function(servicePoint, classname) { var servicePointId = servicePoint.servicePointId; if (servicePoint.name.search("Handikapvenlig") >= 0) { servicePointId += "-Handikapvenlig"; } var isChecked = false; if ($(gls.prefix + 'droppoint-id').value == servicePointId) { isChecked = true; gls.setOpeningHours(servicePointId); } var container = new Element('li', { 'id': servicePointId }); container.addClassName(gls.prefix + "container" + classname); var radio = new Element('input', { 'type': "radio", 'id': gls.prefix + "servicePointId-" + servicePointId, 'value': servicePointId }); radio.addClassName(gls.prefix + "radio"); radio.name = "droppoint"; if (isChecked) { radio.checked = "checked"; } var address = new Element('div'); address.addClassName(gls.prefix + "address"); var label = new Element('label', { 'for': gls.prefix + "servicePointId-" + servicePointId }); var name = new Element('h4').update(servicePoint.name); var droppointaddress = new Element('div').update('
' + servicePoint.deliveryAddress.streetName + " " + servicePoint.deliveryAddress.streetNumber + "
" + servicePoint.deliveryAddress.postalCode + " " + servicePoint.deliveryAddress.city + '
'); droppointaddress.addClassName(gls.prefix + "content"); Event.observe(radio, 'change', function(event) { gls.bounceMarkerOnRadioClick(this); gls.setAddress(this, gls.servicePoints); }); label.appendChild(name); label.appendChild(droppointaddress); address.appendChild(label); container.appendChild(radio); container.appendChild(address); $(gls.prefix + 'addresses').appendChild(container); var option = new Element('option', { 'value': servicePointId, 'data-name' : servicePoint.name, 'data-address' : servicePoint.deliveryAddress.streetName + " " + servicePoint.deliveryAddress.streetNumber, 'data-postcode' : servicePoint.deliveryAddress.postalCode, 'data-city' : servicePoint.deliveryAddress.city, }); option.innerHTML = servicePoint.name + ', ' + servicePoint.deliveryAddress.streetName + " " + servicePoint.deliveryAddress.streetNumber + ', ' + servicePoint.deliveryAddress.postalCode + ' ' + servicePoint.deliveryAddress.city; $(gls.prefix+'droppoint-select').appendChild(option); }; this.addMarker = function(servicePoint, map) { var servicePointId = servicePoint.servicePointId; if (servicePoint.name.search("Handikapvenlig") >= 0) { servicePointId += "-Handikapvenlig"; } var myLatlng = new google.maps.LatLng(servicePoint.coordinate.northing, servicePoint.coordinate.easting); var marker = new google.maps.Marker({ position: myLatlng, title: servicePoint.name, servicePointId: gls.prefix + 'servicePointId-' + servicePointId }); marker.setMap(map); google.maps.event.addListener(marker, 'click', gls.toggleBounce); return marker; }; this.toggleBounce = function() { var x = 0; while (x < gls.markers.length) { gls.markers[x].setAnimation(null); x++; } this.setAnimation(google.maps.Animation.BOUNCE); gls.selectRadioOnMarkerClick(this); }; this.selectRadioOnMarkerClick = function(marker) { $(marker.servicePointId).checked = true; gls.setAddress($(marker.servicePointId), gls.servicePoints); gls.setOpeningHours(marker.servicePointId.replace(gls.prefix + "servicePointId-", "")); var x = 0; while (x < gls.markers.length) { gls.markers[x].setAnimation(null); x++; } marker.setAnimation(google.maps.Animation.BOUNCE); }; this.bounceMarkerOnRadioClick = function(radio) { var bouncex = false; var x = 0; while (x < gls.markers.length) { gls.markers[x].setAnimation(null); if (gls.markers[x].servicePointId == radio.id) { bouncex = x; gls.setOpeningHours(radio.value); } x++; } if (bouncex !== false) { gls.markers[bouncex].setAnimation(google.maps.Animation.BOUNCE); } }; this.setOpeningHours = function(servicePointId) { var handikapvenlig = ""; if (servicePointId.search("Handikapvenlig") >= 0) { servicePointId = servicePointId.replace("-Handikapvenlig", ""); handikapvenlig = " Handikapvenlig"; } var openingheader = new Element('div'); openingheader.addClassName(gls.prefix + "opening-header"); openingheader.innerHTML = $(gls.prefix + "opening-header-text").innerHTML; $(gls.prefix + 'opening-container').innerHTML = ""; $(gls.prefix + 'opening-container').appendChild(openingheader); if (gls.servicePoints) { var x = 0; while (x < gls.servicePoints.length) { if (gls.servicePoints[x].servicePointId == servicePointId) { var droppointaddress = new Element('div').update('' + gls.servicePoints[x].name + handikapvenlig + '