/*
This faqt is an extention to the "How can I sort a TABLE with DOM 
access in IE5 and NN6?" faqt by Martin Honnen - thanks Martin!
Please read his faqt and the one about table sorting in Netscape 4 if 
you need support for that browser.
The following contains code has been tested in IE5.00, Netscape6.1 and 
Kmeleon0.6 (the lightweight Gecko based browser for win32).
*/
function findTableParent(node) {
  	while(node.tagName.toUpperCase() != 'TABLE') {
		node = node.parentNode;
	}
return node;
}

function findColumnParent(node) {
	// alert('node: '+node);
	while(node.tagName.toUpperCase() != 'TD') {
		// alert('Vorher: '+node.tagName);
		node = node.parentNode;
		// alert(node.tagName);
	}
return node;
}

function createRowsArray(table) {
	var rows = new Array();
	var r = 0;
	if((table.tHead == null) && (table.tFoot == null)) {
		for(var r1 = 0; r1 < table.rows.length; r1++, r++) {
      			rows[r] = table.rows[r1];
		}
	} else {
    		for(var t = 0; t < table.tBodies.length; t++) {
			for(var r1 = 0; r1 < table.tBodies[t].rows.length; r1++, r++) {
				rows[r] = table.tBodies[t].rows[r1];
			}
		}
	}
  return rows;
}

function insertSortedRows(table, rows) {
	
  	if(document.all) {
		var rowsCopy = new Array(rows.length);
	}
	for(var r = 0; r < rows.length; r++) {
    		if(document.all) {
			rowsCopy[r] = rows[r].cloneNode(true);
		}
		table.deleteRow(rows[r].rowIndex);
	}
	var tableSection = table.tBodies[table.tBodies.length - 1];
	var rowsToHide=filters[currentFilter];
	var rowsDisplayNumber=0;
	for(var r = 0; r < rows.length; r++) {
		var row = document.all ? rowsCopy[r] : rows[r];
		var rowFiltered=false;
		for(var j=0;j<rowsToHide.length;j++) {
			if(row.id==rowsToHide[j]) {
				row.style.display='none';
				rowFiltered=true;
			}
		}
		if(rowFiltered==false) {
			var prefix = 'classic_line';
			if (row.className.indexOf('marked') > -1)
				prefix = 'classic_line_marked'; 
			if(rowsDisplayNumber%2==0) {
				row.className = prefix+'_0';
			} else {
				row.className = prefix+'_1';
			}
			document.all?row.style.display='inline':row.style.display='table-row';
			rowsDisplayNumber++;
		}
		tableSection.appendChild(row);
	}
}

function sortTable(table, sortFun) {
	var rows = createRowsArray(table);
	if(rows.length > 0) {
		if (sortFun.col > -1) {
	
			rows.sort(sortFun);
		}
		insertSortedRows(table, rows);
	}
	// Silly IE Bug
		sillyIEBug(); //defined in classicBet_.js
	// END IE Bug

}

function compareAlessThanB(A,B){
  	return A < B ? - 1 :(A == B ? 0 : 1);
}
function compareBlessThanA(A,B){
	return B < A ? - 1 :(A == B ? 0 : 1);
}
function sortRowsAlpha(row1 , row2) {
	var column = sortRowsAlpha.col;
	var cell1 = row1.cells[column].firstChild ? row1.cells[column].firstChild.nodeValue: '' ;
	var cell2 = row2.cells[column].firstChild ? row2.cells[column].firstChild.nodeValue: '';
	// alert('Cell1: '+cell1+' Cell2: '+cell2);
	return sortRowsAlpha.compare(cell1,cell2);
}
function sortRowsCustom(row1 , row2) {
	var column = sortRowsCustom.col;
	var cell1 = sortRowsCustom.getValue(row1.cells[column].firstChild);
	var cell2 = sortRowsCustom.getValue(row2.cells[column].firstChild);
	return sortRowsCustom.compare(cell1,cell2);
}

