var pivotGrid_FieldValueCMParams = new Array();
var pivotGrid_HeaderCMParams = new Array();

ASPxRect = _aspxCreateClass(null, {
	constructor: function(left, top, right, bottom){
		this.left = left;
		this.top = top;
		this.right = right;
		this.bottom = bottom;
	},
	Contains: function(x, y) {
		return this.left < x && x < this.right && this.top < y && y < this.bottom;
	},
	Intersects: function(rect) {
		if(rect.left > this.right || rect.top > this.bottom || 
			this.left > rect.right || this.top > rect.bottom) return false;
		return true;
	},
	ToString: function() {
		return "{" + this.left + "," + this.top + "," + this.right + "," + this.bottom + "}";
	}
});

function _aspxGetBounds(elem) {
	 var left = _aspxGetAbsoluteX(elem),
		top = _aspxGetAbsoluteY(elem),
		right = left + elem.offsetWidth,
		bottom = top + elem.offsetHeight;
	return new ASPxRect(left, top, right, bottom);
}

ASPxClientPivotGrid = _aspxCreateClass(ASPxClientControl, {
	constructor: function(name){
	    this.constructor.prototype.constructor.call(this, name);
	    
        this.filterValues = "";
        this.filterPersister = "";
        this.filterFieldIndex = "";
        this.headerMenuHideColumn = "";
        this.fieldMenuCellState = "";
        this.CustomizationFieldsVisibleChanged = new ASPxClientEvent();
        this.AfterCallback = new ASPxClientEvent();
        this.BeforeCallback = new ASPxClientEvent();
        this.CellClick = new ASPxClientEvent();
        this.CellDblClick = new ASPxClientEvent();
        this.PopupMenuItemClick = new ASPxClientEvent();
        this.Targets = new Object();
    },
    GetChildElementById: function(childName){
        if(!_aspxIsExists(this.childrenCache)) 
            this.childrenCache = new Object();
        if(!_aspxIsExistsElement(this.childrenCache[childName])) 
            this.childrenCache[childName] = _aspxGetElementById(this.name + "_" + childName);
        return this.childrenCache[childName];
    },
    GetMainElement: function() { return this.GetChildElementById("ME"); }, 
    GetMainDiv: function() { return this.GetChildElementById("MTD"); },        
    GetMainTable: function() { return this.GetChildElementById("MT"); },
    GetEmptyAreaCell: function(intAreaID) { return this.GetChildElementById("pgArea" + intAreaID); },
    GetHeadersTable: function(stringAreaID) { return this.GetChildElementById("ACC" + stringAreaID); },
    GetFilterWindowContent: function(){ return this.GetChildElementById("FPC"); },
    GetPager: function(){  return this.GetChildElementById("PGR"); },
    GetArrowDragDownImage: function(){  return this.GetChildElementById("IADD"); },
    GetArrowDragUpImage: function(){  return this.GetChildElementById("IADU"); },
    GetDragHideFieldImage: function(){  return this.GetChildElementById("IDHF"); },
    GetResizerImage: function(){  return this.GetChildElementById("FPWR"); },
    GetCustomizationFieldsWindow: function() {
        return aspxGetPopupControlCollection().Get(this.name + "_DXCustFields");
    },
    GetCustomizationFieldsWindowElement: function() {
        return this.GetCustomizationFieldsWindow().GetWindowElement(-1);
    },
    GetDataHeadersPopupCell: function() { return this.GetChildElementById("DHPC"); },
    GetDataHeadersPopup: function() { 
		return aspxGetPopupControlCollection().Get(this.name + "_DHP");
    },
    GetDataHeadersPopupWindowElement: function() { 
		var popupControl = this.GetDataHeadersPopup();
		if(popupControl == null) return null;
		return popupControl.GetWindowElement(-1);
    },
    IsDataHeadersPopupExists: function() {
		var headersPopup = this.GetDataHeadersPopup();
		if(!_aspxIsExists(headersPopup)) return false;
		var headersPopupWindow = headersPopup.GetWindowElement(-1);
		return _aspxIsExists(headersPopupWindow) && _aspxIsExists(headersPopupWindow.id);
    },
    Initialize: function(){
		this.constructor.prototype.Initialize.call(this);
    
        if(this.__aspxPivotFilterWindow == null) {
            this.__aspxPivotFilterWindow = new ASPxClientPopupFilterWindow(this.name);
        }
        var resizerImage = this.GetResizerImage();
        if(resizerImage != null) 
            resizerImage.ondragstart = function() { this.releaseCapture(); return false; };
        this.AfterCallBackInitialize();        
    },   
    AfterInitialize: function() {		
		this.constructor.prototype.AfterInitialize.call(this);
		if(this.IsDataHeadersPopupExists()) {
			var headersPopup = this.GetDataHeadersPopup();
			headersPopup.SetSize(1, 1);
			if(headersPopup.IsVisible()) 
				headersPopup.Hide();
        }
    },    
    AfterCallBackInitialize: function() {
        this.AssignContextMenus(pivotGrid_FieldValueCMParams, pivotGrid_FieldValueContextMenuHandler);
        this.AssignContextMenus(pivotGrid_HeaderCMParams, pivotGrid_HeaderContextMenuHandler);
    },
    AssignContextMenus: function(cmParams, handler) {
        if(_aspxIsExists(cmParams) && _aspxIsExists(cmParams[this.name])) {
            var params = cmParams[this.name];
            for(var i = 0; i < params.length; i++) {
                var id = params[i][0];
                var elem = this.GetChildElementById(id);
                if(elem != null) {
                    elem.contextMenuParams = params[i];
                    elem.pivotClientID = this.name;
                    _aspxAttachEventToElement(elem, "contextmenu", handler);   
                }                    
            }            
        }
    },
    CreateLoadingPanel: function(content){
        if(content != null){
			content.innerHTML = '';
            var element = this.CreateLoadingPanelInsideContainer(content);
            if(element != null) {
					element.style.borderWidth = 0;
				for(var i = 0; i < element.childNodes.length; i++) {
					var child = element.childNodes[i];
					if(_aspxIsExists(child.style))
						child.style.border = "0";
				}
            }
        }
    },
    ShowFilterPopup: function(headerId, index) {
        var headerElement = _aspxGetElementById(headerId);
        var fwContent = this.GetFilterWindowContent();
        if(headerElement == null || fwContent == null) return;
        this.__aspxPivotFilterWindow.Show(headerElement, this.GetMainElement());
        if(!_aspxIsExists(fwContent.dxFieldIndex) || fwContent.dxFieldIndex != index) {
            this.CreateLoadingPanel(fwContent);
            this.CreateCallback("FS" + index);
        }
    },
    PerformCallbackInternal: function(el, arg) {        
        if(this.callBacksEnabled) {
            this.OnBeforeCallback(el);
            this.CreateCallback(arg);
        }
        else
            this.SendPostBack(arg);
    },
    OnCallback: function(result) {              
        var value = new String(result);
        var values = value.split("|");
        if(values[0] == "F") 
            this.doFilterCallback(values);
        if(values[0] == "G") 
            this.doGridRefreshCallback(values);
        this.OnAfterCallback();
    },
    OnCallbackError: function(result, data) {
        this.constructor.prototype.OnCallbackError.call(this, result, data);
		this.OnAfterCallback();
    },
    OnBeforeCallback: function(el) {
        var mainDiv = this.GetMainDiv();
        if(mainDiv == null) return; 
        
        var mainElement = this.GetMainElement();
        this.CreateLoadingPanelWithAbsolutePosition(mainDiv, mainElement);
        this.CreateLoadingDiv(mainDiv, mainElement);
                
        if(_aspxIsExists(this.RaiseBeforeCallback))
		    this.RaiseBeforeCallback();
    },
    OnAfterCallback: function() {
        this.childrenCache = new Object();    
		this.isInitialized = false;		
		
		if(_aspxIsExists(this.RaiseAfterCallback))
		    this.RaiseAfterCallback();
    },    
    GetCallbackState: function() {
		var elem = this.GetChildElementById("CallbackState");
		if(elem == null) return "";
		return "CB|" + elem.value;
    },
    doFilterCallback: function(values) {
        if(values.length < 5) return;
        var fwContent = this.GetFilterWindowContent();
        if(fwContent == null) return;
        this.filterValues = values[1];
        this.filterPersister = values[2];
        this.filterFieldIndex = values[3];
        fwContent.dxFieldIndex = this.filterFieldIndex;
        fwContent.innerHTML = values[4];
    },
    doGridRefreshCallback: function(values) {        
        var mainDiv = this.GetMainDiv();
        if(mainDiv == null) return;
        if(values.length < 2 || mainDiv == null) return;
        var innerHTML = "";
        for(var i = 1; i < values.length; i ++) {
            innerHTML += values[i];
        }
        mainDiv.innerHTML = innerHTML;
    },
    isAllFilterValuesChecked: function() {
		var i = 0;
		while(_aspxIsExists(checkBox = document.getElementById(this.name + 'FTRI' + i))) {
			if(!checkBox.checked) return false;
			i++;
		}
		return true;
    },
    filterValueChanged: function(index) {
		var showAllCheckBox = document.getElementById(this.name + 'FTRIAll');
		if(index < 0) {
			var i = 0, checkBox;
			var newFilterValues = '', newChar = showAllCheckBox.checked ? 'T' : 'F';			
			while(_aspxIsExists(checkBox = document.getElementById(this.name + 'FTRI' + i))) {
				checkBox.checked = showAllCheckBox.checked;
				newFilterValues += newChar;
				i++;
			}	
			this.filterValues = newFilterValues;		
		} else {
			var newChar = this.filterValues.charAt(index) == 'T' ? 'F' : 'T';
			this.filterValues = this.filterValues.substr(0, index) + newChar + this.filterValues.substr(index + 1, this.filterValues.length - index - 1);			
			showAllCheckBox.checked = this.isAllFilterValuesChecked();
        }
        var btn = this.GetChildElementById("FPWOK");
		if(btn != null)  {
			btn.disabled = this.filterValues.indexOf("T") == -1;
		}
    },
    applyFilter: function() {
        this.PerformCallbackInternal(this.GetMainTable(), "F|" + this.filterValues + '|' + this.filterPersister + '|' + this.filterFieldIndex);
        if(this.__aspxPivotFilterWindow != null)
		    this.__aspxPivotFilterWindow.Hide();
    },
    headerMouseDown: function(root, e) {
        if(!_aspxGetIsLeftButtonPressed(e)) return;
	    var drag = new ASPxClientDragHelper(e, root, true);
	    var targets = new ASPxClientCursorTargets();
	    this.configureDrag(drag, targets);
	    this.configureTargets(targets, drag);
	    this.registerTargets(targets, drag.obj);
    },
    configureDrag: function(drag, targets) {        
		drag.pGrid = this;
		drag.targets = targets;
        drag.onCloneCreated = this.cloneCreated;
        drag.onEndDrag = this.endDrag;
    },
    configureTargets: function(targets, drag) {
	    targets.pGrid = this;
	    targets.drag = drag;
	    targets.onTargetChanging = this.targetChanging;
	    targets.onTargetChanged = this.targetChanged;
    },
    registerTargets: function(targets, dragObj) {
	    targets.addElement(this.GetCustomizationFieldsWindowElement());
	    targets.addElement(this.GetEmptyAreaCell(0));
	    targets.addElement(this.GetEmptyAreaCell(1));
	    targets.addElement(this.GetEmptyAreaCell(2));
	    targets.addElement(this.GetEmptyAreaCell(3));	   	    
	    
	    var targetIds = new Array();
	    targetIds.push("pgGroupHeader", "pgHeader");
	    targets.RegisterTargets(this.GetDataHeadersPopupWindowElement(), targetIds);
	    targets.RegisterTargets(this.GetHeadersTable("RowArea"), targetIds);	    
        targets.RegisterTargets(this.GetHeadersTable("ColumnArea"), targetIds);
        targets.RegisterTargets(this.GetHeadersTable("DataArea"), targetIds);
        targets.RegisterTargets(this.GetHeadersTable("FilterArea"), targetIds);
	    targets.UnregisterTargets(dragObj, targetIds);
	    
	    targets.dataHeadersPopup = this.IsDataHeadersPopupExists() ? this.GetDataHeadersPopup() : null;
	    
	    if(targets.dataHeadersPopup != null) {
			targets.dataHeadersPopupCell = this.GetDataHeadersPopupCell();
			targets.dataHeadersPopupCellRect = _aspxGetBounds(targets.dataHeadersPopupCell);
		}
    },
    headerClick: function(element) {
        if(element.id.indexOf("sorted") > 0){
			this.ResetFilterCache(this.filterFieldIndex);
            this.PerformCallbackInternal(element, 'S|' + element.id);
        }
    },
    ResetFilterCache: function(indexToReset) {
        var fwContent = this.GetFilterWindowContent();
        if(fwContent != null && (!_aspxIsExists(indexToReset) || indexToReset == fwContent.dxFieldIndex))
		    fwContent.dxFieldIndex = -1;
    },
    cloneCreated: function(clone) {
        var list = new Array();
        var width = this.obj.offsetWidth + "px";
        clone.style.width = width;
        list.push(clone);
        for(var i = 0; i < clone.childNodes.length; i ++) {
			if(_aspxIsExists(clone.childNodes[i].style))
				clone.childNodes[i].style.width = width;
            list.push(clone.childNodes[i]);            
        }
        for(var i = 0; i < list.length; i ++) {
            if(!_aspxIsExists(list[i].style)) continue;
            var width = new String(list[i].style.width);
            if(width.indexOf("%") > -1) {
                list[i].style.width = "";
            }
        }
    },
    targetChanging: function(targets) {
        targets.targetTag = targets.isLeftPartOfElement();
        if(targets.pGrid.IsTargetElementArea(targets)) {
            targets.targetTag = true;
        }
        
        if(targets.dataHeadersPopup == null) return;
		var popupControl = targets.dataHeadersPopup;
		if(targets.dataHeadersPopupCellRect.Contains(targets.x, targets.y)) {				
			if(!popupControl.IsVisible()) {				
				popupControl.Show();	
				var windowElem = popupControl.GetWindowElement(-1);			
				popupControl.bounds = _aspxGetBounds(windowElem);
				
				var list = new Array();
				_aspxGetElementsByPartialId(windowElem, 'pgHeader', list);
				for(var i = 0; i < list.length; i++)
					for(var j = 0; j < targets.list.length; j++) {
						if(targets.list[j].targetElement == list[i]) {
							targets.list[j].absoluteX = _aspxGetAbsoluteX(list[i]);
							targets.list[j].absoluteY = _aspxGetAbsoluteY(list[i]);
							break;
						}
					}
			}
		} else {
			if(_aspxIsExists(popupControl.bounds) && popupControl.bounds != null && popupControl.IsVisible()) {
				if(popupControl.bounds.Contains(targets.x, targets.y)) {
					if(_aspxIsExists(targets.targetElement) && targets.targetElement.id.indexOf("DHP") < 0) 
						targets.cancelChanging();
				} else {				
					popupControl.Hide();
					popupControl.bounds = null;									
				}
			}
		}
    },
    IsTargetElementArea: function(targets) {
        return targets.targetElement != null && targets.targetElement.id.indexOf("_pgArea") > -1;
    },
    IsTargetAllowed: function(targets) {
        var allowedAreas = targets.pGrid.allowedAreas[targets.drag.obj.id];
        if(allowedAreas == null) return true;
        return _aspxArrayIndexOf(allowedAreas, targets.targetElement.id) > -1;
    },
    targetChanged: function(targets) {
       if(__aspxDragHelper == null) return;
       if(targets.targetElement == __aspxDragHelper.obj) return;
       if(targets.targetElement != null && targets.pGrid.IsTargetAllowed(targets)) {
            targets.pGrid.setDragImagesPosition(targets.targetElement, targets.targetTag);
       } else {
            targets.pGrid.targetImagesChangeVisibility("hidden");
       }
    },    
    targetImagesChangeVisibility: function(vis) {
        if(this.GetArrowDragDownImage() == null) return;
        this.GetArrowDragDownImage().style.visibility = vis;
        this.GetArrowDragUpImage().style.visibility = vis;
        if(__aspxDragHelper != null) {
            __aspxDragHelper.removeElementFromDragDiv();
        }
    },
    setDragImagesPosition: function(el, isLeft) {
        this.targetImagesChangeVisibility("hidden");
        if(el == this.GetCustomizationFieldsWindowElement()) {
            __aspxDragHelper.addElementToDragDiv(this.GetDragHideFieldImage());
        } else {
			var mainElement = this.GetMainElement();
			var arrowUp = this.GetArrowDragUpImage();
            var arrowDown = this.GetArrowDragDownImage();
            var left = 0, top = 0;
			if(el.id.indexOf("DHP") < 0) {
				left = _aspxGetAbsoluteX(el);// - _aspxGetIEDocumentClientOffset(true);
				top = _aspxGetAbsoluteY(el);// - _aspxGetIEDocumentClientOffset(false);
			} else {
				var windowElem = this.GetDataHeadersPopupWindowElement();
				left = _aspxGetAbsoluteX(el) - _aspxGetIEDocumentClientOffset(true);
				top = _aspxGetAbsoluteY(el) - _aspxGetIEDocumentClientOffset(false);
				arrowDown.style.zIndex = windowElem.style.zIndex + 1;
				arrowUp.style.zIndex = windowElem.style.zIndex + 1;
			}
			if(!isLeft)
				left += el.offsetWidth;
            this.targetImagesChangeVisibility("visible");

			var x = left - (arrowDown.offsetWidth / 2);
			x = isLeft ? Math.floor(x) : Math.ceil(x);
            _aspxSetAbsoluteX(arrowDown, x);
            _aspxSetAbsoluteY(arrowDown, (top - arrowDown.offsetHeight));
            _aspxSetAbsoluteX(arrowUp, x);
            _aspxSetAbsoluteY(arrowUp, (top + el.offsetHeight));
        }
    },
    endDrag: function(drag) {
		drag.pGrid.targetImagesChangeVisibility("hidden");
		
        if(drag.targets == null || drag.targets.targetElement == null) return;
        drag.pGrid.PerformCallbackInternal(drag.targets.targetElement, 'D' + '|' + drag.obj.id + '|' + drag.targets.targetElement.id + '|' + drag.targets.targetTag);
    },
    HideCustomizationFields: function() {
        this.ChangeCustomizationFieldsVisibilityInternal(false, true);
    },
    ShowCustomizationFields: function(animate) {
        this.ChangeCustomizationFieldsVisibilityInternal(true, animate);
    },
    IsCustomizationFieldsVisible: function() {
        var fieldsWindow = this.GetCustomizationFieldsWindow();
        if(fieldsWindow == null) return false;
        return fieldsWindow.IsVisible();
    },
    ChangeCustomizationFieldsVisibilityInternal: function(visible, animate) {
        if(!_aspxIsExists(visible)) {
            visible = !this.IsCustomizationFieldsVisible();
        }
        var fieldsWindow = this.GetCustomizationFieldsWindow();
        if(fieldsWindow == null) return;
        var oldEnableAnimation = fieldsWindow.enableAnimation;
        fieldsWindow.enableAnimation = animate;
        if(visible) {
            fieldsWindow.Show();
        } else {
            fieldsWindow.Hide();
        }
        fieldsWindow.enableAnimation = oldEnableAnimation;
    },
    showHeaderMenu: function(e, hideColumn, fieldID) {
        this.headerMenuHideColumn = hideColumn;
        this.headerMenuFieldID = fieldID;
        var menu = aspxGetControlCollection().Get(this.name + "_HM");
        if(menu == null) return;
        this.SetMenuItemVisibilityState(menu, "Hide", hideColumn != "");
        this.SetMenuItemVisibilityState(menu, "HideList", this.IsCustomizationFieldsVisible());
		this.SetMenuItemVisibilityState(menu, "ShowList", !this.IsCustomizationFieldsVisible());
			
        this.showMenu(e, menu);
    },
    SetMenuItemVisibilityState: function(menu, name, isVisible) {
		menuItem = menu.GetItemByName(name);
        if(menuItem != null )
			menuItem.SetVisible(isVisible);
    },
    onHeaderMenuClick: function(itemName) {
        switch(itemName) {
            case "Refresh": this.PerformCallbackInternal(this.GetMainTable(), ""); break;
            case "Hide": this.PerformCallbackInternal(this.GetMainTable(), this.headerMenuHideColumn); break;
            case "HideList": this.HideCustomizationFields(); break;
            case "ShowList": this.ShowCustomizationFields(true); break;
            default: 
				if(_aspxIsExists(this.RaisePopupMenuItemClick))
					this.RaisePopupMenuItemClick("HeaderMenu", itemName, this.headerMenuFieldID, -1);
				break;
        }
    },
    showFieldValueMenu: function(e, state, iscollapsed, fieldID, itemIndex) {
        this.fieldMenuCellState = state;
        this.fieldMenuFieldID = fieldID;
        this.fieldMenuFieldValueIndex = itemIndex;
        var menu = aspxGetControlCollection().Get(this.name + "_FVM");
        if(menu == null) return;        
        this.SetMenuItemVisibilityState(menu, "Expand", iscollapsed && state != "");
        this.SetMenuItemVisibilityState(menu, "Collapse", !iscollapsed && state != "");
        this.SetMenuItemVisibilityState(menu, "ExpandAll", state != "");
        this.SetMenuItemVisibilityState(menu, "CollapseAll", state != "");
			
        this.showMenu(e, menu);
    },
    showMenu: function(e, menu) {
        menu.ShowInternal(e);
        e.returnValue = false;
        e.cancelBubble = true;
    },
    onFieldValueMenuClick: function(itemName) {
        switch(itemName) {
            case "Expand": 
            case "Collapse": this.PerformCallbackInternal(this.GetMainTable(), this.fieldMenuCellState); break;
            case "ExpandAll": this.PerformCallbackInternal(this.GetMainTable(), this.fieldMenuCellState + "EA"); break;
            case "CollapseAll": this.PerformCallbackInternal(this.GetMainTable(), this.fieldMenuCellState + "CA"); break;
            default: 
				if(_aspxIsExists(this.RaisePopupMenuItemClick))
					this.RaisePopupMenuItemClick("FieldValueMenu", itemName, this.fieldMenuFieldID, this.fieldMenuFieldValueIndex);
				break;
        }
    },
    DoPagerClick: function(element, value) {
        this.PerformCallbackInternal(element, "P|" + value);
    }  
});

