var promotedDocumentRule=new function(){function i(n){n!==null&&n!==undefined&&($("#promoteDocumentRuleError").html(n.statusText),$("#promoteDocumentRuleError").show())}function u(){return promotedDocumentSearchAPIUrl}function p(){return spellCheckSearchAPIUrl}function w(){return linguisticsSearchAPIUrl}function b(n,t){for(var i=0,r=n.length;i<r;i++)if(n[i].Data!==null&&n[i].Data.ProductId===t)return n[i];return null}function k(n,t){for(var i=0,r=n.length;i<r;i++)if(n[i]!==null&&n[i].DocumentId()===t)return n[i];return null}function o(n,t){for(var i=0,r=n.length;i<r;i++)if(n[i].Query()!==null&&n[i].Query()===t)return n[i].DocumentIds();return null}function d(n,t){for(var i=0,r=n.length;i<r;i++)if(n[i].Query()!==null&&n[i].Query()===t)return i;return-1}function s(n){if(n===null||n==="")return"";n=n.replace(/\/Date\((-?\d+)\)\//,"$1");var t=new Date(parseInt(n));return t.getMonth()+1+"/"+t.getDate()+"/"+t.getFullYear()}function r(n,t){var i=this;i.DocumentId=ko.observable(n);i.ImageFileName=ko.observable(t)}function f(t,i){var u=this;u.Query=ko.observable(t);u.DocumentIds=ko.observableArray([new r("",n),new r("",n),new r("",n),new r("",n),new r("",n)]);i!==null&&i!==undefined&&$.each(u.DocumentIds(),function(n,t){t.DocumentId(i[n])})}function g(){var t=this;t.Id=ko.observable("");t.RuleGroupId=ko.observable("");t.RuleFor=ko.observable("support");t.Name=ko.observable("");t.Query=ko.observable("");t.DocumentIds=ko.observableArray(["","","","",""]);t.Querys=ko.observableArray();t.DocumentAnalysisLogic=function(){var i=[],r=[];return r.push("Document Ids"),$.each(t.Querys(),function(n,t){r.push(t.Query())}),i.push(r),$.each(t.DocumentIds(),function(r,u){var f=[];f.push(u);$.each(t.Querys(),function(t,i){var r=k(i.DocumentIds(),u);r!==null&&r!==undefined?f.push(r.ImageFileName()):f.push(n)});i.push(f)}),i};t.DocumentAnalysis=ko.computed(t.DocumentAnalysisLogic,this);t.EndDate=ko.observable("");t.StartDate=ko.observable("");t.AddQuery=function(n){var i=function(){t.Querys.push(new f($("#promoteDocumentQuery").val(),t.DocumentIds()));$("#promoteDocumentQuery").val("");e()};n===null||n===undefined||n===!0?l($("#promoteDocumentQuery").val(),i):i()};t.RemoveQuery=function(n){t.Querys.remove(n)};t.RefreshQuerys=function(){var n=[];$.each(promotedDocumentViewModel.Querys(),function(t,i){n.push(i.Query())});t.Querys([]);$.each(n,function(n,i){t.Querys.push(new f(i,t.DocumentIds()))})};t.QuerySuggestion=ko.observable("");t.QuerySuggestionVisibility=ko.observable(!1)}function nt(){promotedDocumentViewModel.RuleGroupId("");promotedDocumentViewModel.Name("");promotedDocumentViewModel.Query("");promotedDocumentViewModel.DocumentIds(["","","","",""]);promotedDocumentViewModel.Querys([]);promotedDocumentViewModel.EndDate("");promotedDocumentViewModel.StartDate("");$("#promoteDocumentQueryTerm").val("");promotedDocumentViewModel.QuerySuggestion("");promotedDocumentViewModel.QuerySuggestionVisibility(!1);promotedDocumentViewModel.RuleFor("support")}function tt(n,t){promotedDocumentViewModel.DocumentIds()[n-1]=t;promotedDocumentViewModel.RefreshQuerys();e()}function it(t,i){var u=o(promotedDocumentViewModel.Querys(),i),r;u!==null&&u!==undefined&&$.each(u,function(i,r){if(r.DocumentId()===null||r.DocumentId()===undefined||r.DocumentId()==="")r.ImageFileName(n);else{var u=b(t,r.DocumentId());u!==null?r.ImageFileName(a):r.ImageFileName(v)}});promotedDocumentViewModel.DocumentAnalysisLogic();r=d(promotedDocumentViewModel.Querys(),i);r!==-1&&r+1<promotedDocumentViewModel.Querys().length&&c(promotedDocumentViewModel.Querys()[r+1].Query())}function rt(n){var t=o(promotedDocumentViewModel.Querys(),n);t!==null&&$.each(t,function(n,t){t.ImageFileName(y)})}function ut(n){(nt(),n.RulePhrases!=null&&n.RulePhrases!=undefined&&n.RulePhrases!=="")&&(promotedDocumentViewModel.Id(n.Id),promotedDocumentViewModel.RuleGroupId(n.RuleGroupId),promotedDocumentViewModel.Name(n.RuleName),promotedDocumentViewModel.EndDate(s(n.EndDate)),promotedDocumentViewModel.StartDate(s(n.StartDate)),$.each(n.DocumentIds,function(n,t){promotedDocumentViewModel.DocumentIds()[n]=t}),$.each(n.RulePhrases,function(n,t){promotedDocumentViewModel.Querys.push(new f(t,promotedDocumentViewModel.DocumentIds()))}),promotedDocumentViewModel.RuleFor(n.RuleFor))}function h(){if(promotedDocumentViewModel!=null){var n=[];return $.each(promotedDocumentViewModel.Querys()[0].DocumentIds(),function(t,i){var r="support";promotedDocumentViewModel.RuleFor()!==null&&promotedDocumentViewModel.RuleFor()!==undefined&&promotedDocumentViewModel.RuleFor()!==""&&(r=promotedDocumentViewModel.RuleFor());n.push({DocumentId:i.DocumentId(),RuleFor:r})}),n}return{}}function c(n){if($("#promoteDocumentRuleError").hide(),t!=null&&(t.abort(),t=null),n==="")return!1;rt(n);var r=[{Name:"include-fast-query",Value:document.location.hash=="#info"}],u={IncludeRefiners:!0,IncludeCategoryTree:!1,VirtualAssistantData:typeof getVirtualAssistantDataForSearch==typeof Function?getVirtualAssistantDataForSearch():{},FilteredDocuments:typeof h==typeof Function?h():{},Options:{withqueryunderstandingenabled:!0,UrlReferrer:sessionStorage.SearchNextUrlReferrer||"",WithNoTrackingEnabled:document.location.hash==="#notracking",ResultOptions:r,IncludeGraph:document.location.hash==="#info"},Categories:typeof GetSelectedCategories==typeof Function?GetSelectedCategories():[],Profile:{Segment:profile.Segment,CustomerSet:profile.CustomerSet,Language:profile.Language,Country:profile.Country}};return t=$.ajax({url:getSearchUrl(n),xhrFields:{withCredentials:!0},type:"POST",beforeSend:function(n){n.setRequestHeader(correlationIdField,correlationId)},data:u,success:function(t){it(t.Results,n)},error:i}),!1}function e(){$.each(promotedDocumentViewModel.Querys(),function(n,t){n===0&&c(t.Query(),t.DocumentIds())})}function ft(n,t){$("#promoteDocumentRuleError").hide();var r=w(),u=$("<div/>").text(n).html(),f=r.format(profile.Language,u).replace("&amp;","&");$.ajax({url:f,type:"GET",beforeSend:function(n){n.setRequestHeader(correlationIdField,correlationId)},success:function(n){var r=n,i="";return $(r.tokens).each(function(n,t){i+=t.token+" "}),i.trim()===$("#promoteDocumentQuery").val().trim()?t():(promotedDocumentViewModel.QuerySuggestionVisibility(!0),promotedDocumentViewModel.QuerySuggestion(i.trim())),i},error:i})}function l(n,t){$("#promoteDocumentRuleError").hide();var u=p(),r={text:n,spellcheck:{term:{min_word_length:3,size:1,field:"keywords"}}};$requestText=JSON.stringify(r);$.ajax({url:u,type:"POST",data:JSON.stringify(r),beforeSend:function(n){n.setRequestHeader(correlationIdField,correlationId)},success:function(n){var r=n,i="";return $(r.spellcheck).each(function(n,t){t.options.length===0?i+=t.text+" ":$(t.options).each(function(n,t){i+=t.text+" "})}),ft(i,t)},error:i})}function et(n){$("#promoteDocumentRuleError").hide();(n===null||n===undefined)&&(n=promotedDocumentViewModel.Query);var r=profile.Country||"us",f=profile.Language||"en",o="&profile.Country="+r+"&profile.Language="+f;t=$.ajax({url:u()+"GetRuleFromRuleGroup?phrase="+n+o,xhrFields:{withCredentials:!0},type:"GET",beforeSend:function(n){n.setRequestHeader(correlationIdField,correlationId)},success:function(n){ut(n);$("#promoteDocument").toggle();e()},error:i})}function ot(){var n,r;$("#promoteDocumentRuleError").hide();n=[];$.each(promotedDocumentViewModel.Querys(),function(t,i){n.push(i.Query())});r={RuleName:promotedDocumentViewModel.Name(),RulePhrases:n,Segment:profile.Segment||"gen",Language:profile.Language||"en",Country:profile.Country||"us",StartDate:promotedDocumentViewModel.StartDate(),EndDate:promotedDocumentViewModel.EndDate(),Active:!0,DocumentIds:promotedDocumentViewModel.DocumentIds(),Id:promotedDocumentViewModel.Name()};t=$.ajax({url:u()+"SaveOrUpdateRule",xhrFields:{withCredentials:!0},type:"POST",data:r,beforeSend:function(n){n.setRequestHeader(correlationIdField,correlationId)},success:function(){$("#promoteDocument").toggle()},error:i})}function st(){var n,r;$("#promoteDocumentRuleError").hide();n=[];$.each(promotedDocumentViewModel.Querys(),function(t,i){n.push(i.Query())});r={documentId:promotedDocumentViewModel.Name()};t=$.ajax({url:u()+"DeleteRule",xhrFields:{withCredentials:!0},type:"POST",data:r,beforeSend:function(n){n.setRequestHeader(correlationIdField,correlationId)},success:function(){$("#promoteDocument").toggle()},error:i})}function ht(){}var a={src:"Content/images/greentick32.png",title:"Document exists for the query result",cssclass:"glyphicon glyphicon-ok-sign",color:"green"},v={src:"Content/images/redclose32.png",title:"Document doesn't exists for the query result",cssclass:"glyphicon glyphicon-remove-sign",color:"red"},n={src:"Content/images/clear32.png",title:"",cssclass:"",color:""},y={src:"Content/images/loading.gif",title:"",cssclass:"glyphicon glyphicon-refresh glyphicon-refresh-animate",color:""},t=null;return $("#magicalbutton").click(function(){var n=l(mastheadViewModel.Term())}),{getPromotedDocumentRule:et,promotedDocumentChange:tt,PromotedDocumentViewModel:g,DocumentViewModel:r,updatePromotedDocumentRule:ot,removePromotedDocumentRule:st,promotedDocumentButtonClick:ht}},promotedDocumentViewModel=new promotedDocumentRule.PromotedDocumentViewModel;ko.applyBindings(promotedDocumentViewModel,$("#promoteDocument")[0])