	/*
	(C) www.dhtmlgoodies.com, October 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	*/	
	var tableWidget_tableCounter = 0;
	var tableWidget_arraySort = new Array();
	var tableWidget_okToSort = true;
	var activeColumn = new Array();
	
	function addEndCol(obj)
	{
		if(document.all)return;
		var rows = obj.getElementsByTagName('TR');
		for(var no=0;no<rows.length;no++){
			var cell = rows[no].insertCell(-1);
			cell.innerHTML = ' ';
			cell.style.width = '13px';
			cell.width = '13';
		}	
	}
	
	function highlightTableHeader()
	{
		this.className='statfej_a';
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode.parentNode;
			this.parentNode.style.top = divObj.scrollTop + 'px';

		}
			
	}
	
	function deHighlightTableHeader()
	{
		this.className='statfej';		
	}
	
	function mousedownTableHeader()
	{
		this.className='statfej';
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode.parentNode;
			this.parentNode.style.top = divObj.scrollTop + 'px';
		}		
	}
	
	function stripHTML(oldString) {
	   var newString = "";
	   var inTag = false;
	   for(var i = 0; i < oldString.length; i++) 
	    {
			if(oldString.charAt(i) == '<') inTag = true;
			if(oldString.charAt(i) == '>') {
		         if(oldString.charAt(i+1)!="<") { inTag = false;	i++;}
		     }
			if(!inTag) newString += oldString.charAt(i);
	   }
	   return newString;
	}	
	
	function sortNumeric(a1,b1){
		var a = (a1>0)?a1:0;
		var b = (b1>0)?b1:0;
		if(a1.length>20) a=a1;
		if(b1.length>20) b=b1;
		a = stripHTML(a);
		b = stripHTML(b);
		a = a.replace('.','');
		b = b.replace('.','');
		return a/1 - b/1;
		/*
		a = stripHTML(a1);
		b = stripHTML(b1);
		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;
		//*/
	}
	
    function sortSizing(a,b) {
		var a_g=a.substr(a.length-2,2);
		if(a_g=='kB'){var a_n=a.substr(0,a.length-3);a=a_n*1024;}
		if(a_g=='MB'){var a_n=a.substr(0,a.length-3);a=a_n*1024*1024;}
		if(a_g=='GB'){var a_n=a.substr(0,a.length-3);a=a_n*1024*1024*1024;}
		if(a_g==' B'){var a_n=a.substr(0,a.length-2);a=a_n;}
		var a_g=b.substr(b.length-2,2);
		if(a_g=='kB'){var a_n=b.substr(0,b.length-3);b=a_n*1024;}
		if(a_g=='MB'){var a_n=b.substr(0,b.length-3);b=a_n*1024*1024;}
		if(a_g=='GB'){var a_n=b.substr(0,b.length-3);b=a_n*1024*1024*1024;}		
		if(a_g==' B'){var a_n=b.substr(0,b.length-2);b=a_n;}
		return a/1 - b/1;
	}
	
	
	function sortString(a, b) {

	  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
	  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
	  return 0;
	}	
	function cancelTableWidgetEvent()
	{
		return false;
	}
	
	function sortTable()
	{
		if(!tableWidget_okToSort)return;
		tableWidget_okToSort = false;
		/* Getting index of current column */
		var obj = this;
		var indexThis = 0;
		while(obj.previousSibling){
			obj = obj.previousSibling;
			if(obj.tagName=='TD')indexThis++;		
		}
		
		var images = this.getElementsByTagName('DIV');
		
		if(this.getAttribute('direction') || this.direction){
			direction = this.getAttribute('direction');
			if(navigator.userAgent.indexOf('Opera')>=0)direction = this.direction;
			if(direction=='ascending'){
				direction = 'descending';
				this.setAttribute('direction','descending');
				this.direction = 'descending';	
			}else{
				direction = 'ascending';
				this.setAttribute('direction','ascending');		
				this.direction = 'ascending';		
			}
		}else{
			direction = 'ascending';
			this.setAttribute('direction','ascending');
			this.direction = 'ascending';
		}
		
		if(direction=='descending'){
			images[1].style.color='#ff6600';
			images[0].style.color='#ffffff';
		}else{
			images[0].style.color='#ff6600';
			images[1].style.color='#ffffff';
		}

		
		var tableObj = this.parentNode.parentNode.parentNode;
		var tBody = tableObj.getElementsByTagName('TBODY')[0];
		
		var widgetIndex = tableObj.id.replace(/[^\d]/g,'');
		var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String
		if(activeColumn[widgetIndex] && activeColumn[widgetIndex]!=this){
			var images = activeColumn[widgetIndex].getElementsByTagName('div');
            images[1].style.color='#ffffff';
			images[0].style.color='#ffffff';
			if(activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction');			
		}

		activeColumn[widgetIndex] = this;
		
		var cellArray = new Array();
		var cellObjArray = new Array();
		for(var no=1;no<(tableObj.rows.length-1);no++){
			var content= tableObj.rows[no].cells[indexThis].innerHTML+'';
			console.log(no);
			cellArray.push(content);
			cellObjArray.push(tableObj.rows[no].cells[indexThis]);
		}
		
		if(sortMethod=='N'){
			cellArray = cellArray.sort(sortNumeric);
		}else 
		if(sortMethod=='F'){
			cellArray = cellArray.sort(sortSizing);
		} else {
			cellArray = cellArray.sort(sortString);
		}
		
		if(direction=='descending'){
			for(var no=cellArray.length;no>=0;no--){
				for(var no2=0;no2<cellObjArray.length;no2++){
					if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
						cellObjArray[no2].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[no2].parentNode);				
					}				
				}			
			}
		}else{
			for(var no=0;no<cellArray.length;no++){
				for(var no2=0;no2<cellObjArray.length;no2++){
					if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
						cellObjArray[no2].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[no2].parentNode);				
					}				
				}			
			}				
		}
		
		for(var no2=0;no2<cellObjArray.length;no2++){
			cellObjArray[no2].removeAttribute('allreadySorted');		
		}

		tableWidget_okToSort = true;
		
		
	}
	
	function rendez(objId,width,height,sortArray)
	{
		width = width + '';
		height = height + '';
		var obj = document.getElementById(objId);
		if(navigator.userAgent.indexOf('MSIE')>=0){
			obj.parentNode.style.overflowY = 'hidden';
		}		
		tableWidget_arraySort[tableWidget_tableCounter] = sortArray;
		if(width.indexOf('%')>=0){
			obj.style.width = width;
			obj.parentNode.style.width = width;
		}else{
			obj.style.width = width + 'px';
			obj.parentNode.style.width = width + 'px';
		}
		obj.id = 'tableWidget' + tableWidget_tableCounter;
		
		obj.cellSpacing = 0;
		obj.cellPadding = 0;
//		obj.className='statfej';
		var tHead = obj.getElementsByTagName('THEAD')[0];
		var cells = tHead.getElementsByTagName('TD');
		for(var no=0;no<cells.length;no++){
			cells[no].className = 'statfej';
			cells[no].onselectstart = cancelTableWidgetEvent;
			if(no==cells.length-1){
				cells[no].style.borderRight = '0px';	
			}
			if(sortArray[no]){
				cells[no].onmouseover = highlightTableHeader;
				cells[no].onmouseout =  deHighlightTableHeader;
				cells[no].onmousedown = mousedownTableHeader;		
				cells[no].onmouseup = highlightTableHeader;		
				cells[no].onclick = sortTable;	
				
				var img = document.createElement('div');
				img.innerHTML  =  '&#9660;';
				cells[no].appendChild(img);	
				img.style.display='inline';
				img.style.float='right';
				var img = document.createElement('div');
				img.innerHTML  =  '&#9650;';
				cells[no].appendChild(img);	
				img.style.display='inline';
				img.style.float='right';
				
			}else{
				cells[no].style.cursor = 'default';	
			}
			
			
		}		
		var tBody = obj.getElementsByTagName('TBODY')[0];
		for(var no=1;no<obj.rows.length;no++){
			obj.rows[no].onmouseover = highlightDataRow;
			obj.rows[no].onmouseout = deHighlightDataRow;
		}
	
		tableWidget_tableCounter++;
	}
	
	
	function highlightDataRow()
	{
		if(navigator.userAgent.indexOf('Opera')>=0)return;
		if(on==1) this.className='stattart_on';
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode;
			var tHead = divObj.getElementsByTagName('TR')[0];
			tHead.style.top = divObj.scrollTop + 'px';
			
		}	
	}
	
	function deHighlightDataRow()
	{
		if(navigator.userAgent.indexOf('Opera')>=0)return;
		this.className=null;
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode;
			var tHead = divObj.getElementsByTagName('TR')[0];
			tHead.style.top = divObj.scrollTop + 'px';
		}			
	}
	
	function sorrend(id) {	var order = []; 
			$('#'+id+' tbody tr').each( function(e) { order.push($(this).attr('id'));  });
			return order.join(';')
	}
	
