﻿function initDataGrid(elm)
{
	if (window._dataGridFunctions == null)	
	{
		window._dataGridFunctions = 
		{
		_findCellCtlById: function(tdCell, elementId, recursive)
		{	
			var children = tdCell.childNodes; var count = children.length;			
			for (var i = 0; i <= count; i++)		
			{
				var child = children[i];
				if (child && child.getAttribute)
				{
					if (child.id.endsWith(elementId))
						return child;
					else if (recursive)	
					{	
						child = this._findCellCtlById(child, elementId, recursive);
						if (child)
							return child;
					}	
				}		
			}
			return null;
		},
		//
		_findCellCtlByType: function(tdCell, elementType)
		{	
			var children = tdCell.childNodes; var count = children.length;			
			for (var i = 0; i <= count; i++)		
			{
				var child = children[i];
				if (child && child.getAttribute && (child.getAttribute("elementType") == elementType || child.getAttribute("type") == elementType))
					return child;
			}
			return null;
		},
		//
		_onCheckBoxChange: function()
		{	
			try
			{		
				if (this._rowChkBoxes != null)
				{
					var chks = this._rowChkBoxes; var count = chks.length;					
					for (var i = 0; i < count; i++)
						if (!chks[i].disabled)
							chks[i].checked = this.checked;
				}
				else if (this._hdrCheckBox != null)
				{
					var checked = true;
					var chks = this._hdrCheckBox._rowChkBoxes; var count = chks.length;
					for (var i = 0; i < count; i++)
						if (!chks[i].checked)
							{checked = false; break;}
					this._hdrCheckBox.checked = checked;
				}
			}
			catch (e) {displayErr("DataGrid_onCheckBoxChange", e);}
		},
		//
		getRowCount: function() {return this.rows.length - (this._trHeader && !this._isScrollable ? 1 : 0) - (this._trFooter ? 1 : 0);},
		//
		getCellValue: function(row, col)
		{
			var td = this.rows[row + (this._trHeader && !this._isScrollable ? 1 : 0)].cells[col];
			return td.innerHTML;
		},
		//
		getCellControlByType: function(row, col, elementType)
		{
			var td = this.rows[row + (this._trHeader && !this._isScrollable ? 1 : 0)].cells[col];		
			if (!td)
				return null;
			var ctl = this._findCellCtlByType(td, elementType);
			if (ctl)
				{ctl.parentDataGrid = this; ctl.parentRowIndex = row;}	
			return ctl;
		},
		//
		getColControlsByType: function(col, elementType, includeNulls)
		{	
			var ctls = new Array(); var index = -1; var count = this.getRowCount();
			for (var i = 0; i < count; i++)		
			{		
				var	ctl = this.getCellControlByType(i, col, elementType);			
				if (ctl != null || includeNulls)
					{index++; ctls[index] = ctl;}		
			}
			return ctls;
		},
		//
		getCellControlById: function(row, col, elementId, recursive)
		{
			var td = this.rows[row + (this._trHeader && !this._isScrollable ? 1 : 0)].cells[col];
			if (!td) return null;
			var ctl = this._findCellCtlById(td, elementId, recursive);
			if (ctl)
				{ctl.parentDataGrid = this; ctl.parentRowIndex = row;}	
			return ctl;
		},
		//
		getColControlsById: function(col, elementId, includeNulls, recursive)
		{	
			var ctls = new Array(); var index = -1; var count = this.getRowCount();
			for (var i = 0; i < count; i++)		
			{		
				var	ctl = this.getCellControlById(i, col, elementId, recursive);			
				if (ctl != null || includeNulls)
					{index++; ctls[index] = ctl;}		
			}
			return ctls;
		},
		//
		getFooterControlByType: function(col, elementType)
		{
			if (!this._trFooter)
				return null;
				
			var td = this._trFooter.cells[col];		
			var ctl = this._findCellCtlByType(td, elementType);
			if (ctl)
				{ctl.parentDataGrid = this; ctl.parentRowIndex = this._trFooter;}	
			return ctl;
		},
		//
		getCellHiddenField: function(row, col)	{return this.getCellControlByType(row, col, "hidden");},
		//
		getCellCheckBox: function(row, col)	{return this.getCellControlByType(row, col, "CheckBox");},
		getColCheckBoxes: function(col, checkedOnly, includeNulls)		
		{
			var chks = null;
			if (this._trHeader != null)
			{
				var td = this._trHeader.cells[col];
				if (td._hdrCheckBox != null)
					var chks = td._hdrCheckBox._rowChkBoxes;
			}
			if (chks == null)
				chks = this.getColControlsByType(col, "CheckBox", includeNulls);
			//	
			if (checkedOnly)
			{
				var chksChecked = new Array(); var index = -1; var count = chks.length;
				for (var i = 0; i < count; i++)
					if (chks[i].checked)
						{index++; chksChecked[index] = chks[i];}
						
				chks = chksChecked;
			}
			return chks;
		},
		//
		getCellOptionBox: function(row, col)				{return this.getCellControlByType(row, col, "OptionBox");},
		getColOptionBoxes: function(col, includeNulls)	{return this.getColControlsByType(col, "OptionBox", includeNulls);},
		getColSelOptionBox: function(col)		
		{
			var opts = this.getColOptionBoxes(col);
			for (var i = 0; i < opts.length; i++)
				if (opts[i].checked)
					return opts[i];
			return null;
		},
		//
		getCellNumberBox: function(row, col)				{return this.getCellControlByType(row, col, "NumberBox");},
		getColNumberBoxes: function(col, includeNulls)	{return this.getColControlsByType(col, "NumberBox", includeNulls);},
		//
		getCellTextBox: function(row, col)				{return this.getCellControlByType(row, col, "TextBox");},
		getColTextBoxes: function(col, includeNulls)		{return this.getColControlsByType(col, "TextBox", includeNulls);},
		//
		getCellHyperLink: function(row, col)				{return initHyperLink(this.getCellControlByType(row, col, "HyperLink"));},
		getColHyperLinks: function(col, includeNulls)		
		{
			var lnks = this.getColControlsByType(col, "HyperLink", includeNulls);
			for (var i = 0; i < lnks.length; i++)
				lnks[i] = initHyperLink(lnks[i]);
			return lnks;
		},
		//
		getCellLabel: function(row, col)				{return initLabel(this.getCellControlByType(row, col, "Label"));},
		getColLabels: function(col, includeNulls)		
		{
			var lbls = this.getColControlsByType(col, "Label", includeNulls);
			for (var i = 0; i < lbls.length; i++)
				lbls[i] = initLabel(lbls[i]);
			return lbls;
		},
		//
		getFooterText: function (col)						{return this._trFooter != null ? this._trFooter.cells[col].innerHTML : null;},
		setFooterText: function (col, text)
		{
			if (this._trFooter != null)
				this._trFooter.cells[col].innerHTML = text;
		},
		//
		getFooterOptionBox: function(col)				{return this.getFooterControlByType(col, "OptionBox");}
		};
	}
	//	
	elm = extendElement(elm)
	elm = Object.extend(elm, window._dataGridFunctions);
	//
	elm._isScrollable = castBool(elm.getAttribute("isScrollable"));	
	if (elm._isScrollable)
	{	
		elm._tblHeaderTable = document.getElementById(elm.id + "_HeaderTable");	
		if (elm._tblHeaderTable)
		{
			elm._tblHeaderTable.style.width = elm.offsetWidth;		
			elm._tblHeaderTable.parentNode.style.visibility = "visible";
		}
		//
		elm._divScrollArea = document.getElementById(elm.id + "_ScrollArea");		
		elm._divScrollArea._fldPos = document.getElementById(elm.id + "_ScrollPos");	
		elm._divScrollArea.scrollTop = elm._divScrollArea._fldPos.value;
		elm._divScrollArea.onscroll = function() {this._fldPos.value = this.scrollTop;}		
		elm._divScrollArea.style.visibility = "visible"
	}				
	//	
	elm._trHeader = castBool(elm.getAttribute("hasHeader")) ? (elm._tblHeaderTable == null ? elm.rows[0] : elm._tblHeaderTable.rows[0]) : null;
	elm._trFooter = castBool(elm.getAttribute("hasFooter")) ? elm.rows[elm.rows.length - 1] : null;
	//
	if (elm._trHeader != null)
	{
		var cellCount = elm._trHeader.cells.length;
		var rowCount = elm.getRowCount();
		var rowFirst = elm._isScrollable ? 0 : 1;
		var rowLast = elm._isScrollable ? rowCount - 1 : rowCount;
		//
		for (var i = 0; i < cellCount; i++)
		{
			// Setup checkboxes
			var td = elm._trHeader.cells[i];		
			var hdrCheckBox = elm._findCellCtlByType(td, "CheckBox"); td._hdrCheckBox = hdrCheckBox;					
			//
			if (hdrCheckBox != null)
			{			
				hdrCheckBox.onclick = elm._onCheckBoxChange;
				var rowChkBoxes = new Array(); var index = -1;
				//
				var allItemsChecked = true;
				//				
				for (var j = rowFirst; j <= rowLast; j++)
				{
					var rowCheckBox = elm._findCellCtlByType(elm.rows[j].cells[i], "CheckBox");										
					if (rowCheckBox != null)
					{	
						rowCheckBox.parentDataGrid = elm; rowCheckBox.parentRowIndex = j;					
						//
						rowCheckBox._hdrCheckBox = hdrCheckBox;																
						rowCheckBox.onclick = elm._onCheckBoxChange;
						//
						index++; rowChkBoxes[index] = rowCheckBox;
						//
						if (!rowCheckBox.checked)
							allItemsChecked = false;							
					}	
				}		
				//
				if (rowChkBoxes.length > 0 && allItemsChecked)
					hdrCheckBox.checked = true;
				//	
				hdrCheckBox._rowChkBoxes = rowChkBoxes; 	
			}
			
			// Hide sort buttons (if empty data)			
			if (rowCount == 0)
			{
				var btns = td.getElementsByTagName("INPUT"); var count = btns.length;
				for (var j = 0; j < count; j++)				
					if (btns[j].getAttribute("elementType") == "ImageButton")
						extendElement(btns[j]).setDisplay(false);
			}			
		}
	}	
	
	// Adjust footer (if empty data)	
	if (castBool(elm.getAttribute("showEmptyData")))
	{
		var td = elm._trFooter.cells[0];
		td.colSpan = elm._trFooter.cells.length;
		td.className = "GridTdRight";
		//
		while (elm._trFooter.cells.length > 1)
			elm._trFooter.deleteCell(1);
	}
	//
	return elm;
}
