/**
* <script>
*
* EasyGUI Table Script
*
* @author Gaetan Lauff <glauff@plansoft.de>
* @copyright Plan Software GmbH, 2002, 2003
* @version	1.3
* @package 	Base
*/

var lastActiveSortElement = false;
var tableOffsetWidth = false;
var tableOffsetHeight = false;
var descend = false;
var lastSortCol = false;
var	regexpNumber = /^-?\d+([.,]\d*)?\s*$/;	// numbers (floats) with optional whitespaces at the end

function isyScrollHandler(id) {
	addEvent(document.getElementById(id), "scroll", setScrollPos, false);
}

function isyClickHandler(id) {
	addEvent(document.getElementById(id), "click", selectRow, false);
}

function selectRow(evt) {
	element = getElement(evt);

	if (element == null || element == false || element.nodeName == "DIV" || element.id == "") {
		return false;
	}
	
    var currentTable = document.getElementById(getTableFrom(element));

    if ((baseTable = currentTable.getAttribute('baseTable')) != null) {
    	currentTable = document.getElementById(baseTable);
    }
    
    deselectRow(currentTable.id);

  	var currentRow = document.getElementById(getRowFrom(element));
  	    
    var highlightBgColor 	= currentTable.getAttribute('backgroundcolorhighlight');
    var highlightTextColor 	= currentTable.getAttribute('textcolorhighlight');
    var tableHasFixedColumns = parseInt(currentTable.getAttribute('fixedcolumns'));
    
    if (tableHasFixedColumns) {
  		var correspondingRow 	= document.getElementById(currentRow.getAttribute('correspondingRow'));
  		 	
    	for (var i = 0; i < correspondingRow.cells.length; i++) {
		  	correspondingRow.cells[i].style.backgroundColor = highlightBgColor;
		   	correspondingRow.cells[i].style.color 			= highlightTextColor;
		}
	}
	
    for (var i = 0; i < currentRow.cells.length; i++) {
    	currentRow.cells[i].style.backgroundColor 	= highlightBgColor;
    	currentRow.cells[i].style.color 			= highlightTextColor;
    }
    
    currentTable.setAttribute('lastselectedrowid', currentRow.id);
    document.isycat[currentTable.id].value = currentRow.id;
    	
	fixedLines = parseInt(currentTable.getAttribute('fixedlines'));

    if (fixedLines > 0) {
    	fixedLine = document.getElementById(currentTable.id + '_TABLE').rows[0];

    	for (var i=0; i < currentRow.cells.length; i++) {
    		if (fixedLine.cells[i].id.indexOf('ISYFILL') == -1 && currentRow.cells[i].submitvalue != null) {
    			document.isycat[fixedLine.cells[i].id].value = currentRow.cells[i].submitvalue;
    		}
    	}
    }
   
    if ((toggleRowStyles = currentTable.getAttribute('togglerowstyles')) != null) {
		setRowStyles(currentTable.id);
	}
	
	return true;
}

function deselectRow(inTable) {
	var currentTable = document.getElementById(inTable);
	
	if ((restoreRowId = currentTable.getAttribute('lastselectedrowid')) != null) {
	    var restoreRow = document.getElementById(restoreRowId);
	    
	    var tableHasFixedColumns = parseInt(currentTable.getAttribute('fixedcolumns'));
	    
	    if (tableHasFixedColumns) {
	  		var correspondingRow 	= document.getElementById(restoreRow.getAttribute('correspondingRow'));
	  	
	    	for (var i = 0; i < correspondingRow.cells.length; i++) {
			  	correspondingRow.cells[i].style.backgroundColor = restoreRow.style.backgroundColor;
			   	correspondingRow.cells[i].style.color 			= restoreRow.style.color;
			}
		}
			    
	    for (var i = 0; i < restoreRow.cells.length; i++) {
	    	restoreRow.cells[i].style.backgroundColor 	= restoreRow.style.backgroundColor;
	    	restoreRow.cells[i].style.color 			= restoreRow.style.color;
	    }
	}
}

function getColumnFrom(element) {
	if (element.nodeName == "TD") {	
		return element.id
	}
		
	while (element.nodeName != "TD") {
		element = element.parentNode;
	}
	
	return element.id
}

function getRowNumFrom(element) {
	var rowid	= getRowFrom(element);
	var tableid	= getTableFrom(element);
	
	return rowid.substr(tableid.length+1, rowid.length - tableid.length - 1);
}

function getRowFrom(element) {	
	if (element.nodeName == "TR") {	
		return element.id
	}
		
	while (element.nodeName != "TR") {
		element = element.parentNode;
	}
	
	return element.id
}

function getTableFrom(element) {
	if (element.nodeName == "DIV") {	
		return element.id;
	}
		
	while (element.nodeName != "DIV") {
		element = element.parentNode;
	}

	return element.id;
}

function setScrollPos(evt) {
	var element = getElement(evt);
	var fixedLine = document.getElementById(element.id+"_FL");
	var fixedColumn = document.getElementById(element.id+"_FC");
	
    if (fixedLine != null) {
    	fixedLine.style.left = - document.getElementById(element.id).scrollLeft;
    }
    
    if (fixedColumn != null) {
    	fixedColumn.style.top = - document.getElementById(element.id).scrollTop ;	
    }
}
	
