function LicenceSearcher(n){function i(n){$('button[data-button="btnApplySelected"]').prop("disabled",!n);$('button[data-button="btnApply"]').prop("disabled",!n)}function r(){var u=s(),r;if(!u)return!1;i(!1);r=t;TrExCore.AjaxAntiForgeryToken(r);App.PostAsync(n.urls.processApplicationRequest,r,{success:function(n){n.HasError?(App.showAlertMessage("danger",n.ErrorMessages),i(!0)):window.location.href=n.Url},error:function(){i(!0)}})}function u(n,i,r){t.Items.push({PublishedLicenceId:n,LanguageId:r,Quantities:i})}function s(){if(t.Items.length==0)return App.showAlertMessage("warning",n.messages.invalid_selection),!1;var i=!0;return $.each(t.Items,function(t,r){r.PublishedLicenceId<=0&&(App.showAlertMessage("danger",n.messages.invalid_published_licence_id),i=!1);var u=r.Quantities[0].LicenceAuthorityId,f=$("select[data-publishedlicenceid='"+r.PublishedLicenceId+"']");u==null||u==""?(App.showAlertMessage("danger",n.messages.invalid_licence_authority_id),i=!1,$(f).addClass("red")):$(f).removeClass("red");r.LanguageId<=0&&(App.showAlertMessage("danger",n.messages.invalid_language_id),i=!1);(r.Quantity<=0||r.Quantity>o)&&(App.showAlertMessage("danger",n.messages.invalid_quantity),i=!1)}),i}function h(n){n.hide();var t=n.siblings('div[data-area="divDescriptionMore"]');t.show();t.css("height","auto");t.scrollTop(0);n.closest('div[data-area="divDescription"]').css("height","inherit")}function c(){$('a[data-button="btnRemoveLa"]').unbind("click").on("click",function(n){var t;n.preventDefault();n.stopPropagation();var r=$(this).closest('div[data-area="divApplicationQuantities"]'),i=$(this).closest('div[data-area="divApplicationQty"]'),u=parseInt(i.data("licence-authority-count"));i.remove();t=r.find('div[data-area="divApplicationQty"]');t.length<u&&t.first().find('a[data-button="btnAddLa"]').show()})}function f(n){var t=[],i=n.parents(".row.published-licence"),r=i.find('div[data-area="divApplicationQuantities"]').first(),u=r.find('div[data-area="divApplicationQty"]');return $.each(u,function(){var n=$(this),r=parseInt(n.data("licence-authority-count")),u=n.find('input[name="Quantity"]').val(),i;i=r>1?n.find('select[name="SelectedLicenceAuthorityId"]').val():n.find("h5[data-licence-authority-id]").data("licence-authority-id");t.push({LicenceAuthorityId:i,Quantity:u})}),t}var e=$(document).width()>768?"desktop":"mobile",o=Number(n.defaultMaxQuantity),t;this.search=function(t){var u=null,f=null,e=null,o=null,s=null,i,r,h;t.bundleId&&(u=t.bundleId);t.subscriptionId&&(f=t.subscriptionId);t.businessTypeId&&(e=t.businessTypeId);t.licenceAuthorityId&&(o=t.licenceAuthorityId);t.searchKeyword&&(s=t.searchKeyword);i=$("#DIV_SearchLicences");r={bundleId:u,businessTypeId:e,licenceAuthorityId:o,searchKeyword:s,subscriptionId:f};TrExCore.AjaxAntiForgeryToken(r);h=this;App.PostAsync(n.urls.search,r,{beforeSend:function(){i.html(function(){var n=$("#loadingLicences");return n.html()})},success:function(n){i.empty();i.prepend(n);h.bindEvents()},error:function(){i.empty()}})};this.bindEvents=function(){$('button[data-button="btnSearch"]').unbind("click").on("click",function(n){n.preventDefault();search()});$('div[data-area="divDescriptionMore"]').unbind("click").on("click",function(n){n.preventDefault();n.stopPropagation();var t=$(this);t.hide();t.siblings('div[data-area="divDescriptionLess"]').show();$(this).closest('div[data-area="divDescription"]').css("height","auto")});$('button[data-button="btnDescriptionMore"]').unbind("click").on("click",function(n){n.preventDefault();n.stopPropagation();var t=$(this).closest('div[data-area="divDescriptionLess"]');h(t)});$('button[data-button="btnDescriptionLess"]').unbind("click").on("click",function(n){n.preventDefault();n.stopPropagation();var t=$(this).closest('div[data-area="divDescriptionMore"]');t.hide();t.siblings('div[data-area="divDescriptionLess"]').show();$(this).closest('div[data-area="divDescription"]').css("height","inherit")});$(":checkbox").unbind("click").on("click",function(n){n.stopPropagation();$('button[data-button="btnApplySelected"]').prop("disabled",$(":checkbox:checked").length==0);var i=this.id,t=$('a[data-button="btnSelectRow"][data-publishedlicenceid="'+i+'"]');this.checked?(t.addClass("active"),$("#"+$(this).attr("id")).prop("checked",!0)):($("#"+$(this).attr("id")).prop("checked",!1),t.removeClass("active"))});$('[name*="Quantity"]').keydown(function(n){$.inArray(n.keyCode,[46,8,9,27,13,110])!==-1||n.keyCode==65&&n.ctrlKey===!0||n.keyCode==67&&n.ctrlKey===!0||n.keyCode==88&&n.ctrlKey===!0||n.keyCode>=35&&n.keyCode<=39||(n.shiftKey||n.keyCode<48||n.keyCode>57)&&(n.keyCode<96||n.keyCode>105)&&n.preventDefault()});$('[name*="Quantity"]').unbind("click").on("click",function(){this.focus();this.select()});$('button[data-button="btnApply"]').unbind("click").on("click",function(n){n.preventDefault();t={Items:[]};var i=$(this).attr("data-publishedlicenceid"),e=f($(this)),o=$(this).attr("data-languageid");u(i,e,o);r()});$('button[data-button="btnApplySelected"]').unbind("click").on("click",function(n){n.preventDefault();t={Items:[]};$(":checkbox[data-screen='"+e+"']:checked").each(function(){var n=this.id,t=f($(this)),i=$(this).attr("data-languageid");u(n,t,i)});r()});$('a[data-button="btnAddLa"]').unbind("click").on("click",function(n){var u;n.preventDefault();n.stopPropagation();var i=$(this).closest('div[data-area="divApplicationQuantities"]'),r=$(this).closest('div[data-area="divApplicationQty"]'),f=parseInt(r.data("licence-authority-count")),t=r.clone();t.find('a[data-button="btnAddLa"]').addClass("hidden");t.find('a[data-button="btnRemoveLa"]').removeClass("hidden");i.append(t);c();u=i.find('div[data-area="divApplicationQty"]');u.length===f&&$(this).hide()})}}