/************************************************************************** 
*************************************************************************** 
*  Program Name: $Id: table_control.js,v 1.1.1.1 2007/12/10 00:51:16 neo Exp $
*  Program Author:  Michael T. Schock
*  Creation Date: 05-15-2006
*  CVS Revision: $Revision: 1.1.1.1 $
*  Copyright (c) 2006
*************************************************************************** 
*************************************************************************** 
*  Program Summary:
*  Javascript functions for the table class
*
*
*************************************************************************** 
**************************************************************************/

//  Main sort function
function Column_Sort(headerObj, tableNumber)
{
	//  Variables
	var headerParent;
	var headerIndex;
	var table;
	var data;
	var sortFunction;
	var firstRow = new Array();
	var newRows = new Array();
	var sortDirection;

	//  Get the row, column, and table data
	headerIndex = headerObj.cellIndex;
	table = Get_Parent(headerObj, 'TABLE');

	//  Get column data
	if(table.rows.length <= 1)
		return;
	data = Get_Inner_Text(table.rows[1].cells[headerIndex]);

	//  Set the default column function
	sortFunction = Sort_Caseinsensitive;

	//  Date type (dd-mm-yyyy)
	if (data.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/))
	sortFunction = Sort_Date;

	//  Date type (dd-mm-yy)
	if (data.match(/^\d\d[\/-]\d\d[\/-]\d\d$/))
	sortFunction = Sort_Date;

	//  Date type (dd-MMM-yy)
	if (data.match(/^\d\d[\/-]\w\w\w[\/-]\d\d$/))
	sortFunction = Sort_Date;

	//  Date type (dd-MMM-yyyy)
	if (data.match(/^\d\d[\/-]\w\w\w[\/-]\d\d\d\d$/))
	sortFunction = Sort_Date;

	//  Currency type
	if (data.match(/^[?$]/))
	sortFunction = Sort_Currency;

	//  Numeric type
	if (data.match(/^[\d\.]+$/))
	sortFunction = Sort_Numeric;

	SORT_COLUMN_INDEX = headerIndex;

	//  Set up table data
	for (i=0;i<table.rows[0].length;i++)
	{
		firstRow[i] = table.rows[0][i];
	}

	for (j=1;j<table.rows.length;j++)
	{
		newRows[j-1] = table.rows[j];
	}

	//  Sort the table data
	newRows.sort(sortFunction);

	//  Get the sort direction
	sortDirection = headerObj.title;

	if (sortDirection == 'Ascending')
	{
		headerObj.title = "Descending";
	}
	else
	{
		headerObj.title = "Ascending";
		newRows.reverse();
	}

	for (i=0;i<newRows.length;i++)
	{
		if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1)))
			table.tBodies[0].appendChild(newRows[i]);
	}

	// Sortbottom rows only
	for (i=0;i<newRows.length;i++)
	{
		if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1))
			table.tBodies[0].appendChild(newRows[i]);
	}

}