function sortRowsTwoColumns(row1, row2) {
	var column = sortRowsTwoColumns.col; 
	var betID1 = row1.cells[column].id.substring(3, row1.cells[column].id.indexOf("_"));
	var betID2 = row2.cells[column].id.substring(3, row2.cells[column].id.indexOf("_"));
	var cell1a = startDatesArray[betID1]
	var cell2a = startDatesArray[betID2]
//	var cell1a = row1.cells[column].firstChild.nodeValue;
//	var cell2a = row2.cells[column].firstChild.nodeValue;
    var cell1b = row1.cells[column+1].firstChild.nodeValue;
	var cell2b = row2.cells[column+1].firstChild.nodeValue;
	var date1=cell1a.split('.');
	var time1=cell1b.split(':');
	var date2=cell2a.split('.');
	var time2=cell2b.split(':');
	var value1=date1[2]+''+date1[1]+''+date1[0]+''+time1[0]+''+time1[1];
	var value2=date2[2]+''+date2[1]+''+date2[0]+''+time2[0]+''+time2[1];
	return sortRowsTwoColumns.compare(value1,value2);
}

function doSortTableTwoColumns(table, col, direction) {
	sortRowsTwoColumns.col = col;
	if( direction == true ){
		sortRowsTwoColumns.compare = compareBlessThanA;
	} else {
		sortRowsTwoColumns.compare = compareAlessThanB;
  	}
	sortTable(table, sortRowsTwoColumns);
	return direction = (direction!=true)?true:false;
}

function getNumericalValue(myRow, myCol) {
var value=null;
if(myRow.cells.length>myCol) {
		value=myRow.cells[myCol].firstChild?myRow.cells[myCol].firstChild.nodeValue : '999999'; //SL '-1'
	}
	if(value!=null) {
		value=value.replace(/,/,'.');
	} else {
		value='999999';//SL '-1'
	}
	var myNumericalValue = parseFloat(value);
	if(isNaN(myNumericalValue)) {
		myNumericalValue=999999; //SL -1
	}
	return(myNumericalValue);
	
}


function sortRowsNumber(row1 , row2) {
	var column = sortRowsNumber.col;
	var cell1 = getNumericalValue(row1, column);
	var cell2 = getNumericalValue(row2, column);
	// alert('Cell1: '+cell1+' Cell2: '+cell2);
	return sortRowsNumber.compare(cell1,cell2);
}

/*
  Sort a table column alphabetically.
  @param table The table to be sorted.
  @param col The column to sorted upon.
  @param direction Cause ascending order ('a' first) if null or false.
  @returns <tt>!direction</tt> so that repeated calls of the form 
<tt>direction=doSortTable(a,b,direction);</tt> cause the table column 
to alternate between ascending and descending order.
*/

function doSortTable(table, col, direction) {
	sortRowsAlpha.col = col;
	if( direction == true ){
		sortRowsAlpha.compare = compareBlessThanA;
	} else {
		sortRowsAlpha.compare = compareAlessThanB;
  	}
	sortTable(table, sortRowsAlpha);
	return direction = (direction!=true)?true:false;
}
/*
  Sort a table column based on a custom numerical 'order' criteria of 
the cell.
  @param table The table to be sorted.
  @param col The column to sorted upon.
  @param customFunction Provides an order value given the contence of 
the cell.
  @param direction Cause ascending order if null or false.
  @returns <tt>!direction</tt> so that repeated calls of the form 
<tt>direction=doSortTable(a,b,direction);</tt> cause the table column 
to alternate between ascending and descending order.
*/
function doSortTableCustom(table, col, customFunction, direction) {
	sortRowsCustom.col = col;
	sortRowsCustom.getValue = customFunction;
	if( direction == true ){
		sortRowsCustom.compare = compareBlessThanA;
	} else {
		sortRowsCustom.compare = compareAlessThanB;
	}
	sortTable(table, sortRowsCustom);
	return direction = (direction!=true)?true:false;
}