function pivotGrid_GetGrid(id) {
    return aspxGetControlCollection().Get(id);
}
function pivotGrid_GetGridFromMenu(menu) {
    if(menu == null) return null;
    var pos = menu.name.lastIndexOf("_");
    if(pos > -1) {
        return pivotGrid_GetGrid(menu.name.substring(0, pos));
    } 
    return null;
}
function pivotGrid_OnHeaderMenuClick(s, args) {
    var grid = pivotGrid_GetGridFromMenu(s);
    if(grid != null) {
        grid.onHeaderMenuClick(args.item.name);
    }
}
function pivotGrid_ShowHeaderMenu(id, e, hideColumn, fieldID) {
    var grid = pivotGrid_GetGrid(id);
    if(grid != null) {
        grid.showHeaderMenu(e, hideColumn, fieldID);
    }
}
function pivotGrid_OnFieldValueMenuClick(s, args) {
    var grid = pivotGrid_GetGridFromMenu(s);
    if(grid != null) {
        grid.onFieldValueMenuClick(args.item.name);
    }
}
function pivotGrid_ShowFieldValueMenu(id, e, state, iscollapsed, fieldID, itemIndex) {
    var grid = pivotGrid_GetGrid(id);
    if(grid != null) {   
        grid.showFieldValueMenu(e, state, iscollapsed, fieldID, itemIndex);
    }
}
function pivotGrid_HeaderMouseDown(id, element, e) {
    var grid = pivotGrid_GetGrid(id);
    if(grid != null) {
       grid.headerMouseDown(element, e);            
    }
}
function pivotGrid_HeaderClick(id, element, e) {
    var grid = pivotGrid_GetGrid(id);
    if(grid != null) {
        grid.headerClick(element);
    }
}

