// Array of all DataGrids data object on the page
var DataGridEx_DataObjects = new Array();

/////////////////////////////////////////////////////////////
// Returns data grid data object
function DataGridEx_GetDataObject(Item)
{
//	return Item.parentElement.parentElement.attributes["DataObject"].nodeValue;	K VYMAZANI POKUD SE NASLEDUJICI RADKA OSVEDCI
	return Item.parentNode.parentNode.DataObject;
}
		
/////////////////////////////////////////////////////////////
//
function DataGridEx_AddItemToSel(Item)
{
	// Remember new selected item
	this.SelectedItems.push(Item);
	
	// Higlight selected item
	Item.OrigClassName = Item.className;
	Item.className = "GridSelectedItem";
	
	// Call user's function
	if(this.OnRowSelectJScriptFunction != null)
		this.OnRowSelectJScriptFunction(Item);
}

/////////////////////////////////////////////////////////////
//
function DataGridEx_RemoveAllSel()
{
	for(var i = 0; i < this.SelectedItems.length; i++)
	{
		this.SelectedItems[i].className = this.SelectedItems[i].OrigClassName;	
		
		// Call user's function
		if(this.OnRowUnselectJScriptFunction != null)
			this.OnRowUnselectJScriptFunction(this.SelectedItems[i]);
	}	
	
	this.SelectedItems = new Array();
}

////////////////////////////////////////////////////////////////
//
function DataGridEx_RemoveItemFromSel(Item)
{	
	// Remove item from selected items array
	var a = new Array();
	var found = false;
	
	for(var i = 0; i < this.SelectedItems.length; i++)
	{
		if(this.SelectedItems[i] != Item)
			a.push(this.SelectedItems[i]);			
		else
		{
			found = true;
			
			// Back to original class
			Item.className = Item.OrigClassName;	
		}
	}
	
	this.SelectedItems = a;
	
	// Call user's function
	if(found && this.OnRowUnselectJScriptFunction != null)
		this.OnRowUnselectJScriptFunction(Item);
	
	return found;
}

/////////////////////////////////////////////////////////////////
//
function DataGridEx_OnSubmit()
{
	// Prepare string with IDs
	var s = "";
	
	for(var i = 0; i < this.SelectedItems.length; i++)
	{
		if(i != 0)
			s += ", ";
			
		s += this.SelectedItems[i].attributes.getNamedItem("DataID").nodeValue.toString();
	}

	// Send string to server	
	if(s!="")
		SendStringToServer(s, this.SelItemsControlName);	
}

/////////////////////////////////////////////////////////////////
//
function DataGridEx_Preselect()
{
	var col = this.Grid;
	var selectedID = this.SelectedItems;
	this.SelectedItems = new Array();
	
	for(var i = 0; i < col.rows.length; i++)
	{
		try
		{
			var ID = parseInt(col.rows[i].attributes.getNamedItem("DataID").nodeValue);
			
			for(var ii = 0; ii < selectedID.length; ii++)
			{
				if(selectedID[ii] == ID)
				{
					this.AddItemToSel(col.rows[i]);
					break;
				}
			}
		}
		catch(e)
		{
		}
	}
}

/////////////////////////////////////////////////////////////////
//
function DataGridEx_OnItemClick(Item)
{
	var dataObject = DataGridEx_GetDataObject(Item);
	
	if(dataObject.MultiSelection)
	{
		if(event.ctrlKey)
		{
			// Add item to selection or remove item from selection
			if(!dataObject.RemoveItemFromSel(Item))
				dataObject.AddItemToSel(Item);
		}
		else
{
			// Destroy current selection
			dataObject.RemoveAllSel();
			
			// Select new item
			dataObject.AddItemToSel(Item);
		}
	}
	else	
	{
		// Destroy current selection
		dataObject.RemoveAllSel();
		
		// Select new item
		dataObject.AddItemToSel(Item);
	}
}

/////////////////////////////////////////////////////////////////
// Funkce volaná tlačítkem posunu na jinou stránku
function DataGridEx_MovePage(command)
{
	SendStringToServer(command, this.Grid.id + "MovePage");
	document.forms[0].submit();
}

/////////////////////////////////////////////////////////////////
// Funkce volaná tlačítkem změni třídícího kriteria
function DataGridEx_SortColumn(column)
{
	SendStringToServer(column, this.Grid.id + "SortColumn");
	document.forms[0].submit();
}

/////////////////////////////////////////////////////////////////
// Funkce volaná tlačítkem pro změnu počtu řádek v gridu
function DataGridEx_ChangeRowCount(command)
{
	SendStringToServer(command, this.Grid.id + "ChangeRowCount");
	document.forms[0].submit();
}

/////////////////////////////////////////////////////////////////
//
function DataGridEx_OnSubmitHandler()
{
	for(var i = 0; i < DataGridEx_DataObjects.length; i++)
	{
		DataGridEx_DataObjects[i].OnSubmit();
	}
}


////////////////////////////////////////////////////////////////
// DataGridEx data object contructor
function DataGridExData(
	Grid, 
	MultiSelection, 
	SelItemsControlName, 
	OnRowSelectJScriptFunctionName,
	OnRowUnselectJScriptFunctionName)
{
	// Set properties
	this.Grid					= Grid;
	this.MultiSelection			= MultiSelection;
	this.SelectedItems			= new Array();	
	this.SelItemsControlName	= SelItemsControlName;
	
	// Set user's client functions
	this.OnRowSelectJScriptFunction   = OnRowSelectJScriptFunctionName;
	this.OnRowUnselectJScriptFunction = OnRowUnselectJScriptFunctionName;
	
	// Attach submit event (IE x Mozilla)
	if(document.forms[0].attachEvent)	
		document.forms[0].attachEvent('onsubmit', DataGridEx_OnSubmitHandler);
	else
		document.forms[0].addEventListener('submit', DataGridEx_OnSubmitHandler, false);
	
	// Add this instance to instances array
	DataGridEx_DataObjects.push(this);	
}

							
DataGridExData.prototype.AddItemToSel		= DataGridEx_AddItemToSel;
DataGridExData.prototype.RemoveAllSel		= DataGridEx_RemoveAllSel;
DataGridExData.prototype.RemoveItemFromSel	= DataGridEx_RemoveItemFromSel;	
DataGridExData.prototype.OnSubmit			= DataGridEx_OnSubmit;
DataGridExData.prototype.Preselect			= DataGridEx_Preselect;
DataGridExData.prototype.MovePage			= DataGridEx_MovePage;
DataGridExData.prototype.SortColumn			= DataGridEx_SortColumn;
DataGridExData.prototype.ChangeRowCount		= DataGridEx_ChangeRowCount;
