// ------------------------------------------------------------------------------------- //// Vormo SortableTable Class// ------------------------------------------------------------------------------------- //// This is a class for making tables sortable// Author: Seth Mangum// Modified for NFS 9/6/2006Vormo.SortableTable = function(source, initKey) {	this.Table = document.getElementById(source);	this.Rows = new Array();	this.Types = new Array();	this.Head = document.createElement('thead');	this.HeadTR = document.createElement('tr');	this.Head.appendChild(this.HeadTR);		this.BrandsOffArray = new Array();	this.BranshShowCheckedOnly = false;		this.SortKey = 2;	this.SortDirection = -1;	this.lastSortedHead;	this.alertCheck = 0;	if (initKey) this.SortKey = initKey;		var currentBody = this.Table.getElementsByTagName('tbody')[0];	var newBody = document.createElement('tbody');		var sortabletable = this;		this.SwapHead = function() {		//Extract the contents of the thead so we can replace		//it with one that has onclicks		var thead = sortabletable.Table.getElementsByTagName('thead')[0];		var ths = thead.getElementsByTagName('th');		for (var i=0; i<ths.length; i++) {			var tmp = ths[i];						//Extract the supplied data type			var type='text';			if (tmp.getAttribute('datatype')) type = tmp.getAttribute('datatype');			sortabletable.Types[i] = type;						//Create the new thead elements. The THs are contained in			//a separate class object so we can add onclicks that remember			//the index number of the TH			var tmpTH = new Vormo.SortableTH(sortabletable,tmp.firstChild.nodeValue, i)                        tmpTH.TH.style.paddingRight = tmp.style.paddingRight;                        tmpTH.TH.style.paddingLeft = tmp.style.paddingLeft;                        tmpTH.TH.style.width = tmp.style.width;		}				//Replace the current THEAD with the new one		sortabletable.Table.replaceChild(sortabletable.Head, thead);	};		this.ScrapeBody = function() {		//Extract the TRs and put them in an array. We put the whole		//TR object in the array to preserve any onclicks on the TRs		//or TDs when we do the sort		var tbody = sortabletable.Table.getElementsByTagName('tbody')[0];		var trs = tbody.getElementsByTagName('tr');		for (var i=0; i<trs.length; i++) {                        if(trs[i].parentNode.parentNode.id==tbody.parentNode.id) {                            trs[i].onmouseover = function() { this.className+=" row_over"; }                            trs[i].onmouseout = function() {                                 var cn = this.className;                                cn = cn.replace(/ row_over/,'');                                cn = cn.replace(/row_over/,'');                                this.className = cn;                            }                            sortabletable.Rows[sortabletable.Rows.length] = trs[i];                        }		}	};		this.SortTable = function(keyIndex) {	    if (typeof(keyIndex)=='undefined') {	        keyIndex = sortabletable.SortKey;	    } else {	        if (sortabletable.SortKey == keyIndex) sortabletable.SortDirection *= -1;    		sortabletable.SortKey = keyIndex;	    }				//Set the appropriate TH to the appropriate style		if (sortabletable.lastSortedHead) {			sortabletable.lastSortedHead.className = '';		}		var sortedHead = sortabletable.Head.childNodes[0].childNodes[sortabletable.SortKey]		sortedHead.className = 'sorted_head forward';		if (sortabletable.SortDirection == -1) sortedHead.className = 'sorted_head backward';		sortabletable.lastSortedHead = sortedHead;				//Sort the TRs. The sort compare function		//extracts the appropraite TD value and sorts		//on that.		if (sortabletable.SortDirection==1) sortabletable.Rows.sort(sortabletable.compareRows);		else sortabletable.Rows.sort(sortabletable.compareRows_reverse);				//Create a new TBODY from the newly sorted array		//of TRs		var newTbody = document.createElement('tbody');		var flip = false;		var offStr = sortabletable.BrandsOffArray.toString();		var checkArray = new Array();                for (var r=0; r<sortabletable.Rows.length; r++) {		    // Check if this row is for a brand that's in		    // the "hide" array. If so, set it's class to		    // one where it's hidden.		    var brandTd = sortabletable.Rows[r].getElementsByTagName('td')[1];		            if (brandTd.firstChild.firstChild) {                        var brandTdText = brandTd.firstChild.firstChild.nodeValue;		            } else {                        var brandTdText = brandTd.firstChild.nodeValue;		            }                    // Look for rows that aren't checked                    var checkTd = sortabletable.Rows[r].getElementsByTagName('td')[0];                    var checkInp = checkTd.getElementsByTagName('input')[0];                    var hideChecked = false;                    if (sortabletable.BranshShowCheckedOnly && checkInp.checked==false) hideChecked = true;                                        if (checkInp) if (checkInp.checked==true) checkArray[checkArray.length] = r;                    if (offStr.indexOf(brandTdText)>-1 | hideChecked==true) {                        // Found it, so hide it                        sortabletable.Rows[r].className = 'off';                    } else {                        sortabletable.Rows[r].className = '';                        if (flip) sortabletable.Rows[r].className = 'alt';                        flip = (flip) ? false : true;                        newTbody.appendChild(sortabletable.Rows[r]);                    }                    }				//Swap the old TBODY for the new one		var tbody = sortabletable.Table.getElementsByTagName('tbody')[0];		sortabletable.Table.replaceChild(newTbody, tbody);                 // IE does not retain the "checked" attribute of the input when it's                // set programmatically, so we have to store a reference to any "checked"                // inputs and then reset them to checked after the tbody is swapped                if (Vormo.isIE) {                    for (var i=0; i<checkArray.length; i++) {                        var idx = checkArray[i];                        var inp = sortabletable.Rows[idx].getElementsByTagName('td')[0].getElementsByTagName('input')[0];                        inp.checked = true;                    }                }	};		this.compareRows = function(a,b) {		var colsA = a.getElementsByTagName('td');		var colsB = b.getElementsByTagName('td');        var sortValA = '';        var sortValB = '';                // Check if the node is not a text node. If so, try the next node down        if (colsA[sortabletable.SortKey].childNodes.length>0) {            var nodeA = colsA[sortabletable.SortKey].firstChild;            if (nodeA.nodeType!=3) nodeA = nodeA.firstChild;            if (nodeA) sortValA = nodeA.nodeValue;        }        if (colsB[sortabletable.SortKey].childNodes.length>0) {            var nodeB = colsB[sortabletable.SortKey].firstChild;            if (nodeB.nodeType!=3) nodeB = nodeB.firstChild;            if (nodeB) sortValB = nodeB.nodeValue;        }		if (sortabletable.Types[sortabletable.SortKey]=='date') {			sortValA = sortabletable.CreateDate(sortValA);			sortValB = sortabletable.CreateDate(sortValB);		}				if (sortabletable.Types[sortabletable.SortKey]=='number') {			sortValA *=1;			sortValB *=1;		}		    	if (sortabletable.Types[sortabletable.SortKey]=='currency') {			sortValA = Vormo.ParseCurrency(sortValA);			sortValB = Vormo.ParseCurrency(sortValB);		}				//Make sure we go case-insensitive		if (typeof(sortValA)=='string') sortValA = sortValA.toLowerCase();		if (typeof(sortValB)=='string') sortValB = sortValB.toLowerCase();		if (sortValA<sortValB) return -1;		if (sortValA>sortValB) return 1;		return 0;	};		this.compareRows_reverse = function(a,b) {		return sortabletable.compareRows(b,a);	};		this.CreateDate = function(str) {		//Expectd format: m/d/yyyy		var str = str;		var Year;		var Month;		var Day;		var JSdate = new Date();				var Month = str.split('/')[0]*1 - 1;		var Day = str.split('/')[1];		var Year = str.split('/')[2];				JSdate.setFullYear(Year,Month,Day)		return JSdate;	};		this.FormatDate = function(myDate) {		var str = (myDate.getMonth()+1) + '/' + myDate.getDate() + '/' + myDate.getFullYear();		return str;	};		this.MouseOverRow = function(row,isOver) {		//alert(row);		var cssName = 'row_def';		if (isOver) cssName = 'row_over';		if (Vormo.isOpera & isOver) cssName = 'row_over_opera';		row.className = cssName;	};				this.SwapHead();	this.ScrapeBody();	this.SortTable(this.SortKey);}// Method adds or removes brand names from the array that// determines whether they are hiddenVormo.SortableTable.prototype.ToggleBrandFilter = function(brandStr) {    if (brandStr!=null) {        if (brandStr=='All') {            this.BrandsOffArray.length = 0;        } else {            var existsIdx = -1;            // Loop through the array to see if the name is already in there            for (var i=0; i<this.BrandsOffArray.length; i++) {                if (this.BrandsOffArray[i]==brandStr) {                    existsIdx = i;                    break;                }            }            if (existsIdx > -1) {                // It's there, so remove it                this.BrandsOffArray.splice(existsIdx,1);                } else {                // It's not there, so add it                this.BrandsOffArray[this.BrandsOffArray.length] = brandStr;            }        }    }    this.SortTable();};Vormo.SortableTH = function(sortabletable, text, index) {	this.Index = index;	this.Head = sortabletable.Head;	this.Parent = sortabletable;	this.TH = document.createElement('th');	this.Text = document.createTextNode(text);	this.TH.appendChild(this.Text);	sortabletable.HeadTR.appendChild(this.TH);		var sortableth = this;	this.TH.onclick = function() {		sortableth.Parent.SortTable(sortableth.Index);	}}// ------------------------------------------------------------------------------------- //// END Vormo Tooltip Class// ------------------------------------------------------------------------------------- //