function pivotGrid_ShowFilterPopup(name, headerId, index) {
	if(__aspxClientPopupFilterWindowShowAgain) {
		__aspxClientPopupFilterWindowShowAgain = false;
		return;
	}
    var pg = aspxGetControlCollection().Get(name);
    if(pg != null) 
        pg.ShowFilterPopup(headerId, index);
}    

function pivotGrid_PerformCallback(name, el, value) {
    var pg = pivotGrid_GetGrid(name);
    if(pg != null) 
        pg.PerformCallbackInternal(el, value);
}

function pivotGrid_FilterValueChanged(name, index) {
    var pg = pivotGrid_GetGrid(name);
    if(pg != null) 
        pg.filterValueChanged(index);
    else
		alert("PivotGrid was not found");
}

function pivotGrid_ApplyFilter(name) {
    var pg = pivotGrid_GetGrid(name);
    if(pg != null) 
        pg.applyFilter();
    else
		alert("PivotGrid was not found");
}

function pivotGrid_HideFilter(name) {
	var pg = pivotGrid_GetGrid(name);
    if(pg != null) 
        pg.__aspxPivotFilterWindow.Hide();
}

function pivotGrid_ClearSelection(){
    if(!__aspxOpera && !__aspxOpera9) return;
    if (_aspxIsExists(window.getSelection)){
        if (__aspxSafari)
            window.getSelection().collapse();
        else
            window.getSelection().removeAllRanges();
    }
    else if (_aspxIsExists(document.selection)){
        if(_aspxIsExists(document.selection.empty))
            document.selection.empty();
		else if(_aspxIsExists(document.selection.clear))
			document.selection.clear();
    }
}

