var quickquote=angular.module("parcel2go.quickquote",[]);quickquote.directive("qqLookup",["$http","_settings","$timeout",function(n,t,i){return{restrict:"E",templateUrl:"QUICK_QUOTE_LOOKUP",replace:!0,scope:{required:"=",country:"=",town:"=",id:"@",uselookup:"=",postcodeinput:"=",postcodeelementname:"=",towninput:"=",townelementname:"=",regex:"=",placeholder:"="},link:function(r,u){var e,f=function(){r.items=[];r.loading=!1;r.visible=!1;r.noResults=!1};f();r.select=function(n){r.postcodeinput=n.code;r.towninput=n.city;f();i(function(){$(":focus").blur()},0)};u.on("click",function(n){n.stopPropagation()});$(document).on("click",function(){r.$apply(function(){f()})});r.focusPostcodeInput=function(){$("#"+r.postcodeelementname).focus();r.towninput=""};r.performLookup=function(i,u){$.trim(i).length>1&&r.uselookup?(u&&($("#"+r.townelementname).val(""),r.towninput=""),e=Date.now?Date.now():+new Date,r.loading=!0,r.visible=!0,r.noResults=!1,n.post(t.endPoints.lookup+"?countryId="+r.country+"&val="+$.trim(i)+"&rel="+e).success(function(n){n.success&&n.relevance==e?(r.visible=!0,r.items=n.items,r.items.length||(r.noResults=!0),r.loading=!1):n.success||(r.visible=!1)}).error(function(){f()})):f()}}}}]);quickquote.controller("QuickQuoteController",["$scope","_settings","$http","$locale","$filter","$timeout",function(n,t,r,u,f){function h(i,r,u){var f,e=[],l=[],o,h,a;if(u===!1&&i.$invalid){for(f=0;f<i.$error.required.length;f++)i.$error.required[f].$setViewValue(i.$error.required[f].$viewValue);return}if(e.push("col="+n.model.Form.CollectionCountry),e.push("dest="+n.model.Form.DeliveryCountry),n.model.Form.CollectionPostcode&&e.push("cp="+n.model.Form.CollectionPostcode),n.model.Form.DeliveryPostcode&&e.push("dp="+n.model.Form.DeliveryPostcode),n.model.Form.CourierId&&e.push("courier="+n.model.Form.CourierId),n.model.Form.CollectionTown&&n.model.CollectionCountries.selected.HasAutocomplete&&e.push("ct="+n.model.Form.CollectionTown),n.model.Form.DeliveryTown&&n.model.DeliveryCountries.selected.HasAutocomplete&&e.push("dt="+n.model.Form.DeliveryTown),n.model.Form.IsAmazonDelivery&&e.push("amazon="+n.model.Form.IsAmazonDelivery),e.push("mdd="+(n.model.mdd?"1":"0")),n.model.CurrentQuoteType!=="Default"&&t.showQuoteTypesOnGetQuote===!1&&(n.model.CurrentQuoteType="Default"),n.model.CurrentQuoteType==="Default"||n.model.CurrentQuoteType===undefined)for(f=0;f<n.model.Form.Parcels.length;f++)n.model.Form.Parcels[f].Weight=c(n.model.Form.Parcels[f].Weight),o=n.model.Form.Parcels[f],h=o.Quantity+"~"+s(o.Weight),(n.model.Mode==="GetQuote"||n.model.Mode==="sameday"||!n.state.simple&&o.Length&&o.Width&&o.Height)&&(h+="|"+[s(o.Length),s(o.Width),s(o.Height)].join("|")),l.push(h);else if(n.model.CurrentQuoteType.indexOf("Pallet")>0)for(a=v(),f=0;f<a.length;f++){var o=a[f],y=p(o),h=o.Quantity+"~"+y.Weight;h+="|"+[o.Length,o.Width,y.Height].join("|");l.push(h)}n.model.Mode&&n.model.Mode!=="GetQuote"&&e.push("mode="+n.model.Mode);l.length&&e.push("p="+l.join("^"));n.model.CurrentQuoteType&&e.push("quotetype="+n.model.CurrentQuoteType);window.location=(r||n.getQuoteEndpoint)+"?"+e.join("&")}function v(){return n.model.Form.Parcels[0].Weight!=undefined&&n.model.Form.Parcels[0].Weight!=null?n.model.Form.Parcels:y()}function y(){var r=n.model.QuoteTypes.first(function(t){return t.Type===n.model.CurrentQuoteType}),u=[],t;for(i=0;i<r.QuoteTypeOptions.length;i++)r.QuoteTypeOptions[i].Weight=c(r.QuoteTypeOptions[i].Weight),t=r.QuoteTypeOptions[i],t.Quantity>0&&u.push({Quantity:t.Quantity,Weight:t.Weight,Height:t.Height,Length:t.Length,Width:t.Width});return u}function p(t){return t.Weight==null?t:n.quoteOptions.where(function(n){return(t.Height==undefined||n.Height>=t.Height)&&n.Weight>=t.Weight}).sort(function(n,t){return n.Weight>t.Weight}).first()}function s(n){return f("number")(n)||n}function c(n){return n<.1?.1:n}function w(){var h=!1,f,e,o,s,v,u,y,i,r,p;if(t.features.enableStickyInput===!0&&n.model.Form.Parcels.length===0)try{var c=getQueryStringValue("col"),l=getQueryStringValue("dest"),a=getQueryStringValue("quotetype");if((a==="Default"||a===undefined)&&(c&&l&&(n.model.DeliveryCountries.selected=n.model.DeliveryCountries.first(function(n){return n.Id===parseInt(l)}),n.model.CollectionCountries.selected=n.model.CollectionCountries.first(function(n){return n.Id===parseInt(c)})),f=getQueryStringValue("cp"),f&&(n.model.Form.CollectionPostcode=f),e=getQueryStringValue("dp"),e&&(n.model.Form.DeliveryPostcode=e),o=getQueryStringValue("ct"),o&&(n.model.Form.CollectionTown=o),s=getQueryStringValue("dt"),s&&(n.model.Form.DeliveryTown=s),v=getQueryStringValue("mdd"),n.model.mdd=v=="1",u=getQueryStringValue("p"),u)){for(y=u.split("~")[0],i=u.split("~")[1].split("|"),r=0;r<i.length;r++)i[r]=parseFloat(i[r])||0;p={Quantity:parseFloat(y),Weight:i[0],Length:i[1],Width:i[2],Height:i[3]};n.model.Form.Parcels.push(p);h=!0;n.checkSimpleView()}}catch(w){h=!1}}function b(t){var i=localStorage.getItem("selected-quote-type");n.model.CurrentQuoteType=i!=null&&n.model.Mode==="GetQuote"?i:t}function l(n,t,i){while(t<i)n.push(++t);return n}var a=navigator.geolocation,e,o;n.state={simple:!0,large:!1,locatorAvailable:!!a,quantities:l([],0,50),palletQuantities:l([0],0,10),collectionCountryDisabled:!1,deliveryCountryDisabled:!1,simpleCountries:t.simple,loaded:!1,quoteOptions:[]};n.redirect={};n.init=function(t,i){n.getQuoteEndpoint=t;n.model=i;angular.forEach(n.model.DeliveryCountries,function(n){n.showCountry=!0});n.model.Mode==="sameday"&&(n.RequireLengthAndDimensions=!0);n.model.mdd=n.model.mdd?!0:!1;b(n.model.CurrentQuoteType);w();n.quoteOptions=n.getQuoteOptions();n.state.loaded=!0;n.model.Mode=="PalletGetQuote"&&n.loadPalletDropDowns()};n.loadPalletDropDowns=function(){angular.forEach(n.model.PalletCollectionCountries,function(t){t.Id==n.model.Form.CollectionCountry&&(n.model.PalletCollectionCountries.selected=t)});angular.forEach(n.model.PalletDeliveryCountries,function(t){t.Id==n.model.Form.DeliveryCountry&&(n.model.PalletDeliveryCountries.selected=t)})};n.$watchCollection("model.Form.Parcels",function(){n.model.Form.Parcels.length<1&&n.add()});e=0;n.updateCollectionPostcode=function(t){n.model.Form.CollectionPostcode=t};n.updateDeliveryPostcode=function(t){n.model.Form.DeliveryPostcode=t};n.$watch("model.Form.CollectionPostcode",function(){angular.forEach(t.switchers,function(t){var i=new RegExp(t.regex);t.fromCountryId==e&&t.toCountryId==n.model.CollectionCountries.selected.Id&&n.model.Form.CollectionPostcode&&!i.test(n.model.Form.CollectionPostcode.toUpperCase())&&(n.model.CollectionCountries.selected=n.model.CollectionCountries.first(function(n){return n.Id==e}),e=0);n.model.Form.CollectionCountry==t.fromCountryId&&n.model.Form.CollectionPostcode&&i.test(n.model.Form.CollectionPostcode.toUpperCase())&&(e=n.model.CollectionCountries.selected.Id,n.model.CollectionCountries.selected=n.model.CollectionCountries.first(function(n){return n.Id==t.toCountryId}),n.state.collectionCountryDisabled&&(n.model.Form.DeliveryPostcode=""))})});o=0;n.$watch("model.Form.DeliveryPostcode",function(){angular.forEach(t.switchers,function(t){var i=new RegExp(t.regex);t.fromCountryId==o&&n.model.DeliveryCountries.selected&&t.toCountryId==n.model.DeliveryCountries.selected.Id&&n.model.Form.DeliveryPostcode&&!i.test(n.model.Form.DeliveryPostcode.toUpperCase())&&(n.model.DeliveryCountries.selected=n.model.DeliveryCountries.first(function(n){return n.Id==o}),o=0);n.model.Form.DeliveryCountry==t.fromCountryId&&n.model.Form.DeliveryPostcode&&i.test(n.model.Form.DeliveryPostcode.toUpperCase())&&(o=n.model.DeliveryCountries.selected.Id,n.model.DeliveryCountries.selected=n.model.DeliveryCountries.first(function(n){return n.Id==t.toCountryId}),n.state.deliveryCountryDisabled&&(n.model.Form.CollectionPostcode=""))})});n.$watch("model.CollectionCountries.selected",function(){if(t.features.defaultHostingCountryRequiredForQuote==!0&&(n.model.CollectionCountries.selected&&n.model.CollectionCountries.selected.Id!=t.defaultCollectionCountryId?(n.model.DeliveryCountries.selected=n.model.DeliveryCountries.first(function(n){return n.Id==t.defaultCollectionCountryId}),n.state.deliveryCountryDisabled=!0):n.state.deliveryCountryDisabled=!1),n.model.CollectionCountries.selected&&n.model.CollectionCountries.selected.Id||!n.model.Form.CollectionCountry||(n.model.CollectionCountries.selected=n.model.CollectionCountries.first(function(t){return t.Id==n.model.Form.CollectionCountry})),n.model.Form.CollectionCountry=n.model.CollectionCountries.selected.Id,n.checkSimpleView(),t.defaultCollectionCountryId!=n.model.CollectionCountries.selected.Id?r.get(t.endPoints.redirect+"?collectionCountryId="+n.model.CollectionCountries.selected.Id).success(function(t){t.DestinationDescription?(n.redirect=t,$("#international-redirect-modal").modal("show")):n.redirect={}}):n.redirect={},n.model.DeliveryRoutesByCountryId){var i=[];angular.forEach(n.model.DeliveryRoutesByCountryId,function(t){t.Item1==n.model.CollectionCountries.selected.Id&&i.push(t.Item2)});angular.forEach(n.model.DeliveryCountries,function(n){n.showCountry=!1;angular.forEach(i,function(t){(n.Id==t||n.Id==0)&&(n.showCountry=!0)})});n.model.DeliveryCountries.selected.showCountry||(n.model.DeliveryCountries.selected=n.model.DeliveryCountries.first(function(n){return n.showCountry}))}n.model.Form.CollectionTown=""});n.$watch("model.DeliveryCountries.selected",function(){t.features.defaultHostingCountryRequiredForQuote==!0&&(n.model.DeliveryCountries.selected&&n.model.DeliveryCountries.selected.Id!=t.defaultCollectionCountryId?(n.model.CollectionCountries.selected=n.model.CollectionCountries.first(function(n){return n.Id==t.defaultCollectionCountryId}),n.state.collectionCountryDisabled=!0):n.state.collectionCountryDisabled=!1);n.model.DeliveryCountries.selected&&n.model.DeliveryCountries.selected.Id||!n.model.Form.DeliveryCountry||(n.model.DeliveryCountries.selected=n.model.DeliveryCountries.first(function(t){return t.Id==n.model.Form.DeliveryCountry}));n.model.DeliveryCountries.selected!=undefined&&(n.model.Form.DeliveryCountry=n.model.DeliveryCountries.selected.Id);n.model.DeliveryCountries.selected.RegexFormat==undefined&&(n.model.Form.DeliveryPostcode="");n.model.Form.DeliveryTown="";var i=function(){t.simple.indexOf(n.model.Form.DeliveryCountry)>-1&&n.model.Mode!=="GetQuote"&&(n.state.simple=!0);jQuery&&(n.state.simple?jQuery("body").removeClass("advanced-quick-quote"):jQuery("body").addClass("advanced-quick-quote"))};n.checkSimpleView(i)});n.checkSimpleView=function(i){var r=n.model.Form&&n.model.Form.Parcels.length&&n.model.Form.Parcels.any(function(n){return!!(n.Height||n.Length||n.Width)});n.state.simple=t.simple.indexOf(n.model.Form.CollectionCountry)>-1&&t.simple.indexOf(n.model.Form.DeliveryCountry)>-1&&!n.state.large&&!r?!0:!1;i&&i()};n.resetDimValues=function(i){n.model.Form.Parcels[0].Length=0;n.model.Form.Parcels[0].Width=0;n.model.Form.Parcels[0].Height=0;i!=="undefined"&&i&&(n.model.Form.Parcels[0].Weight=0,n.model.Form.CollectionPostcode="",n.model.Form.DeliveryPostcode="");t.simple.indexOf(n.model.Form.CollectionCountry)>-1&&t.simple.indexOf(n.model.Form.DeliveryCountry)>-1&&(n.state.simple=!0)};n.add=function(){n.model.Form.Parcels.push({Quantity:1})};n.remove=function(){n.model.Form.Parcels.pop()};n.submit=function(i){if(n.redirect.DestinationDescription){$("#international-redirect-modal").modal("show");return}if(!t.preCheckCountrySelection){h(i,n.getQuoteEndpoint,!1);return}r.post(t.endPoints.preCheckCountrySelection,{collectionCountryId:n.model.Form.CollectionCountry,deliveryCountryId:n.model.Form.DeliveryCountry}).success(function(t){t.data==!1?$("#noservices-modal").modal("show"):h(i,n.getQuoteEndpoint,!1)})};n.sendMultiple=function(n){return h(n,t.endPoints.quickquote,!0),!1};n.userHasSelectedQuoteType=function(){if(n.model.QuoteTypes){var t=0;return angular.forEach(n.model.QuoteTypes,function(n){t+=n.QuoteTypeOptions.sum(function(n){return n.Quantity})}),t>0}return!0};n.getQuoteOptions=function(){if(n.model.QuoteTypes.length<1)return null;var i=[{HeightDescription:t.translations.heightDescription,Height:0,WeightDescription:t.translations.weightDescription,Weight:0}],r=n.model.QuoteTypes.where(function(n){return n.QuoteTypeOptions.length>0}).select(function(n){return n.QuoteTypeOptions.select(function(n){return{HeightDescription:t.translations.heightSelect.format(n.Height),Height:n.Height,WeightDescription:t.translations.weightSelect.format(n.Weight),Weight:n.Weight}})});return[].concat.apply(i,r)};n.getNumberOfParcels=function(){for(var i=n.model.Form.Parcels,r=0,t=0;t<i.length;t++)r+=i[t].Quantity;return r};n.parcelforceTealiumLink=function(){utag.link({pageApplicationStep:"1. Get Quote",pageName:"PF >Retail >Get Quote",pageBreadCrumb:"Home|Get Quote",pageTemplate:"Get a Quote",pageMenuTree:"Parcelforce Worldwide|Retail|Get Quote",pagePersona:"Personal",pageLanguage:"en-gb",pageApplicationName:"Retail"})};n.storeQuoteTypeSelection=function(t){var i=localStorage.getItem("selected-quote-type");i!=null?localStorage.setItem("selected-quote-type",t.Type):localStorage.setItem("selected-quote-type","Default");n.model.CurrentQuoteType=t.Type}}]).directive("pgTrim",function(){return{restrict:"A",require:"ngModel",priority:1,link:function(n,t,i,r){if(i.type!=="radio"&&i.type!=="checkbox"){var u=function(){n.$apply(function(){r.$setViewValue(t.val().trim());r.$render()})};t.on("blur",u)}}}})