function initTable(id) {
	var isyTable 						= document.getElementById(id);
	
	if (isyTable.childNodes[0].rows.length == 0) return;
	/*
	if (isyTable.type == 'pre') {
		alert('returning !'); return;
	}
	*/
	
	var fixedLines 						= isyTable.getAttribute('fixedlines');
	var fixedColumns 					= isyTable.getAttribute('fixedcolumns');
	
	var fixedLinesDiv 					= document.getElementById(isyTable.id + "_FIXED_LINES");
	var fixedColumnsDiv 				= document.getElementById(isyTable.id + "_FIXED_COLUMNS");	
	var absolutlyFixedColumnLineDiv 	= document.getElementById(isyTable.id + "_ABSOLUTLY");

	var tableLines 						= isyTable.getElementsByTagName('tr');
	var tableClass						= isyTable.className;
	var tableHeight						= parseInt(isyTable.style.height);
	var tableWidth						= parseInt(isyTable.style.width);
	
	var renderedTable 					= document.getElementById(isyTable.id + "_TABLE");
	//renderedTable.style.tableLayout = 'fixed';
	
	var renderedTableHeight				= parseInt(renderedTable.offsetHeight);
	var renderedTableWidth				= parseInt(renderedTable.offsetWidth);
	
	renderedTable.className = tableClass;
	
	var deltaHeight = tableHeight - renderedTableHeight;

	var scrollbarWidth = 0;
	var hasScrollbar = false;
	
	if (deltaHeight < 0) {
		if (is_winxp) {
			scrollbarWidth = 17;
		} else {
			scrollbarWidth = 16;
		}
		
		hasScrollbar = true;
	}
	
	var deltaCol = tableWidth - renderedTableWidth - scrollbarWidth;	
	
	if (!(bgcol = getCSSRuleAttribute(isyTable.className, 'backgroundColor'))) {
		bgcol = 'transparent';
	}
	
	if ((selectedRow = isyTable.getAttribute('selected')) == null || selectedRow == '') {
		selectedRow = false;
	}
	
	if ((preSortedColumn = isyTable.getAttribute('presortedcolumn')) == null) {
		preSortedColumn = false;
	}
	
	if ((scrollIntoView = isyTable.getAttribute('scrollintoview')) == null) {
		scrollIntoView = true;
	}
	
	if ((headerStyle = isyTable.getAttribute('headerstyle')) == null) {
		headerStyle = 'easygui-table-header';
	}
	
	if ((selectionType = isyTable.getAttribute('selectiontype')) == null) {
		selectionType = 'none';
	}
	
	fixedLinesDiv.style.backgroundColor 				= bgcol;
	fixedColumnsDiv.style.backgroundColor 				= bgcol;
	absolutlyFixedColumnLineDiv.style.backgroundColor 	= bgcol;
	isyTable.style.backgroundColor						= bgcol;
		
	var fixedLinesHeight 				= 0;
	var fixedLinesWidth 				= 0;
	var fixedColumnsWidth				= 0;
	
	var fixedLineTable 					= "";
	var fixedColumnTable 				= "";
	
	var absolutlyFixedColumnLineHeight 	= 0;
	var absolutlyFixedColumnLine 		= "";
	
	var isFiltrable						= false;
	var isSortable						= false;
	var filtrableColumnsCounter			= 0;
	var sortWidth						= 0;
	
	if (fixedLines > 0) {
		fixedLinesTable		= document.createElement('table');
		fixedLinesTable.className = tableClass;
		
		fixedLinesTableBody = document.createElement('tbody');
		
		for (var i=0; i < fixedLines; i++) {
			origTD = isyTable.childNodes[0].rows[i].cells;
			
			
			tabLin = isyTable.childNodes[0].rows[i].cloneNode(false);

			var filterBoxes = new Array();
			
			for (j=0; j < origTD.length; j++) {
				
				tabLinTd = origTD[j].cloneNode(true);
				cloneCombos = tabLinTd.getElementsByTagName('SELECT');

				for (var k=0; k < cloneCombos.length; k++) {
					cloneCombos[k].name += '_';
				}

				//tabLinTd.style.width = origTD[j].offsetWidth;
				//alert(origTD[j].style.width);
				tabLinTd.style.width = origTD[j].offsetWidth;
				
				tabLinTd.setAttribute('id', origTD[j].id + "_FL");
				var filterBox = document.createTextNode("");
				
				if(tabLinTd.getAttribute('sortable')) {
					var isSortable = true;
					var alignment = getCSSRuleAttribute(origTD[j].className, 'textAlign');
					
					if ((sorttitle = isyTable.getAttribute('sorttitle')) == null) {
						var sorttitle = 'Sortieren'
					}
					
					if ((sac = isyTable.getAttribute('sortarrowcorrection')) == null) {
						var sac = false;
					}
					
					tabLinTd.setAttribute('title', sorttitle);
					tabLinTd.setAttribute('descend', false);
					tabLinTd.setAttribute('tableId', isyTable.id);
					tabLinTd.setAttribute('columnId', j);
					tabLinTd.style.backgroundRepeat = 'no-repeat';
					
					addEvent(tabLinTd, "click", sortColumn, false);
					
					if (sac) {
						tabLinTd.style.width = parseInt(tabLinTd.style.width) + 15;
					}
					
					if (alignment != 'right') {
						tabLinTd.style.backgroundPosition = 'right center';
					} else {
						tabLinTd.style.backgroundPosition = 'left center';
					}
				}
				
				if (tabLinTd.getAttribute('filtrable')) {
					isFiltrable = true;
					filterBox = document.createElement('select');
					filterBox.style.minWidth = "100px";
					filterBox.style.width = "100%";
					filterBox.id = id+"_FILTERBOX_"+j;
					filterBox.name = id+"_FILTERBOX_"+j;
					filterBox.setAttribute('tableId', isyTable.id);
					filterBox.setAttribute('columnId', j);
					addEvent(filterBox, "change", filterColumn, false);
				}

				filterBoxes[j] = filterBox;
				tabLin.appendChild(tabLinTd);
				
				origTD[j].style.width = parseInt(tabLinTd.style.width);

				var combos = origTD[j].getElementsByTagName('SELECT');
								
				for (var k=0; k < combos.length; k++) {
					combos[k].style.visibility = 'hidden';
					combos[k].fixedColumnCombo = true;
				}
			}

			var filterLine = tabLin.cloneNode(true);
			fixedLinesTableBody.appendChild(tabLin);
			
			if (isFiltrable) {
				for (k = 0; k < filterBoxes.length; k++) {
					filterLine.childNodes[k].innerHTML = '';
					filterLine.childNodes[k].appendChild(filterBoxes[k]);
					filterLine.style.height = "25px";
				}

				fixedLinesTableBody.appendChild(filterLine);
			}
		}
		
		fixedLinesTable.setAttribute('id', isyTable.id+"_FL");
		fixedLinesTable.style.position = 'relative';
		fixedLinesTable.style.tableLayout = 'fixed';
		//fixedLinesTable.style.tableLayout = 'auto';
		fixedLinesTable.style.borderSpacing 	= 0;
		fixedLinesTable.style.borderCollapse = 'collapse';
		fixedLinesTable.border 	= 0;
		fixedLinesTable.cellPadding = 0;
		fixedLinesTable.cellSpacing = 0;
		
		if (headerStyle != '') {
			fixedLinesTable.className = headerStyle;
		}
		
		if (hasScrollbar) {
			fixedLinesDiv.style.width = parseInt(fixedLinesDiv.style.width) - scrollbarWidth;
		}
		
		if(!(borderLeftWidth = getCSSRuleAttribute(isyTable.className, "borderLeftWidth"))) {
			var borderLeftWidth = 0;
		}
		
		if(!(borderRightWidth = getCSSRuleAttribute(isyTable.className, "borderRightWidth"))) {
			var borderRightWidth = 0;
		}
	
		fixedLinesTable.style.width = parseInt(isyTable.childNodes[0].offsetWidth) + parseInt(borderLeftWidth) + parseInt(borderRightWidth);
		fixedLinesTable.appendChild(fixedLinesTableBody);
		
		if (isFiltrable) {
			for (i = 0; i < fixedLines; i++) {
				var correctedHeight = 0;
				allFixedLines = fixedLinesTable.getElementsByTagName('tr');
				
				for (var j=0; j < allFixedLines.length; j++) {
					correctedHeight += parseInt(allFixedLines[j].offsetHeight);
				}
				
				tableLines[0].style.height = correctedHeight;
			}
		}
			
		fixedLinesDiv.appendChild(fixedLinesTable);
		fixedLinesTableHeight = fixedLinesTable.offsetHeight;
		fixedLinesDiv.setAttribute('baseTable', isyTable.id);
		
		for (var i=0; i < fixedLines; i++) {
			fixedLinesTable.rows[i].style.height = renderedTable.rows[i].offsetHeight;
		}
		
			
		for (var j=0; j < fixedLinesTable.rows[0].cells.length; j++) {
			fixedLinesTable.rows[0].cells[j].style.width = renderedTable.rows[0].cells[j].offsetWidth + 'px';
		}
			
	} else {
		fixedLinesDiv.style.visibility = 'hidden';
	}
	
	if (fixedColumns > 0) {
		fixedColumnsTable	= document.createElement('table');
		fixedColumnsTable.className = tableClass;
		
		fixedColumnsTableBody = document.createElement('tbody');
		
		tableWidth 	= 0;
		colWidths 	= 0;
		
		for (var i=0; i < tableLines.length; i++) {
			var tabCol = isyTable.childNodes[0].rows[i].cloneNode(false);
			var filterBoxes = new Array();
			var baseId = isyTable.childNodes[0].rows[i].id;

			tabCol.id = baseId + "_FC";
			tabCol.setAttribute('correspondingRow', baseId);
			isyTable.childNodes[0].rows[i].setAttribute('correspondingRow', tabCol.id);
			
			//alert(isyTable.childNodes[0].rows[i].style.height);
			tabCol.style.height = isyTable.childNodes[0].rows[i].offsetHeight;
			
			for (var j = 0; j < fixedColumns; j++) {
				nstd = isyTable.childNodes[0].rows[i].cells;
				var colspan = parseInt(nstd[j].colSpan);
				
				tdtoinsert = nstd[j].cloneNode(true);
				//alert('Width: ' + nstd[j].style.width);
				tdtoinsert.style.width = nstd[j].offsetWidth;
				tdtoinsert.setAttribute('id', nstd[j].id + "_FC");
				
				var filterBox = document.createTextNode("");
				
				if(tdtoinsert.getAttribute('sortable')) {
					isSortable = true;
				
					tdtoinsert.setAttribute('descend', false);
					tdtoinsert.setAttribute('tableId', isyTable.id);
					tdtoinsert.setAttribute('columnId', j);
					
					if ((sorttitle = isyTable.getAttribute('sorttitle')) == null) {
						var sorttitle = 'Sortieren';
					}
					
					tdtoinsert.setAttribute('title', sorttitle);
					tdtoinsert.style.backgroundImage = 'url(gfx/blank.gif)';
					tdtoinsert.style.backgroundRepeat = 'no-repeat';
					tdtoinsert.style.backgroundPosition = 'right center';
					
					addEvent(tdtoinsert, "click", sortColumn, true);
				}
				
				if (tdtoinsert.getAttribute('filtrable')) {
					isFiltrable = true;
					var filterBox = document.createElement('select');
					filterBox.style.width = "100%";
					filterBox.id = id+"_FILTERBOX_"+j;
					filterBox.name = id+"_FILTERBOX_"+j;
					filterBox.setAttribute('tableId', isyTable.id);
					filterBox.setAttribute('columnId', j);
					addEvent(filterBox, "change", filterColumn, false);
				}
				
				filterBoxes[j] = filterBox;
				tabCol.appendChild(tdtoinsert);	
				colWidths += nstd[j].offsetWidth;

				var combos = nstd[j].getElementsByTagName('SELECT');
								
				for (var k=0; k < combos.length; k++) {
					combos[k].style.visibility = 'hidden';
					combos[k].fixedColumnCombo = true;
				}
				
				j += (colspan - 1);
			}
			
			tableWidth = Math.max(tableWidth, colWidths);	
			colWidths = 0;
			
			var filterLine = tabCol.cloneNode(true);
			fixedColumnsTableBody.appendChild(tabCol);
			
			if (isFiltrable) {
				for (k = 0; k < filterBoxes.length; k++) {
					filterLine.childNodes[k].innerHTML = '';
					filterLine.childNodes[k].appendChild(filterBoxes[k]);
					filterLine.style.height = "25px";
				}

				fixedColumnsTableBody.appendChild(filterLine);
			}		
		}
		
		fixedColumnsTable.style.position 		= 'relative';
		fixedColumnsTable.style.tableLayout 	= 'auto';
		fixedColumnsTable.style.width 			= tableWidth;
		fixedColumnsTable.style.left 			= 0;
		fixedColumnsTable.style.top 			= 0;
		fixedColumnsTable.style.borderSpacing 	= 0;
		fixedColumnsTable.style.borderCollapse 	= 'collapse';
		fixedColumnsTable.border				= 0;
		fixedColumnsTable.cellSpacing			= 0;
		fixedColumnsTable.cellPadding			= 0;
		
		fixedColumnsTable.id =  isyTable.id + "_FC";
		fixedColumnsTable.appendChild(fixedColumnsTableBody);
		
		fixedColumnsDiv.style.height = parseInt(fixedColumnsDiv.style.height) - 16;
		fixedColumnsDiv.style.width = tableWidth;
		fixedColumnsDiv.appendChild(fixedColumnsTable);
		
		fixedColumnsDiv.setAttribute('baseTable', isyTable.id);

		if (selectionType == 'selectrow') {
			addEvent(document.getElementById( isyTable.id + "_FC" ), "click", selectRow, false);
		}
	} else {
		fixedColumnsDiv.style.visibility = 'hidden';
	}
	
	if (fixedColumns > 0 && fixedLines > 0) {
		absolutlyFixedLineColumnTable		= document.createElement('table');
		absolutlyFixedLineColumnTable.className = tableClass;
		
		absolutlyFixedLineColumnTableBody 	= document.createElement('tbody');
		absolutlyTabLines 					= fixedColumnsDiv.getElementsByTagName('tr');
		
		for (var i=0; i < fixedLines; i++) { 
			absolutlyFixedLineColumn = absolutlyTabLines[i].cloneNode(true);
			absolutlyFixedLineColumn.id = absolutlyTabLines[i].id + "_AF";
			
			alCohols = absolutlyFixedLineColumn.getElementsByTagName('td');
			
			for (var j=0; j < alCohols.length; j++) { 
				if (alCohols[j].getAttribute('sortable')) {
					alCohols[j].id += "_ABSOLUTLY";
					
					if ((sorttitle = isyTable.getAttribute('sorttitle')) == null) {
						var sorttitle = 'Sortieren';
					}
					
					alCohols[j].setAttribute('title', sorttitle);
					alCohols[j].setAttribute('descend', 0);
					alCohols[j].setAttribute('tableId', isyTable.id);
					alCohols[j].setAttribute('columnId', j);
				}
			}
				
			absolutlyFixedLineColumnTableBody.appendChild(absolutlyFixedLineColumn);
			absolutlyFixedColumnLineHeight += absolutlyTabLines[i].offsetHeight;
		}
		
		absolutlyFixedLineColumnTable.style.position 		= 'relative';
		absolutlyFixedLineColumnTable.style.tableLayout 	= 'fixed';
		absolutlyFixedLineColumnTable.width 				= fixedColumnsDiv.childNodes[0].offsetWidth;
		absolutlyFixedLineColumnTable.height 				= fixedLinesDiv.childNodes[0].offsetHeight;
		absolutlyFixedLineColumnTable.border 				= 0;
		absolutlyFixedLineColumnTable.cellPadding 			= 0;
		absolutlyFixedLineColumnTable.cellSpacing 			= 0;
		
		absolutlyFixedLineColumnTable.appendChild(absolutlyFixedLineColumnTableBody);
		absolutlyFixedLineColumnTable.setAttribute('id', isyTable.id+"_AF");
		absolutlyFixedColumnLineDiv.appendChild(absolutlyFixedLineColumnTable);
		absolutlyFixedColumnLineDiv.setAttribute('baseTable', isyTable.id);
	}

	if (selectionType == 'selectrow') {
		addEvent(document.getElementById(isyTable.id), "click", selectRow, false);
	}
	
	addEvent(document.getElementById(isyTable.id), "scroll", setScrollPos, false);
	
	if ((userFunction = isyTable.getAttribute('userfunction')) == null) {
		userFunction = false;
	}
			
	if (userFunction) {
		for (i=0; i < tableLines.length; i++) {
			addEvent(tableLines[i], "click", eval(userFunction), true);
		}
	}
	
	if (isFiltrable) {
		var columns = tableLines[0].getElementsByTagName('TD');
		eval("var filterColumns = "+isyTable.id+"_filterColumns = new Array("+columns.length+");");
		arrayLength = tableLines.length - fixedLines;
		
		for (var k = 0; k < filterColumns.length; k++) {
			if (columns[k].getAttribute('filtrable') != null) {
				filterColumns[k] = new Array(arrayLength);
			} else {
				filterColumns[k] = "NO_FILTER";
			}
		}
		
		lineCount = 0;
		
		for (var i = fixedLines; i < tableLines.length; i++) {
			innerColumns = tableLines[i].getElementsByTagName('TD');
			
			for (var j = 0; j < innerColumns.length; j++) {
				if (innerColumns[j].firstChild.nodeType == 3) {
					var text = innerColumns[j].firstChild.data;
					
				} else {
					var text = null;
				}
				
				if (((filtercriterium = innerColumns[j].getAttribute('filtercriterium')) == null) || filtercriterium == '') {
					var filtercriterium = text;
				}
				
				if (filterColumns[j] != "NO_FILTER") {
					filterColumns[j][lineCount] = new column(tableLines[i].id, filtercriterium);
				}
			}
				
			lineCount++;
		}
		
		eval(isyTable.id+"_filterColumns = filterColumns");		
		fillFilterBoxes(isyTable.id);
	}
		
	if (isSortable) {
		var columns = tableLines[0].getElementsByTagName('TD');
		eval("var sortColumns = "+isyTable.id+"_sortColumns = new Array("+columns.length+");");
		arrayLength = tableLines.length - fixedLines;

		for (var k = 0; k < sortColumns.length; k++) {
			sortColumns[k] = new Array(arrayLength);
		}
	
		lineCount = 0;
		
		for (var i = fixedLines; i < tableLines.length; i++) {
			innerColumns = tableLines[i].getElementsByTagName('TD');
			
			for (var j = 0; j < innerColumns.length; j++) {
				if (innerColumns[j].firstChild && innerColumns[j].firstChild.nodeType == 3) {
					var text = innerColumns[j].firstChild.data;
					
				} else {
					var text = null;
				}
				
				if (((sortcriterium = innerColumns[j].getAttribute('sortcriterium')) == null) || sortcriterium == '') {
					var sortcriterium = text;
				}

				sortColumns[j][lineCount] = new column(tableLines[i].id, sortcriterium);	
			}
				
			lineCount++;
		}
		
		eval(isyTable.id+"_sortColumns = sortColumns");
	}
	
	
	if (deltaCol > 0) {
		j = 0;
		
		for (var i=0; i < renderedTable.rows.length; i++) {
			fillCol = document.createElement('TD');
			fillCol.style.width = deltaCol;
			fillCol.id = "ISYFILL_" + i + "_" + j;
			fillCol.className = renderedTable.rows[i].childNodes[0].className;
			renderedTable.rows[i].appendChild(fillCol);
		}
	}
	
	if (preSortedColumn) {
		sortColumn(preSortedColumn);
	}
	
	selectionType = selectionType.toLowerCase();
	if (selectionType == 'selectrow') {

		if (selectedRow) {
			selectRow(selectedRow);
				
			if ( scrollIntoView != 'disabled' ) {
				scrollToTableElement(selectedRow);
			}
		} else {
			if (renderedTable.rows.length > 1) {
				selectRow(renderedTable.rows[1].id);
			}
		}
	}
	
	if ((toggleRowStyles = isyTable.getAttribute('togglerowstyles')) != null) {
		setRowStyles(currentTable.id);
	}
}