function pivotGrid_PagerClick(name, element, id) {
    var pg = pivotGrid_GetGrid(name);
    if(pg != null) pg.DoPagerClick(element, id);
}

function DoCustomizationFieldsVisibleChanged(sender){
	var name = sender.name.substr(0, sender.name.length - ("_DXCustFields").length);
	var control = aspxGetControlCollection().Get(name);
	if(control == null) return;
	if(_aspxIsExists(control.RaiseCustomizationFieldsVisibleChanged)) {
		control.RaiseCustomizationFieldsVisibleChanged();
	}
}

function pivotGrid_FieldValueContextMenuHandler(e) {
    var source = _aspxGetEventSource(e);
    while(source != null && !_aspxIsExists(source.contextMenuParams))
        source = _aspxGetParentNode(source);
    if(source == null) return;
    pivotGrid_ShowFieldValueMenu(source.pivotClientID, e, source.contextMenuParams[1], source.contextMenuParams[2] == "true" ? true : false,
        source.contextMenuParams[3], source.contextMenuParams[4]); 
} 

function pivotGrid_HeaderContextMenuHandler(e) {
    var source = _aspxGetEventSource(e);
    while(source != null && !_aspxIsExists(source.contextMenuParams))
        source = _aspxGetParentNode(source);
    if(source == null) return;
    pivotGrid_ShowHeaderMenu(source.pivotClientID, e, source.contextMenuParams[1], source.contextMenuParams[2]); 
}

