currentCol = 0
previousCol = -1
toReverse  = false

/*
 * Compare Alphabetic data
 */
function CompareAlpha(a, b) {
	if (a[currentCol] < b[currentCol]) { return -1; }
	if (a[currentCol] > b[currentCol]) { return 1; }
	return 0;
}

/*
 * Compare Alphabetic data ignoring the case.
 */

function CompareAlphaIgnore(a, b) {
	

	strA = a[currentCol].toLowerCase();
	strB = b[currentCol].toLowerCase();

	strA=removeHtmlTags(strA);
	strB =removeHtmlTags(strB);


	
	if (strA < strB) { 
		toReverse = true ;
		return -1; 
	}
	else {
		if (strA > strB) { 
			toReverse = true;
			return 1; 
		}
		else { 
			return 0; 
		}
	}
}

/*
 * Compare Dates.
 */

function CompareDate(a, b) {
	// this one works with date formats conforming to Javascript specifications, e.g. m/d/yyyy
	
	datA = new Date(extractContents(a[currentCol]));
	datB = new Date(extractContents(b[currentCol]));
	if (datA < datB) {
		toReverse = true;
		return -1;
	}
		
	else {
		if (datA > datB) {
			toReverse = true;
			return 1; 
		}
		else { 
			return 0; 
		}
	}
}


function removeHtmlTags(strSource) {
	strValue=strSource.replace(/\<.*?\>/g, "");
	return strValue;
	}



/*
 * Extract the contents from the TD
 */
function extractContents(str){

var indexOfClsBrace = 0;
var indexOfOpnBrace = 0;

indexOfClsBrace = str.indexOf(">");
str = str.substr(indexOfClsBrace+1);

indexOfOpnBrace = str.indexOf("<");
str = str.substr(0,indexOfOpnBrace);

return str;

}

/*
 * Compare European date.
 */
function CompareDateEuro(a, b) {
	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; }
	}
}

/*
 * Compare numeric data.
 */
function CompareNumeric(a, b) {
	numA = a[currentCol]
	numB = b[currentCol]
	if (isNaN(numA)) { return 0;}
	else {
		if (isNaN(numB)) { return 0; }
		else { return numA - numB; }
	}
}


/*
 * Method to sort tables.
 */
function TableSort(myTable, myCol, myType) {

	// Create a two-dimensional array and fill it with the table's content

	var mySource = document.getElementById(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.getElementById(myTable).rows[i].cells[j].innerHTML;
			
		}
	}

	if (myCol == previousCol) {
		if(toReverse == true)
			myArray.reverse(); 
	}
	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++) {
			mySource.rows[i].cells[j].innerHTML = myArray[i][j]
		}
	}
	
	// remember the current sort column for the next pass
	previousCol = myCol; 
	return 0;
}