function setRowStyles(tableId) {
	isyTableDiv = document.getElementById(tableId);
	
	var fixedColumns = isyTableDiv.getAttribute('fixedcolumns');
	var fixedLines	 = isyTableDiv.getAttribute('fixedlines');
	var selectedRow = isyTableDiv.getAttribute('lastselectedrowid');
		
	if ((rowColor = isyTableDiv.getAttribute('rowcolor')) == null) {
		rowColor = 'black';
	}
	
	if ((rowBackgroundColor = isyTableDiv.getAttribute('rowbackgroundcolor')) == null) {
		rowBackgroundColor = 'white';
	}
	
	if ((altRowColor = isyTableDiv.getAttribute('altrowcolor')) == null) {
		altRowColor = 'black';
	}
	
	if ((altRowBackgroundColor = isyTableDiv.getAttribute('altrowbackgroundcolor')) == null) {
		altRowBackgroundColor = 'lightgrey';
	}
	
	if (fixedColumns > 0) {
		fixedColumnsTable = document.getElementById(tableId + '_FC');
		fixedColumnsLines = fixedColumnsTable.getElementsByTagName('tr');
	}
	
	var isyTable 	= document.getElementById(tableId + '_TABLE');
	var tableRows	= isyTable.getElementsByTagName('tr');
	
	rowCount = 0;
	
	for (var i = fixedLines; i < tableRows.length; i++) {
		rowCols = tableRows[i].getElementsByTagName('td');
		
		if (selectedRow != tableRows[i].id) {
			if (fixedLines > 0 && fixedColumns > 0) {
				fixedColumnRowCols = fixedColumnsLines[i].getElementsByTagName('td');
			}
			
			if ((rowCount % 2) == 0) {
				rowCol = rowColor;
				rowBgCol = rowBackgroundColor;
			} else {
				rowCol = altRowColor;
				rowBgCol = altRowBackgroundColor;
			}
			
			for (var j=0; j < rowCols.length; j++) {
				rowCols[j].style.color = rowCol;
				rowCols[j].style.backgroundColor = rowBgCol;
			}
			
			if (fixedColumns > 0) {
				for (var k=0; k < fixedColumnRowCols.length; k++) {
					fixedColumnRowCols[k].style.color = rowCol;
					fixedColumnRowCols[k].style.backgroundColor = rowBgCol;
				}
			}
		}
		
		rowCount++;
	}
}
	
