function debug(s)
{
	var x;
	if((x=$("debug")))
	{
		x.innerHTML+=s+"<br />";
		if(!x.styleSet)
		{
			x.styleSet=1;
			x.style.border="1px solid red";
			x.style.padding="5px";
			x.style.font="9pt \"courier new\"";
		}
	}
}

tableSorting=new Object();

function makeSortable(id)
{
	if(tableSorting[id]){alert("Trying to make a table sortable twice... ("+id+")"); return;}
	table=$(id);
	captions=new Array();
	row1=table.rows[0];
	for(i=0;i<row1.cells.length;i++)
	{
		cap=row1.cells[i].innerHTML.stripTags().toLowerCase();
		captions.push(cap);
		if(!row1.cells[i].hasClassName("stus"))
		{
			row1.cells[i].innerHTML="<a class=\"stsort\" id=\""+id+"-"+cap+"\" href=\"javascript:sortTable(\'"+id+"\',\'"+cap+"\')\">"+row1.cells[i].innerHTML+"</a><div class=\"stind\" id=\""+id+"-"+cap+"-ind\"></div>";
		}
	}
	for(i=1;i<table.rows.length;i++)
	{
		table.rows[i].setAttribute("originalIndex",i);
		cells=table.rows[i].cells;
		for(c=0;c<cells.length;c++)
		{
			if(cells[c].hasClassName("stfi"))
			{
				cells[c].innerHTML="<a class=\"stfilter\" href=\"javascript:filterTable(\'"+id+"\',\'"+captions[c]+"\',\'"+cells[c].innerHTML.stripTags()+"\')\" title=\"Show only '"+cells[c].innerHTML.stripTags()+"'\">"+cells[c].innerHTML+"</a>";
			}
			//cells[c].className+=" col-"+captions[c];
		}
	}
	tableSorting[id]=new Object();
	tableSorting[id].sorted=0;
	tableSorting[id].filtered=0;
	tableSorting[id].captions=captions;
	tableSorting[id].highlight="";
	tableSorting[id].totalRows=table.rows.length-1;
	tableSorting[id].visibleRows=table.rows.length-1;
	tableSorting[id].litRows=0;
	div=document.createElement("DIV");
	div.id=id+"-controls";
	div.className="stcontrols";
	div.innerHTML="<a href=\"javascript:resetSorting(\'"+id+"\')\">Reset Table</a> | <span id=\""+id+"-info\"></span> | Highlight: <input class=\"sthighlight\" id=\""+id+"-highlight\" onkeydown=\"highlightTable(event,\'"+id+"\',this.value)\" />";
	table.parentNode.insertBefore(div,table);
	refreshTableCaptions(id);
}

function refreshTableCaptions(tid)
{
	var d=$(tid+"-info");
	dt="";
	if(tableSorting[tid].visibleRows<tableSorting[tid].totalRows)
	{
		dt+=tableSorting[tid].visibleRows+" of "+tableSorting[tid].totalRows+" rows shown.";
	}
	if(tableSorting[tid].litRows>0)
	{
		dt+=" "+tableSorting[tid].litRows+" rows highlighted.";
	}
	d.innerHTML=dt;
	tableSorting[tid].captions.each(
		function(cid)
		{
			var c=$(tid+"-"+cid+"-ind");
			if(c)
			{
				h="";

				if(tableSorting[tid].filtered&&tableSorting[tid].filtered[0]==cid)
				{
					h+=tableSorting[tid].filtered[1];
				}

				if(tableSorting[tid].sorted&&tableSorting[tid].sorted[0]==cid)
				{
					if(tableSorting[tid].sorted[1]==1) h+=" &uarr;";
					else h+=" &darr;";
				}
				if(h!="")
				{
					c.innerHTML=h;
					c.show();
				}
				else
				{
					c.hide();
				}
			}
		}
	);
}

