

function CustomerTableSort(ascendingIcon, descendingIcon, colors) {

	/**
	* saves informations about data types
	*/
	this.data = new Array();
	
	this.activeColumn = new Array();
	
	this.tableCounter = 1;
	
	this.ascendingIcon = ascendingIcon;
	this.descendingIcon = descendingIcon;
	
	this.icons = new Array();
	
	this.colors = colors;
	
	this.currentActiveObj = null;

	this.init = function(tableID, typeArray) {
		var object = document.getElementById(tableID);
		//save table counter to object
		object.setAttribute('tableIndex', this.tableCounter);
		object.tableIndex = this.tableCounter;
		
		//save type informations
		this.data[this.tableCounter] = typeArray;
		
		var tableHead = object.getElementsByTagName('THEAD')[0];
		var cells = tableHead.getElementsByTagName('TD');
		for(var i=0; i<cells.length; i++){
			if(typeArray[i]){
			//	cells[i].onclick = this.sortTable(cells[i]);	
			} else{
				cells[i].style.cursor = 'default';	
			}
		}		
		
		this.tableCounter++;
	}

	this.sortNumeric = function(a,b) {
		a = a.replace(/,/,'.');
		b = b.replace(/,/,'.');
		a = a.replace(/[^\d\.\/]/g,'');
		b = b.replace(/[^\d\.\/]/g,'');
		if(a.indexOf('/')>=0)a = eval(a);
		if(b.indexOf('/')>=0)b = eval(b);
		return a/1 - b/1;
	}
	
	
	this.sortString = function(a,b) {	
	  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
	  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
	  return 0;
	}
	
	

	this.sortTable = function(tableID, obj,indexThis) {
		/* Getting index of current column */
	/*	var indexThis = 0;
		while(obj.previousSibling){
			obj = obj.previousSibling;
			if(obj.tagName=='TD') {
				indexThis++;
			}
		}
	*/	
	
		if(this.currentActiveObj != null) {
			this.currentActiveObj.className = this.currentActiveObj.className.replace(/ active/g,'');
		}
		obj.className = obj.className+' active';
		
		this.currentActiveObj = obj;
	
		if(obj.getAttribute('direction') || obj.direction){
			direction = obj.getAttribute('direction');
			if(navigator.userAgent.indexOf('Opera')>=0) {
				direction = obj.direction;
			}
			
			if(direction=='ascending') {
				direction = 'descending';
				obj.setAttribute('direction','descending');
				obj.direction = 'descending';	
			} else {
				direction = 'ascending';
				obj.setAttribute('direction','ascending');		
				obj.direction = 'ascending';		
			}
			
		} else {
			direction = 'ascending';
			obj.setAttribute('direction','ascending');
			obj.direction = 'ascending';
		}
		
		var tableObj = obj.parentNode.parentNode.parentNode;
		var tBody = tableObj.getElementsByTagName('TBODY')[0];
		
		var tableIndex = tableObj.getAttribute('tableIndex');
		if(!tableIndex) {
			tableIndex = tableObj.tableIndex;
		}
		
		
		currentColumn = document.getElementById('col' + tableIndex + '_' + (indexThis+1));

		if(this.icons[tableIndex]) {
			this.icons[tableIndex].innerHTML='';
		}

		icon = document.getElementById(tableID+'Icon_'+indexThis);
		if(icon) {
			this.icons[tableIndex] = icon;
		}

		var sortMethod = this.data[tableIndex][indexThis]; // N = numeric, S = String
		if(this.activeColumn[tableIndex] && this.activeColumn[tableIndex]!=obj){
			if(this.activeColumn[tableIndex]) {
				this.activeColumn[tableIndex].removeAttribute('direction');
			}
		}

		this.activeColumn[tableIndex] = obj;
		
		var cellArray = new Array();
		var cellObjArray = new Array();
		for(var i=1;i<tableObj.rows.length;i++){
			//var content= tableObj.rows[i].cells[indexThis].innerHTML+'';
			
			var content = this.getCellContent(tableObj.rows[i].cells[indexThis]);
			cellArray.push(content);
			cellObjArray.push(tableObj.rows[i].cells[indexThis]);
		}
		
		if(sortMethod=='N'){
			cellArray = cellArray.sort(this.sortNumeric);
		}else{
			cellArray = cellArray.sort(this.sortString);
		}
		
		if(direction=='descending'){

			if(this.icons[tableIndex]) {
				this.icons[tableIndex].innerHTML = ' <img src="'+this.descendingIcon+'" alt="" />';
			}
		
			for(var j=cellArray.length;j>=0;j--){
				for(var i=0;i<cellObjArray.length;i++){
					if(cellObjArray[i].getAttribute('longdesc') == cellArray[j] && !cellObjArray[i].getAttribute('allreadySorted')){
						cellObjArray[i].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[i].parentNode);				
					}				
				}			
			}
		}else{

			if(this.icons[tableIndex]) {
				this.icons[tableIndex].innerHTML = ' <img src="'+this.ascendingIcon+'" alt="" />';
			}

			for(var i=0;i<cellArray.length;i++){
				for(var j=0;j<cellObjArray.length;j++){
					if(cellObjArray[j].getAttribute('longdesc') == cellArray[i] && !cellObjArray[j].getAttribute('allreadySorted')){
						cellObjArray[j].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[j].parentNode);				
					}				
				}			
			}				
		}
		
		for(var i=0;i<cellObjArray.length;i++){
			cellObjArray[i].removeAttribute('allreadySorted');		
		}		

		var object = document.getElementById(tableID);		
		var tableBody = object.getElementsByTagName('TBODY')[0];
		var rows = tableBody.getElementsByTagName('TR');

		for(var i=0; i<rows.length; i++){
			
			var colorIndex =  i % 2;
			rows[i].className= this.colors[colorIndex];
			
		}
	}
	
	
	this.getCellContent = function(cellObj) {
		
		var content = cellObj.innerHTML+'';
		var attributeContent = cellObj.getAttribute('longdesc');
		if(navigator.userAgent.indexOf('Opera')>=0) {
			attributeContent = cellObj.longdesc;
		}
		
	//	alert('content: '+content+', att:'+attributeContent);
		
		if(attributeContent) {
	//	alert('return att:'+attributeContent);
			return attributeContent;
		}
		
		return content;
	}
}