function column(id, value) {
	this.id = id;
	this.value = value;
}

function filterColumn(evt) {
	var el = getElement(evt);
	
	var colId = el.getAttribute('columnId');
	var tableId = el.getAttribute('tableId');
	
	var activeFilter = new Array();
	var compareFilter = '';
	
	eval("var filterColumns = "+tableId+"_filterColumns;");
	
	filterTable = document.getElementById(tableId).getElementsByTagName('TBODY');
	
	if ((hasFixedColumns = parseInt(document.getElementById(tableId).getAttribute('fixedColumns')))) {
		fixedFilterTable = document.getElementById(tableId+"_FC").getElementsByTagName('TBODY');	
	}
	
	for (var k = 0 ; k < filterColumns.length; k++) {
		if (filterColumns[k] != "NO_FILTER") {
			var filterBox = document.getElementById(tableId+"_FILTERBOX_"+k);
			activeFilter[k] = filterBox.options[filterBox.selectedIndex].value;
			compareFilter += filterBox.options[filterBox.selectedIndex].value;
		} else {
			activeFilter[k] = '';
		}
	}

	var cols = filterColumns.length;
	var rows = filterColumns[0].length;
	var filter = new Array();
	var hasFilter = true;
	
	for (var k=0; k < rows; k++) {
		for (var j=0; j < cols; j++) {
			if (filterColumns[j] != "NO_FILTER") {
				filter[j] = filterColumns[j][k].value;
				rowToFilter = filterColumns[j][k].id;
			} else {
				filter[j] = '';
			}
		}
		
		toFilter = '';
				
		for (var u=0; u < filter.length; u++) {
			if (activeFilter[u] == "ALL") {
				toFilter += "ALL";
			} else {
				toFilter += filter[u];
			}
		}
			
		if (compareFilter == toFilter) {
			document.getElementById(rowToFilter).style.display = '';
				
			if (hasFixedColumns) {
				document.getElementById(rowToFilter+"_FC").style.display = '';
			}
		} else {
			document.getElementById(rowToFilter).style.display = 'none';
			
			if (hasFixedColumns) {
				document.getElementById(rowToFilter+"_FC").style.display = 'none';
			}
		}
	}
}
	
