// Add children support to Gecko browsers
if(!document.documentElement.children)
{
	HTMLElement.prototype.__defineGetter__("children", function () {
		var children = [];
	
		for(var v=0; v < this.childNodes.length; v++)
			if(this.childNodes[v].nodeType == 1)
				children[children.length] = this.childNodes[v];
	
		return children;
	});
}

function sort(cellNode)
{
	var oTable = cellNode.parentNode.parentNode.parentNode;
	var oThead = oTable.tHead;
	var oTbody = oTable.tBodies[0];
	var oRow = cellNode.parentNode;

	// Table row and cell containing possible button
	var oButtonsRow = oThead.rows[oThead.rows.length-1];
	var oButtonCell = oButtonsRow.cells[cellNode.cellIndex];
	var oButton = oButtonCell.children[0];

	// Collection of buttons
	var colButtons = oButtonsRow.getElementsByTagName("img");
	
	// Add events so that text can not be selected
	if(!cellNode.onmousedown && !cellNode.onselectstart)
	{
		for(var i=0; i<oRow.cells.length; i++)
		{
			if(document.all) oRow.cells[i].onselectstart = function() { return false }
			else oRow.cells[i].onmousedown = function(e) { e.preventDefault(); }
		}	
	}
	
	// Set ascending attribute
	cellNode.ascending = (!cellNode.ascending)? true:false;

	// Set other cells ascending value to false
	for(var j=0; j<oRow.cells.length; j++)
	{
		if(oRow.cells[j] != cellNode)
		{
			oRow.cells[j].ascending = false;
		}
	}

	// If table has arrow buttons
	if(oButton.nodeName == "IMG")
	{
		oButton.src = (cellNode.ascending)? "/ci/images/button/arrow_asc.gif":"/ci/images/button/arrow_desc.gif";
		
		// Set all other buttons to unsorted except the current one
		for(var i=0; i<colButtons.length; i++)
		{
			if(colButtons[i] != oButton)
			{
				colButtons[i].src = "/ci/images/button/arrow_unsorted.gif";
			}
		}
	}

	var arrTableData = [];
	var node;

	for(var i=0; (node = oTbody.rows[i]); i++)
	{
		arrTableData[i] = [];
		// betbjim 19.10.06: 
		// Not every row (<tr>) has so many cell (<td>) as the cell number of the current row!
		// It is possible to have something like <td collspan="15">  
		if(node.cells.length - 1 < cellNode.cellIndex){
			arrTableData[i][0] = node.cells[0].innerHTML;
			arrTableData[i][1] = node;
		} else {
			arrTableData[i][0] = node.cells[cellNode.cellIndex].innerHTML;
			arrTableData[i][1] = node;
		  }
	}

	// Reorganize date so it can be sorted
	if(cellNode.getAttribute("colType") == "date")
	{
		var regExp = /\d+/g;
		var arrTmp = [];
		
		for(var v=0; v<arrTableData.length; v++)
		{
			arrTmp = arrTableData[v][0].match(regExp);
			arrTableData[v][0] = arrTmp[2] + arrTmp[1] + arrTmp[0];
		}
	}

	// If number field remove text to sort
	if(cellNode.getAttribute("colType") == "number")
	{
		var regExp = /\d+/;
		var arrTmp = [];
		
		for(var i=0; i<arrTableData.length; i++)
		{
			arrTmp = arrTableData[i][0].match(regExp);
			arrTableData[i][0] = arrTmp;
		}
	}
	
	if(cellNode.ascending)
	{
		if(cellNode.getAttribute("colType") == "number") arrTableData.sort(compare);
		else arrTableData.sort();
	}
	else arrTableData.reverse();
	
	for(var j=0; j<arrTableData.length; j++)
	{
		// Correct background colors for rows
		var bgColor = (j % 2)? "#eeeeee":"";
		arrTableData[j][1].bgColor = bgColor;
		
		oTbody.appendChild(arrTableData[j][1]);	
	}
	
	
}

// Sort function for numbers
function compare(a, b)
{
	return a[0] - b[0];
}