function pivotGrid_AfterCallBackInitialize(id) {
    var pg = pivotGrid_GetGrid(id);
    if(pg != null) 
        pg.AfterCallBackInitialize();
}

function pivotGrid_CellClick(id, value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex) {
    var pg = pivotGrid_GetGrid(id);
    if(pg != null && _aspxIsExists(pg.RaiseCellClick)) 
        pg.RaiseCellClick(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex);
}

function pivotGrid_CellDoubleClick(id, value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex) {
    var pg = pivotGrid_GetGrid(id);
    if(pg != null && _aspxIsExists(pg.RaiseCellDblClick)) 
        pg.RaiseCellDblClick(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex);
}    
	ASPxClientPivotGrid.prototype.RaiseCustomizationFieldsVisibleChanged = function(){
		if(!this.CustomizationFieldsVisibleChanged.IsEmpty()){
			var args = new ASPxClientEventArgs();            
			this.CustomizationFieldsVisibleChanged.FireEvent(this, args);
		}
	}    
	ASPxClientPivotGrid.prototype.RaiseCellClick = function(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex){
		if(!this.CellClick.IsEmpty()){
			var args = new ASPxClientClickEventArgs(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex);
			this.CellClick.FireEvent(this, args);
		}
	}    
	ASPxClientPivotGrid.prototype.RaiseCellDblClick = function(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex){
		if(!this.CellDblClick.IsEmpty()){
			var args = new ASPxClientClickEventArgs(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex);
			this.CellDblClick.FireEvent(this, args);
		}
	}    
	ASPxClientPivotGrid.prototype.RaiseAfterCallback = function(){
		if(!this.AfterCallback.IsEmpty()){
			var args = new ASPxClientEventArgs();            
			this.AfterCallback.FireEvent(this, args);
		}
	}    
	ASPxClientPivotGrid.prototype.RaiseBeforeCallback = function(){
		if(!this.BeforeCallback.IsEmpty()){
			var args = new ASPxClientEventArgs();            
			this.BeforeCallback.FireEvent(this, args);
		}
	}  
    ASPxClientPivotGrid.prototype.RaisePopupMenuItemClick = function(menuType, menuItemName, fieldID, fieldValueIndex) {
		if(!this.PopupMenuItemClick.IsEmpty()){
			var args = new ASPxClientPivotMenuItemClickEventArgs(menuType, menuItemName, fieldID, fieldValueIndex);            
			this.PopupMenuItemClick.FireEvent(this, args);
		}
	}
	ASPxClientPivotGrid.prototype.GetCustomizationFieldsVisibility = function(){
	    return this.IsCustomizationFieldsVisible();
	}
	ASPxClientPivotGrid.prototype.SetCustomizationFieldsVisibility = function(value){
	    this.ChangeCustomizationFieldsVisibilityInternal(value);
	}
	ASPxClientPivotGrid.prototype.ChangeCustomizationFieldsVisibility = function(){
	    this.ChangeCustomizationFieldsVisibilityInternal();
	}
    ASPxClientPivotGrid.prototype.PerformCallback = function(args){
        if(!_aspxIsExists(args)) args = "";
        this.ResetFilterCache();
        this.PerformCallbackInternal(this.GetMainTable(), "C|" + args);
    }
ASPxClientClickEventArgs = _aspxCreateClass(ASPxClientEventArgs, {
    constructor: function(value, columnIndex, rowIndex, columnValue, rowValue, columnFieldName, rowFieldName, columnValueType, rowValueType, dataIndex){
        this.constructor.prototype.constructor.call();
        this.Value = value;
        this.ColumnIndex = columnIndex;
        this.RowIndex = rowIndex;
        this.ColumnValue = columnValue;
        this.RowValue = rowValue;
        this.ColumnFieldName = columnFieldName;
        this.RowFieldName = rowFieldName;
        this.ColumnValueType = columnValueType;
        this.RowValueType = rowValueType;
        this.DataIndex = dataIndex;
    }
});
ASPxClientPivotMenuItemClickEventArgs = _aspxCreateClass(ASPxClientEventArgs, {
    constructor: function(menuType, menuItemName, fieldID, fieldValueIndex) {
        this.constructor.prototype.constructor.call();
        this.MenuType = menuType;
        this.MenuItemName = menuItemName;
        this.FieldID = fieldID;
        this.FieldValueIndex = fieldValueIndex;
    }
});