/**
MB_TableSorter class. Author: Manos Batsis, mailto:xcircuit@yahoo.com
main methods:
	sortOn(column to base the sort, button/header matching the column)
	setSortFuncs(array of inegers. used to set the sort function for each column: 0 for default, 1 for case-insensitive, 2 for date (US), 3 for date (european))
	autoCalled: readTable(table object) reads a table
	updateTblCellsTxt(table object) updates the contents of the table	
*/

//-----------------------------------------------------------------------------

var gaTableSorterArray;
var goTableSorter; // Our tableSorter Object
var giPageCount = 1;
var giTableWidth;
var ciPageSize = 100;
var giPageSize = ciPageSize;

//--------------------------------------------------

//Call all functions to initialize a paged table
function initTable(lsTable, liPageSize, liShowColumnIndex, liMasterColumnIndex, lsMasterColumnFilter, lbDontPageFirst, lbDontSkipRowOne)
{
	var loTable;
	if (!(gaTableSorterArray && gaTableSorterArray[lsTable]))
	{
		loTable = getElement(lsTable);
		if (loTable)
		{

			if (liPageSize==''){liPageSize=ciPageSize}
			if (liPageSize)
			{
				giPageSize = ciPageSize = liPageSize;
			}
			goTableSorter = initTableSorter(loTable, liShowColumnIndex, liMasterColumnIndex, lsMasterColumnFilter, lbDontSkipRowOne);

			if (goTableSorter.table && goTableSorter.table.offsetWidth && !goTableSorter.table.width)
			{
				giTableWidth = goTableSorter.table.width;
				goTableSorter.table.width = giTableWidth;
			}

			//call paging function to initialize page size
			if (!lbDontPageFirst)
			{
				goTableSorter.pageTable(1, giPageSize, null, 1);
			}
			
			if (loTable.offsetWidth)
			{
				//goTableSorter.hideColumn(loTable, 0);
			}
			
			pageCounters('elPageCount');

			// add it to the array
			if (!gaTableSorterArray)
			{
				gaTableSorterArray = new Array()
			}
			gaTableSorterArray[lsTable] = goTableSorter;


		}//(loTable)
	}//(!(gaTableSorterArray && gaTableSorterArray[lsTable]))
} // initTable()

//--------------------------------------------------

function initTableSorter(lsElem, liShowColumnIndex, liMasterColumnIndex, lsMasterColumnFilter, lbDontSkipRowOne)// oElem is the target table of the document; our tableSorter will deal with this table
{
	var oElem, lbSkipRowOne;
	oElem = getElement(lsElem);
	if (oElem)
	{
		lbSkipRowOne = lbDontSkipRowOne ? 0 : 1;
		goTableSorter = new MB_TableSorter(oElem, lbSkipRowOne);// 1 means "omit 1st row (headers)"
		
		goTableSorter.ShowColumnIndex = liShowColumnIndex;
		goTableSorter.MasterColumnIndex = liMasterColumnIndex;
		goTableSorter.MasterColumnFilter = lsMasterColumnFilter;
		
		goTableSorter.setSortFuncs([0,0,1,0,4]);// use the case insensitive sorting for all columns

	}
	return (goTableSorter);
	//sortTable(0, document.getElementsByTagName("button")[0]);// do first sort
}//initTableSorter

//-----------------------------------------------------------------------------

//generic function to get a field
function getElement(loField)
{
	if (loField && typeof(loField) == 'string') //loField instanceof String)
	{
		loField = document.getElementById(loField);
	}
	return (loField)
} // getElement()

//--------------------------------------------------

//display the correct number of page counters for data set & page size
function pageCounters(loTable)
{
	var laCells, lsRowDisplay, liFirstRecord, liCellCount, liIndex;
	var liTableWidth
	loTable = getElement(loTable);
	if (loTable)
	{
		laCells = loTable.getElementsByTagName('td');
		lsRowDisplay = document.all ? 'block' : 'table-row';
		liCellCount = laCells.length;
		for (liIndex = 1; liIndex < liCellCount; liIndex++)
		{
			laCells[liIndex].style.display = 'none';
		}
		liTableWidth = loTable.width || loTable.offsetWidth;
		for (liIndex = 1; liIndex < liCellCount; liIndex++)
		{
			lbShowRecord = (liIndex <= Math.ceil(liCellCount * ciPageSize / giPageSize) - 1
				&& (!liTableWidth || loTable.offsetWidth < liTableWidth - 10))
			laCells[liIndex].style.display = lbShowRecord ? lsRowDisplay : 'none';
		}
	}
} // pageCounters()

