var Catalogue=function(n,t,i,r){var u=this,f=new CatalogueForm(t,n);f.ConfigureValidation();u.CatalogueData=ko.validatedObservable(f,{});u.HasCatalogueError=ko.observable(!1);n.AllowFormatSelection||u.CatalogueData().CatalogueFormat(n.DefaultFormat);console.log(u.CatalogueData());console.log(n);u.Processing=ko.observable(!1);u.Submitted=ko.observable(!1);u.ShowDownloadableCatalogues=ko.observable(!1);u.CountryOptions=ko.observableArray();u.LockCountryForParty=ko.observable(n.LockCountryForParty);u.CatalogueProducts=ko.observableArray(n.CatalogueProducts);u.CatalogueIds=ko.observableArray();u.CatalogueIds.subscribe(function(n){u.CatalogueData().CatalogueIds(n)});u.CatalogueIds(ko.utils.arrayMap(n.CatalogueProducts,function(n){return n.Id}));u.SelectedCatalogues=ko.computed(function(){return ko.utils.arrayFilter(u.CatalogueProducts(),function(n){var t=ko.utils.arrayFirst(u.CatalogueIds(),function(t){return t===n.Id});return!!t})});u.Confirm=function(){if(u.Submitted(!0),u.CatalogueData.errors&&u.CatalogueData.errors().length>0){u.CatalogueData.errors.showAllMessages();return}if(u.CatalogueData().CatalogueIds().length!==0){u.Processing(!0);u.CatalogueData().CatalogueFormat()!==1&&u.ScrollToTop();var n=u.CatalogueData().GetObject(),t=ko.mapping.toJS(n);i.RequestCatalogue(t).then(function(n,t,i){n.HasError?(u.HasCatalogueError(!0),u.Processing(!1)):i.status===201&&(window.location.href=r.urlRequested)},function(n){u.Processing(!1);common.ProcessAjaxError(n)});u.CatalogueData().CatalogueFormat()===1&&setTimeout(function(){u.ShowDownloadableCatalogues(!0);u.Processing(!1)},2e3)}};u.PopulateCountries=function(){var n=u.CatalogueData().CountryId();i.GetCountries().done(function(t){u.CountryOptions(t);u.CatalogueData().CountryId(n)})};u.PopulateCountries();u.SchoolSearch=function(n,t){n&&(n.length<2||i.SchoolSearch(n,u.CatalogueData().CountryId()).done(t))};u.UserSearch=function(n){n&&(n.length<3||n.indexOf("@")!==-1&&n.indexOf(".")!==-1&&i.UserSearch(n).then(function(n){n&&u.CatalogueData().UpdateData(n);n&&n.SchoolId&&n.SchoolName&&u.CatalogueData().SelectedSchool({Id:n.SchoolId,Name:n.SchoolName})}))};u.CatalogueData().Email.subscribe(u.UserSearch);u.CatalogueData().NewSchoolName.subscribe(function(n){u.CatalogueData().SchoolId()||n&&i.SchoolSearch(n).then(function(n){n&&n.length&&n.length===1&&u.CatalogueData().SelectedSchool(n[0])})});u.CatalogueData().SelectedSchool.subscribe(function(n){u.CatalogueData().SetSchool(n);n&&n.Id&&i.GetSchoolAddress(n.Id).then(function(n){if(n&&n.length){var t=ko.utils.arrayFirst(n,function(n){return n.Type==="Delivery"});t&&u.CatalogueData().SetAddress(t)}})});u.ScrollToTop=function(){$("#top").scrollintoview()};u.CatalogueData.errors.showAllMessages(!1)},CatalogueForm=function(n,t){var i=this;ko.utils.objectForEach(n,function(t){i[t]=ko.observable(n[t])});i.SelectedSchool=ko.observable();i.SelectedSchoolAddress=ko.observable();i.Country=ko.observable();i.SetSchool=function(n){n&&n.Id?(i.SchoolId(n.Id),i.SchoolName(n.Name),i.NewSchoolName(n.Name)):(i.SchoolId(null),i.SchoolName(null))};i.SetAddress=function(n){if(n)for(var t in n)n.hasOwnProperty(t)&&i[t]&&n[t]&&i[t].call(i,n[t])};i.ConfigureValidation=function(){i.FirstName.extend({required:!0});i.LastName.extend({required:!0});i.Email.extend({required:!0,email:!0});i.AddressLine1.extend({required:{onlyIf:function(){return!i.CatalogueFormat()}}});i.City.extend({required:{onlyIf:function(){return!i.AddressType()}}})};i.UpdateData=function(n){i.FirstName(n.FirstName);i.LastName(n.LastName);i.CountryId(n.CountryId);n.HasSchool&&(i.SchoolId(n.SchoolId),i.SchoolName(n.SchoolName),i.NewSchoolName(n.SchoolName))};i.GetObject=function(){return ko.mapping.fromJS(i)};i.PopulateAddress=function(){var n=i.AddressType()?t.SchoolStreetAddress:t.PartyStreetAddress;i.SetAddress(n)};i.AddressType.subscribe(i.PopulateAddress);i.Populate=function(n){for(var t in n)n.hasOwnProperty(t)&&i[t]&&n[t]&&i[t].call(i,n[t]);i.NewSchoolName(i.SchoolName());i.PopulateAddress();i.CountryId()||i.CountryId(n.MarketCountryId)};i.Populate(t)},CatalogueService=function(n){var t=this;t.RequestCatalogue=function(t){return $.ajax(n.apiCatalogue,{data:JSON.stringify(t),type:"post",contentType:"application/json; charset=utf-8'"})};t.SchoolSearch=function(t,i){return $.ajax({type:"get",dataType:"json",url:n.apiSchool+"/Search",cache:!1,data:{query:t,countryId:i,country:!0}})};t.UserSearch=function(t){return $.ajax({type:"get",dataType:"json",url:n.apiCatalogue+"/Search",cache:!1,data:{email:t}})};t.GetCountries=function(){return $.ajax({type:"get",dataType:"json",url:n.apiCountry})};t.GetSchoolAddress=function(t){return console.log(t),$.ajax({type:"get",dataType:"json",url:n.apiSchoolAddress+"?schoolId="+t})}},Client;(function(n){var t=function(){function t(n){this.catalogueProducts=ko.observableArray([]);this.catalogueProduct=ko.observable();this.allowFormatSelection=ko.observable();this.defaultFormat=ko.observable();this.email=ko.observable();this.firstName=ko.observable();this.lastName=ko.observable();this.schoolName=ko.observable();this.schoolId=ko.observable();this.countryId=ko.observable();this.marketCountryId=ko.observable();this.partyStreetAddress=ko.observable();this.schoolStreetAddress=ko.observable();this.lockCountryForParty=ko.observable();this.fromJs(n)}return t.prototype.fromJs=function(t){this.catalogueProduct(t.CatalogueProduct);this.allowFormatSelection(t.AllowFormatSelection);this.defaultFormat(t.DefaultFormat);this.email(t.Email);this.firstName(t.FirstName);this.lastName(t.LastName);this.schoolName(t.SchoolName);this.schoolId(t.SchoolId);this.countryId(t.CountryId);this.marketCountryId(t.MarketCountryId);this.partyStreetAddress(t.PartyStreetAddress);this.schoolStreetAddress(t.SchoolStreetAddress);this.lockCountryForParty(t.LockCountryForParty);this.catalogueProducts(t.CatalogueProducts.map(function(t){return new n.KnockoutCatalogueProductModel(t)}))},t.prototype.toJs=function(){return{CatalogueProduct:this.catalogueProduct(),AllowFormatSelection:this.allowFormatSelection(),DefaultFormat:this.defaultFormat(),Email:this.email(),FirstName:this.firstName(),LastName:this.lastName(),SchoolName:this.schoolName(),SchoolId:this.schoolId(),CountryId:this.countryId(),MarketCountryId:this.marketCountryId(),PartyStreetAddress:this.partyStreetAddress(),SchoolStreetAddress:this.schoolStreetAddress(),LockCountryForParty:this.lockCountryForParty(),CatalogueProducts:this.catalogueProducts().map(function(n){return n.toJs()})}},t}();n.KnockoutCatalogueRequestModel=t})(Client||(Client={})),function(n){var t=function(){function n(n){this.catalogueFormat=ko.observable();this.firstName=ko.observable();this.lastName=ko.observable();this.email=ko.observable();this.contactPhoneNumber=ko.observable();this.schoolId=ko.observable();this.schoolName=ko.observable();this.newSchoolName=ko.observable();this.catalogueIds=ko.observableArray([]);this.region=ko.observable();this.fullName=ko.observable();this.addressLine1=ko.observable();this.addressLine2=ko.observable();this.addressLine3=ko.observable();this.postcode=ko.observable();this.city=ko.observable();this.countryId=ko.observable();this.isDefault=ko.observable();this.type=ko.observable();this.otherInformation=ko.observable();this.addressType=ko.observable();this.fromJs(n)}return n.prototype.fromJs=function(n){this.catalogueFormat(n.CatalogueFormat);this.firstName(n.FirstName);this.lastName(n.LastName);this.email(n.Email);this.contactPhoneNumber(n.ContactPhoneNumber);this.schoolId(n.SchoolId);this.schoolName(n.SchoolName);this.newSchoolName(n.NewSchoolName);this.catalogueIds(n.CatalogueIds);this.region(n.Region);this.fullName(n.FullName);this.addressLine1(n.AddressLine1);this.addressLine2(n.AddressLine2);this.addressLine3(n.AddressLine3);this.postcode(n.Postcode);this.city(n.City);this.countryId(n.CountryId);this.isDefault(n.IsDefault);this.type(n.Type);this.otherInformation(n.OtherInformation);this.addressType(n.AddressType)},n.prototype.toJs=function(){return{CatalogueFormat:this.catalogueFormat(),FirstName:this.firstName(),LastName:this.lastName(),Email:this.email(),ContactPhoneNumber:this.contactPhoneNumber(),SchoolId:this.schoolId(),SchoolName:this.schoolName(),NewSchoolName:this.newSchoolName(),CatalogueIds:this.catalogueIds(),Region:this.region(),FullName:this.fullName(),AddressLine1:this.addressLine1(),AddressLine2:this.addressLine2(),AddressLine3:this.addressLine3(),Postcode:this.postcode(),City:this.city(),CountryId:this.countryId(),IsDefault:this.isDefault(),Type:this.type(),OtherInformation:this.otherInformation(),AddressType:this.addressType()}},n}();n.KnockoutCatalogueRequestDto=t}(Client||(Client={})),function(n){var t=function(){function t(t){this.urls=t;this.ajax=new n.AjaxService}return t.prototype.requestCatalogue=function(n){console.log(this.urls);var t=this.urls.apiCatalogue;return console.log(n),console.log("json",JSON.stringify(n)),this.ajax.post(t,JSON.stringify(n))},t.prototype.schoolSearch=function(n,t){var i=this.urls.apiSchool+"/Search",r={query:n,countryId:t,country:!0};return this.ajax.getWithData(i,r)},t.prototype.userSearch=function(n){var t=this.urls.apiCatalogue+"/Search",i={email:n};return this.ajax.getWithData(t,i)},t.prototype.getCountries=function(){return this.ajax.get(this.urls.apiCountry)},t.prototype.getSchoolAddress=function(n){return console.log(n),this.ajax.get(this.urls.apiSchoolAddress+"?schoolId="+n)},t}();n.CatalogueService=t}(Client||(Client={})),function(n){var t=function(){function t(t,i,r){var u=this;this.service=t;this.model=i;this.blankData=r;this.data=ko.observable(new n.KnockoutCatalogueRequestDto({}));this.submitted=ko.observable(!1);this.processing=ko.observable(!1);this.hasError=ko.observable(!1);this.showCatalogue=ko.observable(!1);this.catalogueUrl=ko.observable("");this.isValid=ko.computed(function(){return n.Common.validateEmail(u.data().email())&&u.data().firstName()&&u.data().lastName()});this.canRequest=ko.computed(function(){return!u.submitted()&&u.isValid()});this.catalogueUrl(i.CatalogueProduct.ContentBlobUrl);this.data().fromJs(this.blankData)}return t.prototype.handleRequestSuccess=function(){this.processing(!1);this.showCatalogue(!0)},t.prototype.handleRequestFailure=function(){this.processing(!1);this.hasError(!0)},t.prototype.request=function(){var n=this,t;this.submitted(!0);this.processing(!0);t=this.data().toJs();this.service.requestCatalogue(t).then(function(t){return n.handleRequestSuccess(t)},function(t){return n.handleRequestFailure(t)})},t}();n.CatalogueDetail=t}(Client||(Client={}))