/*supportato fino alla versione 2003 di visual studio*/
/* sostituisce onitDataPanel.js
Gli oggetti utilizzati da wzMsDatagrid e da OnitGrid sono in comune 
*/
/* =========================--   ONIT GRID  --=========================================*/

/*master object array*/
var wzMsDatagrid=new Object;

function /*class*/ wzMsDatagrid_event(oSender,oEvent){
	this.sender=oSender;
	this.event=oEvent;
	this.newIndex=null;
}

function /*class*/ wzMsDatagrid_Object(dgrId,altCss, selCss, iteCss, selIx, selHandler,offItem,sKeys,cascSty,orSelSty,selOpt,unId,dblClkHandler){
	this.ItemStyle=new wzMsDatagrid_Style(iteCss);
	this.AlternatingItemStyle=new wzMsDatagrid_Style(altCss);
	this.SelectedItemStyle=new wzMsDatagrid_Style(selCss);
	this.fromCode=function(iCode){ if(iCode==0){return this.ItemStyle}else {if(iCode==1){return this.AlternatingItemStyle}else{return this.SelectedItemStyle}} };
	this.SelectedIndex=selIx;
	this.ClientId=dgrId;
	this.UniqueID=unId;	
	this.ClientSelectionHandler=selHandler;
	this.ClientDoubleClickHandler=dblClkHandler;
	this.CascadeStyles=(cascSty==null?false:cascSty); /*indica che gli stili vengono messi in cascata*/
	/*private - da non richiamare negli applicativi*/
	this.offsetItem=offItem; /* indica da quale riga partono gli items*/
	this.keys=wzMsDatagrid_splitKeys(sKeys);/*elenco delle chiavi per identificare le colonne*/
	this.originalSelStyle=orSelSty;
	this.SelectionOption=(selOpt==null?3:selOpt);/* 2=rowClick, 3=clientOnly */
	/* metodi */
	this.getItem=wzMsDatagrid_getItem;
	this.getSelectedItem=function(){return this.getItem(this.SelectedIndex);}
	this.setSelectedIndex=wzMsDatagrid_setSelectedIndex;/*seleziona un elemento, true se tutto ok*/
	this.getSelObject=wzMsDatagrid_getSelObject;
	this.getSelText=wzMsDatagrid_getSelText;
	this.update=wzMsDatagrid_update; /*aggiorna i dati nel controllo hidden*/
	this.getItemCell=wzMsDatagrid_getItemCell; /*restituisce una cella dato indice base 0 e il key*/
	this.getCellText=wzMsDatagrid_getCellText; /*ottiene il testo contenuto in una cella dato indice base 0 e il key */
}

function /*class*/ wzMsDatagrid_Style(css){
	this.CssClass=css;
}

/*ritorna la tablerow*/
function /*method*/ wzMsDatagrid_getItem(rowIndex){
	var oTable=document.getElementById(this.ClientId);
	var oRet=null;
	if (oTable !=null && rowIndex>=0){
		if (oTable.rows.length>(rowIndex+this.offsetItem))
		oRet=oTable.rows[rowIndex+this.offsetItem];
	}
	return oRet;
}

/*implementa la selezione e fa scattare eventuale evento client*/
function /*method*/ wzMsDatagrid_setSelectedIndex(rowIndex){
	var oNewSel= this.getItem(rowIndex);
	var oOldSel= this.getSelectedItem();
	var blnRet=false;
	var styleCode= ((this.SelectedIndex%2==0)?0:1);
	/*vecchi stili*/
	var oldRowIx=this.SelectedIndex;
	var originalSelStyleBk=this.originalSelStyle;
	
	if (oNewSel!=null && oOldSel!=oNewSel){
		/*salvataggio dello stile di origine*/
		this.originalSelStyle=oNewSel.className;
		
		if (this.CascadeStyles){
			/*accodo lo stile della selezione*/
			oNewSel.className+=" " + this.SelectedItemStyle.CssClass;
		}else{
			/*sovrascrivo lo stile della selezione*/
			oNewSel.className=this.SelectedItemStyle.CssClass;
		}
		
		this.SelectedIndex=rowIndex;
		this.update();
		blnRet=true;
		
		/*deselezione della vecchia riga*/
		if (oOldSel!=null){
			if (originalSelStyleBk!=null){
				oOldSel.className=originalSelStyleBk;
			}else{
				/*rimpiazzo il vecchio stile con quello di default*/
				oOldSel.className=this.fromCode(styleCode).CssClass;
			}
		}
	}
	return blnRet;
}

/*ottiene il contenuto di una cella in base al key */
function /*method*/ wzMsDatagrid_getSelObject(sKey){
	return this.getItemCell(this.SelectedIndex,sKey);
}

/*ottiene il testo contenuto in una cella della riga selezionata in base al key */
function /*method*/ wzMsDatagrid_getSelText(sKey){
	var oCell=this.getSelObject(sKey);
	var sRet=null;
	if (oCell!=null){
		sRet=oCell.innerText
	}
	return sRet;
}

/*ottiene una cella in base a indice riga e key*/
function /*method*/ wzMsDatagrid_getItemCell(index, sKey){
	var  iCol,ix, oRet ;
	var curRow=this.getItem(index);
	if (sKey=="" || curRow==null) return null;
	ix=-1;
	for (iCol=0;iCol<this.keys.length ;iCol++ ){
		if (this.keys[iCol]==sKey){
			ix=iCol;
			break;
		}
	}
	if (ix>=0){
		oRet=curRow.cells[ix];
	}
	return oRet;
}