//--------------------------------------------------

function DateMonthToNum(lsMonth)
{
	var laMonths = new Array("","jan","feb","mar","apr","may","jun", "jul", "aug", "sep", "oct", "nov", "dec")
	var liCount
	lsMonth = lsMonth.toLowerCase()

	for (liCount = 1; liCount <= 12; liCount++)
	{
		if(lsMonth == laMonths[liCount])
			return liCount
	}
	
	// if a number is passed in then the same val will be passed out
	return lsMonth
}

//--------------------------------------------------

function ShowPagingNumbers(lsTable, liCurrentPage)
{
	ShowPagingNumbers2(lsTable, liCurrentPage, 0)
}


function ShowPagingNumbers2(lsTable, liCurrentPage, liMaxPage)
{
	var	liShowMin, liShowMax
	var liCount, loCell
	var lsCellShow = document.all ? 'block' : 'table-cell';


	

	if (liMaxPage == 0 && gaTableSorterArray != null)
	{
		liMaxPage = Math.floor(gaTableSorterArray[lsTable].recordCount/gaTableSorterArray[lsTable].pageSize) +1
	}
	
	liShowMin =  liCurrentPage - 15
	liShowMax = liCurrentPage + 15


	// if you are near the end of the paging, start earlier
	if (liShowMax > liMaxPage)
	{
		liShowMin = liShowMin - (liShowMax - liMaxPage)
	}
	
	// if you are near the start of the paging, show more paging 
	if (liShowMin < 1)
	{
		liShowMax = liShowMax + Math.abs(liShowMin)
	}

	for (liCount = 1; liCount <= liMaxPage;liCount++)
	{
		loCell = getElement(lsTable + "Paging" + liCount)
	
		if (loCell)
		{
			if (liCount < liShowMin
				|| liCount > liShowMax)
			{
				loCell.style.display = 'none';
			}
			else
			{
				loCell.style.display = lsCellShow;
			}
			
			if (liCount == liCurrentPage)
			{
				loCell.className = 'PagingHighlight';
			}
			else
			{
				loCell.className = 'PagingNormal';
			}
		
		} //loCell
	
	}//for

	loCell = getElement(lsTable + "Paging" + liCurrentPage);
	if (loCell != null)
	{
		loCell.className = 'PagingHighlight';
		loCell.style.display = lsCellShow;
	}
	
	
	

}//ShowPagingNumbers

//--------------------------------------------------

//call both functions when page size changes
function changePageSize(loTable, liPageSize)
{
	var loTableSorter;
	if (typeof(lsTable) == 'string')
	{
		loTableSorter = gaTableSorterArray[lsTable];
		loTable = getElement(lsTable);
	}
	if (!loTableSorter)
	{
		loTableSorter = goTableSorter;
	}
	loTableSorter.pageSize = liPageSize ? Number(liPageSize) : loTableSorter.pageSize;
	loTableSorter.pageTable();
	pageCounters('elPageCount');
} // changePageSize()

//--------------------------------------------------

function columnEvent(liFieldIndex, lsTable)
{
/*
	loCell = getElement(lsTable + "Paging" + liCurrentPage);
	loCell.style.display = lsCellShow;
	if (loCell != null)
	{
		loCell.className = 'PagingHighlight';
	}
*/

	var loTableSorter;
	if (typeof(lsTable) == 'string')
	{
		loTableSorter = gaTableSorterArray[lsTable];
	}
	
	//alert(loTableSorter);
	
	if (!loTableSorter)
	{
		loTableSorter = goTableSorter;
	}
	if (loTableSorter)
	{
		if(window.event && event.ctrlKey)
		{
			loTableSorter.hideColumn('rsTable', liFieldIndex)
		}
		else
		{
			if (loTableSorter != null)
			{
			//alert(liFieldIndex);
				loTableSorter.sortTable(liFieldIndex, lsTable);
			}
		}
	}
} // columnEvent()

