
	
	var _bInHeaderClick = false;
	var _Page_ClientValidate = null;
	var _UniqueColumnFilterList = null;


	function initializeGridValidation()
	{
		if (document.readyState == 'complete' && typeof(Page_ClientValidate) == 'function')
		{
			//redirect all calls to Page_ClientValidate to beforePage_ClientValidate
			//so that the user will be prompted to save changes
			_Page_ClientValidate = Page_ClientValidate; 
			Page_ClientValidate = validateChangedRowsBeforeSumbit;
		}
	}	
	
	document.attachEvent('onreadystatechange', initializeGridValidation);
	
		
	if (typeof initializeGridLayout != 'function')
	{		
		//if function initializeGridLayout does not already exist then add one to the page
		var script = document.createElement("script");
		script.language='javascript'
		script.type = 'text/javascript';
		
		//initializeGridLayout is called by the basegrid so each page needs it
		script.text = 'function initializeGridLayout(){}';
		
		var heads = document.getElementsByTagName('head');
		if (heads[0]) {
			heads[0].appendChild(script);
		}
	}
	
	
	function initializeGrid(gridID)
	{
	
		try
		{	
			var grid = igtbl_getGridById(gridID);
			//if the grid is readonly then disable the deleteall checkbox
			if (grid.AllowUpdate == 2) //2=no
			{
				var col = grid.Bands[0].getColumnFromKey('Delete');
				//Not every page has a delete button.
				if (col != null)
				{
					var oChk = igtbl_getElementById(col.Id).getElementsByTagName('INPUT')[0];
					if(oChk != undefined)
					{
						oChk.style.visibility = 'hidden';
						col.setHidden(true); //Clark added this to hide the Delete Column
					}
				}
			}
		
			//call the initializeGridLayout function that should be in each page
			initializeGridLayout(gridID);
		}
		catch(e)
		{
			showAlertBox('Errors were encountered in initializeGridLayout:<br/>'+e.message);
		}
	
	}
		
	function headerClick(gridID, columnID, button)
	{
		_bInHeaderClick = true;
		try
		{
			if (igtbl_getColumnById(columnID).Key == 'Delete')
			{
				var grid = igtbl_getGridById(gridID);
				var oChk = igtbl_getElementById(igtbl_getColumnById(columnID).Id).getElementsByTagName('INPUT')[0];
				var oCell = null;
				var checked = !oChk.checked
				//var bandID = igtbl_getColumnById(columnID).Band.Index;
				
				//get out if the checkbox is hidden
				if(oChk.style.visibility == 'hidden')
				{
					oChk.checked = false;
					return false;
				}
				
				/*if(checked == true)
				{
					var msg = 'Check all items to be deleted?';
					if(msgBox(msg, 0,1,1,0, 0,0,'','Delete all?') == 'No')
						return;
					else //make sure the checkbox is checked
						oChk.checked = true;
				}*/
			
				for (i = 0; i < grid.Rows.length; i++)
				{	
					oCell = grid.Rows.getRow(i).getCellFromKey("Delete");
					if (oCell != null && igtbl_isColumnHeader(oCell.Id) == false && oCell.isEditable() == true && oCell.Element.children[0].style.visibility != 'hidden')
						oCell.setValue(checked);
				}
			}
		}
		catch(e){}
		_bInHeaderClick = false;
	}	
	
	
	function beforeExitEditMode(gridID, cellID)
	{
		var cell = igtbl_getCellById(cellID);
		//Columntype 0 is the standard text so dont bother validating
		
		if (doesColumnHaveUniqueFilter(gridID,cell.Column.Key))
		{
			restoreOriginalValueList(gridID,cell.Column);
		}
		
		
		if(cell.Column.ColumnType != 0) 
		{
			if (validateCell(cell) == false)
			{
				return true; //cancel event
			}
		}
		return false; //dont cancel the event
	}
	
	
	function setToggleDeleteCheck(gridID)
	{
		try
		{
			var checked = true;
			var grid = igtbl_getGridById(gridID);
			var oCell = null;
			
			for (i = 0; i < grid.Rows.length; i++)
			{			
				oCell = grid.Rows.getRow(i).getCellFromKey("Delete");
				checked = oCell.getValue();
				if (checked == false && oCell.isEditable() == true && oCell.Element.children[0].style.visibility != 'hidden')
					break;
				else if (checked == false && oCell.isEditable() == false && oCell.Element.children[0].style.visibility != 'hidden')
					checked = true; //reset if the cell was not editable
					
			}
			var oChk = igtbl_getElementById(grid.Bands[0].getColumnFromKey('Delete').Id).getElementsByTagName('INPUT')[0];
			oChk.checked = checked;
		}
		catch(e){}
	}
	
	
	function newRow(gridID, bands, validate)
	{
		if (validate != false && fireValidators(gridID) == false)
			return;
		
		
		_boolDirty = true;
		
		if (bands == null)
		{ //default to 0 band if nothing was passed in
			var sBands = new Array(0);
			sBands[0] = 1;
		}
		else
			var sBands = bands.split(',');
			
		var i = 0;
		var iRows = 0;
		var iRow = 0;
		var parentRow;
		var grid = igtbl_getGridById(gridID);

		for(i=0; i<sBands.length; i++)
		{
			if(i==0){
				parentRow = grid.Bands[i].addNew();
			}
			else
			{
				grid.Bands[i].addNew()
			}
		}
        if(parentRow){
            for (i=0;i<parentRow.cells.length;i++){
                if(!parentRow.cells[i].Column.Hidden){
            		parentRow.getCell(i).activate(); 
	            	parentRow.getCell(i).beginEdit();
            		return parentRow;
            	}
            }
	    }
		//parentRow.activate();
	}

	
	function beforeCellUpdate(gridID, cellID, newValue) 
	{	
		try
		{
			var cell = igtbl_getCellById(cellID);
			//every page that cares about the aftercellchange event should have
			//afterCellUpdated included in the page
			//if not do nothing in the catch 
			var ret = beforeCellUpdated(gridID, cell, newValue);
			if (ret == false) //if the function returns false then dont continue
				return;
		}
		catch(e){}	
	}
	
	function afterCellUpdate(gridID, cellID) 
	{	
		var col = igtbl_getColumnById(cellID);
		var cell = igtbl_getCellById(cellID);
		var row = igtbl_getRowById(cellID);
		
		_boolDirty = true;
			
		try
		{
			//every page that cares about the aftercellchange event should have
			//afterCellUpdated included in the page
			//if not do nothing in the catch 
			var ret = afterCellUpdated(gridID, col, row, cell);
			if (ret == false) //if the function returns false then dont continue
				return;
		}
		catch(e){}	
		
			
		if (col.Key == "Delete")
		{
			if(cell.getValue() == true)
			{
				igtbl_getElementById(row.Id).className += ' DeleteRow';
				toggleEditableCells(row, false);
			}
			else
			{
				//restore the old styless
				igtbl_getElementById(row.Id).className = igtbl_getElementById(row.Id).className.replace(' DeleteRow', '');
				toggleEditableCells(row, true);
			}
			
			//if the headerclick called this method then dont toggle the header
			if(_bInHeaderClick == false) 
				//make sure the toggle checkbox is set correctly
				setToggleDeleteCheck(gridID);
		}
		else
		{
			var cell = igtbl_getCellById(cellID);
			//igtbl_getElementById(cell.Id).style.backgroundColor = '#B3D5B3';
			cell.getElement().style.backgroundColor = '#B3D5B3';
			//cell.Element.className += ' DirtyCell';  //have to move the stylesheet after the </body>tag for this to work
		}
		
		try
		{
			//call this function if the page has it
			gridChanged(gridID, cellID);
		}
		catch(e){}//dont worry about it if an error happens
	}
	
	function toggleEditableCells(row, editable)
	{
		for(iCol=0; iCol<row.Band.Columns.length; iCol++)
		{
			if(row.getCell(iCol).Column.Key != 'Delete')
			{
				if (row.getCell(iCol).Element.wasEditable == null) //save wether the cell started of editable
					row.getCell(iCol).Element.wasEditable = row.getCell(iCol).isEditable();
				
				if (row.getCell(iCol).Element.wasEditable != false)
					//set the cells editable property
					row.getCell(iCol).setEditable(editable);
			}
		}
		
		//recursive call for all childrows
		if(row.Rows != null)
		{
			for(iRow = 0; iRow<row.Rows.length; iRow++)
			{
				toggleEditableCells(row.Rows.getRow(iRow), editable);			
			}
		}	
	}
	
	
	function fireValidators(gridID)
	{
		//this function is used to fire the validators 
		//for the current row
		var grid = igtbl_getGridById(gridID);
		var row = grid.getActiveRow();
		
		if(row == null)
			return true;
		else
			row = getRootParentRow(row);
			
		return validateRow(row);
	}
	
	
	function validateRow(row)
	{
		//this function will fire any validators for the row
		//that was passed in
		var iCol = 0;
		var iRow = 0;
		var iVal = 0;
		var grid = igtbl_getGridById(row.gridId);
		var editor = document.getElementById(row.gridId + '_tb');
		
		
		//if the current row is being deleted do not validate it or its children
        var oCell = row.getCellFromKey("Delete");		
		if (oCell != null && oCell.getValue() == true)
		    return true;
		
		
		for(iCol=0; iCol<row.Band.Columns.length; iCol++)
		{
			if(row.Band.Columns[iCol].Validators.length > 0)
			{
			    if (validateCell(row.getCell(iCol)) == false)
			        return false;
			}
		}
		
		//loop through the child rows if there are any
		if(row.Rows != null && row.Rows.length > 0)
		{
			for(iRow=0; iRow<row.Rows.length; iRow++)
			{
				//recursive call for all the child rows
				validateRow(row.Rows.getRow(iRow));
			}
		}	
		
		//all cells in the row were valid
		return true;
	}

    function validateCell(cell)
    {
        //put the cell into edit mode so the validators will be hooked to the correct controls
        //cell.beginEdit();
	    //cell.endEdit()
	    
	    for(iVal=0; iVal<cell.Column.Validators.length; iVal++)
		{
			var validator = getValidator(cell.Column.Validators[iVal].toString());
			if(validator != null)
			{
		        var valid = showRequiredFieldValidator(cell, validator);	
				if (valid == false)
				{
				    //the cell needs to be in edit mode so user cant move off of it
				    //cell.activate();
				    cell.beginEdit()
				    return false;
			    }
			}
		}
    
    }

	function isRequiredFieldValiator(val)
	{
		if(val.evaluationfunction != null && val.evaluationfunction.toString().indexOf('RequiredFieldValidator') >= 0)
			return true;
		else
			return false;
	}
	
	function showRequiredFieldValidator(cell, validator)
	{
	    //get the html element for th cell so the validator can be placed next to it
		var td = igtbl_getElementById(cell.Id);
        var grid = igtbl_getGridById(cell.Row.gridId);
        var cellIsInEditMode = (igtbl_inEditMode(cell.Row.gridId) == true && cell == grid.oActiveCell)
	    var ctrlToValidate = validator.controltovalidate;
		
		
		if(cell.Column.editorControl != null && cell.Column.editorControl.getValue != null)
		    document.getElementById(validator.controltovalidate).value = cell.Column.editorControl.getValue();
		    		    //cell.setValue(cell.Column.editorControl.getValue());
		    
		
		
		//create a hidden control for validation of each cell if it doesnt already exist
		//need to do this for each grid and band so that when the page finally submits the validators
		//are not linked to a hidden with the value from a different cell
		var hdID = cell.Row.gridId + 'b' + cell.Column.Band.Index.toString() + 'c' + cell.Index.toString();
		var hd = document.getElementById('_hdValidate'+hdID);
		if (hd == null)
		{
			hd = document.createElement("<INPUT ID='_hdValidate"+hdID +"' TYPE='HIDDEN'>");
			document.forms[0].appendChild(hd);
		}
			
		
		 if(cellIsInEditMode == false)
		 {
		    //hook the validator up to our own control to validate it
		    hd.value = cell.getValue();
			if(hd.value == null || hd.value.toLowerCase() == 'null') 
			{
				hd.value = ' ';
				cell.setValue(' ', false);
			}
			validator.setAttribute("controltovalidate",hd.id);
			ValidatorHookupControlID(hd.Id, validator);    
		 
		 }

		
		
		//position the validator
		validator.style.position="absolute";
		validator.style.left=igtbl_getLeftPos(td);
		validator.style.top=igtbl_getTopPos(td) + td.clientHeight;
		//check if the validator is valid
	    ValidatorEnable(validator, true)
		var valid =  validator.isvalid;
		
	    //restore the validator to the grid
        //validator.setAttribute("controltovalidate",ctrlToValidate);
	    //ValidatorHookupControlID(ctrlToValidate, validator);

		return valid;
	
	}
	
	function getRootParentRow(row)
	{
		while(row.ParentRow != null)
		{
			row = row.ParentRow;
		}	
		return row;
	}
	
	
	function getValidator(id)
	{
		var i = 0;
		for(i=0; i<Page_Validators.length; i++)
		{
			if(Page_Validators[i].id == id)
				return Page_Validators[i];
		}
	}
		
	
	function validateChangedRows(gridID)
	{
		if (gridID == null) gridID = 'oGrid';
		
		var iRow = 0;
		var grid = igtbl_getGridById(gridID);
		
		//check if the acitve cell is valid first
		if(grid.oActiveCell != null && validateCell(grid.oActiveCell) == false)
		    return false;
		
		
		//loop through all the rows and check if the row is in the changed rows collection
		for(iRow=0; iRow<grid.Rows.length; iRow++)
		{
			if (validateChangedRow(grid, grid.Rows.getRow(iRow)) == false)
				return false;
		}
	}
	
	
	function validateChangedRowsBeforeSumbit()
	{
		var iRow = 0;
		var grid = null;
		var iGrid = 0;
		var tbls = null
		
		//loop through all the grids on the page
		tbls = document.getElementsByTagName('TABLE');
		for(iGrid=0; iGrid<tbls.length; iGrid++)
		{
		    /* Try catch added by Loren Hayden 9/23/2005
			-----------------------------------------------------------------------
			The try catch secion in this function added due to an error that occured
			with the TMSDropdown when selecting a different item. The item found
			by the document.getElementsByTagName('INPUT')[iGrid].id call would return
			back and empty string thus producing an error.
			-----------------------------------------------------------------------
			*/
			try
			{
				//every webgrid id starts with "G_" us this and 
				//and the  igtbl_getGridById function to find all the grids
				var id = tbls(iGrid).id;
				if (id != null && id != '' && id.indexOf('G_') == 0) //posible grid id
				{
				    grid = igtbl_getGridById(id.replace('G_', ''));
    				
				    if (grid != null)
				    {
				    	//check if the acitve cell is valid first
		                if(grid.oActiveCell != null && validateCell(grid.oActiveCell) == false)
		                    return false;
					    
					    //loop through all the rows and check if the row is in the changed rows collection
					    for(iRow=0; iRow<grid.Rows.length; iRow++)
					    {
						    if (validateChangedRow(grid, grid.Rows.getRow(iRow)) == false)
							    return false; //get out and validate the next grid
					    }
				    }
			    }
			}
			catch( e )
			{
				//Nothing Critial needs to be handled here
			}
		}
		
		//call original function to make sure all validators on the page are checked
		return _Page_ClientValidate();
	}
	
	
	function validateChangedRow(grid, row)
	{
		//if the row had changed then validate it
		if(grid.ChangedRows[row.Id] != null || grid.AddedRows[row.Id] != null)
		{
			if (validateRow(row) == false)
				return false;
		}
	
	    
		//loop through the child rows if there are any
		/*
		if(row.Rows != null && row.Rows.length > 0)
		{
			for(iRow=0; iRow<row.Rows.length; iRow++)
			{
				//recursive call for all the child rows
				validateChangedRow(grid, row.Rows.getRow(iRow));
			}
		}*/	
	}
	
	/*
	function validateCell(cell)
	{
		//check if the column has validators
		if(cell.Column.Validators.length > 0)
		{
			var iVal = 0;
			var val = null;
			
			for(iVal=0; iVal<cell.Column.Validators.length; iVal++)
			{
				val = getValidator(cell.Column.Validators[iVal].toString());
				if (showRequiredFieldValidator(cell, val) == false)
					return false;
			}
		}
		
		return true;
	}
	*/
	
	function movePager(tbl, pager)
	{
		tbl.rows[tbl.rows.length-1].cells[0].innerHTML = '';
		tbl.rows[tbl.rows.length-1].cells[0].appendChild(pager);
		pager.style.display = '';
	}
	
	
			
	
	function AddToUniqueColumnFilterList(gridId, columnKey)
	{
		var grid = igtbl_getGridById(gridId);
		
		var column = grid.Bands[0].getColumnFromKey(columnKey);
		var newIndex;
		var i = 0;
		var doesColumnHaveValueList = ! (column.ValueList == null);
		var doesGridExist = ! (grid == null);
		var doesColumnExist = ! (column == null);
		var isColumnAlreadyDefined = false;
		
		if (doesColumnHaveValueList == false || doesGridExist == false || doesColumnExist == false)
		{
			return;
		}

		if (_UniqueColumnFilterList == null)
		{
			_UniqueColumnFilterList = new Array();
			newIndex = 0;
		}
		else
		{
			newIndex = _UniqueColumnFilterList.length;
			for (i=0 ; i < _UniqueColumnFilterList.length; i++)
			{
				isColumnAlreadyDefined = _UniqueColumnFilterList[i][0] == gridId && _UniqueColumnFilterList[i][1] == columnKey;
				if (isColumnAlreadyDefined)
				{
					newIndex = i;
				}
			}
		}
		
		_UniqueColumnFilterList[newIndex] = new Array(3);
		_UniqueColumnFilterList[newIndex][0] = gridId;
		_UniqueColumnFilterList[newIndex][1] = columnKey;
		_UniqueColumnFilterList[newIndex][2] = column.ValueList;

	}

	function doesColumnHaveUniqueFilter(gridId, columnKey)
	{
		var i = 0;
		var doesColumnHaveFilter = false;
		
		if (_UniqueColumnFilterList == null)
		{
			return false;
		}
		
		for (i=0; i<_UniqueColumnFilterList.length; i++)
		{
			if (_UniqueColumnFilterList[i][0] == gridId && _UniqueColumnFilterList[i][1] == columnKey)
			{
				doesColumnHaveFilter = true;
				break;
			}
			
		}
		
		return doesColumnHaveFilter;
	}

	function restoreOriginalValueList(gridId, column)
	{

		var i = 0;
		
		if (_UniqueColumnFilterList == null)
		{
			return;
		}
		
		for (i=0; i<_UniqueColumnFilterList.length; i++)
		{
			if (_UniqueColumnFilterList[i][0] == gridId && _UniqueColumnFilterList[i][1] == column.Key)
			{
				column.ValueList = _UniqueColumnFilterList[i][2];
				
				break;
			}
			
		}
	
	}

	function getOriginalValueList(gridId, columnkey)
	{
		var i = 0;
		
		if (_UniqueColumnFilterList == null)
		{
			return null;
		}
		
		for (i=0; i<_UniqueColumnFilterList.length; i++)
		{
			if (_UniqueColumnFilterList[i][0] == gridId && _UniqueColumnFilterList[i][1] == columnkey)
			{
				return _UniqueColumnFilterList[i][2];
			}
			
		}
		
	}

	function beforeEnterEditModeHandler(gridName, cellId){
		var cell = igtbl_getCellById(cellId);
		var gd = igtbl_getGridById(gridName);
		var columnIndex = cell.Column.Index
		
		if (doesColumnHaveUniqueFilter(gridName, cell.Column.Key))
		{
			var rowCount = 0;
			var exclusionListCount = 0;
			var numRows = gd.Rows.length;
			var numOfExcludedRows = numRows - 1;
			var exclusionList = new Array(numOfExcludedRows);

			for (rowCount=0;rowCount<numRows;rowCount++)
			{
				var row = gd.Rows.getRow(rowCount);
				var rowCell = row.getCell(columnIndex);
				
				if (rowCell != cell)
				{
				    
					exclusionList[exclusionListCount] = new Array(2);
					exclusionList[exclusionListCount][0] = rowCell.getValue();
					exclusionList[exclusionListCount][1] = rowCell.Element.innerText;
					exclusionListCount++;
				}
			}
			cell.Column.ValueList = buildNewValueList(exclusionList,gridName, cell.Column.Key );
		}
		
		try
		{
			return beforeEnterEditMode(gridName, cellId);
		}
		catch(e){}
	}
	
	
	function buildNewValueList(exclusionList,gridName, columnkey)
	{
		var origValueList = getOriginalValueList(gridName, columnkey);
		var newValueListLength = origValueList.length - exclusionList.length;
		var newValueList = new Array(newValueListLength);
		var valListCount;
		var newValueListCount = 0;
		
		

		for (valListCount=0; valListCount<origValueList.length; valListCount++)
		{
			if (isItemInExclusionList(exclusionList,origValueList[valListCount][1]) == false)
			{
				newValueList[newValueListCount] = new Array(2);
				newValueList[newValueListCount][0] = origValueList[valListCount][0];
				newValueList[newValueListCount][1] = origValueList[valListCount][1];
				newValueListCount++;
			}
		}
		return newValueList;
	}

	function isItemInExclusionList(exclusionList,valueId)
	{
		var i;
		var valueIdExists = false;

		for (i=0;i<exclusionList.length;i++)
		{
			if (exclusionList[i][1] == valueId)
			{
				valueIdExists = true;
				break;
			}
		}
		return valueIdExists;
	}
	
	
	
	function columnSizeChanged(gridId, columnId, newWidth)
	{
		
		var col = igtbl_getColumnById(columnId);
		
		try
		{
			var pgName = getCurrentPageName();
			var band = 'Band'+col.Band.Index;
			var key = pgName + '-' + gridId + '-' + band;
			var value = '';
		
			for(i=0; i<col.Band.Columns.length; i++)
			{
				value += col.Band.Columns[i].Key + '~' + col.Band.Columns[i].getWidth() + '||';
			}
			if (value.length > 0)
				value = value.substring(0, value.length -2);
				
			setClientPreference(key, value);		
		}
		catch(e){}
	}
	
	function renumberOrderColumn( gridId, cellId, columnKey, dataColumnKey )
	{
		var cell = igtbl_getCellById( cellId );
		var row = cell.Row;
		var id = row.getCellFromKey( dataColumnKey );

		//Make sure its	correct column
		if( cell.Column.Key != columnKey )
		{
			return;
		}

		//check to see if it is a new record if it is set the next order value
		if( ( id.getValue() == '00000000-0000-0000-0000-000000000000' )
		   || ( id.getValue() == null ) 
		   || ( id.getValue() == undefined ) 
		   || (id.getValue() == '' ))
		   {
			    cell.setValue( row.getIndex() + 1 );
				return;
			}
			
		var gd = igtbl_getGridById( gridId );
		var newIndex = ( cell.getValue() <= 0 ) ? 0 : cell.getValue() - 1;

		var rowCount = gd.Rows.length - 1;	
		
		if( newIndex  > rowCount )
			newIndex = gd.Rows.length;

		var cellCount = row.cells.length;

		for( var cellIndex = 0; cellIndex < cellCount; cellIndex++ )
		{
				var currCell = row.getCell(cellIndex);
				currCell.Element.style.backgroundColor = '#B3D5B3';
		}
		
		row.remove();
		gd.Rows.insert( row, newIndex );
				
		for( var i=0; i <= rowCount; i++ )
		{
			var deleteCell = gd.Rows.getRow( i ).getCellFromKey('Delete');
			
			if( deleteCell.getValue() == false )
			{
				var currCell = gd.Rows.getRow( i ).getCellFromKey( columnKey );
				currCell.setValue( i + 1 );
			}
		}			
		
	}
	
	function reOrderRows( gridID, orderColumnKey )
	{
			var grid = igtbl_getGridById( gridID );
			var rowIndex = 0;
			var rowValue = 1;
			
			var row = grid.Rows.getRow(rowIndex);
			
			while( row != null )
			{
				var deleteCell = row.getCellFromKey('Delete');
				
				if( deleteCell.getValue() == false )
				{
					var orderCell = row.getCellFromKey( orderColumnKey );
					orderCell.setValue( rowValue );
					rowValue++;
				}
				
				rowIndex++;
				row = grid.Rows.getRow(rowIndex);
			}
	}
	
	function hideGridCell( cell )
	{
		if( cell != null )
		{
			cell.setEditable( false );
			cell.Element.style.visibility='hidden';
			cell.Element.style.width = '0px';
		}			
	}
	
	function disableGridCell( cell )
	{
		if( cell != null )
		{
			cell.setEditable( false );
		}			
	}	
	
	function cellToUpper( cell, columnKey )
	{
		if( cell.Column.Key == columnKey )
		{
			var value = cell.getValue();
			cell.setValue( value.toUpperCase() );
		}	
	}

	function isFirstRowSelected(gridID)
	{ 
	    var grid = igtbl_getGridById(gridID);
	    //return grid.Rows.getRow(0).getSelected();
	    var activeRow = grid.getActiveRow();
        if(activeRow.ParentRow != null)
        {
            siblings = activeRow.ParentRow.Rows;
        }
        else
        {
            siblings = grid.Rows;
        }
        return siblings.getRow(0).getSelected();	    
	}
	
	function isLastRowSelected(gridID)
	{
	    var grid = igtbl_getGridById(gridID);
	    //return grid.Rows.getRow(grid.Rows.length-1).getSelected();
	    var activeRow = grid.getActiveRow();
	    if(activeRow.ParentRow != null)
        {
            siblings = activeRow.ParentRow.Rows;
        }
        else
        {
            siblings = grid.Rows;
        }
        return siblings.getRow(siblings.length-1).getSelected();	    
	}    
	
	
		function moveSelectedUp(gridID, orderColumnKey)
	{ 
	    var grid = igtbl_getGridById(gridID);
	    var row = null
	    var prevRow = null;
	    var prevIndex = 0
	    var activeRow = grid.getActiveRow();
        if(activeRow.ParentRow != null)
        {
            siblings = activeRow.ParentRow.Rows;
        }
        else
        {
            siblings = grid.Rows;
        }
        var rowCount = siblings.length;
        
	    for(var iRow=0; iRow < rowCount; iRow++)
	    {
	        row = siblings.getRow(iRow)
	        if(row.getSelected() == true)
	        {
	            prevRow = row.getPrevRow();
	            if (prevRow != null)
	            {   
	                prevIndex = prevRow.getIndex();
                    siblings.remove(iRow); //remove the current row
	                siblings.insert(row, prevIndex); //insert the row at index of the next row
	                row.getCellFromKey(orderColumnKey).setValue(row.getIndex() + 1);
                    prevRow.getCellFromKey(orderColumnKey).setValue(prevRow.getIndex() + 1);
	            }
	        }
	    }
        siblings.reapplyRowStyles(); //restore alternating row colors
	}
	
	function moveSelectedDown(gridID, orderColumnKey)
	{ 
	    var grid = igtbl_getGridById(gridID);
	    var row = null
	    var nextRow = null;
	    var nextIndex = 0
	    var activeRow = grid.getActiveRow();
        if(activeRow.ParentRow != null)
        {
            siblings = activeRow.ParentRow.Rows;
        }
        else
        {
            siblings = grid.Rows;
        }
        var rowCount = siblings.length;
	    
	    for(var iRow=rowCount-1; iRow>=0; iRow--)
	    {
            row = siblings.getRow(iRow)
	        if(row.getSelected() == true)
	        {
	            nextRow = row.getNextRow();
	            if (nextRow != null)
	            {   nextIndex = nextRow.getIndex();
                    siblings.remove(nextIndex); //remove the nextrow
	                siblings.insert(nextRow, iRow); //insert the next row at the current position
	                row.getCellFromKey(orderColumnKey).setValue(row.getIndex() + 1);
                    nextRow.getCellFromKey(orderColumnKey).setValue(nextRow.getIndex() + 1);
	            }
	        }
	    }
        siblings.reapplyRowStyles(); //restore alternating row colors
	}