(function(){function e(e){if(!e){return false}if(u8.Crm.FilterExpressions.isAggregationFunction(e["function"])){return true}if(e.fieldId==u8.Crm.Schema.VirtualFieldId.NumberOfRecords){return true}return false}u8.Crm.FilterNode=function(e){$.extend(this,e);if(this.children){for(var t=0;t<this.children.length;t++){var i=this.children[t];if(i){i=new u8.Crm.FilterNode(i);i.parent=this;this.children[t]=i}}}};$.extend(u8.Crm.FilterNode.prototype,{isLeaf:function(){return this.nodeType=="FilterLeafNode"},isRoot:function(){return this.parent?false:true},isAdvanced:function(){if(this.isLeaf()){return e(this.lValue)}if(!this.hasChildren()){return}for(var t=0;t<this.children.length;t++){var i=this.children[t];if(!i.isAdvanced()){return false}}return true},isAdvancedAndNormal:function(){if(this.isLeaf()){return false}if(!this.hasChildren()){return false}var e=0,t=0;for(var i=0;i<this.children.length;i++){var r=this.children[i];if(r.isAdvanced()){e++}else{t++}if(e&&t){return true}}return false},hasChildren:function(){return this.children&&this.children.length>0},getParents:function(e){if(this.parent){e.push(this.parent);this.parent.getParents(e)}},each:function(e){e(this);if(this.hasChildren()){for(var t=0;t<this.children.length;t++){var i=this.children[t];i.each(e)}}return this},remove:function(){if(this.parent){for(var e=0;e<this.parent.children.length;e++){if(this.parent.children[e]==this){this.parent.children.splice(e,1);break}}delete this.parent}else if(this.filter){this.filter.root=null;delete this.filter}return this},addChild:function(e){if(!e){$error(u8.Base.ErrorUids.arguments,"Invalid or missing argument 'child' in a call to 'u8.Crm.FilterNode.addChild'.")}if(this.isLeaf()){$error(u8.Base.ErrorUids.operation,"Cannot add a child to a leaf node!")}if(!this.children){this.children=[]}e._setParent(this);this.children.push(e);return e},insertChildAfter:function(e,t){if(!this.children){return this.addChild(t)}var i;if(!e){i=0}else{for(var r=0;r<this.children.length;r++){if(this.children[r]==e){break}}if(r>=this.children.length-1){return this.addChild(t)}i=r+1}this.children.splice(i,0,t);t.parent=this;return t},getFilter:function(){if(this.filter){return this.filter}if(this.parent){return this.parent.getFilter()}return null},replace:function(e){var t=this.parent;if(t){for(var i=0;i<t.children.length;i++){if(t.children[i]==this){t.children[i]=e;e._setParent(t);break}}}else if(this.filter){this.filter.setRoot(e)}this._setParent(null);return e},and:function(){if(this.isLeaf()&&this.parent&&this.parent.operator=="AND"){return this.parent}if(this.operator=="AND"){return this}var e=Array.prototype.slice.call(arguments,0);e.unshift("AND");return this._combine.apply(this,e)},or:function(){if(this.isLeaf()&&this.parent&&this.parent.operator=="OR"){return this.parent}if(this.operator=="OR"){return this}var e=Array.prototype.slice.call(arguments,0);e.unshift("OR");return this._combine.apply(this,e)},equals:function(e,t){return this._combineWith(e,"=",t)},greater:function(e,t){return this._combineWith(e,">",t)},greaterEqual:function(e,t){return this._combineWith(e,">=",t)},less:function(e,t){return this._combineWith(e,"<",t)},lessEqual:function(e,t){return this._combineWith(e,"<=",t)},clone:function(){var e=new u8.Crm.FilterNode;e.nodeType=this.nodeType;if(this.isLeaf()){e.lValue=u8.Crm.Filter.cloneValue(this.lValue);e.operator=this.operator;e.rValue=u8.Crm.Filter.cloneValue(this.rValue)}else if(this.children){e.operator=this.operator;e.children=[];for(var t=0;t<this.children.length;t++){var i=this.children[t];e.addChild(i.clone())}}return e},_combine:function(e,t,i,r){var n=this.getFilter();if(arguments.length>2){var h;if(typeof t=="object"&&arguments.length==2){h=t}else{h=n.create(t,i,r)}if(!h){return this}}else if(arguments.length>1){h=t}if(this.isLeaf()||this.operator!=e){var s=u8.Crm.Filter.createNode(e);this.replace(s);s.addChild(this);if(h){s.addChild(h)}return s}this.addChild(h);return this},_combineWith:function(e,t,i){if(this.isLeaf()){return this._combine("AND")}this.addChild(u8.Crm.Filter.createNode(e,t,i));return this},_setParent:function(e){if(!e){if(this.filter){delete this.filter}else{delete this.parent}}else if(e){this.parent=e;if(this.filter){delete this.filter}}}})})();