(function(){"use strict";u8.Crm.RepSets=function(e){$.extend(this,e);this.items=null;if(!this.defaultRepSet){this.defaultRepSet=this.createDefaultRepSet()}this.bind()};u8.Crm.RepSets.makeClass({name:"RepSets",events:["Save","Delete","ItemsChange"]});$.extend(u8.Crm.RepSets.prototype,{findByName:function(e){if(!this.items){return null}for(var t=0;t<this.items.length;t++){if(this.items[t].name===e){return this.items[t]}}return null},dispose:function(){if(this._isDisposed){return}this._isDisposed=true;this.unbind();delete this.items},include:function(e){this._updateItems(e)},load:function(e){var t=this;function s(s,i){if(t._isDisposed){return}var n=i.response?i.response.repSets:null;t._buildRepSets(n);t.items=n;i={};t.raise("ItemsChange",i);if(typeof e=="function"){e(t,i)}}u8.request("RepsChannel.GetRepSets",{},s,true)},save:function(e,t){var s=this;function i(e,i){var n=i.response;var r=n?s._buildRepSet(n.repSet):null;i={repSet:r,error:i.error,isUpdate:n?n.isUpdate:false,isNew:n?n.isNew:false};if(r){s._updateItems(r)}s.raise("Save",i);if(typeof t=="function"){t(s,i)}}var n=e;var r=new u8.Base.Request("RepsChannel.SaveRepSet",{repSet:n});r.xsi={repSet:{".":"RepSet",".items":"Reps",".items.*":"Rep",".items.Rep.repId":"@id"}};r.execute(i)},delete_:function(e,t){var s=this;function i(i,n){var r=n.response?n.response.success:false;if(!r){return}n={success:r};if(s.items){for(var u=0;u<s.items.length;u++){if(s.items[u].name==e){s.items.splice(u,1);break}}}s.raise("ItemsChange",n);s.raise("Delete",n);if(typeof t=="function"){t(s,n)}}u8.request("RepsChannel.DeleteRepSet",{repSetName:e},i)},createDefaultRepSet:function(){var e=new u8.Crm.RepSet;e.items.push({repId:"S_CurrentRep"});return e},_buildRepSets:function(e){if(!e){return}for(var t=0;t<e.length;t++){e[t]=this._buildRepSet(e[t])}},_buildRepSet:function(e){return new u8.Crm.RepSet(e)},_updateItems:function(e){if(!this.items){this.items=[]}var t=false;for(var s=0;s<this.items.length;s++){var i=this.items[s];if(i.name==e.name){this.items[s]=e;t=true;break}}if(!t){this.items.push(e);this.items.sort(function(e,t){return(e.name||"").localeCompare(t.name)})}this.raise("ItemsChange",{})}});$.extend(u8.Crm.RepSets,{createRepSetFromIds:function(e,t){var s=new u8.Crm.RepSet(t);s.addReps(e);return s}});u8.Crm.RepSets._xsiNoSerialize=true})();