<!--

// expandable_sortable_table javascript
function toggle_extra_rows( theObject )
{
	if(!theObject) return;
	var element = document.getElementById( theObject );
	var toggleLink = document.getElementById( theObject + '_show_link' );
	var showText = toggleLink.getAttribute( "showText" );
	var hideText = toggleLink.getAttribute( "hideText" );
	var numRows = element.getAttribute( "numRows" );
	
	if( element.getAttribute( "showingAll" ) == null )
		element.setAttribute( "showingAll", "hidden" );

	if ( element.getAttribute( "showingAll" ) == "hidden" )
	{
		element.setAttribute( "showingAll", "visible" );
		hide_rows( theObject, numRows, false );
		toggleLink.innerHTML = hideText;
	}
	else
	{
		element.setAttribute( "showingAll", "hidden" );
		hide_rows( theObject, numRows, true );
		toggleLink.innerHTML = showText;
	}
}

// Hide/show/set the row visibility
function hide_rows( tableName, numRows, hideRows )
{
	var theTable=document.getElementById(tableName);
	var tbody = theTable.tBodies[0];

	if (numRows == -1)
	{
		// special case for always showing all rows, regardless
		for( var i = 0; i < tbody.rows.length; i++)
		{
			tbody.rows[i].style.display = "";
		}
	}
	else
	{
		for( var i = 0; i < tbody.rows.length; i++ )
		{
			if( hideRows == true && i >= numRows )
				tbody.rows[i].style.display = "none";
			else
				tbody.rows[i].style.display = "";
		}
	}
	
	// Determine if there are enough elements in the table to justify having the toggle link
	var toggleLink=document.getElementById(tableName + "_ToggleLink");
	if( tbody.rows.length <= numRows || numRows == -1)
		toggleLink.style.display = "none";
	else
		toggleLink.style.display = "block";
	
}

// Sort the table, based on the column heading that was clicked
function sort_table( theColumn )
{
	// Go up the chain until we hit the enclosing table
	var theTable = theColumn;
	while( theTable != null && theTable.tagName.toLowerCase() != 'table' )
	{
		theTable = theTable.parentNode;
	}
	
	// Did we never hit a table?  If so, do get out.
	if( theTable == null )
		return;
	
	// Determine the index of this column
	var cells = theTable.rows[0].cells;
	var len = cells.length;
	for (i = 0; i < len; i++) {
		if (cells[i] == theColumn) break;
	}
	var colNum = i;
	
	// Make a temporary copy of the table's rows
	// sort those using array.sort
	// Set the table's rows to be the now ordered set
	var numRows = theTable.tBodies[0].rows.length;
	var tempArray = new Array();
	for(var i = 0; i < numRows; i++ )
	{
		tempArray[i] = theTable.tBodies[0].rows[i];
	}
	
	// Get the sorting type (ie, how we should treat the data in the cells)
	var sortType = theColumn.getAttribute( 'datatype' );
	
	// Determine whether we're sorting ascending or descending
	var bDescending;
	
	if( theColumn.getAttribute( "sortOrder" ) == "Ascending" )
	{
		theColumn.setAttribute( "sortOrder", "Descending" );
		bDescending = true;
	}
	else
	{
		theColumn.setAttribute( "sortOrder", "Ascending" );
		bDescending = false;
	}

	// Set the up/down sorting indicator for this column
	SetColumnSortIndicator( theTable, theColumn, bDescending );

	// Sort the rows
	tempArray.sort( compareColData( colNum, bDescending, sortType ) );

	for(var i = 0; i < numRows; i++ )
	{
		theTable.tBodies[0].appendChild( tempArray[i] );
	}

	// Get the row hiding/showing back to normal - toggle twice, and it'll be fixed.
	toggle_extra_rows( theTable.getAttribute( "ID" ) );
	toggle_extra_rows( theTable.getAttribute( "ID" ) );
	
	// Fix the row colorations
	fixColors( theTable );
}

function SetColumnSortIndicator( theTable, theColumn, bDescending )
{
	// If there's nothing to sort, then don't show the indicator
	// (also if there's only one row, since one can only sort more
	// than one object.)
	if( theTable.tBodies[0].rows.length <= 1 )
		return;
	
	var uid = "sortIndicator" + theTable.getAttribute( "id" );

	var sortNode = document.getElementById( uid );
	if( sortNode != null )
	{
		var theParent = sortNode.parentNode;
		theParent.removeChild( sortNode );
	}

	// create the sort indicator node
	var sortNode = document.createElement( "span" );
	sortNode.setAttribute( "id", uid );
	var textNode = document.createTextNode( "" );
	sortNode.appendChild( textNode );

	// Set the sort indicator to point in the right direction (up or down)
	if( bDescending == true )
		sortNode.innerHTML = "&nbsp;^";
	else
		sortNode.innerHTML = "&nbsp;v";
	
	// Place the indicator on this column heading (this should move it from its
	// previous home automatically)
	theColumn.appendChild( sortNode );
}

