
addEvent(window,"load",sortables_init);var SORT_COLUMN_INDEX;var monthName=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');function sortables_init(){if(!document.getElementsByTagName)
return;tbls=document.getElementsByTagName("table");for(ti=0;ti<tbls.length;ti++){thisTbl=tbls[ti];if(((' '+thisTbl.className+' ').indexOf(" sortable ")!=-1)&&(thisTbl.id)){ts_makeSortable(thisTbl);}}}
function ts_makeSortable(table){var firstRow;for(var i=0;i<table.rows.length;i++){firstRow=table.rows[i];if(firstRow.cells&&firstRow.cells[0].nodeName=="TD"){firstRow=table.rows[i-1];break;}}
if(!firstRow)
return;var inputs=document.getElementsByTagName("input");for(var i=0;i<inputs.length;i++){if(inputs[i].type.toLowerCase()=='checkbox')
addEvent(inputs[i],"change",ts_persistCheckbox);}
for(var i=0;i<firstRow.cells.length;i++){var cell=firstRow.cells[i];if(cell.childNodes.length>0&&cell.className.indexOf("nosort")==-1){var link=document.createElement("a");link.href="#";link.style.textDecoration="none";link.className="sortheader";addEvent(link,"click",ts_resortTable);var l=cell.childNodes.length;while(cell.childNodes.length>0){link.appendChild(cell.childNodes[0]);}
var span=document.createElement("span");span.className="sortarrow";span.innerHTML='&nbsp;&nbsp;';link.appendChild(span);cell.appendChild(link);}}}
function ts_getInnerText(el){if(typeof el=="string"||typeof el=="undefined")
return el;if(el.ts_allText)
return el.ts_allText;var str=new Array();var cs=el.childNodes;for(var i=0;i<cs.length;i++){switch(cs[i].nodeType){case 1:if(cs[i].tagName.toLowerCase()=='input'){if(cs[i].type.toLowerCase()=='text')
str.push(cs[i].value)
else if(cs[i].type.toLowerCase()=='checkbox')
str.push(cs[i].checked)
else
str.push(ts_getInnerText(cs[i]));}
else if(cs[i].tagName.toLowerCase()=='select'){str.push(cs[i].options[cs[i].selectedIndex].value);}else{str.push(ts_getInnerText(cs[i]));}
break;case 3:str.push(cs[i].nodeValue);break;}}
return el.ts_allText=str.join(" ");}
function ts_expireCache(el){var cs=el.childNodes;if(typeof el.ts_allText=="undefined")
return false
for(var i=0;i<cs.length;i++){if(cs[i].nodeType==1){if(cs[i].tagName.toLowerCase()=='input')
return delete el.ts_allText
else if(cs[i].tagName.toLowerCase()=='select')
return delete el.ts_allText
else if(ts_expireCache(cs[i]))
return delete el.ts_allText}}
return false}
function ts_persistCheckbox(event){var chkbox=event.currentTarget?event.currentTarget:event.srcElement;chkbox.defaultChecked=chkbox.checked;return true;}
function ts_resortTable(event){var lnk=event.currentTarget?event.currentTarget:event.srcElement;var span;if(lnk.tagName&&lnk.tagName.toLowerCase()=='span')
span=lnk;else{for(var ci=0;ci<lnk.childNodes.length;ci++){if(lnk.childNodes[ci].tagName&&lnk.childNodes[ci].tagName.toLowerCase()=='span')
span=lnk.childNodes[ci];}}
var td=lnk.parentNode;while(td.tagName!='TD'&&td.tagName!='TH')
td=td.parentNode;var column=td.cellIndex;var table=getParent(td,'TABLE');var nonHeaderIndex;for(nonHeaderIndex=0;nonHeaderIndex<table.rows.length;nonHeaderIndex++){if(table.rows[nonHeaderIndex].cells&&table.rows[nonHeaderIndex].cells[0].nodeName=="TD"){break;}}
if(nonHeaderIndex==0||nonHeaderIndex>=table.rows.length)
return;var itm=ts_getInnerText(table.rows[nonHeaderIndex].cells[column]);var dateregex=new RegExp("^\\d\\d[\\/-](\\d\\d|"+
monthName.join("|")+')[\\/-]\\d\\d(\\d\\d)?$',"i")
if(!isNaN(parseFloat(itm)))
sortfn=ts_sort_currency
else if(itm.match(dateregex))
sortfn=ts_sort_date
else if(itm.match(/^[£€$]/))
sortfn=ts_sort_currency
else if(itm.match(/^[\d\.]+$/))
sortfn=ts_sort_numeric
else
sortfn=ts_sort_caseinsensitive
SORT_COLUMN_INDEX=column;var newRows=new Array();var bottomRows=new Array();if(table.tFoot.rows.length||table.tHead.rows.length){for(var j=0;j<table.tBodies[0].rows.length;j++){if(table.rows[j].className.indexOf('sortbottom')==-1)
newRows.push(table.tBodies[0].rows[j])
else
bottomRows.push(table.tBodies[0].rows[j]);}}else{for(var j=nonHeaderIndex;j<table.rows.length;j++){if(table.rows[j].className.indexOf('sortbottom')==-1)
newRows.push(table.rows[j])
else
bottomRows.push(table.rows[j]);}}
newRows.sort(sortfn);if(span.getAttribute("sortdir")=='down'){ARROW='&nbsp;&uarr;';newRows.reverse();span.setAttribute('sortdir','up');}else{ARROW='&nbsp;&darr;';span.setAttribute('sortdir','down');}
for(var i=0;i<newRows.length;i++){table.tBodies[0].appendChild(newRows[i]);ts_expireCache(newRows[i].cells[SORT_COLUMN_INDEX]);}
for(i=0;i<bottomRows.length;i++){table.tBodies[0].appendChild(bottomRows[i]);}
var allspans=document.getElementsByTagName("span");for(var ci=0;ci<allspans.length;ci++){if(allspans[ci].className=='sortarrow'){if(getParent(allspans[ci],"table")==getParent(lnk,"table")){allspans[ci].innerHTML='&nbsp;&nbsp;';}}}
span.innerHTML=ARROW;GreenBar(table);}
function getParent(el,pTagName){if(el==null)
return null;else if(el.nodeType==1&&el.tagName.toLowerCase()==pTagName.toLowerCase())
return el;else
return getParent(el.parentNode,pTagName);}
function ts_sort_date(a,b){aa=ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);bb=ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);aaBits=aa.split(/\/|-/);for(i=0;i<monthName.length;i++){if(monthName[i].toLowerCase()==aaBits[1].toLowerCase()){aa=aaBits[0]+'/'+(i<9?'0':'')+(i+1)+'/'+aaBits[2];break;}}
if(aa.length==10){dt1=aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);}else{yr=aa.substr(6,2);dt1=(parseInt(yr)<50?'20':'19')+yr+aa.substr(3,2)+aa.substr(0,2);}
bbBits=bb.split(/\/|-/);for(i=0;i<monthName.length;i++){if(monthName[i].toLowerCase()==bbBits[1].toLowerCase()){bb=bbBits[0]+'/'+(i<9?'0':'')+(i+1)+'/'+bbBits[2];break;}}
if(bb.length==10){dt2=bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);}else{yr=bb.substr(6,2);dt2=(parseInt(yr)<50?'20':'19')+yr+bb.substr(3,2)+bb.substr(0,2);}
if(dt1==dt2)
return 0;if(dt1<dt2)
return-1;return 1;}
function ts_sort_currency(a,b){aa=ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');bb=ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');return parseFloat(aa)-parseFloat(bb);}
function ts_sort_numeric(a,b){aa=parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));if(isNaN(aa))
aa=0;bb=parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));if(isNaN(bb))
bb=0;return aa-bb;}
function ts_sort_caseinsensitive(a,b){aa=ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();bb=ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();if(aa==bb)
return 0;if(aa<bb)
return-1;return 1;}
function ts_sort_default(a,b){aa=ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);bb=ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);if(aa==bb)
return 0;if(aa<bb)
return-1;return 1;}
function addEvent(elm,evType,fn,useCapture){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 added");}}
if(typeof Array.prototype.push=='undefined'){Array.prototype.push=function(){var b=this.length;for(var i=0;i<arguments.length;i++){this[b+i]=arguments[i];}
return this.length}}