

currentCol = 0
previousCol = -1

function CompareAlpha(a, b) {
	if (a[currentCol] < b[currentCol]) { return -1; }
	if (a[currentCol] > b[currentCol]) { return 1; }
	return 0;
}




function CompareAlphaIgnore(a, b) {
	strA = a[currentCol].toLowerCase();
	strB = b[currentCol].toLowerCase();
	if (strA < strB) { return -1; }
	else {
		if (strA > strB) { return 1; }
		else { return 0; }
	}
}

function CompareDate(a, b) {
	// this one works with date formats conforming to Javascript specifications, e.g. m/d/yyyy
	datA = new Date(a[currentCol]);
	datB = new Date(b[currentCol]);
	if (datA < datB) { return -1; }
	else {
		if (datA > datB) { return 1; }
		else { return 0; }
	}
}

function CompareDateEuro(a, b) {
	// this one works with european date formats, e.g. d.m.yyyy
	strA = a[currentCol].split(".");
	strB = b[currentCol].split(".")
	datA = new Date(strA[2], strA[1], strA[0]);
	datB = new Date(strB[2], strB[1], strB[0]);
	if (datA < datB) { return -1; }
	else {
		if (datA > datB) { return 1; }
		else { return 0; }
	}
}

function CompareNumeric(a, b) {
	//window.alert ("CompareNumeric");
	numA = a[currentCol]
	numB = b[currentCol]
	if (isNaN(numA)) { return 0;}
	else {
		if (isNaN(numB)) { return 0; }
		else { return numA - numB; }
	}
}

function TableSort(myTable, myCol, myType) {

	// Create a two-dimensional array and fill it with the table's content
	var mySource = document.all(myTable);
	var myRows = mySource.rows.length;
	var myCols = mySource.rows(0).cells.length;
	currentCol = myCol
	myArray = new Array(myRows)
	for (i=0; i < myRows; i++) {
		myArray[i] = new Array(myCols)
		for (j=0; j < myCols; j++) {
			 
			myArray[i][j] = document.all(myTable).rows(i).cells(j).innerText
		}
	}

	if (myCol == previousCol) {
		myArray.reverse(); // clicked the same column as previously - reverse the sort
	}
	else { // clicked on a new column - sort as indicated
		switch (myType) {
			case "a":
				myArray.sort(CompareAlpha);
				break;
			case "ai":
				myArray.sort(CompareAlphaIgnore);
				break;
			case "d":
				myArray.sort(CompareDate);
				break;
			case "de":
				myArray.sort(CompareDateEuro);
				break;
			case "n":
				myArray.sort(CompareNumeric);
				break;
			default:
				myArray.sort()
		}
	}

	// Re-write the table contents
	for (i=0; i < myRows; i++) {
		for (j=0; j < myCols; j++) {
			if(j==0){ 
			mySource.rows(i).cells(0).innerHTML = '<a href="/info/'+myArray[i][1]+'"><img src="/failai/whois.png" alt="info"/></a>';  }else{

			mySource.rows(i).cells(j).innerText = myArray[i][j]; }
		}
	}

	previousCol = myCol; // remember the current sort column for the next pass
	return 0;
}