function compareColData( colNum, bDescending, sortType )
{
	switch( sortType )
	{
		case "numeric":
			return numericSort( colNum, bDescending );
		case "wow":
			return wowSort( bDescending );
		default:	// "alphanumeric" also goes here
			return alphaSort( colNum, bDescending );
	}
}

function wowSort( bDescending )
{
	return function( n1, n2 )
	{
		// WoW rankings specially formatted as "<level> <Character Name>"
		// We will sort bDescending on the level first, if they have the same level
		// then sort by !bDescending on the Character Name
		// To minimalize the impact elsewhere, we ignore the colNum argument and always
		// get the WoW specific attributes
		var level1 = Number(n1.getAttribute( "level" ));
		var level2 = Number(n2.getAttribute( "level" ));
		if (level1 != level2)
			return (bDescending?(level1 - level2):(level2 - level1));
		var name1 = n1.getAttribute( "charname" );
		var name2 = n2.getAttribute( "charname" );
		if (name1 == name2)
			return 0;
		if (name1 > name2)
			return (bDescending?-1:1);
		return (bDescending?1:-1);
	};
}

function alphaSort( colNum, bDescending )
{
	return function( n1, n2 )
	{
		if( String(n1.getAttribute( "colData" + colNum )).toUpperCase() > String(n2.getAttribute( "colData" + colNum ) ).toUpperCase() )
		{
			return bDescending ? +1 : -1;
		}
		else
		{
			return bDescending ? -1 : +1;
		}
	};
}

function numericSort( colNum, bDescending )
{
	return function( n1, n2 )
	{
		if( Number(n1.getAttribute( "colData" + colNum )) > Number(n2.getAttribute( "colData" + colNum )) )
		{
			return bDescending ? +1 : -1;
		}
		else
		{
			return bDescending ? -1 : +1;
		}
	};
}

function fixColors( theTable )
{
	var len = theTable.rows.length;
	for(var i = 1; i < len; i++ )	// start at one so we don't touch the header row
	{
		// respect multiple classes
		var classname = theTable.rows[i].className;
		if(classname.indexOf("est_row1")!=-1)
			classname = classname.replace(/est_row1/g, "");
		if(classname.indexOf("est_row2")!=-1)
			classname = classname.replace(/est_row2/g, "");

		if( i % 2 == 0 )
			classname += " est_row1";
		else
			classname += " est_row2";
		theTable.rows[i].className = classname;
	}
}

// for expanding individual rows
function toggle_row_content( theImage )
{
	// Go up the chain until we hit the enclosing table
	var theTable = theImage;
	while( theTable != null && theTable.tagName.toLowerCase() != 'table' )
	{
		theTable = theTable.parentNode;
	}

	// Did we never hit a table?  If so, do get out.
	if( theTable == null )
		return;

	var theRow = theTable.rows[1];

	if( theRow.className == "est_toggle_display_on" )
	{
		theRow.className = "est_toggle_display_off";
		theImage.className = "est_toggle_up";
	}
	else
	{
		theRow.className = "est_toggle_display_on";
		theImage.className = "est_toggle_down";
	}
}

//------------------

function Est_FilterRows(outer_objid, filter)
{
	var outer_obj = document.getElementById(outer_objid);
	var est_table = GetFirstChild(outer_obj, 'TABLE');

	var numRows = est_table.tBodies[0].rows.length;
	var vis_rows = 0;
	for(var i = 0; i < numRows; i++ )
	{
		var row = est_table.tBodies[0].rows[i];
		var match = false;
		if(filter=='')
		    match = true;
		else
			for(var filter_num=1; filter_num<=3; filter_num++ )
			{
				var row_filter = row.getAttribute( "filterData" + filter_num );
				if(row_filter==null)
					break;
				if(filter==row_filter)
				{
					match = true;
					continue;
				}
			}

		if (match)
		{
			row.style.display = '';
			var classname = row.className;
			if(classname.indexOf("altrows_row")!=-1)
				classname = classname.replace(/altrows_row[12]/g, "");

			if( vis_rows % 2 == 0 )
				classname += " altrows_row1";
			else
				classname += " altrows_row2";
			row.className = classname;
			vis_rows++;
		}
		else
		{
			row.style.display = 'none';
		}
		
	}
}

//-->

