// Copyright 2001 Roman Koch (roman@romankoch.ch).
// See http://www.romankoch.ch/capslock/tablesort.htm for more information.

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 CompareAlphaName(a, b) {
    strA = a[currentCol].toLowerCase().split(" ");
    strB = b[currentCol].toLowerCase().split(" ");
    nameA = strA[1];
    nameB = strB[1];
    if (nameA < nameB) { return -1; }
    else {
	if (nameA > nameB) { 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": // ASCII
	    myArray.sort(CompareAlpha);
	    break;
	case "ai": // case-Insensitive ASCII
	    myArray.sort(CompareAlphaIgnore);
	    break;
	case "an": // case-Insensitive ASCII, but sort on last word (for Names)
	    myArray.sort(CompareAlphaName);
	    break;
	case "d": // American-style date mm/dd/yyyy
	    myArray.sort(CompareDate);
	    break;
	case "de": // European-style date dd.mm.yyyy 
	    myArray.sort(CompareDateEuro);
	    break;
	case "n": // Numerical
	    myArray.sort(CompareNumeric);
	    break;
	default:
	    myArray.sort();
	}
    }
    
    // Re-write the table contents
    for (i=0; i < myRows; i++) {
	for (j=0; j < myCols; j++) {
	    mySource.rows(i).cells(j).innerText = myArray[i][j];
	}
    }
    
    previousCol = myCol; // remember the current sort column for the next pass
    return 0;
}

