
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//'                                                                '
//'   source       :   tablesort.inc                                 '
//'   Description  :   included in sse.inc. user procedures        '
//'                                                                '
//'                                                                '
//'   Cerium content manager (based on the Sellenger site engine) 									 '
//'	(c) 1997-2007 Jos Verhoeff, cerium             '
//'   For more information http://www.cerium.nl              '
//'                                                                '
//'                                                                '
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



var ascending,lastclicked=-1,activeCol = 0;
var SORTING=0, sorting=0 // no array any more

// call instead of void()
function myvoid() {
}

function doSort(a,b) {
	var islink = false;
	//alert(a.getElementsByTagName("TD").item(activeCol).firstChild.data)
	// x=y.mybof;
	if (a.getElementsByTagName("TD").item(activeCol).firstChild)
		islink = !(a.getElementsByTagName("TD").item(activeCol).firstChild.data);
	if (islink) {
		var tt = a.getElementsByTagName("TD").item(activeCol).firstChild.firstChild.data;
	} else {
		var tt = a.getElementsByTagName("TD").item(activeCol).firstChild.data;
	}	
	//var isNotNum = ((tt.indexOf(' ') == -1) && (tt.indexOf('-') == -1) && (tt.indexOf('+') == -1) && (tt.indexOf('*') == -1)) || (isNaN(parseInt( tt,10)));
	var isNotNum = (isNaN(parseInt( tt,10)) || (tt.indexOf('-') > 0) || (tt.indexOf('+') > 0) || (tt.indexOf('/') > 0) || (tt.indexOf('*') > 0));
	if (isNotNum) {
		if (islink) {
			return (sortAlpha2(a,b))
		} else {
			return (sortAlpha(a,b))
		}
	} else {
		return (sortNumerical3(a,b))
	}
}   

function resetSort(prev,now){//keeps track of current direction
	sorting++;
	if(prev!=now){
	sorting = SORTING;
	}
	ascending=Boolean(sorting & 1);
}

function sortNumerical(a,b){
	var text1 =Number( a.getElementsByTagName("TD").item(activeCol).firstChild.data);
	var text2 =Number( b.getElementsByTagName("TD").item(activeCol).firstChild.data);
	return ascending ?text2-text1:text1-text2;
}


function sortNumerical3(a,b){
	var text1 =parseInt( a.getElementsByTagName("TD").item(activeCol).firstChild.data,10);
	var text2 =parseInt( b.getElementsByTagName("TD").item(activeCol).firstChild.data,10);
	if (isNaN(text1)) text1 = 0
	if (isNaN(text2)) text2 = 0
	return ascending ? text2-text1:text1-text2;
}

function sortAlpha(a,b){
	var text1 = a.getElementsByTagName("TD").item(activeCol).firstChild.data.toLowerCase() ;
	var text2 = b.getElementsByTagName("TD").item(activeCol).firstChild.data.toLowerCase();
	if (text1 < text2)
		return ascending ? -1 : 1;
	else if (text1 > text2)
		return ascending ? 1 : -1;
	else return 0;
}

function sortAlpha2(a,b){
	var text1 =a.getElementsByTagName("TD").item(activeCol).firstChild.firstChild.data.toLowerCase();
	var text2 = b.getElementsByTagName("TD").item(activeCol).firstChild.firstChild.data.toLowerCase();
	if (text1 < text2)
		return ascending ? -1 : 1;
	else if (text1 > text2)
		return ascending ? 1 : -1;
	else return 0;
}


function sortTable(whichTable,whichCol){
	var theTable = whichTable;
	while (theTable.tagName != 'TABLE') { 
		//theTable = theTable.parentElement;
		theTable = theTable.parentNode;
	}
	var theTBody = theTable.getElementsByTagName('TBODY')[0];
	var rowsToSort = theTBody.getElementsByTagName('TR');
	var numRows = r=rowsToSort.length;
	activeCol = whichCol;
	resetSort(lastclicked,activeCol);
	var theSortedRows = new Array();
	for (var i=0; i < numRows; i++){
		theSortedRows[i] = rowsToSort[i].cloneNode(true);
	}
	theSortedRows.sort(doSort);
	lastclicked=activeCol;// remember this column
	theTable.removeChild(theTBody);
	theTBody = document.createElement("TBODY");
	theTable.appendChild(theTBody);
	for (var j=0; j< numRows; j++){
		theTBody.appendChild(theSortedRows[j]);
	}
}