/*
  Sort a table column numerically.
  @param table The table to be sorted.
  @param col The column to sorted upon.
  @param direction Cause ascending order ('1' first) if null or false.
  @returns <tt>!direction</tt> so that repeated calls of the form 
<tt>direction=doSortTable(a,b,direction);</tt> cause the table column 
to alternate between ascending and descending order.
*/
function doSortTableNumerical(table, col, direction) {
	sortRowsNumber.col = col;
	if( direction == true ){
		 sortRowsNumber.compare = compareBlessThanA;
	} else {
	sortRowsNumber.compare = compareAlessThanB;
	}
	sortTable(table, sortRowsNumber);
	return direction = (direction!=true)?true:false;
}


/*
  Sort a table column nothing, only to set filter.
  @param table The table to be filtered.
  @param direction Cause ascending order ('1' first) if null or false.
  @returns <tt>!direction</tt> so that repeated calls of the form 
<tt>direction=doSortTable(a,b,direction);</tt> cause the table column 
to alternate between ascending and descending order.
*/
function doSortTableNothing(table, direction) {
	sortRowsNumber.col = -1;
	if( direction == true ){
		 sortRowsNumber.compare = compareBlessThanA;
	} else {
	sortRowsNumber.compare = compareAlessThanB;
	}
	sortTable(table, sortRowsNumber);
	return direction = (direction!=true)?true:false;
}


function getOrderBasedOnLiga(ligaNode) {
	if((ligaNode.nodeType==1) && (ligaNode.nodeName=='A')) {
		if(ligaSortArray[ligaNode.parentNode.getAttribute("id")]!=null) {
//alert(' return ligaSortArray[ligaNode.parentNode.getAttribute("id")]['+ligaNode.parentNode.getAttribute("id")+']: '+ligaSortArray[ligaNode.parentNode.getAttribute("id")]);			
			return(ligaSortArray[ligaNode.parentNode.getAttribute("id")]);
		}
	}else if((ligaNode.nodeType==1) && (ligaNode.nodeName=='TD')) {
		if(ligaSortArray[ligaNode.getAttribute("id")]!=null) {
//alert(' return ligaSortArray[ligaNode.getAttribute("id")]['+ligaNode.getAttribute("id")+']: '+ligaSortArray[ligaNode.getAttribute("id")]);			
			return(ligaSortArray[ligaNode.getAttribute("id")]);
		}
	} else { 
		if(ligaNode.nodeType==3) { // Text
			return(getOrderBasedOnLiga(ligaNode.parentNode));
		}
	}
	return(0);
}

	imageArray=new Array();
	imageArray[0]='SORTNUMBER';
	imageArray[1]='SORTDATE';
	imageArray[2]='SORTLEAGUE';
	imageArray[3]='SORTQUOTE1';
	imageArray[4]='SORTQUOTEX';
	imageArray[5]='SORTQUOTE2';

	function changeImage(imgElement,currentDirection) {
		for(var i=0;i<imageArray.length;i++) {
			if(imgElement.id==imageArray[i]) {
				currentDirection?imgElement.src='../images/yellow_down.gif':imgElement.src='../images/yellow_up.gif';
			} else {
				var tmpElement=document.getElementById(imageArray[i]);
				if(tmpElement.direction) {
					tmpElement.src='../images/down.gif'
				} else {
					tmpElement.src='../images/down.gif'
				}
			}
		}
	}

	function initSort(){
		// alert('Vorher: '+initSortArray['SORTDATE']);
		for(var elemID in initSortArray) {
			var elem = document.getElementById(elemID);
			elem.direction=initSortArray[elemID];
		}
		// alert('nachher: '+initSortArray['SORTDATE']+' '+document.getElementById('SORTDATE').direction);

	}