function sortColumn(evt) {
	if (typeof(evt) != "string") {
		var el = getElement(evt);
		var col = document.getElementById(getColumnFrom(el));
		var colIdx = col.getAttribute('columnId');
	} else {
		var el = document.getElementById(evt);
		var col = document.getElementById(getColumnFrom(el) + "_FL");
		var colIdx = col.getAttribute('columnId');
	}

	if (el.tagName == "SELECT") return;

	var sTable = document.getElementById(getTableFrom(col));
	var baseTableId = sTable.getAttribute('baseTable');
	
	if (baseTableId != null) {
		sTable = document.getElementById(baseTableId + '_TABLE');
		var baseTable = document.getElementById(baseTableId);
	}
	
	if ((sortarrows = baseTable.getAttribute('sortarrows')) == null) {
		var sortarrows = false;
	}
	
	descend = col.getAttribute('descend');
	
	if(descend || descend == "true") {
		col.setAttribute('descend', false);
		
		if (sortarrows) {
			col.style.backgroundImage = 'url(gfx/z-a.gif)';
		}
	} else {
		col.setAttribute('descend', true);
		
		if (sortarrows) {
			col.style.backgroundImage = 'url(gfx/a-z.gif)';
		}
	}
	
	if (lastSortCol) {
		if (lastSortCol != col) {
			lastSortCol.style.backgroundImage = 'url(gfx/blank.gif)';
		}
	}
				
	eval("var sortColumns = " + baseTableId + "_sortColumns;");
	
	sortColumns[colIdx].sort(natcompare);
	
	if ((hasFixedColumns = parseInt(baseTable.getAttribute('fixedcolumns')))) {
		var fixedSortTable = document.getElementById(baseTableId + "_FC");
	}
	
	for ( var n = 0; n < sortColumns[colIdx].length; n++ ) 
	{
		sTable.firstChild.appendChild( document.getElementById( sortColumns[colIdx][n].id ) );

		if ( hasFixedColumns ) 
		{
   			fixedSortTable.firstChild.appendChild( document.getElementById(sortColumns[colIdx][n].id + "_FC" ) );
   		}
	}

	lastSortCol = col;
	
	if ((toggleRowStyles = baseTable.getAttribute('togglerowstyles')) != null) {
		setRowStyles(baseTableId);
	}
}

