
$.fn.makeSortable=function(){var theads=$('thead',this);var tbodys=$('tbody',this);var tables=[];for(var i=0,thead;(thead=theads[i]);i++){tables.push(new Table(thead,tbodys[i]));}
var row=tables[0].thead.rows[tables[0].thead.rows.length-1];for(var i=0;i<row.cells.length;i++){row.cells[i].setAttribute('nowrap','true');var linkEl=$('span.sortable',row.cells[i])[0];if(linkEl){linkEl.onclick=$.hitch({linkEl:linkEl,tables:tables},headingClicked);linkEl.setAttribute('columnId',i);row.cells[i].appendChild(linkEl);if($(linkEl).attr('sortOrder')){row.cells[i].appendChild(createSortArrow($(linkEl).attr('sortOrder')));for(var j=0,thead;(thead=tables[j]);j++){thead.makeSortable(i);}}}}};var headingClicked=function(){var td=this.linkEl.parentNode;var tr=td.parentNode;var column=this.linkEl.getAttribute('columnId')||td.cellIndex;var arrows=$('span.sortArrow',td);var previousSortOrder='';if(arrows.length>0){previousSortOrder=arrows[0].getAttribute('sortOrder');}
for(var i=0,table;(table=this.tables[i]);i++){table.headingClicked(column);}
var arrows=$('span.sortArrow',tr);for(var j=0;j<arrows.length;j++){var arrowParent=arrows[j].parentNode;arrowParent.removeChild(arrows[j]);if(arrowParent!=td)arrowParent.appendChild(createSortArrow());}
td.appendChild(createSortArrow((null==previousSortOrder||''==previousSortOrder||'DESC'==previousSortOrder)?'ASC':'DESC'));};var createSortArrow=function(sortOrder){var spanEl=document.createElement('span');spanEl.className='sortArrow';switch(sortOrder){case'ASC':spanEl.appendChild(document.createTextNode(' \u2191'));spanEl.setAttribute('sortOrder','ASC');break;case'DESC':spanEl.appendChild(document.createTextNode(' \u2193'));spanEl.setAttribute('sortOrder','DESC');break;default:spanEl.appendChild(document.createTextNode('\u00A0\u00A0'));}
return spanEl;};function Table(thead,tbody){this.thead=thead;this.tbody=tbody;this.isOdd=false;this.sortColumnIndex=-1;this.newRows=-1;this.makeSortable=function(i){this.sortColumnIndex=i;this.newRows=[];for(var j=0;j<this.tbody.rows.length;j++){this.newRows[j]=this.tbody.rows[j];}}
this.headingClicked=function(column){if(this.tbody.rows.length<=1){return false;}
var itm='';var rowNum=0;while(''==itm&&rowNum<this.tbody.rows.length){itm=this.getInnerText(this.tbody.rows[rowNum].cells[column]);rowNum++;}
var sortfn=this.determineSortFunction(itm);if(column==this.sortColumnIndex){newRows=this.newRows;newRows.reverse();}else{this.sortColumnIndex=column;var newRows=new Array();for(var j=0;j<this.tbody.rows.length;j++){newRows[j]=this.tbody.rows[j];}
newRows.sort(sortfn);}
this.moveRows(newRows);this.newRows=newRows;}
this.moveRows=function(newRows){this.isOdd=false;for(var i=0;i<newRows.length;i++){var rowItem=newRows[i];this.doStripe(rowItem);this.tbody.appendChild(rowItem);}}
this.getInnerText=function(el){if('string'==typeof el||'undefined'==typeof el){return el;}
if(el.innerText){return el.innerText;}
var str=el.getAttribute('sortingInnerText');if(null!=str&&''!=str){return str;}
str='';var cs=el.childNodes;var l=cs.length;for(var i=0;i<l;i++){if(1==cs[i].nodeType){str+=this.getInnerText(cs[i]);break;}else if(3==cs[i].nodeType){str+=cs[i].nodeValue;break;}}
el.setAttribute('sortingInnerText',str);return str;}
this.determineSortFunction=function(itm){var sortfn=this.sortCaseInsensitive;itm=$.trim(itm);if(itm.match(/^\d?[\.,\d]*\d+$/)){sortfn=this.sortNumeric;}
return $.hitch(this,sortfn);}
this.sortCaseInsensitive=function(a,b){var aa=this.getInnerText(a.cells[this.sortColumnIndex]).toLowerCase();var bb=this.getInnerText(b.cells[this.sortColumnIndex]).toLowerCase();if(aa==bb){return 0;}else if(aa<bb){return-1;}else{return 1;}}
this.sortDate=function(a,b){var aa=this.getInnerText(a.cells[this.sortColumnIndex]);var bb=this.getInnerText(b.cells[this.sortColumnIndex]);var dt1,dt2,yr=-1;if(aa.length==10){dt1=aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);}else{yr=aa.substr(6,2);if(parseInt(yr,10)<50){yr='20'+yr;}else{yr='19'+yr;}
dt1=yr+aa.substr(3,2)+aa.substr(0,2);}
if(bb.length==10){dt2=bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);}else{yr=bb.substr(6,2);if(parseInt(yr,10)<50){yr='20'+yr;}else{yr='19'+yr;}
dt2=yr+bb.substr(3,2)+bb.substr(0,2);}
if(dt1==dt2){return 0;}else if(dt1<dt2){return-1;}
return 1;}
this.sortCurrency=function(a,b){var aa=this.getInnerText(a.cells[this.sortColumnIndex]).replace(/[^0-9.]/g,'');var bb=this.getInnerText(b.cells[this.sortColumnIndex]).replace(/[^0-9.]/g,'');return parseFloat(aa)-parseFloat(bb);}
this.sortNumeric=function(a,b){var cleanup=function(value){return $.trim(value).replace(/\./g,'').replace(/,/g,'.');};var aa=parseFloat(cleanup(this.getInnerText(a.cells[this.sortColumnIndex])));if(isNaN(aa)){aa=0;}
var bb=parseFloat(cleanup(this.getInnerText(b.cells[this.sortColumnIndex])));if(isNaN(bb)){bb=0;}
return aa-bb;}
this.makeStandardIPAddress=function(val){var vals=val.split('.');for(x in vals){val=vals[x];while(3>val.length){val='0'+val;}
vals[x]=val;}
val=vals.join('.');return val;}
this.sortIP=function(a,b){var aa=this.makeStandardIPAddress(this.getInnerText(a.cells[this.sortColumnIndex]).toLowerCase());var bb=this.makeStandardIPAddress(this.getInnerText(b.cells[this.sortColumnIndex]).toLowerCase());if(aa==bb){return 0;}else if(aa<bb){return-1;}else{return 1;}}
this.doStripe=function(rowItem){rowItem=$(rowItem);if(this.isOdd){rowItem.removeClass('yel_back');rowItem.addClass('whi_back');rowItem.addClass('odd');}else{rowItem.removeClass('whi_back');rowItem.removeClass('odd');rowItem.addClass('yel_back');}
this.isOdd=!this.isOdd;}
this.getEventTarget=function(e){var targ;if(!e){e=window.event;}
if(e.target){targ=e.target;}else if(e.srcElement){targ=e.srcElement;}
if(targ.nodeType==3){targ=targ.parentNode;}
return targ;}}
$.hitch=function(scope,method){if(!method){method=scope;scope=null;}
if(typeof method=="string"){scope=scope||window;if(!scope[method]){throw(['method not found']);}
return function(){return scope[method].apply(scope,arguments||[]);};}
return!scope?method:function(){return method.apply(scope,arguments||[]);};}