

	/* ------------------------------------------------------------------------------------------
	 */
	var tables;

	function getInnerText(el) {
		if (typeof el == 'string'   ) return el;
		if (typeof el == 'undefined') return el;

		// Not needed but it is faster
		if (el.innerTxt) return el.innerTxt;
		if (el.alt     ) return el.alt;
		if (el.title   ) return el.title;
		if (el.checked ) return 'x';

		var str = "";
		var cs  = el.childNodes;
		var l   = cs.length;

		for (var i = 0; i < l; i++) {
			switch (cs[i].nodeType) {
				case 1: // ELEMENT_NODE
					str += getInnerText(cs[i]);
					break;
				case 3:	// TEXT_NODE
					str += cs[i].nodeValue;
					break;
			}
		}

		str = str.replace(/^ */, '').replace(/ *$/, '');
		str = (str && str.match && str.match(/^-?[0-9 ]+$/) ?
			   parseInt(str) :  str);
		str = (str && str.match && str.match(/^-?[0-9 \.]+$/) ?
			   parseFloat(str) :  str);

		return (el.innerTxt = str);
	}

	function sortTable(body, order) {
		var tabl = body.parentNode;

	//	tabl.removeChild(body);
		body.display = 'none';

		function compare(o1, o2) {
			var p, a, b;

			for (var o = 0; o < order.length; o++) {
				p = parseInt(order[o])
				a = getInnerText(o1.cells[p]);
				b = getInnerText(o2.cells[p]);

		//		getObj('debug').innerHTML += r1 + ' - ' + order[o] + ': ' + a + ' ' + (a > b ? '>' : '<') + ' ' + b + '<br/>';

				if (isNaN(parseFloat(a)) != isNaN(parseFloat(b)))
					return isNaN(parseFloat(a)) ? -1 : 1;

				if (order[o] == (p + '+')) if (b > a) { return 1; break; } else if (b < a) break;
				if (order[o] == (p + '-')) if (b < a) { return 1; break; } else if (b > a) break;
			}

			return (a == b ? 0 : -1);
		}

		var rows = new Array();
		for (var r = 0; r < body.rows.length; r++)
			rows[r] = body.rows[r];

		rows.sort(compare);
		var e = 0;
		for (var r = 0; r < rows.length; r++) {
			rows[r].className = ((e ^= 1) == 1 ? 'e1' : 'e0');

			body.removeChild(rows[r]);
			body.appendChild(rows[r]);
		}

//		tabl.appendChild(body);
		body.display = '';
	}

	function reorderTable(hr, l, reorder) {
		var table  = tables.item(l);
		var body   = table.tBodies[0];
		var legend = (table.tHead ? table.tHead.rows[hr] : null);

		var pos = reorder.getAttribute('position');
		var ord = legend.getAttribute('order');
		var arr = ord.split(',');
		var nar = new Array();

		/* parse old order */
		for (var a = 0; a < arr.length; a++) {
			if (arr[a] == (pos + '+')) {
				/* flip */
				if (a == 0)
					pos = (pos + '-');
				else
					pos = (pos + '+');

				/* remove */
				arr[a] = null;
				break;
			}

			if (arr[a] == (pos + '-')) {
				/* flip */
				if (a == 0)
					pos = (pos + '+');
				else
					pos = (pos + '-');

				/* remove */
				arr[a] = null;
				break;
			}
		}

		/* build new order */
		nar.push(pos);
		for (var a = 0; a < arr.length; a++)
			if (arr[a])
				nar.push(arr[a]);

		/* set new order */
		legend.setAttribute('order', nar.join(','));

		/* sort the body after this orders */
		sortTable(body, nar);
	}

	function injectOrdering(hr) {
		tables = document.getElementsByTagName('table');

		var len = tables.length;
		/* search for all tables */
		for (var l = 0; l < len; l++) {
			var tabl = tables.item(l);
			var tbdy = tabl.tBodies[0];
			var trow = (tabl.tHead ? tabl.tHead.rows[hr] : null);

			/* we have our 'legend' and the suppose to be sortable body */
			if (tbdy && trow) {
				/* save the default sorting order */
				trow.setAttribute('order', '');

				for (var c = 0; c < trow.cells.length; c++) {
					var cll = trow.cells[c];

					if (!cll.className.match(/action/)) {
						// 1+,2+,3+,4+
						var arr =          trow.getAttribute('order'   ).split(',');
						arr.push(c + '+'); trow.setAttribute('order', arr.join(','));

						cll.setAttribute('position', c);
						cll.style.cursor = 'pointer';
						cll.onclick = new Function("reorderTable(" + hr + "," + l + ", this);");
					}
				}
			}
		}
	}