//--------------------------------------------------

function MB_TableSorter(loSortTable, skipRowOne)
{
	this.skipRowOne = (skipRowOne && skipRowOne == 1) ?  1 : 0;
		
	if (loSortTable instanceof Array)
	{
		this.matrix = loSortTable;
	}
	else
	{
		this.matrix = this.readTable(loSortTable);
	}
	if (this.matrix && this.matrix.length)
	{
		this.table = loSortTable;
		this.oDataElem = document.createElement("tbody");
		this.lastColIndex = -1;
		this.currentColIndex = -1;
		this.arrSortFuncs = new Array();
		this.arrowDirection = null;
		this.pageSize = ciPageSize;
		this.pageCount = 1;
		this.lastPage = Math.ceil(this.matrix.length / this.pageSize);
		this.tableWidth = null;
	}
}//MB_TableSorter

//--------------------------------------------------

// sort table by column index
MB_TableSorter.prototype.sortTable = function(iColIndex, oElem)
{
	var lsOriginalStyle, loParentElement, loTableSorter;
	if (window.event && event.srcElement)
	{
		event.srcElement.originalCursor = event.srcElement.style.cursor;
		event.srcElement.style.cursor = 'wait';
		loParentElement = event.srcElement.parentElement;
		while (loParentElement)
		{
			loParentElement.originalCursor = event.srcElement.style.cursor;
			loParentElement.style.cursor = 'wait';
			loParentElement = loParentElement.parentElement;
		}
	}
	window.document.body.style.cursor = 'wait';
	//alert('window.document.body.style.cursor: ' + window.document.body.style.cursor)
	this.sortOn(iColIndex, oElem);
	this.pageTable(1);
	this.updateTblCellsTxt(this.table);
	if (window.event && event.srcElement)
	{
		event.srcElement.style.cursor = event.srcElement.originalCursor;
		loParentElement = event.srcElement.parentElement;
		while (loParentElement)
		{
			loParentElement.style.cursor = event.srcElement.originalCursor;
			loParentElement = loParentElement.parentElement;
		}
	}
	window.document.body.style.cursor = 'auto';
}//sortTable

//-----------------------------------------------------------------------------

MB_TableSorter.prototype.previousPage = function()
{
	var liPage
	liPage = Math.max(this.pageCount-1,1)
	this.pageTable(liPage)
}
//--------------------------------------------------

MB_TableSorter.prototype.nextPage = function()
{
	var liPage, liMaxPage
	liMaxPage = Math.floor(this.recordCount/this.pageSize) +1
	liPage = Math.min(this.pageCount+1,liMaxPage)
	this.pageTable(liPage)
}
//--------------------------------------------------

