var table;
var limittable;
var titles = new Array();
var elements = new Array();
var rowArray = new Array();		
var titleRowArray = new Array();	
var titleRowCellArray = new Array();	
var titleSpanCellArray = new Array();	
var colSpanArray = new Array();		
var colTitleFilled = new Array();
var sortIndex;
var descending = false;
var nRow, actualNRow, maxNCol;
var origColor;				
var isIE;				
var selectedColor = "#980203";
var defaultColor = "#980203";
var titleFace = 'b';			
var updownColor = 'gray';
var linkEventString = 'style=\'color:' + defaultColor + '\'onMouseOver=\'setCursor(this);' + 'setColor(this,"selected");\' ' + 'onMouseOut=\'setColor(this,"default");\' ' + 'onClick=\'sortTable(';
var ascChr = "";			
var desChr = "";			


function initTable(obj)
{
	if (!checkBrowser()) return;
	var countCol;
	var nChildNodes;
	var innerMostNode;
	var nColSpan, nRowSpannedTitleCol, colPos;
	var cell, cellText;
	var titleFound = false;
	var rNRowSpan, rNColSpan;

	if (obj.tagName == "TABLE")
	{
		table = obj;
	}
	else
	{
		table = document.getElementById(obj);
	}

	if (table == null) return;

	if (table.tagName != "TABLE") return;

	maxNCol = table.rows[table.rows.length-1].cells.length;

	rowArray = new Array();
	colSpanArray = new Array();
	colTitleFilled = new Array();
	titleRowArray = new Array();
	titleRowCellArray = new Array();
	
	for (var i=0; i<maxNCol; i++)
		colTitleFilled[i] = false;

	nRow = table.rows.length;

	if (nRow < 1) return;

	actualNRow = 0;
	rNRowSpan = 0;
	rNColSpan = 0;
	nRowSpannedTitleCol = 0;

	for (var i=0; i<nRow; i++)
	{
		nColSpan = 1, colPos = 0;
		for (var j=0; j<table.rows[i].cells.length; j++)
		{	
			if (titleFound == false)
			{
				if (table.rows[i].cells[j].rowSpan > 1) 
				{
					if (table.rows[i].cells[j].colSpan < 2)
					{
						titleSpanCellArray[colPos] = table.rows[i].cells[j];
						colTitleFilled[colPos] = true;
						nRowSpannedTitleCol++;
					}
					if (table.rows[i].cells[j].rowSpan-1 > rNRowSpan)
					{
						rNRowSpan = table.rows[i].cells[j].rowSpan - 1;
						if (table.rows[i].cells[j].colSpan > 1)
							rNColSpan = rNRowSpan + 1;
					}
				}
			}
			if (table.rows[i].cells[j].colSpan > 1 && rNColSpan == 0)
			{ 
				nColSpan = table.rows[i].cells[j].colSpan;
				colPos += nColSpan;
			}
			else
			{
				colPos++;
			}
		}
					
		if (titleFound == false && nColSpan == 1 && rNRowSpan == 0 && rNColSpan == 0 && titleFound == false)
		{
			
			colSpanArray[i] = true;
			titleFound = true;

			countCol = 0;
			for (var j=0; j<table.rows[i].cells.length + nRowSpannedTitleCol; j++)
			{
				if (colTitleFilled[j] != true)
				{
					titleRowCellArray[j] = table.rows[i].cells[countCol];
					countCol++;
				}
				else
				{
					titleRowCellArray[j] = titleSpanCellArray[j];
				}
			}
		}
		else if (titleFound == true && nColSpan == 1 && rNRowSpan == 0)
		{
			var cell = new Array();
			for (var j=0; j<table.rows[i].cells.length; j++)
			{
				var tdattributes = '';
				if (table.rows[i].cells[j].rowSpan > 1) return;
				var properties = getPropertiesTag(table.rows[i].cells[j].childNodes);
				var cellprop = new Array();
				cellprop[0] = ( properties.title ? properties.title.value : table.rows[i].cells[j].firstChild.data );
				tdattributes += 'style="' + table.rows[i].cells[j].style.cssText + '" ';
				tdattributes += 'class="' + table.rows[i].cells[j].className + '" ';
				tdattributes += 'width="' + table.rows[i].cells[j].width + '" ';
				tdattributes += 'align="' + table.rows[i].cells[j].align + '" ';
				tdattributes += 'valign="' + table.rows[i].cells[j].align + '" ';
				tdattributes += 'height="' + table.rows[i].cells[j].height + '" ';
				tdattributes += 'id="' + table.rows[i].cells[j].id + '" ';
				tdattributes += 'name="' + table.rows[i].cells[j].name + '" ';
				cellprop[1] = tdattributes;
				cellprop[2] = table.rows[i].cells[j];
				cell[j] = cellprop;
			}
			elements[i-1]=cell;
			if (table.rows[i].cells.length > maxNCol){
				return;
			}
			actualNRow++;
			colSpanArray[i] = false;
		}
		else if (nColSpan == 1 && rNRowSpan == 0 && rNColSpan == 0 && titleFound == false)
		{
			colSpanArray[i] = false;
		}
		else
		{
			
			colSpanArray[i] = true;
		}
		if (rNRowSpan > 0) rNRowSpan--;
		if (rNColSpan > 0) rNColSpan--;
	}
	
	if(i <= elements.length){
		limittable = (i-1);
	}
	else
		limittable = elements.length;
	
	if (actualNRow < 1) return;

	for (var j=0; j<maxNCol; j++)
	{
		if (titleRowCellArray[j].childNodes.length == 0) return;
		if (titleRowCellArray[j].firstChild != null)
		{
			nChildNodes = titleRowCellArray[j].firstChild.childNodes.length;
			innerMostNode = titleRowCellArray[j].firstChild;

			while ( nChildNodes != 0)
			{
				innerMostNode =	innerMostNode.firstChild;
				nChildNodes = innerMostNode.childNodes.length;
			}
			
			cellText = innerMostNode.data;
			
		}
		else
		{
			cellText = "column(" + j + ")";
		}
		titleRowArray[j] = cellText;
		var lnk = '<a ' +	linkEventString + j + ',' + '"' + table.id + '"' + ');\'>' + '<' + titleFace + '>' + cellText + '</' + titleFace +'></a>';
		titleRowCellArray[j].innerHTML = lnk;
		titles[j]=lnk;
	}
}