//  Date sorting function
function Sort_Date(a,b)
{
	//  Variables
	var monthName = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var monthIndex = '00';

	// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
	aa = Get_Inner_Text(a.cells[SORT_COLUMN_INDEX]);
	bb = Get_Inner_Text(b.cells[SORT_COLUMN_INDEX]);

	//!  Get the first date
	switch(aa.length)
	{
		case 8:			//  dd-mm-yy
			//  Determine if the year prefix is 19 or 20
			if(aa.substr(6,2) < 50)
				year = '20';
			else
				year = '19';

			//  Set the data for comparison
			date1 = year + aa.substr(6,2) + aa.substr(3,2) + aa.substr(0,2);
			break;
		case 9:			//  dd-MMM-yy
			//  Determine if the year prefix is 19 or 20
			if(aa.substr(7,2) < 50)
				year = '20';
			else
				year = '19';

			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(aa.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date1 = year + aa.substr(7,2) + monthIndex + aa.substr(0,2);

			break;
		case 10:		//  dd-mm-yyyy
			//  Set the data for comparison
			date1 = aa.substr(6,4) + aa.substr(3,2) + aa.substr(0,2);
			break;
		case 11:		//  dd-MMM-yyyy
			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(aa.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date1 = aa.substr(7,4) + monthIndex + aa.substr(0,2);
			break;
	}

	//!  Get the second date
	switch(bb.length)
	{
		case 8:			//  dd-mm-yy
			//  Determine if the year prefix is 19 or 20
			if(bb.substr(6,2) < 50)
				year = '20';
			else
				year = '19';

			//  Set the data for comparison
			date2 = year + bb.substr(6,2) + bb.substr(3,2) + bb.substr(0,2);
			break;
		case 9:			//  dd-MMM-yy
			//  Determine if the year prefix is 19 or 20
			if(bb.substr(7,2) < 50)
				year = '20';
			else
				year = '19';

			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(bb.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date2 = year + bb.substr(7,2) + monthIndex + bb.substr(0,2);

			break;
		case 10:		//  dd-mm-yyyy
			//  Set the data for comparison
			date2 = bb.substr(6,4) + bb.substr(3,2) + bb.substr(0,2);
			break;
		case 11:		//  dd-MMM-yyyy
			//  Get the month index
			for(loop = 0; loop < monthName.length; loop++)
			{
				if(bb.substr(3,3).toUpperCase() == monthName[loop].toUpperCase())
				{
					if(loop < 10)
						monthIndex = '0' + loop;
					else
						monthIndex = loop;
				}
			}

			date2 = bb.substr(7,4) + monthIndex + bb.substr(0,2);
			break;
	}

	//  Return positive, negative, or zero
	if (date1 > date2)
		return 1;

	if (date1 < date2)
		return -1;

	return 0;
}

function Sort_Currency(a,b)
{
	//  Currency 1
	aa = Get_Inner_Text(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');

	//  Currency 2
	bb = Get_Inner_Text(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');

	//  Return positive, negative, or zero
	return parseFloat(aa) - parseFloat(bb);
}

function Sort_Numeric(a,b)
{
	//  First number
	aa = parseFloat(Get_Inner_Text(a.cells[SORT_COLUMN_INDEX]));

	//  Verify that the data is a number
	if (isNaN(aa))
		aa = 0;

	//  Second number
	bb = parseFloat(Get_Inner_Text(b.cells[SORT_COLUMN_INDEX]));

	//  Verify that the data is a number
	if (isNaN(bb))
		bb = 0;

	//  Return positive, negative, or zero
	return aa-bb;
}

function Sort_Caseinsensitive(a,b)
{
	//  First data field
	aa = Get_Inner_Text(a.cells[SORT_COLUMN_INDEX]).toLowerCase();

	//  Second data field
	bb = Get_Inner_Text(b.cells[SORT_COLUMN_INDEX]).toLowerCase();

	//  Return positive, negative, or zero
	if (aa==bb) return 0;
	if (aa<bb) return -1;
	return 1;
}


//  Get the parent node of an element by specifying the node type
function Get_Parent(element, typeName)
{
	//  Check for valid element
	if(element == null)
		return null;

	//  Check to see if this is the parent
	if(element.nodeType == 1 && element.tagName.toLowerCase() == typeName.toLowerCase())
		return element;
	else
		return Get_Parent(element.parentNode, typeName);
}

//  Get the inner text of table cell
function Get_Inner_Text(element)
{
	//  Variables
	var stringData;
	var nodes;
	var loop;

	//  Check to see if the element is a string or undefined
	if(typeof element == "string" || typeof element == "undefined")
		return element;

	//  See if we can bypass the search
	if(element.innerText)
		return element.innerText;

	//  Parse through the childNodes to get the data
	nodes = element.childNodes;
	for(loop = 0; loop < nodes.length; loop++)
	{
		switch(nodes[loop].nodeType)
		{
			//  Element Node
			case 1:
				stringData += Get_Inner_Text(nodes[loop]);
				break;
			//  Text Node
			case 3:
				stringData = nodes[loop].nodeValue;
				break;
		}
	}

	//  Return the string
	return stringData;
}

//  Remove highlighting from a selected row.
function Unhighlight(obj, oldIndex)
{
	//  Variables
	var nodes;

	//  Get the rows of the table
	nodes = obj.parentNode;

	//  Reset the class
	nodes.rows[oldIndex].className = nodes.rows[oldIndex].getAttribute('origClass');
}

//  Select a row
function Select_Row(obj, multiSelect, controlNumber, rowNumber)
{
	//  Variables
	var loop;
	var cellCount;
	var pressedClass;
	var deSelect;
	var oldIndex;

	//  Set variables
	deSelect = 0;

	//  Determine if we are selecting or unselecting
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	if (obj.className == pressedClass)
	{
		deSelect = 1;
		obj.className = obj.getAttribute('origClass');
	}
	else
	{
		if(!obj.getAttribute('origClass'))
		{
			obj.setAttribute('origClass', obj.className);
		}

		//  This func will highlight the selected table row
		obj.className = pressedClass;
	}

	//  Set the selected data into the hidden fields
	Fill_Hidden_Fields(obj, multiSelect, controlNumber, deSelect, obj.rowIndex);

	//  This function will unhighlight the other rows
	if (multiSelect == 0)
	{

		//  Set the selected row
		oldIndex = document.getElementById('Table_Selected_' + controlNumber).value;

		if(oldIndex != '' && oldIndex != 'undefined')
			//  Unhighlight the last highlighted row
			Unhighlight(obj, oldIndex);

		//  Set the new selected item
		document.getElementById('Table_Selected_' + controlNumber).value = obj.rowIndex;
	}
}

//  Store data for passing to the next page.
function Fill_Hidden_Fields(obj, multiSelect, controlNumber, deSelect, rowNumber)
{
	//  Variables
	var loop;
	var hiddenData;
	var hiddenName;
	var newString;
	var cellCount;

	cellCount = obj.cells.length;

	//  Set up the name of the hidden field
	hiddenName = 'Table_Control_Hidden_' + controlNumber + '_';

	//  Multiple selections active
	if(deSelect == 0)
	{
		//  Get the previously selected Data and put it into a temporary buffer
		for(loop = 0; loop < cellCount; loop++)
		{  
			//  If multiSelect is on, add the data to the string
			if (multiSelect == 1)
			{
				hiddenData = document.getElementById(hiddenName + loop).value;
				document.getElementById(hiddenName + loop).value = "";
                        }
				
			//  Create the new data string and write to the hidden field
			if(hiddenData == '' || multiSelect == 0)
			{
			        newString = rowNumber + ',' + obj.cells.item(loop).innerHTML;
			}
			else
			{
				newString = hiddenData + ',' + rowNumber + ',' + obj.cells.item(loop).innerHTML;
			}
			
			
			document.getElementById(hiddenName + loop).value = newString;
		}
		
	}
	else
	{
		for(loop = 0; loop < cellCount; loop++)
		{
			//  Remove the data from the hidden field if deSelect == 1
	 		Remove_Row_Data(hiddenName + loop, rowNumber);
		}
	}

}

//  Remove row data from a hidden variable when deselecting a row.
function Remove_Row_Data(hiddenName, rowNumber)
{
	//  Variables
	var hiddenData;
	var start = 0;
	var stop = 0;
	var quantity = 0;
	var count = 1;
	var dataStr;
	var newString;
	var strStart = 0;
	var tempString;
	var remove;
	var firstRun = 0;
	
	
	//  Get hidden data
	hiddenData = document.getElementById(hiddenName).value;
	document.getElementById(hiddenName).value = "";
	
	
	//  Parse the data
	while(start >= 0 || stop >= 0)
	{       
		if(firstRun == 0)
		{  
			start = 0;
			stop = hiddenData.indexOf(",", start + 1);
			firstRun = 1;
		}
		else
		{
			start = hiddenData.indexOf(",", start + 1) + 1;
			stop = hiddenData.indexOf(",", start + 1);
			if(stop < 0)
				stop = hiddenData.length;
		}
		
		count++;
		
		dataStr = hiddenData.substring(start, stop);
		
		if((count % 2) == 0)
		{
		 	if((parseInt(dataStr)) == rowNumber)
			{
				remove = 1;
			}
			else
			{
				if(strStart == 0)
				{
			 		newString = dataStr;
					strStart = 1;
				}
				else
				{
					tempString = newString;
					newString = tempString + ',' + dataStr; 
				}
			}
		}
		else
		{
			if(remove != 1)
			{
				newString = newString + ',' + dataStr;
			}
			else
				remove = 0;
		}
		if(stop == hiddenData.length)
			break;		
		
	}
	
	//  Set the newString into the hidden fieldi
	if(newString == undefined)
		newString = "";
	document.getElementById(hiddenName).value = newString;  
}

//  Clear the values during onload or when performing a sort
function Clear_Hidden(cellCount, controlNumber)
{
	for(loop = 0; loop < cellCount; loop++)
	{
		//  Set up the name of the hidden field
		hiddenName = 'Table_Control_Hidden_' + controlNumber + '_' + loop;
		document.getElementById(hiddenName).value = "";
	}
}

//  Function to create a multi-dimensional array.  (Javascript does not have this normally)
function Create_Multi_Array(numberOfRows,numberOfColumns, initValue)
{
	//  Variables
	var rowCount;
	var colCount;

	//  Create new array
   	var newArray = new Array(numberOfRows);
	
	//  Perform row looping
   	for (rowCount = 0; rowCount < numberOfRows; rowCount++)
   	{
       	//  Extend current array
		newArray[rowCount] = new Array(numberOfColumns);
		
		//  Perform column looping
       	for (colCount = 0; colCount < numberOfColumns; colCount++)
       	{
			//  Put empty data in the array
       	    newArray[rowCount][colCount] = initValue;
       	}
   	}
	
	//  Return the initialized array
   	return(newArray);
}

//  Function to create a single array and fill it with a default value
function Create_Single_Array(numberOfElements, initValue)
{
	//  Variables
	var elementCount;
	
	//  Create new array
	var newArray = new Array(numberOfElements);
	
	//  Perform element looping
	for (elementCount = 0; elementCount < numberOfElements; elementCount++)
	{
		//  Initialize the empty array with zero
		newArray[elementCount] = initValue;
	}
	
	//  Return the initialized array
	return(newArray);
}

//!  Function to traverse the table and reselect items
function Reselect(tableObj, columnHeader, columnData, controlNumber, multiSelect)
{
	//!  Variables
	var colIndex;
	var rowLength;
	var loop;

	//!  Get the index of the column header
	for(loop = 0; loop < tableObj.rows[0].cells.length; loop++)
	{
		if(tableObj.rows[0].cells[loop].innerHTML == columnHeader)
		{
			colIndex = loop;
			break;
		}
	}

	//!  Search the indexed column for the column data
	for(loop = 0; loop < tableObj.rows.length; loop++)
	{
		if(tableObj.rows[loop].cells[colIndex].innerHTML == columnData)
		{
			Select_Row(tableObj.rows[loop], multiSelect, controlNumber);
			return;
		}
	}
}


//!  Function to update the row index of stored data
function Update_Index(rowNumber, controlNumber, multiSelect, type)
{
	//!  Variables
	var tableObj;
	var rowObj;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Get the number of columns (This is the number of hidden items)
	rowObj = tableObj.rows[rowNumber];

	//!  Update the hidden fields
	Fill_Hidden_Fields(rowObj, multiSelect, controlNumber, type, rowNumber);
}

//!  Function to select all the fields in the table
function Select_All_Rows(controlNumber)
{
	//!  Variables
	var tableObj;
	var loop;
	var pressedClass;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Set the class name
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	//!  Get the number of columns (This is the number of hidden items)
	for(loop = 1; loop < tableObj.rows.length; loop++)  
	{
		if(tableObj.rows[loop].className != pressedClass)
			Select_Row(tableObj.rows[loop], 1, controlNumber);
	}
}

//!  Function to invert all the selections in the table
function Invert_Selection(controlNumber)
{
	//!  Variables
	var tableObj;
	var loop;
	var pressedClass;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Set the class name
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	//!  Get the number of columns (This is the number of hidden items)
	for(loop = 1; loop <= tableObj.rows.length; loop++)
	{
		if(tableObj.rows[loop].className != pressedClass)
			Select_Row(tableObj.rows[loop], 1, controlNumber);
		else
			Unhighlight(tableObj.rows[loop], loop);
	}
}

//!  Function to deselect all the selections in the table
function Deselect_All(controlNumber)
{
	//!  Variables
	var tableObj;
	var loop;
	var pressedClass;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Set the class name
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	//!  Get the number of columns (This is the number of hidden items)
	for(loop = 1; loop <= tableObj.rows.length; loop++)
	{
		if(tableObj.rows[loop].className == pressedClass)
			Unhighlight(tableObj.rows[loop], loop);
	}
}

//!  Function to select a range
function Select_In_Range(controlNumber)
{
	//!  Variables
	var tableObj;
	var loop;
	var startPoint;
	var endPoint;
	var pressedClass;

	//!  Set the class name
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Get the start point
	for(loop = 0; loop < tableObj.rows.length; loop++)
	{
		if(tableObj.rows[loop].className == pressedClass)
		{
			startPoint = loop;
			break;
		}
	}

	//!  Get the end point
	for(loop = (tableObj.rows.length - 1); loop >= 0; loop--)
	{
		if(tableObj.rows[loop].className == pressedClass)
		{
			endPoint = loop;
			break;
		}
	}

	//!  Start point must be less than end point
	if(startPoint > endPoint)
		return;

	//!  Get the number of columns (This is the number of hidden items)
	for(loop = startPoint +1; loop <= endPoint - 1; loop++)
	{
		if(tableObj.rows[loop].className != pressedClass)
			Select_Row(tableObj.rows[loop], 1, controlNumber);
	}
}

//!  Function to return the index of a single selection
function Get_Select_Index(controlNumber)
{
	//!  Variables
	var loop = 0;
	var pressedClass;
	var tableObj;
	var index = null;

	//!  Set the class name
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Loop through the table until we get the first selection
	for(loop = 0; loop < tableObj.rows.length; loop++)
	{
		if(tableObj.rows[loop].className == pressedClass)
		{
			index = loop;
			break;
		}
	}

	//!  Return success
	return(index);
}

//!  Function to return an the count of selected items
function Get_Select_Count(controlNumber)
{
	//!  Variables
	var loop = 0;
	var pressedClass;
	var tableObj;
	var index = 0;

	//!  Set the class name
	pressedClass = 'Table_Control_Row_Pressed_' + controlNumber;

	//!  Get the table object
	tableObj = document.getElementById('Table_Control_Main_' + controlNumber);

	//!  Loop through the table until we get the first selection
	for(loop = 0; loop < tableObj.rows.length; loop++)
	{
		if(tableObj.rows[loop].className == pressedClass)
		{
			index++;
		}
	}

	//!  Return success
	return(index);
}

//!  Function to return an an array of the selected items
function Return_Selected(controlNumber, columnIndex, selectCount)
{
	//!  Variables
	var loop = 0;
	var index = 0;
	var stop = 0;
	var start = 0;
	var returnData = new Array(selectCount);

	//!  Set the class name
	hiddenVar = document.getElementById('Table_Control_Hidden_' + controlNumber + '_' + columnIndex).value;

	//  Loop through the string and get the data
	for(loop = 0; loop < returnData.length; loop++)
	{ 
		//  Skip over the table index
		if(loop == 0)
			start = 0;
		else
			start = stop + 1;

		stop = hiddenVar.indexOf(",", start);
		
		//  Get the data position
		start = stop + 1;
		stop = hiddenVar.indexOf(",", start);
		
		if(stop < start)
			stop = hiddenVar.length;
			
		//  Parse the data 
		returnData[loop] = hiddenVar.substring(start, stop);
	}
	
	//!  Return the data array
	return(returnData);

}

//!  Move Rows
/**  function to move rows according to the moveType*/
function Move_Rows(moveType, controlNumber)
{
	//  Variables
	var tableObject;
	var tableLength;
	var loop;

	//  Get objects
	tableObject = document.getElementById('Table_Control_Main_' + controlNumber);
	tableLength = tableObject.rows.length;

	//  No data
	if(tableLength <= 1)
		return;

	//  Loop through the table and find everything that needs to be moved
	if(moveType == 'Up')
	{
		for(loop = 0; loop < tableLength; loop++)
		{
			//  If a highlighted row is encountered, perform action
			if(tableObject.rows[loop].className == 'Table_Control_Row_Pressed_' + controlNumber)
				Move_Up(loop, tableObject, tableLength, controlNumber);
		}
	}
	else if(moveType == 'Down')
	{
		for(loop = (tableLength-1); loop > 0; loop--)
		{
			//  If a highlighted row is encountered, perform action
			if(tableObject.rows[loop].className == 'Table_Control_Row_Pressed_' + controlNumber)
				Move_Down(loop, tableObject, tableLength, controlNumber);
		}
	}
	else if(moveType == 'Top')
	{
		//  Move all selected items to the top
		Move_Top(tableObject, controlNumber);
	}

}

//!  Move Rows up
/**  function to move rows up*/
function Move_Up(loop, tableObject, controlNumber)
{
	//  Variables
	var rowObject;
	var topObject;
	var bottomObject;
	var tempRow;
	var retCode;
	
	//  Get row object
	rowObject = tableObject.rows;

	//  Do not move if it is at the top
	if(loop == 1)
		return

	//  Do not move if the top is selected (This means it is as
	//  far up as it can go)
	if(rowObject[loop-1].className == 'Table_Control_Row_Pressed_' + controlNumber)
		return;

	//  Remove data from data array
	Update_Index(loop, 1, 1, 1);

	// Swap the two items
	newRow = rowObject[loop-1];
	oldRow = rowObject[loop];

	tableObject.tBodies[0].replaceChild(newRow, rowObject[loop]);
	tableObject.tBodies[0].insertBefore(oldRow, newRow);

	//  Add data to data array
	Update_Index((loop - 1), 1, 1, 0);
}

//!  Move Rows down
/**  function to move rows down*/
function Move_Down(loop, tableObject, controlNumber)
{
	//  Variables
	var rowObject;
	var topObject;
	var bottomObject;
	var tempRow;
	var retCode;
	var tableLength;

	//  Get row object
	rowObject = tableObject.rows;

	//  Get the table length
	tableLength = tableObject.rows.length;
	
	//  Do not move if it is at the top
	if(loop == (tableLength-1))
		return

	//  Do not move if the top is selected (This means it is as
	//  far up as it can go)
	if(rowObject[loop+1].className == 'Table_Control_Row_Pressed_' + controlNumber)
		return;

	//  Remove data from data array
	Update_Index(loop, 1, 1, 1);

	// Swap the two items
	newRow = rowObject[loop];
	oldRow = rowObject[loop+1];

	tableObject.tBodies[0].replaceChild(newRow, rowObject[loop+1]);
	tableObject.tBodies[0].insertBefore(oldRow, newRow);

	//  Add data to data array
	Update_Index((loop + 1), 1, 1, 0);
}
//!  Move Rows to the top
/**  function to move rows to the top*/
function Move_Top(tableObject, controlNumber)
{
	//  Variables
	var clusterTop;
	var clusterBottom;
	var packetTop;
	var packetBottom;
	var packetData = 0;
	var dataStart = 0;
	var dataStop = 0;
	var loop;
	var tableLength;
	
	//  Get the table length
	tableLength = tableObject.rows.length;
	
	//  Perform for all packets
	while(packetData == 0)
	{
		//  Get the top of the cluster
		for(loop = 0; loop < tableLength; loop++)
		{
			if(tableObject.rows[loop].className == 'Table_Control_Row_Unpressed_' + controlNumber || tableObject.rows[loop].className == 'Table_Control_Row_Unpressed_Alternate_' + controlNumber)
			{
				clusterTop = loop;
				break;
			}

			if(loop == (tableLength-1))
				return;
		}

		//  Get the bottom of the cluster
		for(loop = clusterTop; loop < tableLength; loop++)
		{
			if(tableObject.rows[loop].className == 'Table_Control_Row_Pressed_' + controlNumber)
			{
				clusterBottom = loop - 1;
				break;
			}

			if(loop == (tableLength-1))
				return;
		}

		//  Packet top
		packetTop = clusterBottom + 1;

		//  Get the packet bottom
		for(loop = packetTop; loop < tableLength; loop++)
		{
			if(tableObject.rows[loop].className == 'Table_Control_Row_Unpressed_' + controlNumber || tableObject.rows[loop].className == 'Table_Control_Row_Unpressed_Alternate_' + controlNumber)
			{
				packetBottom = loop-1;
				break;
			}

			if(loop == (tableLength - 1))
				packetData = 1;

			if(packetTop > 0 && loop == (tableLength - 1))
				packetBottom = (tableLength - 1);
		}

		//  Move the packet above the cluster
		retCode = Move_Packet(tableObject, tableLength, clusterTop, clusterBottom, packetTop, packetBottom);

	}
}

//!  Move packet
/**  function to move info to the top*/
function Move_Packet(tableObject, tableLength, clusterTop, clusterBottom, packetTop, packetBottom)
{
	//  Variables
	var loop;
	var packetArray = new Array(packetBottom - packetTop + 1);
	var packetSize;
	var clusterIndex;

	//  Set the packet size
	packetSize = packetArray.length;

	//  Populate the packet array to store the data and remove rows from the post data
	for(loop = 0; loop < packetSize; loop++)
	{
		packetArray[loop] = tableObject.rows[packetTop + loop];
		Update_Index(packetTop + loop, 1, 1, 1);
	}

	//  Set the cluster index
	clusterIndex = packetSize;

	//  Move the cluster down (as long as it is not at the top)
	if(clusterTop != 1)
	{
		for(loop = clusterBottom; loop > (clusterBottom - packetSize); loop--)
		{
			existingRow = tableObject.rows[loop];
			newRow = tableObject.rows[loop + clusterIndex];
			tableObject.tBodies[0].replaceChild(existingRow, newRow);
			clusterIndex--;
		}
	}
	//  Insert the packet back into the table
	for(loop = 0; loop < packetSize; loop++)
	{
		newElement = packetArray[loop];
		targetElement = tableObject.rows[clusterTop + loop];
		tableObject.tBodies[0].insertBefore(newElement, targetElement);
		Update_Index((clusterTop + loop), 1, 1, 0);
	}

}