/*ottiene il testo contenuto in una cella in base al key */
function /*method*/ wzMsDatagrid_getCellText(index,sKey){
	var oCell=this.getItemCell(index,sKey);
	var sRet=null;
	if (oCell!=null){
		sRet=oCell.innerText
	}
	return sRet;
}

function /*method*/ wzMsDatagrid_update(){
	var bRet=false;
	var oHidden=document.getElementById(this.ClientId+"_selection");
	if (oHidden!=null){
		oHidden.value=this.SelectedIndex;
	}
	return bRet;
}

/*chiamato ad ogni click della riga quando la selezione è lato client
  chiama un eventuale evento client	*/
function /*generic function*/ wmdgs(oRow, rowIndex,ev){
	var oTable=getParentObj(oRow, "table",3)
	var bRet=false;
	var bOut=true;
	if (oTable!=null){		
		var oGrid=wzMsDatagrid[oTable.id];
		if (oGrid!=null){
			if (oGrid.SelectedIndex!=rowIndex){
				/*verifica se c'è un handler da chiamare*/
				if (oGrid.ClientSelectionHandler!=null){
					var eve=new wzMsDatagrid_event(oGrid,ev)
					eve.newIndex=rowIndex;
					var sEv=oGrid.ClientSelectionHandler+"(eve)";
					bOut=eval(sEv);
				}
				if (bOut){
					switch (oGrid.SelectionOption){
						case 2:/*rowClick*/
							var sArgs="Select,"+rowIndex;
							__doPostBack(oGrid.UniqueID,sArgs);
							bRet=true;
							break;
						case 3:/*clientOnly*/
							bRet=oGrid.setSelectedIndex(rowIndex);
							break;
					}
				}
			}
		}
	}
	return bRet;
}

/* scatta al doppio click sulla riga */
function  /*generic function*/ wmddbc(oRow, rowIndex,ev){
	var oTable=getParentObj(oRow, "table",3)
	var bRet=false;
	var bOut=true;
	if (oTable!=null){		
		var oGrid=wzMsDatagrid[oTable.id];
		if (oGrid!=null){
			/*verifica se c'è un handler da chiamare*/
			if (oGrid.ClientDoubleClickHandler!=null){
				var eve=new wzMsDatagrid_event(oGrid,ev);
				var sEv=oGrid.ClientDoubleClickHandler+"(eve)";
				bOut=eval(sEv);
			}
			if (bOut){
				var sArgs="DoubleClick,"+rowIndex;
				__doPostBack(oGrid.UniqueID,sArgs);
				bRet=true;
			}
		}
	}
	return bRet;
}

/*cerca il primo padre con con il nome del tag specificato, deep è 
la profondità massima di ricerca*/
function /*generic function*/ getParentObj(oChild, sTag,deep){
	if (deep<=0) return null;
	if (oChild.parentNode!=null){
		if(oChild.parentNode.tagName.toLowerCase()==sTag.toLowerCase()){
			return oChild.parentNode;
		} else {
			return getParentObj(oChild.parentNode,sTag,deep-1)
		}
	} else {
		return null; 
	}
}

/* funzione da utilizzare per recuperare l'oggetto datagrid */
function /*generic function*/ get_wzMsDatagrid(sClientId){
	return wzMsDatagrid[sClientId];
}

/* funzione da utilizzare per recuperare l'oggetto datagrid */
function /*generic function*/ get_OnitGrid(sClientId){
	return wzMsDatagrid[sClientId];
}


function /*private*/ wzMsDatagrid_splitKeys(sKeys){
	if (sKeys!=null){
		return sKeys.split("|");
	} else {
	return null;
	}
}

/* frame management */

/* registra il valore attuale dello scroll
   nell'hidden dedicato */	
function wzMsDatagrid_regScroll(oFrame,sHidName){
	var oHid=document.getElementById(sHidName);
	if (oHid!=null && oFrame!=null){
		oHid.value=oFrame.scrollLeft+","+oFrame.scrollTop;
	}
}


function wzMsDatagrid_initScroll(sFrameName,sHidName){
	var oHid=document.getElementById(sHidName);
	var oFrame=document.getElementById(sFrameName);

	if (oHid!=null && oFrame!=null){
		var sSpl=oHid.value.split(",");
		if (sSpl.length>1){
			if ( !isNaN(sSpl[0]) && !isNaN(sSpl[1]) ){
				oFrame.scrollLeft=parseInt(sSpl[0],10);
				oFrame.scrollTop=parseInt(sSpl[1],10);				
			}
		}
	}
}	


/* permette di selezionare o deselezionare i check su una colonna */
function OnitGrid_SelDesellAll(oChk){
	var tdNode=getParentObj(oChk, "TD",4);
	var trNode, fnd;
	fnd=-1;
	if (tdNode!=null){
		trNode=tdNode.parentNode;
		for (i=0; i< trNode.cells.length && fnd<0; i++){
			if (tdNode==trNode.cells[i]) fnd=i;
		}
	}
	if (fnd>=0){
		wzMsDatagrid_SelDesellAll(oChk, fnd);
	}
}
	
	
function wzMsDatagrid_SelDesellAll(oChk, colIx){
 var oTable, i, oCell, oChks;
	for (oTable=oChk;(oTable.tagName.toUpperCase())!='TABLE';oTable=oTable.parentNode);
	
  	for (i=1; i<oTable.rows.length; i++){
   		oCell=oTable.rows[i].cells[colIx];
   		oChks=oCell.getElementsByTagName("INPUT");
   		if (oChks.length>0){
   			if (!oChks[0].disabled)
   		 		oChks[0].checked=oChk.checked;
   		}
  	}
}