function sortTable(index,obj)
{
	if( sortIndex == undefined || index!=sortIndex )
		descending = false;
	
	initTable(obj);

	var nChildNodes;
	var innerMostNode;
	var rowContent;
	var rowCount;
	var cell, cellText;
	var newTitle;

	if (index < 0 || index >= maxNCol) return;

	sortIndex = index;
	insertionSort(elements, sortIndex, descending);
	
	listAttrs = table.attributes;
	attrs = '';
	for(var i=0 ; i<listAttrs.length ; i++){
		( listAttrs[i].specified ? attrs += listAttrs[i].nodeName + '="' + listAttrs[i].nodeValue + '" ' : '' );
	}
	var html = '<table ' + attrs + '>';
	var titlehtml = '<tr>';
	for(var m=0 ; m<maxNCol ; m++){
		titlehtml += '<td ';
		if(m==sortIndex && descending){
			titlehtml += 'class="table_sort"' + ( m==maxNCol-1 ? ' style="border-right:0px"' : '');
		}
		else if(m==sortIndex && !descending){
			titlehtml += 'class="table_sort"' +  ( m==maxNCol-1 ? ' style="border-right:0px"' : '');
			}
			else if(m!=maxNCol-1){
					titlehtml += 'class="table_sort"';
				}
				else{
					titlehtml += 'class="table_sort"';
				}
		titlehtml += '>'; 
		titlehtml += titles[m];
		titlehtml += '</td>';
	}
	titlehtml += '</tr>';
	html += titlehtml;
	for (var i=0; i<elements.length && i<limittable ; i++){
		var tmphtml = '<tr>';
		for (var j=0 ; j<elements[i].length ; j++){
			tmphtml += '<td ' + elements[i][j][1] + '>' + elements[i][j][2].innerHTML + '</td>';
		}
		tmphtml += '</tr>';
		html += tmphtml;
	}
	html += '</table>';
	table.parentNode.innerHTML = html;

	if (descending)
		descending = false;
	else
		descending = true;
}

function insertionSort(e, index, order){
	var ind = new Array();
	for(var j=0 ; j<e.length && j<limittable ; j++){
		if ( !isNaN(elements[j][index][0]) )
			var tmp = parseFloat(elements[j][index][0], 10);
		else{
			var tmp = elements[j][index][0];
			tmp = tmp.replace(/á/gi, "a");
			tmp = tmp.replace(/é/gi, "e");
			tmp = tmp.replace(/í/gi, "i");
			tmp = tmp.replace(/ó/gi, "o");
			tmp = tmp.replace(/ú/gi, "u");
			tmp = tmp.toLowerCase();
			}
		ind[j]= tmp;
	}
	var temp = new Array();
	var indtemp;
	var j=0;
	for(var i=1 ; i<ind.length || i<limittable ; i++){
		indtemp = ind[i];
		temp = e[i];
		j = i-1;
		if(order){
			while( (ind[j] < indtemp) && j>=0 ){
				ind[j+1] = ind[j];
				e[j+1] = e[j];
				j--;
			}
		}
		else{
			while( (ind[j] > indtemp) && j>=0 ){
				ind[j+1] = ind[j];
				e[j+1] = e[j];
				j--;
			}
		}
		ind[j+1] = indtemp;
		e[j+1] = temp;
	}
}

function setCursor(obj)
{
	window.status = "Sort by " + obj.firstChild.innerHTML;
	if (isIE)
		obj.firstChild.style.cursor = "hand";
	else
		obj.firstChild.style.cursor = "pointer";
}

function setColor(obj,mode)
{
	if (mode == "selected")
	{
		if (obj.style.color != selectedColor) 
			defaultColor = obj.style.color;
		obj.style.color = selectedColor;
	}
	else
	{	
		obj.style.color = defaultColor;
		window.status = '';
	}
}

function checkBrowser()
{
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("5.") >= 0)
	{
		isIE = true;
		return true;
	}
	else if (navigator.appName == "Netscape" && navigator.appVersion.indexOf("5.") >= 0)
	{
		isIE = false;
		return true;
	}
	else
		return false;
}


function getPropertiesTag(childs){
	var nodes = childs;
	var nName;
	var att;
	if(childs){
		for(p = 0 ; p < nodes.length ; p++){
			nName = nodes[p].nodeName;
			if(nName == 'IMG' || nName == 'INPUT' || nName == 'A'){
				att = nodes[p].attributes;
				return att;
			}
		}
	}
	return false;
}