			var selected = [];
			var prevClass = [];
			var selectedRowIndex = [];
			var autoselect= [];
			var defaultDatagridActions = [];
			var datagridClientIDs = [];
			
			/***************************************************
			To Add an event handler for the Enter key on a datagrid,
			create a function in the page with the signature
			
			DGDefaultAction(datagridIndex)
			***************************************************/
			function FindFirstItemRow(control, datagridIndex)
			{
				//search for control with attribute DGIndex
				try
				{
					var dg = null;
					while (!dg)
					{
						if (control.DGIndex) //it is a datagrid if it has this property
							dg = control;
						else
							control = control.parentElement;
					}
					//Now find the first child
					var firstItemControl = null;
					control = dg.firstChild.firstChild; //the first child is the TBODY
					while (!firstItemControl && control)
					{
						if (control.DGItemIndex) //It is an item row
							firstItemControl = control;
						else
							control = control.nextSibling;
					}
				}
				catch(ex)
				{
				
				}
		
				return firstItemControl;
			}
			
			function DGProcessKeyDown(control, datagridIndex)
			{
					//alert(event.keyCode);

					if (event.keyCode != 37 && event.keyCode != 38 && 
						event.keyCode != 39 && event.keyCode != 40 &&
						event.keyCode != 13
						)
					{
						return;
					}
					var bCancelEvent = false;
					var curRow = selected[datagridIndex];
					switch (event.keyCode)
					{
						case 38: //up
							if (!curRow) //No row is selected. Select the first row.
							{
								try{FindFirstItemRow(control).firstChild.click();}
								catch (ex){}
							}
							else
							{
								if (curRow.previousSibling)
								{
									//click on the first child. clicking on the element
									//will select the row but now change the active row.
									curRow.previousSibling.firstChild.click(); 
								}
							}
							bCancelEvent = true;
							break;
						case 40: //down
							if (!curRow) //No row is selected. Select the first row.
							{
								try{FindFirstItemRow(control).firstChild.click();}
								catch (ex){}
							}
							else
							{
								if (curRow.nextSibling)
								{
									curRow.nextSibling.firstChild.click();
								}
							}
							bCancelEvent = true;
							break;
						case 13: //enter
							if (typeof DGDefaultAction == 'function')
							{
								DGDefaultAction(datagridIndex);
							}
							bCancelEvent = true;
							break;
					}
					
					if (bCancelEvent)
					{
						try {
							event.keyCode = 0;
							event.returnValue = false;
							event.cancelBubble = true;
							return false;
						}
						catch (e) {
							//
						}
					}
			}
			
			function DoubleclickRow(row, rowIndex, datagridIndex)
			{
				//If the row isn't selected, then select it
				if (row.className != "selectedRow" && row.className != "selectedRowInactive")
				{
					SelectRow(row, rowIndex, datagridIndex)
				}
				
				//If a default action exist, then call it.
				if (typeof DGDefaultAction == 'function')
				{
					DGDefaultAction(datagridIndex);
				}
			}
			
			function AutoselectDatagridRows()
			{
				for (var i=0;i<=autoselect.length - 1;i++)
				{
					if (autoselect[i])
					{
						autoselect[i].click();
						autoselect[i] = null;  //To make sure that it only fires once
					}
				}
			}
			
			function SelectRow(row, rowIndex, datagridIndex)
			{
			
				if (row.className == "selectedRow" || row.className == "selectedRowInactive")
				{
					//Unselect
					//selectedRowIndex[datagridIndex] = -1;
					//selected[datagridIndex] = null;
					//row.className = prevClass[datagridIndex];
				}
				else
				{
					if (selected[datagridIndex] != null)
					{
						selected[datagridIndex].className = prevClass[datagridIndex];
					}
					prevClass[datagridIndex] = row.className;
					selectedRowIndex[datagridIndex] = rowIndex;
					row.className = "selectedRow";
					selected[datagridIndex] = row;
				}
				
				updateForm();
			}

			function CheckSelectedIndex(datagridIndex,allowClickWithoutSelection,allowClickWhenOnlyOneItem)
			{
				var bOK = false;
				
				if (selected[datagridIndex] == null)
				{
					var totalResults;
					var datagridName;
					var datagrid;
					
					dataGridName = datagridClientIDs[datagridIndex];
					datagrid = document.getElementById(dataGridName);
					try
					{
						totalResults = datagrid.DGTotalResults;
					}
					catch (ex)
					{
						totalResults = 0;
					}
					
					if ((allowClickWithoutSelection && totalResults > 0) || (allowClickWhenOnlyOneItem && totalResults == 1))
					{
						var firstRow = FindFirstItemRow(datagrid,datagridIndex);
						if (firstRow)
						{
							firstRow.click();
							bOK = true;
						}
						else
						{
							bOK = false;
						}	
					}
					else
					{
						bOK = false;
					}
				}
				else
				{
					bOK = true;
				}
				
				if (!bOK)
				{
					alert("Please select an item.");
				}
				return bOK;
			}
			
			function updateForm()
			{
				//make sure the field exists
				var fld;
				fld = document.getElementById("datagridSelectedIndexes");
				if (fld == null)
				{
					var input = document.createElement('input'); 
					input.type = 'hidden'; 
					input.id = 'datagridSelectedIndexes'; 
					input.name = 'datagridSelectedIndexes'; 
					document.forms[0].appendChild(input); 
				}
				
				var vals = '';
				for (var i=0;i<=selectedRowIndex.length - 1;i++)
				{
					vals += selectedRowIndex[i];
					if(i<selectedRowIndex.length - 1)
						vals = vals + '|'
				}
				
				fld = document.getElementById("datagridSelectedIndexes");
				fld.value = vals;
				
			}

			function DGBlur(datagrid, datagridIndex)
			{
				if (selected[datagridIndex] != null)
				{
					var row = selected[datagridIndex];
					row.className = "selectedRowInactive";
				}
			}

			function DGFocus(datagrid, datagridIndex)
			{
				if (selected[datagridIndex] != null)
				{
					var row = selected[datagridIndex];
					row.className = "selectedRow";
				}
			}

            function DGGiveFocus(datagridIndex)
            {
					var datagridName;
					var datagrid;
					
					dataGridName = datagridClientIDs[datagridIndex];
					datagrid = document.getElementById(dataGridName);
					datagrid.focus();
            }
            
            function DGWindowKeyDown()
            {
			    var bCancel = false;
    			
			    if (event.keyCode == 71 && window.event.ctrlKey) //G
			    {
				    DGGiveFocus(0);
				    bCancel = true;
			    }
    			
			    if (bCancel)
			    {
				    try {
					    event.keyCode = 0;
					    event.returnValue = false;
					    event.cancelBubble = true;
					    return false;
				    }
				    catch (e){}
			    }
            
            }
            document.attachEvent("onkeydown",DGWindowKeyDown);