function fillFilterBoxes(id) {
	var filterArray = eval(id+"_filterColumns");
	var isyTable = document.getElementById(id);
	
	for (var k = 0 ; k < filterArray.length; k++) {
		if (filterArray[k] != "NO_FILTER") {
			var optionCounter = 1;
			var filterValues = new Array();
				
			for (i in filterArray[k]) {
				var value = filterArray[k][i].value;
	
				if (!inArray(filterArray[k][i].value, filterValues)) {
					filterValues[optionCounter++] = value;
				}		
			}
			
			if (filterValues.length > 0) {
				var filterBox = document.getElementById(id+"_FILTERBOX_"+k);
				
				filterBox.options.length = filterValues.length;
			
				if ((filterBoxAllText = isyTable.getAttribute('filterboxalltext')) == null) {
					var filterBoxAllText = "Alle";
				}
				
				filterBox.options[0].text = filterBoxAllText;
				filterBox.options[0].value = 'ALL';
					
				for (i in filterValues) {
					filterBox.options[i].text = filterValues[i];
					filterBox.options[i].value = filterValues[i];
				}
				
				if (ns6) {
					filterBox.selectedIndex = -1;
				}
			}
		}	
	}
}

function IsyCompare(a,b) {
	a = a.value;
	b = b.value;	
	
	if (a == null) {
		return -1;
	}
	
	if (b == null) {
		return 1;
	}

	a_tmp = parseFloat(a);
	b_tmp = parseFloat(b);
	
	if (!isNaN(a_tmp)) {
	   	a = a_tmp;
	}
	    
	if (!isNaN(b_tmp)) {
	  	b = b_tmp;
	}
	
	/**
	 * Clemens 
	 
	if (a.match(/^-*[0-9.,]+$/) && b.match(/^-*[0-9.,]+$/)) {
	    if (!isNaN(a_tmp)) {
	    	a = a_tmp;
	    }
	    
	    if (!isNaN(b_tmp)) {
	    	b = b_tmp;
	    }
	}
    */
    
    var res;
    
    if (typeof(a) == typeof(b)) {
	    if (a < b) {
	    	res = -1;
	    } else {
	    	if (a == b) {
	    		res = 0;
	    	} else {
	    		res = 1;
	    	}
	    }
	} else {
		if (typeof(a) == "number") {
			res = -1;
		} else {
			res = 1;
		}
	}
	
    if (descend || descend == "true") {
    	res = res * -1;
    }
    
    return res;
}

