/*
Table sorting handler, used in conjuction with MindMelon Framework Library
Partial portions based on a script from Stuart Langridge

April 10, 2007
*/

var image_path = "/MFL/Form/images/";
var image_up = "arrow-up.gif";
var image_down = "arrow-down.gif";
var image_none = "arrow-none.gif";
var europeandate = false;
var alternate_row_colors = true;

/* Don't change anything below this unless you know what you're doing */
addEvent(window, "load", sortables_init);
var SORT_COLUMN_INDEX;
var thead = false;

function sortables_init() {
	// Find all tables with class sortable and make them sortable
	if (!document.getElementsByTagName) return;
	tbls = document.getElementsByTagName("table");
	for (ti=0;ti<tbls.length;ti++) {
		thisTbl = tbls[ti];
		if (((' '+thisTbl.className+' ').indexOf("gridsortable") != -1) && (thisTbl.id)) {
			ts_makeSortable(thisTbl);
		}
	}
}

function ts_makeSortable(t) {
	if (t.rows && t.rows.length > 0) {
		if (t.tHead && t.tHead.rows.length > 0) {
			var firstRow = t.tHead.rows[t.tHead.rows.length-1];
			thead = true;
		} else {
			var firstRow = t.rows[0];
		}
	}
	if (!firstRow) return;
	
	// We have a first row: assume it's the header, and make its contents clickable links
	for (var i=0;i<firstRow.cells.length;i++) {
		var cell = firstRow.cells[i];
		var txt = ts_getInnerText(cell);
		if (cell.className != "unsortable" && cell.className.indexOf("unsortable") == -1) {
		  // What direction is this cell sorted by default?
	    if (cell.getAttribute("sortdir") == 'ASC') {
			    ARROW = '&nbsp;&nbsp;<img src="'+ image_path + image_up + '" style="border: 0px;" border="0" alt="&darr;"/>';
    			cell.innerHTML = '<a href="'+cell.getAttribute("link")+'&Dir=DESC&Order='+cell.getAttribute("sortorder")+'" class="sortheader">'+txt+'<span class="sortarrow">' + ARROW + '</span></a>';
	    } else if (cell.getAttribute("sortdir") == 'DESC') {
			    ARROW = '&nbsp;&nbsp;<img src="'+ image_path + image_down + '" style="border: 0px;" border="0" alt="&uarr;"/>';
    			cell.innerHTML = '<a href="'+cell.getAttribute("link")+'&Dir=ASC&Order='+cell.getAttribute("sortorder")+'" class="sortheader">'+txt+'<span class="sortarrow">' + ARROW + '</span></a>';
	    } else {
			    ARROW = '&nbsp;&nbsp;<img src="'+ image_path + image_none + '" style="border: 0px;" border="0" alt="&darr;"/>';
    			cell.innerHTML = '<a href="'+cell.getAttribute("link")+'&Dir=DESC&Order='+cell.getAttribute("sortorder")+'" class="sortheader">'+txt+'<span class="sortarrow">' + ARROW + '</span></a>';
			}
		}
	}
	if (alternate_row_colors) {
		alternate(t);
	}
}

function ts_getInnerText(el) {
	if (typeof el == "string") return el;
	if (typeof el == "undefined") { return el };
	if (el.innerText) return el.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = el.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //ELEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}

function addEvent(elm, evType, fn, useCapture)
// cross-browser event handling for IE5+,	NS6 and Mozilla
{
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be removed");
	}
}

function alternate(table) {
	// Take object table and get all it's tbodies.
	var tableBodies = table.getElementsByTagName("tbody");
	// Loop through these tbodies
	for (var i = 0; i < tableBodies.length; i++) {
		// Take the tbody, and get all it's rows
		var tableRows = tableBodies[i].getElementsByTagName("tr");
		// Loop through these rows
		// Start at 1 because we want to leave the heading row untouched
		for (var j = 0; j < tableRows.length; j++) {
			// Check if j is even, and apply classes for both possible results
			if ( (j % 2) == 0  ) {
				if ( !(tableRows[j].className.indexOf('odd') == -1) ) {
					tableRows[j].className = tableRows[j].className.replace('odd', 'even');
				} else {
					if ( tableRows[j].className.indexOf('even') == -1 ) {
						tableRows[j].className += " even";
					}
				}
			} else {
				if ( !(tableRows[j].className.indexOf('even') == -1) ) {
					tableRows[j].className = tableRows[j].className.replace('even', 'odd');
				} else {
					if ( tableRows[j].className.indexOf('odd') == -1 ) {
						tableRows[j].className += " odd";
					}
				}
			} 
		}
	}
}