if (!Component) var Component = {}


Component.SortableTable = function (id, customIndexAttribute) {
	
	this.CUSTOM_INDEX_ATTRIBUTE = 'xwgindex'; 
	this.id = id;
	this.table = document.getElementById(id);
	
	if(this.table){
		this.thead = this.table.getElementsByTagName('thead')[0];
		this.tbody = this.table.getElementsByTagName('tbody')[0];
		this.init();
	}else{
		alert('Sortable table could not be intialized')
	}
	
	this.sortedColumn = -1;
	
	if(customIndexAttribute){
		this.CUSTOM_INDEX_ATTRIBUTE = customIndexAttribute;
	}
}

Component.SortableTable.prototype.init = function() {
	
	var sortable = this;
	var thead = this.thead;
	var rows = thead.getElementsByTagName('tr');
	var THs = rows[0].childNodes;
	
	for (var i = 0, length=THs.length; i<length; i++) {
		
		
		
		var th = THs[i];
		th.index = i;
		
		th.onclick = function(){
							sortable.sortColumn(this.index, this.getAttribute('sort'));
						};
		
	}
}

Component.SortableTable.prototype.sortColumn = function(column, dataType) {
	
	if (!(document.getElementById && document.cloneNode)) {
		return;
	}
	
	var table = this.table;
	var tbody = this.tbody;
	var rows = tbody.getElementsByTagName('tr');
	
	// Create new index
	var rowIndex = new Array();
	for (var i=0, length=rows.length; i<length; i++) {
		rowIndex[i] = new Object;
		rowIndex[i].oldIndex = i;
		
		var node = rows[i].cloneNode(true).childNodes[column];
		var customIndexAttribute = node.getAttribute(this.CUSTOM_INDEX_ATTRIBUTE);
	
		if(customIndexAttribute){
			rowIndex[i].value= customIndexAttribute;
		}else{
			rowIndex[i].value= node.firstChild.nodeValue;
		}
	}
	

	// Sort index
	if (column == this.sortedColumn) {
		rowIndex.reverse();
	}
	else {
		this.sortedColumn = column;
		
		if(dataType){
		
			if (dataType == 'number') {
				rowIndex.sort(this.rowCompareNumbers);
			}
			else if (dataType == 'decimal') {
				rowIndex.sort(this.rowCompareDecimal);
			}
			else if (dataType == 'date'){
				rowIndex.sort(this.rowCompareDate);
			}
			else {
				rowIndex.sort(this.rowCompare);
			}		
		}
	}
	
	// Insert new rows
	var newTbody = document.createElement('tbody');
	var theRow;
	for (var i=0, length=rowIndex.length; i<length; i++) {
		theRow = rows[rowIndex[i].oldIndex].cloneNode(true);
		newTbody.appendChild(theRow);
	}
	
	table.replaceChild(newTbody,tbody);
	this.tbody = newTbody;
	
	//this.rebuildStyle(table);	
}

Component.SortableTable.prototype.rebuildStyle = function(table) {
	// change class of cells
	var newRows=table.childNodes[1].childNodes;
	var flavor=2;
	for (var i=0; i<newRows.length; i++) {
		newRowCells=newRows[i].childNodes;
		for (var x=0; x<newRowCells.length; x++) {
			var cell=newRowCells[x];
			var style = (this.sortedColumn==x) ? 'Hilited' : 'Standard';
			if (cell.className.indexOf('Standard1')!=-1) cell.className=cell.className.replace(/Standard1/i,style+flavor);
			if (cell.className.indexOf('Standard2')!=-1) cell.className=cell.className.replace(/Standard2/i,style+flavor);
			if (cell.className.indexOf('Hilited1')!=-1) cell.className=cell.className.replace(/Hilited1/i,style+flavor);
			if (cell.className.indexOf('Hilited2')!=-1) cell.className=cell.className.replace(/Hilited2/i,style+flavor);
		}
		(flavor==1) ? flavor=2 : flavor=1;
	}
	
	// change class of headers + remove direction
	headers=table.childNodes[0].childNodes[0].childNodes;
	var style = '';
	for (var i=0; i<headers.length; i++) {
		style = (this.sortedColumn==i) ? 'Hilited' : 'Standard';
		header=headers[i];
		headerAttr=header.getAttributeNode('class');
		header.className='ListHeader'+style;
		var imgs = header.getElementsByTagName('img');
		if (imgs.length>0) {
			img=imgs[0];
			if (img.className=='ListHeaderDirection') {
				img.parentNode.removeChild(img);
			}
		}
		headerLinkAttr=header.getElementsByTagName('a')[0].className='ListHeader ListHeader'+style;
	}
}
		
Component.SortableTable.prototype.rowCompare = function(a,b) {
	var aVal = a.value;
	var bVal = b.value;
	return (aVal == bVal ? 0 : (aVal > bVal ? 1 : -1));
}

Component.SortableTable.prototype.rowCompareNumbers = function(a, b) {
	var aVal = parseInt(Component.SortableTable.prototype.getValidCharacters(a.value));
	var bVal = parseInt(Component.SortableTable.prototype.getValidCharacters(b.value));
	if (isNaN(aVal)) aVal=0;
	if (isNaN(bVal)) bVal=0;
	return (aVal - bVal);
}

Component.SortableTable.prototype.rowCompareDecimal = function(a, b) {
	var aVal = parseFloat(Component.SortableTable.prototype.getValidCharacters(a.value));
	var bVal = parseFloat(Component.SortableTable.prototype.getValidCharacters(b.value));
	if (isNaN(aVal)) aVal=0;
	if (isNaN(bVal)) bVal=0;
	return (aVal - bVal);
}

Component.SortableTable.prototype.rowCompareDecimal = function(a, b) {
	var aVal = parseFloat(Component.SortableTable.prototype.getValidCharacters(a.value));
	var bVal = parseFloat(Component.SortableTable.prototype.getValidCharacters(b.value));
	if (isNaN(aVal)) aVal=0;
	if (isNaN(bVal)) bVal=0;
	return (aVal - bVal);
}

Component.SortableTable.prototype.rowCompareDate = function(a,b) {
	var aVal = parseInt(Component.SortableTable.prototype.getValidCharacters(a.value));
	var bVal = parseInt(Component.SortableTable.prototype.getValidCharacters(b.value));
	if (isNaN(aVal)) aVal=0;
	if (isNaN(bVal)) bVal=0;
	return (aVal - bVal);
}

Component.SortableTable.prototype.getValidCharacters = function(input,chars) {
	var output='';
    output = input.replace(/[^0-9.-]/g,'');
	return output;
}