//display rows matching page size for input page number
MB_TableSorter.prototype.pageTable = function(liPageCount, liPageSize, liHeaderRowsCount, lbFirstScan)
{
	var loRow, laRows, lsRowDisplay, loCell;
	var liIndex, lsCellValue, liFirstRecord, lbShowRecord, lbShowAllRecords, liRecordCount, liFinalRecord;
	var laMultipleRows;
	var loReMasterCol;
	var lbWouldShowRecord
	//this.table = getElement(this.table);
	if (this.table)
	{
		laMultipleRows = [];
		lsRowDisplay = document.all ? 'block' : 'table-row';
		liHeaderRowsCount = liHeaderRowsCount ? liHeaderRowsCount : 1;
			
		if (typeof(liPageCount) == 'undefined' || !liPageCount)
		{
			this.pageCount = Math.min(this.pageCount + 1, Math.ceil(this.recordCount/this.pageSize));
		}
		else if (liPageCount < 1)
		{
			this.pageCount = this.pageCount + liPageCount;
		}
		else
		{
			this.pageCount = liPageCount;
		}
		this.pageSize = liPageSize ? liPageSize : this.pageSize;
		laRows = this.table.getElementsByTagName('tr');
		liRecordCount = laRows.length;
		//show all records if page size > record count
		lbShowAllRecords = this.pageSize > liRecordCount;
		
		// find index of first record to show
		if (!lbShowAllRecords)
		{
			liFirstRecord = Math.max(0, Math.max(this.pageCount - 1, 0) * this.pageSize);
		}
		
		liFinalRecord = liFirstRecord + this.pageSize;
		
		// create regular expression for matching master column
		//loReMasterCol = new RegExp(this.MasterColumnFilter, "i");
		
		//show records from first onwards for page size records
		for (liIndex = liHeaderRowsCount; liIndex < liRecordCount; liIndex++)
		{
			if (laRows[liIndex])
			{
				lbShowRecord = (lbShowAllRecords || (liFirstRecord < liIndex && liIndex <= liFinalRecord));
				
				// decide whether to show a record based on filter
				// this is always checked even if it wouldnt display to move the page bondaries
				if (!lbFirstScan && this.MasterColumnIndex && liIndex <= liFinalRecord)
				{
					// init 'would show record' boolean to ensure it has a value
					lbWouldShowRecord = true;
					
					loCell = laRows[liIndex].getElementsByTagName('td')[this.MasterColumnIndex];
					
					//hide/show rows other than the master row
					//lsCellValue = loCell.innerHTML.replace('^\s*(.*)(&nbsp;)?(.*)\s*$', '$1$3').toLowerCase();
					lsCellValue = loCell.innerHTML.replace('&nbsp;', '').toLowerCase();
					if (lsCellValue == this.MasterColumnFilter) // loReMasterCol.test(lsCellValue)
					{
						loCell = laRows[liIndex].getElementsByTagName('td')[this.ShowColumnIndex];
						laMultipleRows[lsCellValue] = false;
					}
					else
					{
						if (this.ShowColumnIndex != null )
						{
							loCell = laRows[liIndex].getElementsByTagName('td')[this.ShowColumnIndex];
							if (loCell)
							{
								// see if there are other rows matching the current row's 'show' column (assumes they are in order at the moment)
								// if not, dont hide it 
								lsCellValue = loCell.innerHTML.replace('&nbsp;', '');
								if (typeof(laMultipleRows[lsCellValue]) == 'undefined')
								{
									if (laRows[liIndex + 1])
									{
										loCell = laRows[liIndex + 1].getElementsByTagName('td')[this.ShowColumnIndex];
										lbWouldShowRecord = (lsCellValue != loCell.innerHTML.replace('&nbsp;', ''));
										laMultipleRows[lsCellValue] = lbWouldShowRecord;
									}
									else
									{
										lbWouldShowRecord = true;
									}
								}
								else
								{
									lbWouldShowRecord = laMultipleRows[lsCellValue];
								}
							} //(loCell)
						} //(this.ShowColumnIndex)
						
					} // loReMasterCol.test ....
					
					if (!lbWouldShowRecord)
					{
						liFinalRecord ++;
						if (lbShowRecord)
						{
							lbShowRecord = false;
						}
						else
						{
							liFirstRecord ++;
						}
					}
					
				} // this.MasterColumnIndex ...
				
				// actual show/hide of row 
				laRows[liIndex].style.display = lbShowRecord ? lsRowDisplay : 'none';
				
			} // laRows[liIndex]
		} // for (liIndex ...
	}
} // pageTable()

//--------------------------------------------------

//display rows matching page size for input page number
MB_TableSorter.prototype.showRow = function(liRowIndex)
{
	var laRows, lsRowDisplay;
	//this.table = getElement(this.table);
	if (this.table)
	{
		lsRowDisplay = document.all ? 'block' : 'table-row';

		laRows = this.table.getElementsByTagName('tr');
		if (laRows && laRows[liRowIndex])
		{
			laRows[liRowIndex].style.display = laRows[liRowIndex].style.display == 'none' ? lsRowDisplay : 'none';
		}
	}
} // pageTable()

//--------------------------------------------------

