
	// DataGrid class object
	function clsDataGrid( oTable, aRowIndetifier, sClassNormal, sClassOver, sDisplayClassName, sDisplayTagName, sClassSelected )
	{
		this.dataGridTable = oTable;
		if( this.dataGridTable )
		{
		this.sClassOver = ( sClassOver ? sClassOver : '' );
		this.sClassNormal = ( sClassNormal ? sClassNormal : 'gridRow' );
		this.sClassSelected = ( sClassSelected ? sClassSelected : false );

		if( !sDisplayTagName )
		{
			sDisplayTagName = 'div';
		}

		this.aRowIndetifier = aRowIndetifier;

		this.oCheckAllBox = null;
		this.aGridRow = new Array( );
		this.aCheckBox = new Array( );
		this.iRowCount = 0;
		this.bWithCheckBoxes = false;
		this.oGlobalCheckBox = null;

		var
			arObj = this.dataGridTable.getElementsByTagName( sDisplayTagName );

		for( var i = 0; i < arObj.length; i ++ )
		{
			if( arObj[ i ].className == sDisplayClassName )
			{
				this.oDisplay = arObj[ i ];
				break;
			}
		}

		this.iSelectedItems = 0;

		if( this.oDisplay )
		{
			this.iSelectedItems = Math.round( this.oDisplay.innerHTML );
		}
	}
	}

	clsDataGrid.prototype.Init = function()
	{
		if( !this.dataGridTable )
		{
			return false;
		}

		var
			aGridTableRow = this.dataGridTable.getElementsByTagName( 'tr' ),
			i;

		if( !aGridTableRow.length )
		{
			return false;
		}

		for( i = 0; i < aGridTableRow.length; i ++ )
		{
			if( inArray( this.aRowIndetifier, aGridTableRow[ i ].className ) )
			{
				this.oGlobalCheckBox = aGridTableRow[ i ].getElementsByTagName( 'input' )[ 0 ];
				break;
			}
		}

		if( this.oGlobalCheckBox )
		{
			// setting handler for global checkbox
			//this.oGlobalCheckBox.attachEvent( 'onclick', makeDelegate( this, this.ChangeGlobalState ) );
			this.addGridEvent(this.oGlobalCheckBox,'click', this.ChangeGlobalState);
			this.bWithCheckBoxes = true;
		}

		for( ++i ; i < aGridTableRow.length; i ++ )
		{
			if( inArray( this.aRowIndetifier, aGridTableRow[ i ].className ) )
			{
				if( this.bWithCheckBoxes )
				{
					this.aCheckBox[ this.iRowCount ] = aGridTableRow[ i ].getElementsByTagName( 'input' )[ 0 ];

					// setting handler for the checkbox
					if( this.aCheckBox[ this.iRowCount ] && this.bWithCheckBoxes )
					{
						// this.aCheckBox[ this.iRowCount ].attachEvent( 'onclick', makeDelegate( this, this.CheckBoxStateChanged ) );
						this.addGridEvent(this.aCheckBox[this.iRowCount], 'click', this.CheckBoxStateChanged);
					}
				}

				this.aGridRow[ this.iRowCount ] = aGridTableRow[ i ];

				// setting handler for row
				//this.aGridRow[ this.iRowCount ].attachEvent( 'onmouseover', makeDelegate( this, this.OnMouseOver ) );
				this.addGridEvent(this.aGridRow[this.iRowCount], 'mouseover', this.OnMouseOver);
				//this.aGridRow[ this.iRowCount ].attachEvent( 'onmouseout', makeDelegate( this, this.OnMouseOut ) );
				this.addGridEvent(this.aGridRow[this.iRowCount], 'mouseout', this.OnMouseOut);
				
				this.iRowCount ++;
			}
		}
	}