function tableSubmit(evt) {
	column 	= getElement(evt);
	rowid 	= getRowFrom(column);
	tableid = getTableFrom(column);
	document.isycat[tableid].value = rowid;
	ekSubmit(tableid);
}

function getRowCount(id) {
	var tableDiv = getElement(id);
	var table = getElement(id + '_TABLE');	
	var fixedlines = tableDiv.getAttribute('fixedlines');
	rowcount = table.rows.length - parseInt(fixedlines);
	return rowcount;
}

function IsyTable_reset(id) {
	var element = $(id + '_TABLE');
	
	for (var i=element.rows.length - 1; i >= fixedLines; i-- ) {
		element.down().removeChild(element.rows[i]);
	}
	
	document.isycat[id].value = '';
}

/*
natcompare.js -- Perform 'natural order' comparisons of strings in JavaScript.
Copyright (C) 2005 by SCK-CEN (Belgian Nucleair Research Centre)
Written by Kristof Coomans <kristof[dot]coomans[at]sckcen[dot]be>

Based on the Java version by Pierre-Luc Paour, of which this is more or less a straight conversion.
Copyright (C) 2003 by Pierre-Luc Paour <natorder@paour.com>

The Java version was based on the C version by Martin Pool.
Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/


function isWhitespaceChar(a)
{
    var charCode;
    charCode = a.charCodeAt(0);

    if ( charCode <= 32 )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function isDigitChar(a)
{
    var charCode;
    charCode = a.charCodeAt(0);

    if ( charCode >= 48  && charCode <= 57 )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function compareRight(a,b)
{
    var bias = 0;
    var ia = 0;
    var ib = 0;

    var ca;
    var cb;

    // The longest run of digits wins.  That aside, the greatest
    // value wins, but we can't know that it will until we've scanned
    // both numbers to know that they have the same magnitude, so we
    // remember it in BIAS.
    for (;; ia++, ib++) {
        ca = a.charAt(ia);
        cb = b.charAt(ib);

        if (!isDigitChar(ca)
                && !isDigitChar(cb)) {
            return bias;
        } else if (!isDigitChar(ca)) {
            return -1;
        } else if (!isDigitChar(cb)) {
            return +1;
        } else if (ca < cb) {
            if (bias == 0) {
                bias = -1;
            }
        } else if (ca > cb) {
            if (bias == 0)
                bias = +1;
        } else if (ca == 0 && cb == 0) {
            return bias;
        }
    }
}

function natcompare(a,b) 
{
	// -og- 080623 adjusted to IsyCat usage
	a = a.value;
	if ( a == null )
		return -1;
	b = b.value;
	if ( b == null )
		return 1;

	if ( regexpNumber.test( a ) && regexpNumber.test( b ) ) 
	{
		// -og- 080701: two doubles -> we don't need to use natcompare
		a = a.replace( ',', '.' );
		b = b.replace( ',', '.' );
		
		var da = parseFloat( a );
		var db = parseFloat( b );
		if ( !isNaN( da ) && !isNaN( db ) )
		{
		    if ( descend || descend == "true" ) 
		    {
		        if ( da <= db )
		            return 1;
		        else if ( da > db )
		            return -1;
			}
			else
			{
		        if ( da <= db ) 
		            return -1;
		        else if ( da > db )
		            return +1;
			}
		}
		else if ( !isNaN( da ) )
		{
		    if ( descend || descend == "true" ) 
		        return 1;
	        else return -1;
		}
		else if ( !isNaN( db ) )
		{
		    if ( descend || descend == "true" ) 
		        return -1;
	        else return 1;
		}
		
		return 1;
	}
	
    var ia = 0, ib = 0;
	var nza = 0, nzb = 0;
	var ca, cb;
	var result;

    while (true)
    {
        // only count the number of zeroes leading the last number compared
        nza = nzb = 0;

        ca = a.charAt(ia);
        cb = b.charAt(ib);

        // skip over leading spaces or zeros
        while ( isWhitespaceChar( ca ) || ca =='0' ) {
            if (ca == '0') {
                nza++;
            } else {
                // only count consecutive zeroes
                nza = 0;
            }

            ca = a.charAt(++ia);
        }

        while ( isWhitespaceChar( cb ) || cb == '0') {
            if (cb == '0') {
                nzb++;
            } else {
                // only count consecutive zeroes
                nzb = 0;
            }

            cb = b.charAt(++ib);
        }

        // process run of digits
        if (isDigitChar(ca) && isDigitChar(cb)) {
            if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) {
            
				// -og- 080623 adjusted to IsyCat usage
			    if (descend || descend == "true") {
    				result = result * -1;
    			}
    			
                return result;
            }
        }

        if (ca == 0 && cb == 0) {
            // The strings compare the same.  Perhaps the caller
            // will want to call strcmp to break the tie.
            
			// -og- 080623 adjusted to IsyCat usage
		    if (descend || descend == "true") {
	            return nzb - nza;
			}
			
            return nza - nzb;
        }

		// -og- 080623 adjusted to IsyCat usage
	    if (descend || descend == "true") 
	    {
	        if (ca < cb) {
	            return 1;
	        } else if (ca > cb) {
	            return -1;
	        }
		}
		else
		{
	        if (ca < cb) {
	            return -1;
	        } else if (ca > cb) {
	            return +1;
	        }
		}
		
        ++ia; ++ib;
    }
}