//display rows matching page size for input page number
//to call, use (eg.) gaTableSorterArray['yourtable'].showRowFilter(null, "IT & Internet")
MB_TableSorter.prototype.showRowFilter = function(liShowColumnIndex, lsShowColumnFilter, liMasterColumnIndex, lsMasterColumnFilter)
{
	var laRows, lsRowDisplay, loCell, loMasterCell, loRe, lbHideRow, lsMasterCellValue;
	//this.table = getElement(this.table);
	this.MasterColumnFilter = lsMasterColumnFilter ? lsMasterColumnFilter : this.MasterColumnFilter;
	if (this.table && lsShowColumnFilter)
	{
		this.ShowColumnIndex = liShowColumnIndex ? liShowColumnIndex : this.ShowColumnIndex;
		this.ShowColumnFilter = lsShowColumnFilter;
		this.MasterColumnIndex = liMasterColumnIndex ? liMasterColumnIndex : this.MasterColumnIndex;
		lsRowDisplay = document.all ? 'block' : 'table-row';

		laRows = this.table.getElementsByTagName('tr');
		if (laRows)
		{
			//loRe = new RegExp(this.MasterColumnFilter, "i");
			
			//show records from first onwards for page size records
			for (liIndex = 0; liIndex < laRows.length; liIndex++)
			{
				if (laRows[liIndex])
				{
					loCell = laRows[liIndex].getElementsByTagName('td')[this.ShowColumnIndex];
					if (loCell)
					{
						// find all rows matching the filter
						//loRe = new RegExp(this.ShowColumnFilter, "i");
						if (loCell.innerHTML.toLowerCase() == this.ShowColumnFilter)
						{
							loMasterCell = laRows[liIndex].getElementsByTagName('td')[this.MasterColumnIndex];
							lsMasterCellValue = loMasterCell.innerHTML.replace('&nbsp;', '').toLowerCase();
							
							//hide/show rows other than the master row
							if (lsMasterCellValue != this.MasterColumnFilter)
							{
								if (typeof(lbHideRow) == 'undefined')
								{
									lbHideRow = laRows[liIndex].style.display;
								}
								laRows[liIndex].style.display = laRows[liIndex].style.display == 'none' ? lsRowDisplay : 'none';
							}
						}
					}
				}
			}
		}
	}
} // showRowFilter()

//--------------------------------------------------

//display rows associated with a master column 
MB_TableSorter.prototype.showHideChildren = function(lsShowFilter, liType) 
{ 

		//liType 1-show 2-hide
		
        var laRows, lsRowDisplay, loCell, loMasterCell, lsDisplayType
        var liIndex 

        //handle IE/firefox etc 
        lsRowDisplay = document.all ? 'block' : 'table-row'; 

        //this.table = getElement(this.table); 
        if (this.table && lsShowFilter != null) 
        { 

			laRows = this.table.getElementsByTagName('tr'); 
			if (laRows) 
			{ 
				//loop through and find the rows with the master attribute that matches the one passed in and hide them 
				for (liIndex = 0; liIndex < laRows.length; liIndex++) 
				{ 
					if (laRows[liIndex].getAttribute("ShowID") == lsShowFilter) 
					{ 

						// decide if its gonna be shown or hidden, if specified then do that, else do the opposite of what it currently is
						if (liType)
						{
							switch(liType)
							{
								case 1:
									lsDisplayType = lsRowDisplay
									break;
								case 2:
									lsDisplayType = 'none'
									break;
							} //switch
						}
						else
						{
							if (laRows[liIndex].style.display == 'none') 
							{ 
								lsDisplayType = lsRowDisplay 
							} 
							else 
							{ 
								lsDisplayType = 'none' 
							}
						}//else liType

						laRows[liIndex].style.display = lsDisplayType
 

					}//(laRows[liIndex].getAttribute("ShowID") = lsShowFilter) 
				} //for 

			}//(laRows) 
        }//(this.table && lsShowFilter != null) 
} // showHideChildren() 



//--------------------------------------------------

//display rows matching page size for input page number
MB_TableSorter.prototype.letterFilter = function(lsPrefixChar, liColumnIndex)
{
	var loRow, laRows, lsRowDisplay;
	var liIndex, lbShowRecord;
	var lsCellText
	var loRe;
	
	if (lsPrefixChar && this.table)
	{
		loRe = new RegExp("^" + lsPrefixChar, "i")

		laRows = this.table.getElementsByTagName('tr');
		lsRowDisplay = document.all ? 'block' : 'table-row';
		for (liIndex = 1; liIndex < laRows.length; liIndex++)
		{
			lsCellText = laRows[liIndex].getElementsByTagName('td')[liColumnIndex].innerHTML;
			lbShowRecord = (loRe.test(lsCellText))
			laRows[liIndex].style.display = lbShowRecord ? lsRowDisplay : 'none';
		}
	}
} // letterFilter()

