/*
Product Name: dhtmlxSuite 
Version: 4.0.3 
Edition: Professional 
License: content of this file is covered by DHTMLX Commercial or Enterprise license. Usage without proper license is prohibited. To obtain it contact sales@dhtmlx.com
Copyright UAB Dinamenta http://www.dhtmlx.com
*/
/**
*   @desc: hide pivot table related to grid, if any exists, switch grid back to normal mode
*   @type: public
*   @topic: 0
*/ 
dhtmlXGridObject.prototype.hidePivot=function(cont){ 
	if (this._pgridCont){
		if (this._pgrid) this._pgrid.destructor();
		var c=this._pgridCont.parentNode;
		c.innerHTML="";
		if (c.parentNode==this.entBox) 
			this.entBox.removeChild(c);
		this._pgrid=this._pgridSelect=this._pUNI=this._pgridCont=null;
		
	}
}
/**
*   @desc: show pivot table based on grid
*   @type: public
*	@param: cont - html container in which pivot rendered, but default pivot will be rendered over existing grid
*	details: collection of settings; details.column_list - list of columns used in pivot selects; details.readonly - created pivot with fixed configuration, details.action, details.value, action.x, action.y - default values for 4 pivot's selects 
*   @topic: 0
*/ 
dhtmlXGridObject.prototype.makePivot=function(cont,details){
	details=details||{};
	this.hidePivot();
	
	if (!cont){
			var cont=document.createElement("DIV");
			cont.style.cssText="position:absolute; top:0px; left:0px;background-color:white;";
			cont.style.height=this.entBox.offsetHeight+"px";
			cont.style.width=this.entBox.offsetWidth+"px";
			if (this.entBox.style.position!="absolute")
				this.entBox.style.position="relative";
			this.entBox.appendChild(cont);
	}
	
	if (typeof(cont)!="object") cont=document.getElementById(cont)
   
    if (details.column_list)
    	this._column_list=details.column_list;
    else{
		this._column_list=[];
		for (var i=0; i= 0; i--)
		if (resA[i]==av && resB[i]==bv) 
			ret+=this.parseFloat(resC[i]);
	return ret;
}
dhtmlXGridObject.prototype._pivot_action_1=function(a,b,c,av,bv,data){ 
	ret=9999999999;
	var resA=data[a];
	var resB=data[b];
	var resC=data[c];
	
	for (var i = resA.length - 1; i >= 0; i--)
		if (resA[i]==av && resB[i]==bv) 
			ret=Math.min(this.parseFloat(resC[i]),ret);
	if (ret==9999999999) ret="";
	return ret;
}
dhtmlXGridObject.prototype._pivot_action_2=function(a,b,c,av,bv,data){ 
	
	ret=-9999999999;
	var resA=data[a];
	var resB=data[b];
	var resC=data[c];
	for (var i = resA.length - 1; i >= 0; i--)
		if (resA[i]==av && resB[i]==bv) 
			ret=Math.max(this.parseFloat(resC[i]),ret);
	if (ret==-9999999999) ret="";
	return ret;
}
dhtmlXGridObject.prototype._pivot_action_3=function(a,b,c,av,bv,data){ 
	var ret=0;
	var count=0;
	var resA=data[a];
	var resB=data[b];
	var resC=data[c];
	for (var i = resA.length - 1; i >= 0; i--)
		if (resA[i]==av && resB[i]==bv) {
			ret+=this.parseFloat(resC[i]);
			count++;
		}
	return count?ret/count:"";
}
dhtmlXGridObject.prototype._pivot_action_4=function(a,b,c,av,bv,data){ 
	var ret=0;
	var count=0;
	var resA=data[a];
	var resB=data[b];
	var resC=data[c];
	for (var i = resA.length - 1; i >= 0; i--)
		if (resA[i]==av && resB[i]==bv) {
			ret++;
		}
	return ret;
}
dhtmlXGridObject.prototype.parseFloat = function(val){
	val = parseFloat(val);
	if (isNaN(val)) return 0;
	return val;
}
	
dhtmlXGridObject.prototype._renderPivot2=function(){ 
	if (!(this._pivotS.x && this._pivotS.y && this._pivotS.value && this._pivotS.action)) return;
	var action=this["_pivot_action_"+this._pivotS.action];
	var x=this._getUniList(this._pivotS.x);
	var y=this._getUniList(this._pivotS.y);
	
	for (var i=0; i < x.length; i++) {
		for (var j=0; j < y.length; j++) {
			this._pgrid.cells2(j,i+1).setValue(Math.round(action(this._pivotS.x,this._pivotS.y,this._pivotS.value,x[i],y[j],this._pData)*100)/100);
		};
		
	};
}
dhtmlXGridObject.prototype._getUniList=function(col){ 
    if (!this._pUNI[col]){
    	var t={};
    	var a=[];
    	for (var i = this._pData[col].length - 1; i >= 0; i--){
    		t[this._pData[col][i]]=true;
    	}
    	for (var n in t) 
      		if (t[n]===true) a.push(n);
      	this._pUNI[col]=a.sort();
   	}
   	
   	return this._pUNI[col];
}
dhtmlXGridObject.prototype._fillPivotList=function(z,list,miss,v){ 
	if (!miss){
		miss={};
		v=-1;
	}
	z.innerHTML="";
	z.options[z.options.length]=new Option("-select-",-1);
	for (var i=0; i