function doFilterTable(tid)
{
	if(tableSorting[tid].filtered)
	{
		fcn=tableSorting[tid].captions.indexOf(tableSorting[tid].filtered[0]);
		fct=tableSorting[tid].filtered[1];
	}
	else
	{
		fcn=undefined;
		fct=undefined;
	}
	tableSorting[tid].visibleRows=0;
	for(i=1;i<table.rows.length;i++)
	{
		if(fcn)
		{
			if((cell=table.rows[i].cells[fcn]))
			{
				t=cell.innerHTML.stripTags();
				if(t==fct) table.rows[i].show();
				else table.rows[i].hide();
			}
		}
		else
		{
			table.rows[i].show();

		}
		if(table.rows[i].visible()) tableSorting[tid].visibleRows++;
	}
}

function sortNumeric(a,b)
{
    aa = parseFloat(a.cells[sortCellIndex].innerHTML.stripTags());
    if (isNaN(aa)) aa = 0;
    bb = parseFloat(b.cells[sortCellIndex].innerHTML.stripTags());
    if (isNaN(bb)) bb = 0;
    return aa-bb;
}

function sortCase(a,b)
{
    aa = a.cells[sortCellIndex].innerHTML.stripTags().toLowerCase();
    bb = b.cells[sortCellIndex].innerHTML.stripTags().toLowerCase();
    if (aa==bb) return 0;
    if (aa<bb) return -1;
    return 1;
}

function sortOriginal(a,b)
{
    aa = parseInt(a.getAttribute("originalIndex"));
    bb = parseInt(b.getAttribute("originalIndex"));
    return aa-bb;
}

function doSortTable(tid)
{
	if(tableSorting[tid].sorted) var column = tableSorting[tid].captions.indexOf(tableSorting[tid].sorted[0]);
    else var column = -1;
    var table = $(tid);

    if(column>-1)
    {
    	var itm = table.rows[1].cells[column].innerHTML.stripTags();
    	sortfn = sortCase;
    	if (itm.match(/^[\d\.]+$/)) sortfn = sortNumeric;
    	sortCellIndex = column;
    }
    else
    {
		sortfn=sortOriginal;
    }
    var newRows = new Array();
    for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }

    newRows.sort(sortfn);
    if (tableSorting[tid].sorted&&tableSorting[tid].sorted[1]==1) newRows.reverse();

    for (i=0;i<newRows.length;i++)
    {
    	table.tBodies[0].appendChild(newRows[i]);
    }
}

function doHighlightTable(tid)
{
	var table=$(tid);
	var cn=0;

	for(i=1;i<table.rows.length;i++)
	{
		ok=0;
		if(tableSorting[tid].highlight!="")
		{
			for(c=0;c<table.rows[i].cells.length;c++)
			{
				if(table.rows[i].cells[c].hasClassName("sthi")&&table.rows[i].cells[c].innerHTML.stripTags().toLowerCase().indexOf(tableSorting[tid].highlight.toLowerCase())>-1)
				{
					ok=1;
					break;
				}
			}
		}
		if(ok)
		{
			table.rows[i].addClassName("sthighlit");
			cn++;
		}
		else
		{
			table.rows[i].removeClassName("sthighlit");
		}
	}
	tableSorting[tid].litRows=cn;
}

function refreshTable(tid)
{
	doFilterTable(tid);
	doSortTable(tid);
	doHighlightTable(tid);
	refreshTableCaptions(tid);
}

function filterTable(tid,cid,match)
{
	if(tableSorting[tid].filtered&&tableSorting[tid].filtered[0]==cid&&tableSorting[tid].filtered[1]==match)
	{
		tableSorting[tid].filtered=0;
	}
	else
	{
		tableSorting[tid].filtered=[cid,match];
	}
	refreshTable(tid);
	//alert(xid);
}
function sortTable(tid,cid)
{
	if(tableSorting[tid].sorted&&tableSorting[tid].sorted[0]==cid)
	{
		if(tableSorting[tid].sorted[1]==1) tableSorting[tid].sorted[1]=2;
		else tableSorting[tid].sorted=0;
	}
	else
	{
		tableSorting[tid].sorted=[cid,1];
	}
	refreshTable(tid);
}

function highlightTable(e,tid,v)
{
    if (document.all) key = event.keyCode;
    else key = e.which;
	if(key==13)
	{
		tableSorting[tid].highlight=v;
		refreshTable(tid);
	}
return false;
}

function resetSorting(tid)
{
	tableSorting[tid].sorted=0;
	tableSorting[tid].filtered=0;
	refreshTable(tid);
}