var globalFlag = false;

	clsDataGrid.prototype.OnDragDrop = function( )
	{
		return false;
	}
	
	// Row mouse over behaviour
	clsDataGrid.prototype.OnMouseOver = function(ev)
	{
		//var oSrc = findParent( window.event.srcElement, 'tr' );
		var oSrc = findParent( getEventElement(ev), 'tr' );
		var grid = oSrc.Grid;

		if( oSrc && grid && ( !grid.sClassSelected || oSrc.className != grid.sClassSelected ) )
		{
			if(oSrc.className == "gridRow" || oSrc.className == grid.sClassNormal)
			{
				oSrc.className = grid.sClassOver;
			}
		}

		//window.event.cancelBubble = true;
		cancelEvent(ev);
	}

	// Row mouse out behaviour
	clsDataGrid.prototype.OnMouseOut = function(ev)
	{
		//var oSrc = findParent( window.event.srcElement, 'tr' );
		var oSrc = findParent( getEventElement(ev), 'tr' );
		var grid = oSrc.Grid;

		if( oSrc && grid && ( !grid.sClassSelected || oSrc.className != grid.sClassSelected ) )
		{
			if(oSrc.className == grid.sClassOver)
			{
			oSrc.className = grid.sClassNormal;
			}
		}

		//window.event.cancelBubble = true;
		cancelEvent(ev);
	}

	// Changing global state
	clsDataGrid.prototype.ChangeGlobalState = function(ev)
	{
		var
			//oSrc = window.event.srcElement,
			oSrc = getEventElement(ev),
			grid = oSrc.Grid,
			bChecked = oSrc.checked;

		for( var i = 0; i < grid.iRowCount; i ++ )
		{
			if( grid.aCheckBox[ i ] && !grid.aCheckBox[ i ].disabled )
			{
				if( bChecked && !grid.aCheckBox[ i ].checked )
				{
					grid.iSelectedItems ++;
				}
				else if( !bChecked && grid.aCheckBox[ i ].checked )
				{
					grid.iSelectedItems --;
				}

				grid.aCheckBox[ i ].checked = bChecked;

				if( grid.sClassSelected )
				{
					grid.aGridRow[ i ].className = ( bChecked ? grid.sClassSelected : grid.sClassNormal );
				}
			}
		}

		grid.DisplaySelectedItemsCount( );
	}

	// Changing state of single checkbox
	clsDataGrid.prototype.CheckBoxStateChanged = function(ev)
	{
		var
			//oSrc = window.event.srcElement,
			oSrc = getEventElement(ev),
			grid = oSrc.Grid,
			bChecked = false,
			i = -1;

		i = grid.GetIdByCheckBox( oSrc );

		if( i == -1 )
		{
			return false;
		}

		bChecked = grid.aCheckBox[ i ].checked = oSrc.checked;

		if( bChecked )
		{
			grid.iSelectedItems ++;
		}
		else
		{
			grid.iSelectedItems --;
		}

		grid.DisplaySelectedItemsCount( );

		if( grid.oGlobalCheckBox.checked && !bChecked )
		{
			grid.oGlobalCheckBox.checked = false;
		}

		if( grid.sClassSelected )
		{
			grid.aGridRow[ i ].className = ( bChecked ? grid.sClassSelected : grid.sClassNormal );
		}

		//window.event.cancelBubble = true;
		cancelEvent(ev);
	}

	clsDataGrid.prototype.DisplaySelectedItemsCount = function( )
	{
		if( this.oDisplay )
		{
			this.oDisplay.innerHTML = this.iSelectedItems;
		}
	}

	// getting row id by checkbox
	clsDataGrid.prototype.GetIdByCheckBox = function( oCheckBox )
	{
		for( var i = 0; i < this.aCheckBox.length; i ++ )
		{
			if( oCheckBox == this.aCheckBox[ i ] )
			{
				return i;
			}
		}

		return -1;
	}

	clsDataGrid.prototype.addGridEvent = function (el, evname, func) 
	{
		if (typeof el.Grid == "undefined") el.Grid = this;
		addEvent(el,evname, func);
	}