//--------------------------------------------------

//hide column from a table
MB_TableSorter.prototype.hideColumn = function(liColumn, lsColumn)
{
	var loCell, laRows, laCells;
	var liIndex, liFirstRecord;
	var loRe; 
	if (this.table)
	{
		laRows = this.table.getElementsByTagName('tr');
		laCells = laRows[0].getElementsByTagName('td');
		if (!laCells.length)
		{
			laCells = laRows[0].getElementsByTagName('th');
		}
		if (lsColumn)
		{
			lsColumn = lsColumn.replace(' ', '.*');
			loRe = new RegExp(lsColumn, 'i');

			for (liIndex = 0; liIndex < laCells.length; liIndex++)
			{
				if (loRe.test(laCells[liIndex].innerHTML))
				{
					liColumn = liIndex;
				}
			}
		}
		if (liColumn)
		{
			loCell = laCells[liColumn];
			this.table.width = this.table.offsetWidth - loCell.offsetWidth;
			for (liIndex = 0; liIndex < laRows.length; liIndex++)
			{
				loCell = laRows[liIndex].getElementsByTagName('td')[liColumn] || laRows[liIndex].getElementsByTagName('th')[liColumn];
				if (loCell)
				{
					loCell.style.display = 'none';
				}
			}
		}
	}
} // hideColumn()

//--------------------------------------------------

MB_TableSorter.prototype.sortOn = function(iColIndex, clickedElem)
{
	var iFunc, iValue;
	var laSortedArray, laSortColumn ;
	var datA;
	
	this.currentColIndex = iColIndex;
	laSortColumn = new Array(this.matrix.length);


	for (var i=0;i<laSortColumn.length;i++)
	{
		iValue = this.matrix[i][iColIndex];
		//alert('iValue: ' + iValue);
		laSortColumn[i] = iValue + "_mbns_" + i;
		if (typeof(iFunc) == 'undefined' && iValue)
		{
			if (iValue.match(/^\W?[\d,\.]*$/)) 
			{
				// number
				iFunc = 0;
				//break;
			}
			else if (iValue.match(/^\d{1,2}[\\\/\-\s][\d\w]{1,3}[\\\/\-\s,]*\d{1,4}(\s*(\d{1,2}[\.\:]){2}\d{1,2})?$/))
			{
				iValue = iValue.replace(/([\\\/\-\s,]{1,2})(\d{2})([^\\\/\-\w]|$)/, '$120$2');
				datA = new Date();
				datA.setTime(Date.parse(iValue))
				// date
				iFunc = 2;
				//break;
			}
			else if (iValue)
			{
				// text
				iFunc = 1;
				//break;
			}
			//alert('iFunc : ' + iFunc );
		}
	} // for
	
	
	if (this.lastColIndex == iColIndex)
	{
		this.arrowDirection = !(this.arrowDirection);
		laSortColumn.reverse();
	}
	else
	{
		this.arrowDirection = true;
		if (typeof(iFunc) == 'undefined')
		{
			iFunc = (iFunc || this.arrSortFuncs[iColIndex]);
		}

		switch (iFunc)
		{
			case 0 : //number
				laSortColumn.sort(this.floatFunc);
				break;
			case 1 : //text
				laSortColumn.sort(this.noCaseFunc);
				break;
			case 2 : //date
				laSortColumn.sort(this.dateFunc);
				break;
			case 3 : //date
				laSortColumn.sort(this.dateEUFunc);
				break;
			case 4 : 
				laSortColumn.sort(this.floatFunc);
				break;
			default : //text
				laSortColumn.sort(this.noCaseFunc);
		}

		// reverse or normal
		if (iFunc != 1) 
		{
			laSortColumn.reverse();
		}
	}
	
	
	
	
	this.fixArrows(clickedElem);
	
	// build the new array
	laSortedArray = new Array(this.matrix.length);
	for(var j=0; j < laSortedArray.length; j++)
	{
		// loop through and grab the correct row out of the matrix, based upon laSotColumn
		var iRow = laSortColumn[j].substring(laSortColumn[j].indexOf("_mbns_")+6, laSortColumn[j].length);
		laSortedArray[j] = new Array();
		for(var k=0; k < this.matrix[j].length; k++)
		{
			laSortedArray[j][k] = this.matrix[iRow][k];
		}
	}
	this.matrix = laSortedArray;
	this.lastColIndex = iColIndex;
}//sortOn

