/* SortableTable.js - a function for allowing tables to be sorted
 *
 * The author of this program, Safalra (Stephen Morley), irrevocably releases
 * all rights to this program, with the intention of it becoming part of the
 * public domain. Because this program is released into the public domain, it
 * comes with no warranty either expressed or implied, to the extent permitted
 * by law.
 *
 * For more public domain JavaScript code by the same author, visit:
 * http://www.safalra.com/programming/javascript/
 */

SortableTable.SORT=new function(){this.ALPHABETICAL=function(_1,_2){return (_1==_2)?0:(_1<_2)?-1:1;};this.NUMERICAL=function(_3,_4){return _3-_4;};this.ALPHANUMERICAL=function(_5,_6){var _7=0;var _8=getComponents(_5);var _9=getComponents(_6);var _a=isNaN(parseInt(_8[0],10));if(_a!=isNaN(parseInt(_8[0],10))){_7=_a?1:-1;}else{var _b=0;while(_7==0&&_8.length>_b&&_9.length>_b){if(_a){_7=SortableTable.SORT.ALPHABETICAL(_8[_b],_9[_b]);}else{_7=_8[_b]-_9[_b];}_b++;_a=!_a;}if(_7==0&&_8.length!=_9.length){_7=(_8.length==_b)?-1:1;}}return _7;};var _c=/^(\d+|\D+)/;function getComponents(_d){var _e=new Array();while(_d.length>0){var _f=_c.exec(_d)[0];_e.push(_f);_d=_d.substr(_f.length);}return _e;}};function SortableTable(_10){var _11=this;var _12;this.update=function(){_12=new Array();var _13=_10.getElementsByTagName("tbody");for(var i=0;i<_13.length;i++){_12.push({"node":_13[i],"rows":new Array()});var _15=_13[i].getElementsByTagName("tr");for(var j=0;j<_15.length;j++){_12[i].rows.push({"node":_15[j],"cells":new Array()});for(var k=0;k<_15[j].childNodes.length;k++){var _18=_15[j].childNodes[k];if(_18.nodeType==1){var _19=getContent(_18).replace(/\s+/g," ").replace(/^\s|\s$/g,"");var _1a=_18.getAttribute("colspan");if(!_1a){_1a=1;}for(var col=0;col<_1a;col++){_12[i].rows[j].cells.push({"node":_18,"text":_19});}}}}}};function getContent(_1c){var _1d="";for(var i=0;i<_1c.childNodes.length;i++){var _1f=_1c.childNodes[i];switch(_1f.nodeType){case 1:_1d+=getContent(_1f);break;case 3:_1d+=_1f.nodeValue;break;default:}}return _1d;}this.sort=function(_20,_21,_22){function sortFunction(_23,_24){var _25=_23.cells[_20];var _26=_24.cells[_20];return ((_22?1:-1)*_21(_25.text,_26.text,_25.node,_26.node));}for(var i=0;i<_12.length;i++){_12[i].rows.sort(sortFunction);for(var j=0;j<_12[i].rows.length;j++){_12[i].node.removeChild(_12[i].rows[j].node);_12[i].node.appendChild(_12[i].rows[j].node);}}};this.addSortImages=function(_29,_2a,_2b,_2c,_2d,_2e,_2f){var _30=document.createElement("span");_30.className="sortableTableSortImages";var _31=document.createElement("img");_31.setAttribute("width",_2e);_31.setAttribute("height",_2f);var _32=_31.cloneNode(true);_31.className="sortableTableAscendingImage";_31.setAttribute("src",_2c);_31.setAttribute("alt","Sort ascending");_31.setAttribute("title","Sort ascending");_32.className="sortableTableDescendingImage";_32.setAttribute("src",_2d);_32.setAttribute("alt","Sort descending");_32.setAttribute("title","Sort descending");if(_2a){_30.appendChild(_31);_30.appendChild(_32);}else{_30.appendChild(_32);_30.appendChild(_31);}addSortImageNodes(_30,_10.getElementsByTagName("thead")[0].getElementsByTagName("th"),_2b);if(_29){addSortImageNodes(_30,_10.getElementsByTagName("tfoot")[0].getElementsByTagName("th"),_2b);}};function addSortImageNodes(_33,_34,_35){for(var i=0;i<_34.length;i++){var _37=_33.cloneNode(true);var _38=createEventListener(i,_35[i],true);var _39=createEventListener(i,_35[i],false);if(_37.firstChild.addEventListener){_37.firstChild.addEventListener("click",_38,false);_37.lastChild.addEventListener("click",_39,false);}else{if(_37.firstChild.attachEvent){_37.firstChild.attachEvent("onclick",_38);_37.lastChild.attachEvent("onclick",_39);}}_34[i].appendChild(_37);}}function createEventListener(_3a,_3b,_3c){return function(){_11.sort(_3a,_3b,_3c);};}this.update();}
