/********************************************************************************
 ***************************** SORTER *******************************************
 ********************************************************************************/
/* TO UNDERSTAND, BEGING IN 'sorterTableInit'.					*/

/* MAXSORTLEN is the max of table that it can sort.				*/
var	MAXSORTLEN      = 20;
/* currentTID is the ID of the table that is been sorted.			*/
var	currentTID      = -1;
/* sortPosition is the key of sorting for each table.				*/
var	sortPosition    = new Array(MAXSORTLEN);
/* Idem, for the previous key.							*/
var	oldSortPosition = new Array(MAXSORTLEN);
/* Hold the sorting direction for each table.					*/
var	sortDesc        = new Array(MAXSORTLEN);
/* tables holds the needed data of tables.					*/
var	tables          = new Array(MAXSORTLEN);
var	tablesFound     = new Array(MAXSORTLEN);
/* Holds the amount of added table.						*/
var	tableslen       = 0;
/* sortByPosition is the sorting function.					*/
function sortByPosition (a, b) {
	return ((sortDesc[currentTID]?1:-1)*(a[sortPosition[currentTID]]>b[sortPosition[currentTID]]?1:-1));
}
/* sortTable sorts a table depending of an order.				*/
function sortTable (order,tid) {
	tablename         = tables[tid][0];
	table             = document.getElementById(tablename);
	sortPosition[tid] = order;
	if (oldSortPosition[tid] == sortPosition[tid]) {
		sortDesc[tid] = !sortDesc[tid];
	} else {
		sortDesc[tid] = true;
	}
	oldSortPosition[tid] = sortPosition[tid];
	currentTID           = tid;
	tables[tid][1].sort (sortByPosition);
	tablesFound[tid] = new Array(tables[tid][1].length);
	for (i=0; i<tablesFound[tid].length; i++) {
		tablesFound[tid][i] = false;
	}
	showTable (table, tables[tid][1], tables[tid][2], tid);
}
/********************************************************************************
 * sorterTableInit adds a table to the sorter and initializes it.		*
 *	tablename: a string with the table ID.					*
 *	list     : a double array with the values.				*
 *	head     : a simple array with headers titles.				*/

function sorterTableInit (tablename, list, head, align) {
	pos                  = tableslen++;
	tables[pos]          = new Array(4);
	tables[pos][0]       = tablename;
	tables[pos][1]       = list;
	tables[pos][2]       = head;
	tables[pos][3]       = align;
	tablesFound[pos]     = false;
	sortPosition[pos]    = 0;
	oldSortPosition[pos] = -1;
	sortDesc[pos]        = true;
	sortTable (0,pos);
}
ReSendArray     = new Array(4);
function ReSend () {
	divi = ReSendArray [0];
	list = ReSendArray [1];
	head = ReSendArray [2];
	tid  = ReSendArray [3];
	XshowTable (divi, list, head, tid);
}
function showTable (divi, list, head, tid) {
// 	openLoading ();
	divi.innerHTML = '<span class="LoadingClock"><img lowsrc="icons/clock.png" src="images/loading_clock.gif" alt=""/></span>'
	ReSendArray[0] = divi;
	ReSendArray[1] = list;
	ReSendArray[2] = head;
	ReSendArray[3] = tid;
	
// 	if (tableslen == 1) {
		setTimeout ('ReSend();', 200);
// 	} else {
// 		XshowTable (divi, list, head, tid);
// 	}
}
function XshowTable (divi, list, head, tid) {
	rlen = list.length;
	clen = list[0].length;
/*	Filling Header.								*/
	htmlstr = "<center><table class=\"SortedTable\">";
	htmlstr+= "<tr class=\"header\">";
	for (x=0; x<clen; x++) {
		htmlstr+= "<td>";
		if (head[x]) {
			htmlstr+= "<a href=\"#\" class=\"sortheader\" onClick=\"sortTable("+x+","+tid+");return false;\">"+head[x];
			if (!sortDesc[tid] && sortPosition[tid] == x) {
				htmlstr+= "<img alt=\"\" src=\"images/order/asc-on.gif\"/>";
			} else if (sortDesc[tid] && sortPosition[tid] == x) {
				htmlstr+= "<img alt=\"\" src=\"images/order/desc-on.gif\"/>";
			} else {
				htmlstr+= "<img alt=\"\" src=\"images/order/desc-off.gif\"/>";
			}
			htmlstr+= "</a>";
		}
		htmlstr+= "</td>";
	}
	htmlstr+= "</tr>";
/*	Filling Data.								*/
	tr = 0;
	for (y=0; y<(rlen); y++) { /*	FOUND					*/
		if (tablesFound[tid][y]) {
			htmlstr+= "<tr class=\"found\">";
			for (x=0; x<clen; x++) {
				htmlstr+= "<td"+(tables[tid][3]?" align=\""+tables[tid][3][x]+"\"":"")+">"+list[y][x]+"</td>";
			}
			htmlstr+= "</tr>";
		}
	}
	for (y=0; y<(rlen); y++) { /*	NOT FOUND				*/
		if (!tablesFound[tid][y]) {
			htmlstr+= "<tr class=\"not_found\">";
			for (x=0; x<clen; x++) {
				htmlstr+= "<td"+(tables[tid][3]?" align=\""+tables[tid][3][x]+"\"":"")+">"+list[y][x]+"</td>";
			}
			htmlstr+= "</tr>";
		}
	}
	htmlstr+= "</table></center>";
	divi.innerHTML = htmlstr;
	closeLoading ();
}

/********************************************************************************
 ***************************** FINDER *******************************************
 ********************************************************************************/
/* TO UNDERSTAND, BEGING IN 'letFindKey'.					*/

/* getTableId returns the ID that correpons to certain table.			*/
function getTableId (tablename) {
	found = -1;
	for (i=0; i<MAXSORTLEN && found < 0; i++) {
		found = (tables[i][0]==tablename?i:-1);
	}
	return found;
}
/* tableFind marks every row that has a the key.				*/
function tableFind (tid, key) {
	list = tables[tid][1];
	fnd  = tablesFound[tid];
	rlen = list.length;
	clen = list[0].length;
	for (y=0; y<rlen; y++) {
		fnd[y] = false;
	}
	if (key != '') {
		for (y=0; y<rlen; y++) {
			found = false;
			for (x=0; x<rlen && !found; x++) {
				str = ''+list[y][x]+'';
				found = (str.toLowerCase().indexOf(key.toLowerCase())>-1);
			}
			fnd[y] = found;
		}
	}
}
/********************************************************************************
 * letFindKey find the key en rows and puts the on the top.	.		*
 *	tablename: a string with the table ID.					*
 *	input_key: the ID of the input that gives the key.			*/
function letFindKey (tablename, input_key) {
	tid   = getTableId (tablename);
	key   = document.getElementById (input_key).value;
	table = document.getElementById (tables[tid][0]);
	tableFind (tid, key);
	showTable (table, tables[tid][1], tables[tid][2], tid);
/*	This returning if for convenience with forms.				*/
	return false;
}

