//'***********************************************************************************************************************
// PAGE DESCRIPTION:
// 	Client-side coding use to sort the contents of a table.

// PAGE NOTES:
// 	[Need to enter examples of the client side coding, and how to structure the table.]

// ISSUES/TODO:

// LATER VERSION IDEAS:

// MODIFICATIONS:
//'***********************************************************************************************************************

//'***********************************************************************************************************************
// CLIENT SIDE PRE-PROCESSING
//'***********************************************************************************************************************
// Declare Local Variables.
var mnTSCurrentCol = 0
var mnTSPreviousCol = -1
var mnTSSortId = 1

//'***********************************************************************************************************************
// CLIENT SIDE ROUTINES
//'***********************************************************************************************************************
// Initializes the Table Sort routines.
// 	nSortCol = Which column index was the table originally sorted by.
//		nSortId = 1 for ascending; -1 for descending.  Any other value will be set to 1.
//'***********************************************************************************************************************
function tsInit(nSortCol, nSortId) {

	mnTSPreviousCol = nSortCol;
//	if (math.abs(nSortId) != 1) {
//		mnTSSortId = 1;
//	}
//	else {
		mnTSSortId = nSortId;
//	}

	window.status = "SortCol=" + mnTSPreviousCol + "|SortId=" + mnTSSortId;
}

//'***********************************************************************************************************************
//'***********************************************************************************************************************
function tsTableSort(oTable, nCol, sType) {

	// Create a two-dimensional array and fill it with the table's content
	var oSourceTable = document.getElementById(oTable);
	var nRowCount = oSourceTable.rows.length;
	var nColCount = oSourceTable.rows(0).cells.length;

	mnTSCurrentCol = nCol
	aArray = new Array(nRowCount)

	for (i=0; i < nRowCount; i++) {
		aArray[i] = new Array(nColCount)
		for (j=0; j < nColCount; j++) {
			aArray[i][j] = document.getElementById(oTable).rows(i).cells(j).innerHTML;
//Debug Code
//			if (j==nCol) {
//				alert (document.getElementById(oTable).rows(i).cells(j).innerHTML);
//			}
		}
	}

	if (nCol == mnTSPreviousCol) {
		// clicked the same column as previously - reverse the sort
		aArray.reverse(); 
		mnTSSortId = mnTSSortId * (-1);
	}
	else { 
		// clicked on a new column - sort as indicated
		mnTSSortId = 1;
		switch (sType) {
			case "a":
				aArray.sort(pri_tsCompareAlpha);
				break;
			case "ai":
				aArray.sort(pri_tsCompareAlphaIgnore);
				break;
			case "d":
				aArray.sort(pri_tsCompareDate);
				break;
			case "de":
				aArray.sort(pri_tsCompareDateEuro);
				break;
			case "n":
				aArray.sort(pri_tsCompareNumeric);
				break;
			default:
				aArray.sort()
		}
	}

	// Re-write the table contents
	for (i=0; i < nRowCount; i++) {
		for (j=0; j < nColCount; j++) {
			oSourceTable.rows(i).cells(j).innerHTML = aArray[i][j]
		}
	}

	// remember the current sort column for the next pass
	mnTSPreviousCol = nCol; 

	window.status = "SortCol=" + mnTSPreviousCol + "|SortId=" + mnTSSortId;

//	return 0;
}

//'***********************************************************************************************************************
// PRIVATE CLIENT SIDE ROUTINES
//'***********************************************************************************************************************
// Compare Alpha Type Columns.  Case sensative.
//'***********************************************************************************************************************
function pri_tsCompareAlpha(a, b) {
	if (a[mnTSCurrentCol] < b[mnTSCurrentCol]) { return -1; }
	if (a[mnTSCurrentCol] > b[mnTSCurrentCol]) { return 1; }
	return 0;
}

//'***********************************************************************************************************************
// Compare Alpha Type Columns, not case sensative.
//'***********************************************************************************************************************
function pri_tsCompareAlphaIgnore(a, b) {
	strA = a[mnTSCurrentCol].toLowerCase();
	strB = b[mnTSCurrentCol].toLowerCase();
	if (strA < strB) { return -1; }
	else {
		if (strA > strB) { return 1; }
		else { return 0; }
	}
}

//'***********************************************************************************************************************
// Compare standard dates
//'***********************************************************************************************************************
function pri_tsCompareDate(a, b) {
	// this one works with date formats conforming to Javascript specifications, e.g. m/d/yyyy
	datA = new Date(a[mnTSCurrentCol]);
	datB = new Date(b[mnTSCurrentCol]);
	if (datA < datB) { return -1; }
	else {
		if (datA > datB) { return 1; }
		else { return 0; }
	}
}

//'***********************************************************************************************************************
// Compare European Date Formats, or other unusual formats.
//'***********************************************************************************************************************
function pri_tsCompareDateEuro(a, b) {
	// this one works with european date formats, e.g. d.m.yyyy
	strA = a[mnTSCurrentCol].split(".");
	strB = b[mnTSCurrentCol].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; }
	}
}

//'***********************************************************************************************************************
// Compare Number
//'***********************************************************************************************************************
function pri_tsCompareNumeric(a, b) {
	numA = a[mnTSCurrentCol]
	numB = b[mnTSCurrentCol]
	if (isNaN(numA)) { return 0;}
	else {
		if (isNaN(numB)) { return 0; }
		else { return numA - numB; }
	}
}
