9b55e595fbd68668ab7f40d3baa00897531f1a47
[plewww.git] / plekit / tablesort / tablesort.js
1 /*
2         TableSort revisited v5.0 by frequency-decoder.com
3
4         Released under a creative commons Attribution-ShareAlike 2.5 license (http://creativecommons.org/licenses/by-sa/2.5/)
5
6         Please credit frequency decoder in any derivative work - thanks
7
8         You are free:
9
10         * to copy, distribute, display, and perform the work
11         * to make derivative works
12         * to make commercial use of the work
13
14         Under the following conditions:
15
16                 by Attribution.
17                 --------------
18                 You must attribute the work in the manner specified by the author or licensor.
19
20                 sa
21                 --
22                 Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.
23
24         * For any reuse or distribution, you must make clear to others the license terms of this work.
25         * Any of these conditions can be waived if you get permission from the copyright holder.
26 */
27
28 (function() {
29 fdTableSort = {
30         regExp_Currency:        /^[£$€¥¤]/,
31         regExp_Number:          /^(\-)?[0-9]+(\.[0-9]*)?$/,
32         pos:                    -1,
33         uniqueHash:             1,
34         thNode:                 null,
35         tableId:                null,
36         tableCache:             {},
37         tmpCache:               {},
38         sortActiveClass:        "sort-active",
39         /*@cc_on
40         /*@if (@_win32)
41         colspan:                "colSpan",
42         rowspan:                "rowSpan",
43         @else @*/
44         colspan:                "colspan",
45         rowspan:                "rowspan",
46         /*@end
47         @*/
48         
49         addEvent: function(obj, type, fn, tmp) {
50                 tmp || (tmp = true);
51                 if( obj.attachEvent ) {
52                         obj["e"+type+fn] = fn;
53                         obj[type+fn] = function(){obj["e"+type+fn]( window.event );};
54                         obj.attachEvent( "on"+type, obj[type+fn] );
55                 } else {
56                         obj.addEventListener( type, fn, true );
57                 };
58         },
59         removeEvent: function(obj, type, fn, tmp) {
60                 tmp || (tmp = true);
61                 try {
62                         if( obj.detachEvent ) {
63                                 obj.detachEvent( "on"+type, obj[type+fn] );
64                                 obj[type+fn] = null;
65                         } else {
66                                 obj.removeEventListener( type, fn, true );
67                         };
68                 } catch(err) {};
69         },
70         stopEvent: function(e) {
71                 e = e || window.event;
72
73                 if(e.stopPropagation) {
74                         e.stopPropagation();
75                         e.preventDefault();
76                 };
77                 
78                 /*@cc_on@*/
79                 /*@if(@_win32)
80                 e.cancelBubble = true;
81                 e.returnValue  = false;
82                 /*@end@*/
83                 return false;
84         },
85         parseClassName: function(head, tbl) {
86                 var colMatch = tbl.className.match(new RegExp(head + "((-[\\d]+([r]){0,1})+)"));
87                 return colMatch && colMatch.length ? colMatch[0].replace(head, "").split("-") : [];
88         },
89         disableSelection: function(element) {
90                 element.onselectstart = function() {
91                         return false;
92                 };
93                 element.unselectable = "on";
94                 element.style.MozUserSelect = "none";
95         },
96         removeTableCache: function(tableId) {
97                 if(!(tableId in fdTableSort.tableCache)) return;
98
99                 fdTableSort.tableCache[tableId] = null;
100                 delete fdTableSort.tableCache[tableId];
101
102                 var tbl = document.getElementById(tableId);
103                 if(!tbl) return;
104                 var ths = tbl.getElementsByTagName("th");
105                 var a;
106                 for(var i = 0, th; th = ths[i]; i++) {
107                         a = th.getElementsByTagName("a");
108                         if(a.length) a[0].onkeydown = a[0].onclick = null;
109                         th.onclick = th.onselectstart = th = a = null;
110                 };
111         },
112         removeTmpCache: function(tableId) {
113                 if(!(tableId in fdTableSort.tmpCache)) return;
114                 var headers = fdTableSort.tmpCache[tableId].headers;
115                 var a;
116                 for(var i = 0, row; row = headers[i]; i++) {
117                         for(var j = 0, th; th = row[j]; j++) {
118                                 a = th.getElementsByTagName("a");
119                                 if(a.length) a[0].onkeydown = a[0].onclick = null;
120                                 th.onclick = th.onselectstart = th = a = null;
121                         };
122                 };
123                 fdTableSort.tmpCache[tableId] = null;
124                 delete fdTableSort.tmpCache[tableId];
125         },
126         initEvt: function(e) {
127                 fdTableSort.init(false);
128         },
129         init: function(tableId) {
130                 if (!document.getElementsByTagName || !document.createElement || !document.getElementById) return;
131
132                 var tables = tableId && document.getElementById(tableId) ? [document.getElementById(tableId)] : document.getElementsByTagName("table");
133                 var c, ii, len, colMatch, showOnly, match, showArrow, columnNumSortObj, obj, workArr, headers, thtext, aclone, multi, colCnt, cel, allRowArr, rowArr, sortableTable, celCount, colspan, rowspan, rowLength;
134
135                 var a          = document.createElement("a");
136                 a.href         = "#";
137                 a.className    = "fdTableSortTrigger";
138
139                 var span       = document.createElement("span");
140
141                 for(var k = 0, tbl; tbl = tables[k]; k++) {
142
143                         if(tbl.id) {
144                                 fdTableSort.removeTableCache(tbl.id);
145                                 fdTableSort.removeTmpCache(tbl.id);
146                         };
147
148                         allRowArr     = tbl.getElementsByTagName('thead').length ? tbl.getElementsByTagName('thead')[0].getElementsByTagName('tr') : tbl.getElementsByTagName('tr');
149                         rowArr        = [];
150                         sortableTable = false;
151
152                         for(var i = 0, tr; tr = allRowArr[i]; i++) {
153                                 if(tr.getElementsByTagName('td').length || !tr.getElementsByTagName('th').length) { continue; };
154                                 rowArr[rowArr.length] = tr.getElementsByTagName('th');
155                                 for(var j = 0, th; th = rowArr[rowArr.length - 1][j]; j++) {
156                                         if(th.className.search(/sortable/) != -1) { sortableTable = true; };
157                                 };
158                         };
159
160                         if(!sortableTable) continue;
161
162                         if(!tbl.id) { tbl.id = "fd-table-" + fdTableSort.uniqueHash++; };
163
164                         showArrow   = tbl.className.search("no-arrow") == -1;
165                         showOnly    = tbl.className.search("sortable-onload-show") != -1;
166
167                         columnNumSortObj = {};
168                         colMatch         = fdTableSort.parseClassName(showOnly ? "sortable-onload-show" : "sortable-onload", tbl);
169                         for(match = 1; match < colMatch.length; match++) {
170                                 columnNumSortObj[parseInt(colMatch[match], 10)] = { "reverse":colMatch[match].search("r") != -1 };
171                         };
172
173                         rowLength = rowArr[0].length;
174
175                         for(c = 0;c < rowArr[0].length;c++){
176                                 if(rowArr[0][c].getAttribute(fdTableSort.colspan) && rowArr[0][c].getAttribute(fdTableSort.colspan) > 1){
177                                         rowLength = rowLength + (rowArr[0][c].getAttribute(fdTableSort.colspan) - 1);
178                                 };
179                         };
180
181                         workArr = new Array(rowArr.length);
182                         for(c = rowArr.length;c--;){ workArr[c]= new Array(rowLength); };
183
184                         for(c = 0;c < workArr.length;c++){
185                                 celCount = 0;
186                                 for(i = 0;i < rowLength;i++){
187                                         if(!workArr[c][i]){
188                                                 cel = rowArr[c][celCount];
189                                                 colspan = (cel.getAttribute(fdTableSort.colspan) > 1) ? cel.getAttribute(fdTableSort.colspan):1;
190                                                 rowspan = (cel.getAttribute(fdTableSort.rowspan) > 1) ? cel.getAttribute(fdTableSort.rowspan):1;
191                                                 for(var t = 0;((t < colspan)&&((i+t) < rowLength));t++){
192                                                         for(var n = 0;((n < rowspan)&&((c+n) < workArr.length));n++) {
193                                                                 workArr[(c+n)][(i+t)] = cel;
194                                                         };
195                                                 };
196                                                 if(++celCount == rowArr[c].length) break;
197                                         };
198                                 };
199                         };
200
201                         for(c = 0;c < workArr.length;c++) {
202                                 for(i = 0;i < workArr[c].length;i++){
203
204                                         if(workArr[c][i].className.search("fd-column-") == -1 && workArr[c][i].className.search("sortable") != -1) workArr[c][i].className = workArr[c][i].className + " fd-column-" + i;
205
206                                         if(workArr[c][i].className.match('sortable')) {
207                                                 workArr[c][i].className = workArr[c][i].className.replace(/forwardSort|reverseSort/, "");
208
209                                                 if(i in columnNumSortObj) {
210                                                         columnNumSortObj[i]["thNode"] = workArr[c][i];
211                                                         columnNumSortObj["active"] = true;
212                                                 };
213
214                                                 thtext = fdTableSort.getInnerText(workArr[c][i], true);
215                                                 
216                                                 for(var cn = workArr[c][i].childNodes.length; cn--;) {
217                                                         // Skip image nodes and links created by the filter script.
218                                                         if(workArr[c][i].childNodes[cn].nodeType == 1 && (workArr[c][i].childNodes[cn].className == "fdFilterTrigger" || /img/i.test(workArr[c][i].childNodes[cn].nodeName))) {
219                                                                 continue;
220                                                         };
221                                                         if(workArr[c][i].childNodes[cn].nodeType == 1 && /^a$/i.test(workArr[c][i].childNodes[cn].nodeName)) {
222                                                                 workArr[c][i].childNodes[cn].onclick = workArr[c][i].childNodes[cn].onkeydown = null;
223                                                         };
224                                                         workArr[c][i].removeChild(workArr[c][i].childNodes[cn]);
225                                                 };
226
227                                                 aclone = a.cloneNode(true);
228                                                 //aclone.appendChild(document.createTextNode(thtext));
229                                                 aclone.innerHTML = thtext;
230                                                 aclone.title = "Sort on \u201c" + thtext.replace('<br />', '') + "\u201d";
231                                                 aclone.onclick = aclone.onkeydown = workArr[c][i].onclick = fdTableSort.initWrapper;
232                                                 workArr[c][i].appendChild(aclone);
233                                                 if(showArrow) workArr[c][i].appendChild(span.cloneNode(false));
234                                                 workArr[c][i].className = workArr[c][i].className.replace(/fd-identical|fd-not-identical/, "");
235                                                 fdTableSort.disableSelection(workArr[c][i]);
236                                                 aclone = null;
237                                         };
238                                 };
239                         };
240
241                         fdTableSort.tmpCache[tbl.id] = {cols:rowLength, headers:workArr};
242
243                         workArr = null;
244                         multi   = 0;
245
246                         if("active" in columnNumSortObj) {
247                                 fdTableSort.tableId = tbl.id;
248                                 fdTableSort.prepareTableData(document.getElementById(fdTableSort.tableId));
249
250                                 delete columnNumSortObj["active"];
251
252                                 for(col in columnNumSortObj) {
253                                         obj = columnNumSortObj[col];
254                                         if(!("thNode" in obj)) { continue; };
255                                         fdTableSort.multi = true;
256
257                                         len = obj.reverse ? 2 : 1;
258
259                                         for(ii = 0; ii < len; ii++) {
260                                                 fdTableSort.thNode = obj.thNode;
261                                                 if(!showOnly) {
262                                                         fdTableSort.initSort(false, true);
263                                                 } else {
264                                                         fdTableSort.addThNode();
265                                                 };
266                                         };
267
268                                         if(showOnly) {
269                                                 fdTableSort.removeClass(obj.thNode, "(forwardSort|reverseSort)");
270                                                 fdTableSort.addClass(obj.thNode, obj.reverse ? "reverseSort" : "forwardSort");
271                                                 if(showArrow) {
272                                                         span = fdTableSort.thNode.getElementsByTagName('span')[0];
273                                                         if(span.firstChild) { span.removeChild(span.firstChild); };
274                                                         span.appendChild(document.createTextNode(len == 1 ? " \u2193" : " \u2191"));
275                                                 };
276                                         };
277                                 };
278                                 if(showOnly && (fdTableSort.tableCache[tbl.id].colStyle || fdTableSort.tableCache[tbl.id].rowStyle)) {
279                                         fdTableSort.redraw(tbl.id, false);
280                                 };
281                         } else if(tbl.className.search(/onload-zebra/) != -1) {
282                                 fdTableSort.tableId = tbl.id;
283                                 fdTableSort.prepareTableData(tbl);
284                                 if(fdTableSort.tableCache[tbl.id].rowStyle) { fdTableSort.redraw(tbl.id, false); };
285                         };
286                 };
287
288                 fdTableSort.thNode = aclone = a = span = columnNumSortObj = thNode = tbl = allRowArr = rowArr = null;
289         },
290         initWrapper: function(e) {
291                 e = e || window.event;
292                 var kc = e.type == "keydown" ? e.keyCode != null ? e.keyCode : e.charCode : -1;
293                 if(fdTableSort.thNode == null && (e.type == "click" || kc == 13)) {
294                         var targ = this;
295                         while(targ.tagName.toLowerCase() != "th") { targ = targ.parentNode; };
296                         fdTableSort.thNode = targ;
297                         while(targ.tagName.toLowerCase() != "table") { targ = targ.parentNode; };
298                         fdTableSort.tableId = targ.id;
299                         fdTableSort.multi = e.shiftKey;
300                         fdTableSort.addSortActiveClass();
301                         setTimeout(fdTableSort.initSort,5,false);
302                         return fdTableSort.stopEvent(e);
303                 };
304                 return kc != -1 ? true : fdTableSort.stopEvent(e);
305         },
306         jsWrapper: function(tableid, colNums) {
307                 if(!(tableid in fdTableSort.tmpCache)) { return false; };
308                 if(!(tableid in fdTableSort.tableCache)) { fdTableSort.prepareTableData(document.getElementById(tableid)); };
309                 if(!(colNums instanceof Array)) { colNums = [colNums]; };
310
311                 fdTableSort.tableId = tableid;
312                 var len = colNums.length, colNum;
313
314                 if(fdTableSort.tableCache[tableid].thList.length == colNums.length) {
315                         var identical = true;
316                         var th;
317                         for(var i = 0; i < len; i++) {
318                                 colNum = colNums[i];
319                                 th = fdTableSort.tmpCache[tableid].headers[0][colNum];
320                                 if(th != fdTableSort.tableCache[tableid].thList[i]) {
321                                         identical = false;
322                                         break;
323                                 };
324                         };
325                         if(identical) {
326                                 fdTableSort.thNode = th;
327                                 fdTableSort.initSort(true);
328                                 return;
329                         };
330                 };
331
332                 fdTableSort.addSortActiveClass();
333
334                 for(var i = 0; i < len; i++) {
335                         fdTableSort.multi = i;
336                         colNum = colNums[i];
337                         fdTableSort.thNode = fdTableSort.tmpCache[tableid].headers[0][colNum];
338                         fdTableSort.initSort(true);
339                 };
340         },
341         addSortActiveClass: function() {
342                 if(fdTableSort.thNode == null) { return; };
343                 fdTableSort.addClass(fdTableSort.thNode, fdTableSort.sortActiveClass);
344                 fdTableSort.addClass(document.getElementsByTagName('body')[0], fdTableSort.sortActiveClass);
345         },
346         removeSortActiveClass: function() {
347                 if(fdTableSort.thNode == null) return;
348                 fdTableSort.removeClass(fdTableSort.thNode, fdTableSort.sortActiveClass);
349                 fdTableSort.removeClass(document.getElementsByTagName('body')[0], fdTableSort.sortActiveClass);
350         },
351         doCallback: function(init) {
352                 if(!fdTableSort.tableId || !(fdTableSort.tableId in fdTableSort.tableCache)) { return; };
353                 fdTableSort.callback(fdTableSort.tableId, init ? fdTableSort.tableCache[fdTableSort.tableId].initiatedCallback : fdTableSort.tableCache[fdTableSort.tableId].completeCallback);
354         },
355         addClass: function(e,c) {
356                 if(new RegExp("(^|\\s)" + c + "(\\s|$)").test(e.className)) { return; };
357                 e.className += ( e.className ? " " : "" ) + c;
358         },
359         /*@cc_on
360         /*@if (@_win32)
361         removeClass: function(e,c) {
362                 e.className = !c ? "" : e.className.replace(new RegExp("(^|\\s)" + c + "(\\s|$)"), " ").replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
363         },
364         @else @*/
365         removeClass: function(e,c) {
366                 e.className = !c ? "" : e.className.replace(new RegExp("(^|\\s)" + c + "(\\s|$)"), " ").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
367         },
368         /*@end
369         @*/
370         callback: function(tblId, cb) {
371                 var func, parts;
372                 try {
373                         if(cb.indexOf(".") != -1) {                              
374                                 parts = cb.split('.');
375                                 obj   = window;
376                                 for (var x = 0, part; part = obj[parts[x]]; x++) {
377                                         if(part instanceof Function) {
378                                                 (function() {
379                                                         var method = part;
380                                                         func = function (data) { method.apply(obj, [data]) };
381                                                 })();
382                                         } else {
383                                                 obj = part;
384                                         };
385                                 };
386                         } else if(cb + tblId in window) {
387                                 func = window[cb + tblId];
388                         } else if(cb in window) {
389                                 func = window[cb];
390                         } else {
391                                 func = null;
392                         };
393                  } catch(err) {};
394                            
395                 if(!(func instanceof Function)) return;
396                 func(tblId, fdTableSort.tableCache[tblId].thList);                               
397         },
398         prepareTableData: function(table) {
399                 var data = [];
400
401                 var start = table.getElementsByTagName('tbody');
402                 start = start.length ? start[0] : table;
403
404                 var trs = start.rows;
405                 var ths = table.getElementsByTagName('th');
406
407                 var numberOfRows = trs.length;
408                 var numberOfCols = fdTableSort.tmpCache[table.id].cols;
409
410                 var data = [];
411                 var identical = new Array(numberOfCols);
412                 var identVal  = new Array(numberOfCols);
413
414                 for(var tmp = 0; tmp < numberOfCols; tmp++) identical[tmp] = true;
415
416                 var tr, td, th, txt, tds, col, row;
417
418                 var re = new RegExp(/fd-column-([0-9]+)/);
419                 var rowCnt = 0;
420
421                 var sortableColumnNumbers = [];
422
423                 for(var tmp = 0, th; th = ths[tmp]; tmp++) {
424                         if(th.className.search(re) == -1) continue;
425                         sortableColumnNumbers[sortableColumnNumbers.length] = th;
426                 };
427
428                 for(row = 0; row < numberOfRows; row++) {
429
430                         tr              = trs[row];
431                         if(tr.parentNode != start || tr.getElementsByTagName("th").length || (tr.parentNode && tr.parentNode.tagName.toLowerCase().search(/thead|tfoot/) != -1)) continue;
432                         data[rowCnt]    = [];
433                         tds             = tr.cells;
434
435                         for(var tmp = 0, th; th = sortableColumnNumbers[tmp]; tmp++) {
436                                 col = th.className.match(re)[1];
437
438                                 td  = tds[col];
439                                 txt = fdTableSort.getInnerText(td) + " ";
440                                 txt = txt.replace(/^\s+/,'').replace(/\s+$/,'');
441
442                                 if(th.className.search(/sortable-date/) != -1) {
443                                         txt = fdTableSort.dateFormat(txt, th.className.search(/sortable-date-dmy/) != -1);
444                                 } else if(th.className.search(/sortable-numeric|sortable-currency/) != -1) {
445                                         txt = parseFloat(txt.replace(/[^0-9\.\-]/g,''));
446                                         if(isNaN(txt)) txt = "";
447                                 } else if(th.className.search(/sortable-text/) != -1) {
448                                         txt = txt.toLowerCase();
449                                 } else if (th.className.search(/sortable-keep/) != -1) {
450                                         txt = rowCnt;
451                                 } else if(th.className.search(/sortable-([a-zA-Z\_]+)/) != -1) {
452                                         if((th.className.match(/sortable-([a-zA-Z\_]+)/)[1] + "PrepareData") in window) {
453                                                 txt = window[th.className.match(/sortable-([a-zA-Z\_]+)/)[1] + "PrepareData"](td, txt);
454                                         };
455                                 } else if(txt != "") {
456                                         fdTableSort.removeClass(th, "sortable");
457                                         if(fdTableSort.dateFormat(txt) != 0) {
458                                                 fdTableSort.addClass(th, "sortable-date");
459                                                 txt = fdTableSort.dateFormat(txt);
460                                         } else if(txt.search(fdTableSort.regExp_Number) != -1 || txt.search(fdTableSort.regExp_Currency) != -1) {
461                                                 fdTableSort.addClass(th, "sortable-numeric");
462                                                 txt = parseFloat(txt.replace(/[^0-9\.\-]/g,''));
463                                                 if(isNaN(txt)) txt = "";
464                                         } else {
465                                                 fdTableSort.addClass(th, "sortable-text");
466                                                 txt = txt.toLowerCase();
467                                         };
468                                 };
469
470                                 if(rowCnt > 0 && identical[col] && identVal[col] != txt) { identical[col] = false; };
471
472                                 identVal[col]     = txt;
473                                 data[rowCnt][col] = txt;
474                         };
475                         data[rowCnt][numberOfCols] = tr;
476                         rowCnt++;
477                 };
478
479                 var colStyle = table.className.search(/colstyle-([\S]+)/) != -1 ? table.className.match(/colstyle-([\S]+)/)[1] : false;
480                 var rowStyle = table.className.search(/rowstyle-([\S]+)/) != -1 ? table.className.match(/rowstyle-([\S]+)/)[1] : false;
481                 var iCBack   = table.className.search(/sortinitiatedcallback-([\S-]+)/) == -1 ? "sortInitiatedCallback" : table.className.match(/sortinitiatedcallback-([\S]+)/)[1];
482                 var cCBack   = table.className.search(/sortcompletecallback-([\S-]+)/) == -1 ? "sortCompleteCallback" : table.className.match(/sortcompletecallback-([\S]+)/)[1];
483                 iCBack = iCBack.replace("-", ".");
484                 cCBack = cCBack.replace("-", ".");
485                 fdTableSort.tableCache[table.id] = { hook:start, initiatedCallback:iCBack, completeCallback:cCBack, thList:[], colOrder:{}, data:data, identical:identical, colStyle:colStyle, rowStyle:rowStyle, noArrow:table.className.search(/no-arrow/) != -1 };
486                 sortableColumnNumbers = data = tr = td = th = trs = identical = identVal = null;
487         },
488         onUnload: function() {
489                 for(tbl in fdTableSort.tableCache) { fdTableSort.removeTableCache(tbl); };
490                 for(tbl in fdTableSort.tmpCache) { fdTableSort.removeTmpCache(tbl); };
491                 fdTableSort.removeEvent(window, "load", fdTableSort.initEvt);
492                 fdTableSort.removeEvent(window, "unload", fdTableSort.onUnload);
493                 fdTableSort.tmpCache = fdTableSort.tableCache = null;
494         },
495         addThNode: function() {
496                 var dataObj = fdTableSort.tableCache[fdTableSort.tableId];
497                 var pos     = fdTableSort.thNode.className.match(/fd-column-([0-9]+)/)[1];
498                 var alt     = false;
499
500                 if(!fdTableSort.multi) {
501                         if(dataObj.colStyle) {
502                                 var len = dataObj.thList.length;
503                                 for(var i = 0; i < len; i++) {
504                                         dataObj.colOrder[dataObj.thList[i].className.match(/fd-column-([0-9]+)/)[1]] = false;
505                                 };
506                         };
507                         if(dataObj.thList.length && dataObj.thList[0] == fdTableSort.thNode) alt = true;
508                         dataObj.thList = [];
509                 };
510
511                 var found = false;
512                 var l = dataObj.thList.length;
513
514                 for(var i = 0, n; n = dataObj.thList[i]; i++) {
515                         if(n == fdTableSort.thNode) {
516                                 found = true;
517                                 break;
518                         };
519                 };
520
521                 if(!found) {
522                         dataObj.thList.push(fdTableSort.thNode);
523                         if(dataObj.colStyle) { dataObj.colOrder[pos] = true; };
524                 };
525
526                 var ths = document.getElementById(fdTableSort.tableId).getElementsByTagName("th");
527                 for(var i = 0, th; th = ths[i]; i++) {
528                         found = false;
529                         for(var z = 0, n; n = dataObj.thList[z]; z++) {
530                                 if(n == th) {
531                                         found = true;
532                                         break;
533                                 };
534                         };
535                         if(!found) {
536                                 fdTableSort.removeClass(th, "(forwardSort|reverseSort)");
537                                 if(!dataObj.noArrow) {
538                                         span = th.getElementsByTagName('span');
539                                         if(span.length) {
540                                                 span = span[0];
541                                                 while(span.firstChild) span.removeChild(span.firstChild);
542                                         };
543                                 };
544                         };
545                 };
546
547                 if(dataObj.thList.length > 1) {
548                         classToAdd = fdTableSort.thNode.className.search(/forwardSort/) != -1 ? "reverseSort" : "forwardSort";
549                         fdTableSort.removeClass(fdTableSort.thNode, "(forwardSort|reverseSort)");
550                         fdTableSort.addClass(fdTableSort.thNode, classToAdd);
551                         dataObj.pos = -1
552                 } else if(alt) { dataObj.pos = fdTableSort.thNode };
553         },
554         initSort: function(noCallback, ident) {
555                 var thNode      = fdTableSort.thNode;
556                 var tableElem   = document.getElementById(fdTableSort.tableId);
557
558                 if(!(fdTableSort.tableId in fdTableSort.tableCache)) { fdTableSort.prepareTableData(document.getElementById(fdTableSort.tableId)); };
559
560                 fdTableSort.addThNode();
561
562                 if(!noCallback) { fdTableSort.doCallback(true); };
563
564                 fdTableSort.pos = thNode.className.match(/fd-column-([0-9]+)/)[1];
565                 var dataObj     = fdTableSort.tableCache[tableElem.id];
566                 var lastPos     = dataObj.pos && dataObj.pos.className ? dataObj.pos.className.match(/fd-column-([0-9]+)/)[1] : -1;
567                 var len1        = dataObj.data.length;
568                 var len2        = dataObj.data.length > 0 ? dataObj.data[0].length - 1 : 0;
569                 var identical   = dataObj.identical[fdTableSort.pos];
570                 var classToAdd  = "forwardSort";
571
572                 if(dataObj.thList.length > 1) {
573                         var js  = "var sortWrapper = function(a,b) {\n";
574                         var l   = dataObj.thList.length;
575                         var cnt = 0;
576                         var e,d,th,p,f;
577
578                         for(var i=0; i < l; i++) {
579                                 th = dataObj.thList[i];
580                                 p  = th.className.match(/fd-column-([0-9]+)/)[1];
581                                 if(dataObj.identical[p]) { continue; };
582                                 cnt++;
583
584                                 if(th.className.match(/sortable-(numeric|currency|date|keep)/)) {
585                                         f = "fdTableSort.sortNumeric";
586                                 } else if(th.className.match('sortable-text')) {
587                                         f = "fdTableSort.sortText";
588                                 } else if(th.className.search(/sortable-([a-zA-Z\_]+)/) != -1 && th.className.match(/sortable-([a-zA-Z\_]+)/)[1] in window) {
589                                         f = "window['" + th.className.match(/sortable-([a-zA-Z\_]+)/)[1] + "']";
590                                 } else  f = "fdTableSort.sortText";
591
592                                 e = "e" + i;
593                                 d = th.className.search('forwardSort') != -1 ? "a,b" : "b,a";
594                                 js += "fdTableSort.pos   = " + p + ";\n";
595                                 js += "var " + e + " = "+f+"(" + d +");\n";
596                                 js += "if(" + e + ") return " + e + ";\n";
597                                 js += "else { \n";
598                         };
599
600                         js += "return 0;\n";
601
602                         for(var i=0; i < cnt; i++) {
603                                 js += "};\n";
604                         };
605
606                         if(cnt) js += "return 0;\n";
607                         js += "};\n";
608
609                         eval(js);
610                         dataObj.data.sort(sortWrapper);
611                         identical = false;
612                 } else if((lastPos == fdTableSort.pos && !identical) || (thNode.className.search(/sortable-keep/) != -1 && lastPos == -1)) {
613                         dataObj.data.reverse();
614                         classToAdd = thNode.className.search(/reverseSort/) != -1 ? "forwardSort" : "reverseSort";
615                         if(thNode.className.search(/sortable-keep/) != -1 && lastPos == -1) fdTableSort.tableCache[tableElem.id].pos = thNode;
616                 } else {
617                         fdTableSort.tableCache[tableElem.id].pos = thNode;
618                         classToAdd = thNode.className.search(/forwardSort/) != -1 ? "reverseSort" : "forwardSort";
619                         if(!identical) {
620                                 if(thNode.className.match(/sortable-(numeric|currency|date|keep)/)) {
621                                         dataObj.data.sort(fdTableSort.sortNumeric);
622                                 } else if(thNode.className.match('sortable-text')) {
623                                         dataObj.data.sort(fdTableSort.sortText);
624                                 } else if(thNode.className.search(/sortable-([a-zA-Z\_]+)/) != -1 && thNode.className.match(/sortable-([a-zA-Z\_]+)/)[1] in window) {
625                                         dataObj.data.sort(window[thNode.className.match(/sortable-([a-zA-Z\_]+)/)[1]]);
626                                 };
627
628                                 if(thNode.className.search(/(^|\s)favour-reverse($|\s)/) != -1) {
629                                         classToAdd = classToAdd == "forwardSort" ? "reverseSort" : "forwardSort";
630                                         dataObj.data.reverse();
631                                 };
632                         };
633                 };
634                 if(ident) { identical = false; };
635                 if(dataObj.thList.length == 1) {
636                         fdTableSort.removeClass(thNode, "(forwardSort|reverseSort)");
637                         fdTableSort.addClass(thNode, classToAdd);
638                 };
639                 if(!dataObj.noArrow) {
640                         var span = fdTableSort.thNode.getElementsByTagName('span')[0];
641                         if(span.firstChild) span.removeChild(span.firstChild);
642                         span.appendChild(document.createTextNode(fdTableSort.thNode.className.search(/forwardSort/) != -1 ? " \u2193" : " \u2191"));
643                 };
644                 if(!dataObj.rowStyle && !dataObj.colStyle && identical) {
645                         fdTableSort.removeSortActiveClass();
646                         if(!noCallback) { fdTableSort.doCallback(false); };
647                         fdTableSort.thNode = null;
648                         return;
649                 };
650                 if("tablePaginater" in window && tablePaginater.tableIsPaginated(fdTableSort.tableId)) {
651                         tablePaginater.redraw(fdTableSort.tableId, identical);
652                 } else {
653                         fdTableSort.redraw(fdTableSort.tableId, identical);
654                 };
655                 fdTableSort.removeSortActiveClass();
656                 if(!noCallback) { fdTableSort.doCallback(false); };
657                 fdTableSort.thNode = null;
658         },
659         redraw: function(tableid, identical) {
660                 if(!tableid || !(tableid in fdTableSort.tableCache)) { return; };
661                 var dataObj     = fdTableSort.tableCache[tableid];
662                 var data        = dataObj.data;
663                 var len1        = data.length;
664                 var len2        = len1 ? data[0].length - 1 : 0;
665                 var hook        = dataObj.hook;
666                 var colStyle    = dataObj.colStyle;
667                 var rowStyle    = dataObj.rowStyle;
668                 var colOrder    = dataObj.colOrder;
669                 var highLight   = 0;
670                 var reg         = /(^|\s)invisibleRow(\s|$)/;
671                 var tr, tds;
672
673                 for(var i = 0; i < len1; i++) {
674                         tr = data[i][len2];
675                         if(colStyle) {
676                                 tds = tr.cells;
677                                 for(thPos in colOrder) {
678                                         if(!colOrder[thPos]) fdTableSort.removeClass(tds[thPos], colStyle);
679                                         else fdTableSort.addClass(tds[thPos], colStyle);
680                                 };
681                         };
682                         if(!identical) {
683                                 if(rowStyle && tr.className.search(reg) == -1) {
684                                         if(highLight++ & 1) fdTableSort.addClass(tr, rowStyle);
685                                         else fdTableSort.removeClass(tr, rowStyle);
686                                 };
687
688                                 // Netscape 8.1.2 requires the removeChild call or it freaks out, so add the line if you want to support this browser
689                                 // hook.removeChild(tr);
690                                 hook.appendChild(tr);
691                         };
692                 };
693                 tr = tds = hook = null;
694         },
695         getInnerText: function(el, allowBrTags) {
696                 if (typeof el == "string" || typeof el == "undefined") return el;
697                 if(el.innerText) return el.innerText;
698                 var txt = '', i;
699                 for(i = el.firstChild; i; i = i.nextSibling) {
700                         if(allowBrTags && i.nodeName && i.nodeName == "BR") txt += "<br />";
701                         else if(i.nodeType == 3)       txt += i.nodeValue;
702                         else if(i.nodeType == 1)       txt += fdTableSort.getInnerText(i);
703                 };
704                 return txt;
705         },
706         dateFormat: function(dateIn, favourDMY) {
707                 var dateTest = [
708                         { regExp:/^(0?[1-9]|1[012])([- \/.])(0?[1-9]|[12][0-9]|3[01])([- \/.])((\d\d)?\d\d)$/, d:3, m:1, y:5 },  // mdy
709                         { regExp:/^(0?[1-9]|[12][0-9]|3[01])([- \/.])(0?[1-9]|1[012])([- \/.])((\d\d)?\d\d)$/, d:1, m:3, y:5 },  // dmy
710                         { regExp:/^(\d\d\d\d)([- \/.])(0?[1-9]|1[012])([- \/.])(0?[1-9]|[12][0-9]|3[01])$/, d:5, m:3, y:1 }      // ymd
711                         ];
712                 var start, cnt = 0, numFormats = dateTest.length;
713                 while(cnt < numFormats) {
714                         start = (cnt + (favourDMY ? numFormats + 1 : numFormats)) % numFormats;
715                         if(dateIn.match(dateTest[start].regExp)) {
716                                 res = dateIn.match(dateTest[start].regExp);
717                                 y = res[dateTest[start].y];
718                                 m = res[dateTest[start].m];
719                                 d = res[dateTest[start].d];
720                                 if(m.length == 1) m = "0" + String(m);
721                                 if(d.length == 1) d = "0" + String(d);
722                                 if(y.length != 4) y = (parseInt(y) < 50) ? "20" + String(y) : "19" + String(y);
723
724                                 return y+String(m)+d;
725                         };
726                         cnt++;
727                 };
728                 return 0;
729         },
730         sortNumeric:function(a,b) {
731                 var aa = a[fdTableSort.pos];
732                 var bb = b[fdTableSort.pos];
733                 if(aa == bb) return 0;
734                 if(aa === "" && !isNaN(bb)) return -1;
735                 if(bb === "" && !isNaN(aa)) return 1;
736                 return aa - bb;
737         },
738         sortText:function(a,b) {
739                 var aa = a[fdTableSort.pos];
740                 var bb = b[fdTableSort.pos];
741                 if(aa == bb) return 0;
742                 if(aa < bb)  return -1;
743                 return 1;
744         }
745 };
746 })();
747 fdTableSort.addEvent(window, "load",   fdTableSort.initEvt);
748 fdTableSort.addEvent(window, "unload", fdTableSort.onUnload);