//------------------------------------------------------------------------

MB_TableSorter.prototype.setSortFuncs = function(arr)
{
	for(var i=0;i<arr.length;i++)
		this.arrSortFuncs[i] = arr[i];
}//setSortFuncs

//------------------------------------------------------------------------

MB_TableSorter.prototype.noCaseFunc = function(a, b)
{
  var strA = a.toLowerCase().replace(/\s*/,''),
      strB = b.toLowerCase().replace(/\s*/,'');
  if(strA < strB) return -1;
  else if(strA == strB) return 0;
  else return 1;
}//noCaseFunc

//------------------------------------------------------------------------

MB_TableSorter.prototype.floatFunc = function(a, b)
{
	var iResult;
	//alert('a: ' + a);
	//a = a.replace(/[^\d\.]/g,'');
	//b = b.replace(/[^\d\.]/g,'');
	var fA = parseFloat(cleanFloat(a)),
		fB = parseFloat(cleanFloat(b));
	//alert('a: ' + a);
	if (isNaN(fA))
	{
		fA = -999999;
	}
	if (isNaN(fB))
	{
		fB = -999999;
	}
	if (fA < fB) iResult = -1;
	else if (fB == fA) iResult = 0;
	else iResult = 1;
	
	/*
	alert('fA: ' + (fA) + ' fB: ' + (fB) + ' iResult: ' + (iResult)
		 + ' fA < fB: ' + (fA < fB) + ' fB > fA: ' + (fB > fA));
	*/
	return (iResult);
}//floatFunc

//------------------------------------------------------------------------

MB_TableSorter.prototype.dateFunc = function (a, b)
{

	var ldDateA, ldDateB, laDateA, laDateB 


	// split the date on dash then slash
	laDateA = a.substring(0, a.lastIndexOf("_mbns_")).split("-")
	laDateB = b.substring(0, b.lastIndexOf("_mbns_")).split("-")
	
	if (laDateA.length == 1)
		laDateA = a.substring(0, a.lastIndexOf("_mbns_")).split("/")

	if (laDateB.length == 1)
		laDateB = b.substring(0, b.lastIndexOf("_mbns_")).split("/")
	
		

	
	ldDateA = new Date(laDateA[2], DateMonthToNum(laDateA[1]), laDateA[0]); 
	ldDateB = new Date(laDateB[2], DateMonthToNum(laDateB[1]), laDateB[0]);		



	
	if(ldDateA < ldDateB)	
		return -1;
	else if(ldDateA > ldDateB) 
		return 1;
	else return 0;

}//dateFunc

//------------------------------------------------------------------------

MB_TableSorter.prototype.dateEUFunc = function(a, b)
{
	var strA = a.substring(0, a.lastIndexOf("_mbns_")).split("/"), 
		strB = b.substring(0, b.lastIndexOf("_mbns_")).split("/"),
		datA = new Date(strA[2], strA[1], strA[0]), 
		datB = new Date(strB[2], strB[1], strB[0]);
	if(datA < datB) return -1;
	else if(datA > datB) return 1;
    else return 0;
}//dateEUFunc

//------------------------------------------------------------------------