/*
               +-------------------------------------------------------------------------------------------------------- +
THEAD:         | * Nr. | * Annahmeschluss | * Liga | * Spielpaarung *   |   * 1     |   * X     |    * 2    | Tipp | Erg |
HeadCellIndex: |   0   |         1        |    2   |          3         |     4     |     5     |     6     |   7  |  8  |
               +-------------------------------------------------------------------------------------------------------- +
TBODY:         |  Num  | Datum   | Time   |  Liga  | top | Home | Guest | Q1 | CHBX | Qx | CHBX | Q2 | CHBX | Tipp | Erg |
BodyCellIndex: |   0   |   1     |   2    |    3   |  4  |   5  |   6   |  7 |   8  |  9 |  10  | 11 |  12  | 13   | 14  |
               +-------------------------------------------------------------------------------------------------------- +
DiffCellIndex: |   0   |   0     |    -   |   +1   |  -  |  +2  |  +3   | +3 |  -   | +4 |  -   | +5 |  -   |   -  |  -  |
               +-------------------------------------------------------------------------------------------------------- +
*/ 

/*
               +--------------------------------------------------------------------------------------------------------------+
THEAD:         | * Nr. | * Annahmeschluss | * Liga | * Spielpaarung *   |   * 1          |   * X     |    * 2    | Tipp | Erg |
HeadCellIndex: |   0   |         1        |    2   |          3         |     4          |     5     |     6     |   7  |  8  |
               +------------------------------------------------------------------------------------------------------------- +
TBODY:         |  Num  | Datum   | Time   |  Liga  | top | Home | Guest |Status| Q1 |CHBX| Qx | CHBX | Q2 | CHBX | Tipp | Erg |
BodyCellIndex: |   0   |   1     |   2    |    3   |  4  |   5  |   6   |  7   | 8  |  9 | 10 | 11   | 12 |  13  |  14  | 15  |
               +------------------------------------------------------------------------------------------------------------- +
DiffCellIndex: |   0   |   0     |    -   |   +1   |  -  |  +2  |  +3   |hidden| +4 |  - | +5 |  -   | +6 |  -   |   -  |  -  |
               +------------------------------------------------------------------------------------------------------------- +
*/ 

	function doSort(sortElementID) {
		var sortElement=document.getElementById(sortElementID);
		var currentDirection=sortElement.direction;
		currentSortElementID=sortElementID;
//alert('Im doSort('+sortElementID+') mit '+currentDirection);
		switch(sortElementID) {
			case 'SORTNUMBER':	currentDirection=doSortTableNumerical(findTableParent(sortElement), findColumnParent(sortElement).cellIndex, currentDirection);
						break;
			case 'SORTDATE':	currentDirection=doSortTableTwoColumns(findTableParent(sortElement), findColumnParent(sortElement).cellIndex, currentDirection);
						break;
			case 'SORTLEAGUE':	currentDirection=doSortTableCustom(findTableParent(sortElement), findColumnParent(sortElement).cellIndex+1, getOrderBasedOnLiga, currentDirection);
						break;
			case 'SORTQUOTE1':	currentDirection=doSortTableNumerical(findTableParent(sortElement), findColumnParent(sortElement).cellIndex+4, currentDirection);
						break;
			case 'SORTQUOTEX': 	currentDirection=doSortTableNumerical(findTableParent(sortElement), findColumnParent(sortElement).cellIndex+5, currentDirection);
						break;
			case 'SORTQUOTE2':	currentDirection=doSortTableNumerical(findTableParent(sortElement), findColumnParent(sortElement).cellIndex+6, currentDirection);
						break;
			case 'NOTHING':		currentDirection=doSortTableNothing(findTableParent(sortElement), currentDirection);
						break;
		}
		changeImage(sortElement, currentDirection);
		sortElement.direction=currentDirection;
		setCookie();
	}

//------------------------------------------------------------------------------------------

function setFilter(filtername) {
        if(document.filterform!=null && document.filterform.select != null){
		for(var i=0;i<document.filterform.select.length;i++) {
			if(document.filterform.select[i].value==filtername) {
				document.filterform.select[i].checked=true;
			}
		}	
	}	
	currentFilter=filtername;
	var sortElement=document.getElementById(currentSortElementID);
	// alert('Im setFilter() '+sortElement.direction+' '+currentSortElementID+' '+currentFilter);
	sortElement.direction?sortElement.direction=false:sortElement.direction=true;
	doSort(currentSortElementID);
}
