/************************************************************************************************************
(C) www.dhtmlgoodies.com, November 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/
	
var tableWidget_okToSort = true;
var tableWidget_arraySort = new Array();
tableWidget_tableCounter = 1;
var activeColumn = new Array();
var currentColumn = false;
var curColArr = new Array();

function sortNumeric(a,b)
{
	a = a.replace(/,/,'.');
	b = b.replace(/,/,'.');
	a = a.replace(/[^\d\.\/]/g,'');
	b = b.replace(/[^\d\.\/]/g,'');
	if(a.indexOf('/')>=0)a = eval(a);
	if(b.indexOf('/')>=0)b = eval(b);
	//if ((a/1 - b/1)==0) alert(a/1 - b/1);
	return a/1 - b/1;
}


function sortString(a, b) 
{
  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
  return 0;
}

function funhead(obj)
    {
    var ltd_cells=obj.getElementsByTagName("td");;
   for (var i=0; i<ltd_cells.length; i++)
    if (ltd_cells[i].className=='highlightedColumn') ltd_cells[i].className='td';
    } 

function sortTable()
{
	if(!tableWidget_okToSort)return;
	tableWidget_okToSort = false;
	/* Getting index of current column */
	var obj = this;
	var indexThis = 0;
	while(obj.previousSibling)
	{
		obj = obj.previousSibling;
		if(obj.tagName=='TD')indexThis++;		
	}
	
	if(this.getAttribute('direction') || this.direction)
	{
		direction = this.getAttribute('direction');
		if(navigator.userAgent.indexOf('Opera')>=0)direction = this.direction;
		if(direction=='ascending')
		{
			direction = 'descending';
			this.setAttribute('direction','descending');
			this.direction = 'descending';	
		}else{
			direction = 'ascending';
			this.setAttribute('direction','ascending');		
			this.direction = 'ascending';		
		}
	}else{
		direction = 'descending';
		this.setAttribute('direction','descending');
		this.direction = 'descending';
	}
	
	var tableObj = this.parentNode.parentNode.parentNode;
	var tBody = tableObj.getElementsByTagName('TBODY')[0];
	var tHead = tableObj.getElementsByTagName('THEAD')[1];
	
	var widgetIndex = tableObj.getAttribute('tableIndex');
	if(!widgetIndex)widgetIndex = tableObj.tableIndex;
	
	if(currentColumn && currentColumn.id.indexOf('col' + widgetIndex + '_')==0)
	{
		currentColumn.className='';
	}
	
	document.getElementById('col' + widgetIndex + '_' + (indexThis+1)).className='highlightedColumn';
	currentColumn = document.getElementById('col' + widgetIndex + '_' + (indexThis+1));
	funhead(tBody);
		
	var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String
	if(activeColumn[widgetIndex] && activeColumn[widgetIndex]!=this)
	{
		if(activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction');			
	}

	activeColumn[widgetIndex] = this;
	
	var cellArray = new Array();
	var cellObjArray = new Array();
	for(var no=1;no<tableObj.rows.length;no++)
	{
		var content= tableObj.rows[no].cells[indexThis].innerHTML+'';
		cellObjclassn = tableObj.rows[no].cells[indexThis].className;
		if (cellObjclassn!='th' && cellObjclassn!='th1' && cellObjclassn!='th2')
		{
    		cellArray.push(content);
    		cellObjArray.push(tableObj.rows[no].cells[indexThis]);
		}
	}
	
	if(sortMethod=='N'){
		cellArray = cellArray.sort(sortNumeric);
	}else{
		cellArray = cellArray.sort(sortString);
	}
	
	if(direction=='descending'){
		i=0;
		for(var no=cellArray.length;no>=0;no--){
			for(var no2=0;no2<cellObjArray.length;no2++){
				if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
					cellObjArray[no2].setAttribute('allreadySorted','1');	
					tBody.appendChild(cellObjArray[no2].parentNode);
					if (cellObjArray[no2].className!='th')
					{
						cellObjArray[no2].parentNode.className=(i%2==0)?'tdcolsp1':'tdcolsp2';
						cellObjArray[no2].className='highlightedColumn';
					}
					i++;
					//if (no==0 && navigator.userAgent.indexOf('Firefox')<0) tHead.appendChild(cellObjArray[no2].parentNode);
				}				
			}
		}
	}else{
		i=0;
		for(var no=0;no<cellArray.length;no++){
			for(var no2=0;no2<cellObjArray.length;no2++){
				if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
					cellObjArray[no2].setAttribute('allreadySorted','1');	
					tBody.appendChild(cellObjArray[no2].parentNode);
					if (cellObjArray[no2].className!='th')
					{
						cellObjArray[no2].parentNode.className=(i%2==0)?'tdcolsp1':'tdcolsp2';
						cellObjArray[no2].className='highlightedColumn';
					}
					i++;
					//if (no==0 && navigator.userAgent.indexOf('Firefox')<0) tHead.appendChild(cellObjArray[no2].parentNode);				
				}				
			}			
		}				
	}
	
	for(var no2=0;no2<cellObjArray.length;no2++)
	{
		cellObjArray[no2].removeAttribute('allreadySorted');		
	}

	tableWidget_okToSort = true;
	
	
}
function initSortTable(objId,sortArray)
{
	var obj = document.getElementById(objId);
	obj.setAttribute('tableIndex',tableWidget_tableCounter);
	obj.tableIndex = tableWidget_tableCounter;
	tableWidget_arraySort[tableWidget_tableCounter] = sortArray;
	var tHead = obj.getElementsByTagName('THEAD')[1];
	var cells = tHead.getElementsByTagName('TD');
	for(var no=0;no<cells.length;no++){
		if(sortArray[no]){
			cells[no].onclick = sortTable;	
		}else{
			cells[no].style.cursor = 'default';	
		}
	}
	
	tableWidget_tableCounter++;
}