function cleanFloat(lsVal)
{
	return lsVal.replace(/(^\W*|[,])/g,'')

}
//------------------------------------------------------------------------
// Load all data from table into array and return array
MB_TableSorter.prototype.readTable = function(oElem)
{
	var oData, iValue;
	if(oElem.nodeName != "tbody")
		oElem = oElem.getElementsByTagName("tbody")[0];
	if(!oElem)
		return
	var iRows = oElem.getElementsByTagName("tr");
	this.recordCount = iRows.length - this.skipRowOne;
	var laTableData = new Array();
	
	for(var i=0; i+this.skipRowOne < iRows.length; i++)
	{
		laTableData[i] = new  Array();
		var iCols = iRows[i+this.skipRowOne].getElementsByTagName("td");
		for(var j=0; j < iCols.length; j++)
		{
			laTableData[i][j] = iCols[j].innerHTML.replace(/(^\s*|\s*$)/g,'')
			
			// check & clean data before loading (remove images)
			// AMJ: stopped this code running as we want HTML tags etc to be passed
			if (false && iCols[j].childNodes[0].nodeName.match(/text/)  )
			{
				oData = iCols[j].childNodes[0] ? iCols[j].childNodes[0].data : 0;

				if (oData.match(/^\W?(\d{1,3}[,\.]?)+$/))
				{
					oData = oData.replace(/[^\d,.]*/g, '');
				}
				else if (!oData.match(/[<>]/))
				{
					laTableData[i][j] = oData.replace(/<[^>]*>/g, '');
				}

				laTableData[i][j] = oData;
			}

		} //for j
	
		//now add OnClick and onMouseOver
		laTableData[i][j] = iRows[i+this.skipRowOne].onclick
		laTableData[i][j+1] = iRows[i+this.skipRowOne].onmouseover

	} //for i

	return laTableData;	
}//readTable

//------------------------------------------------------------------------

MB_TableSorter.prototype.updateTblCellsTxt = function(oElem)
{
	if(oElem.nodeName != "tbody")
		oElem = oElem.getElementsByTagName("tbody")[0];
	if(!oElem)
		return
	var iRows = oElem.getElementsByTagName("tr");

	for(var i=0; i+this.skipRowOne < iRows.length; i++)
	{
		var iCols = iRows[i+this.skipRowOne].getElementsByTagName("td");
		for(var j=0; j < iCols.length; j++)
		{
			//iCols[j].childNodes[0].data = this.matrix[i][j];
			// we use innerHTML now as data does not allow <input> tags etc 
			iCols[j].innerHTML = this.matrix[i][j];
		} //for j

		// now update the onClick and the onMouseOver
		iRows[i+this.skipRowOne].onclick = this.matrix[i][j]
		iRows[i+this.skipRowOne].onmouseover = this.matrix[i][j+1]



	} //for i



}//updateTblCellsTxt

//------------------------------------------------------------------------

MB_TableSorter.prototype.toTbody = function()
{
	for(i=0;i<this.matrix.length;i++)
	{
		var tTr = document.createElement('tr');
		
		tTr.setAttribute("title", this.matrix[i][0]);
		for(var j=0;j<this.matrix[0].length;j++)
		{
			var tTd  = document.createElement('td');
			tTd.style.width = "30%";
			var tTxt = document.createTextNode(this.matrix[i][j]);
			tTd.appendChild(tTxt);
			tTr.appendChild(tTd);
		}		
		this.oDataElem.appendChild(tTr);
		document.getElementsByTagName("textarea")[0].value = this.oDataElem.innerHTML;
	}
}//toTbody

//------------------------------------------------------------------------

MB_TableSorter.prototype.appendTo = function(containerId, replacableId)
{
	var exElem = document.getElementById(replacableId);
	var sId = exElem.getAttribute("id");
	exElem.removeAttribute("id");
	this.oDataElem.setAttribute("id", sId);
	document.getElementById(containerId).replaceChild(this.oDataElem, exElem);
	this.oDataElem = document.createElement("tbody");
}//appendTo

//------------------------------------------------------------------------

MB_TableSorter.prototype.fixArrows = function(oElem)
{
	try
	{
		if(oElem)
		{
			if(this.lastColIndex != (-1))
			{
				oElem.parentNode.getElementsByTagName(oElem.nodeName)[this.lastColIndex].getElementsByTagName("img")[0].src = "MB_TableSorter/trans9x6.gif";
			}
			oElem.getElementsByTagName("img")[0].src = "MB_TableSorter/arrow" + this.arrowDirection + ".gif";
		}
	}
	catch (e)
	{
	}
}//fixArrows

//-----------------------------------------------------------------------------

