// Table Sort Order (17/12/2004)
// by Vic Phillips http://www.vicsjavascripts.org.uk

// The first row of the table is not included in the sort
// The call to initiate the sort may be an external link or incorperated in the cell of the first row

// tsoSortTable('*TableId*',*TableColum*,'*Mode*');
// where
// *TableId*    = the id of the table to be sorted (string)
// *TableColum* = the table column to be sorted
//                start at 0 from left to right (digit)
// *Mode*       = the sort type as listed below (string)

// Sort Modes
// AA  = Alpha Ascending
// AD  = Alpha Descending
// AT  = Alpha Toggle Ascending/Descending

// NAB  = Numeric Ascending
// ND  = Numeric Descending
// NT  = Numeric Toggle Ascending/Descending

// DA  = Date Format m.d.yyyy Ascending
// DAE = Date Format d.m.yyyy Ascending

// DD  = Date Format m.d.yyyy Descending
// DDE = Date Format d.m.yyyy Descending

// DT  = Date Format m.d.yyyy Toggle Ascending/Descending
// DTE = Date Format d.m.yyyy Toggle Ascending/Descending


// The date format must be consistent in all cells

// for European format dates ie dd/mm/yyyy
//  The Date field separator may be any single alpha charter

// for American format dates ie mm/dd/yyyy
//  The Date field separator must be '/'

// Month names may be used ie Jan or january for month 1


// Do NOT Change
var tsoDir='D';
var tsoTRAry=new Array();
var tsoSortAry=new Array();
var tsoMonthAry=new Array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec')

function tsoSortTable(tsoid,tsoc,tsom){
 tsoTObj=tsogEBId(tsoid);
 tsoAnalyseTable(tsoc);

 tsoId=tsoid; tsoCol=tsoc; tsoMode=tsom.charAt(0);
 if (tsom.charAt(1)=='T'){ if (tsoDir=='A'){ tsoDir='D'; } else { tsoDir='A'; } }
 else {  tsoDir=tsom.charAt(1); }

 if (tsom.charAt(2)==null){ tsom.charAt(2)='A'; }
 if (tsoMode=='A'){ tsoSortAry.sort(tsoSortAlpha); }
 if (tsoMode=='N'){ tsoSortAry.sort(tsoSortNumeric); }
 if (tsoMode=='D'){ tsoDateFormat=tsom.charAt(2); tsoSortDate(tsoSortAry[0][1]); }
 tsoCreateDOMTable();
}

function tsoAnalyseTable(tsoc){
 tsoP=tsoTObj.parentNode
 tsoMark=document.createElement('A');
 tsoP.insertBefore(tsoMark,tsoTObj);
 tsoTC=tsoTObj.cloneNode(false);
 tsoTR=tsoTObj.getElementsByTagName('TR');
 for (tso0=0;tso0<tsoTR.length;tso0++){ tsoTRAry[tso0]=tsoTR[tso0].cloneNode(true); }
 tsoRows=tsoTR.length;
 tsoSortAry=new Array(tsoRows-1);
 for (tso0=1;tso0<tsoRows;tso0++){
  tsoSortAry[tso0-1]=new Array();
  tsoSortAry[tso0-1][0]=tsoTR[tso0];
  tsoSortAry[tso0-1][1]=tsoTR[tso0].getElementsByTagName('TD')[tsoc].firstChild.data;
 }
}

function tsoCreateDOMTable(){
 tsoTBody=document.createElement("TBODY");
 tsoTC.appendChild(tsoTBody);
 tsoP.removeChild(tsoTObj);
 tsoP.insertBefore(tsoTC,tsoMark);
 tsoP.removeChild(tsoMark);
 tsoTBody.appendChild(tsoTRAry[0]);
 if (tsoDir=='A'){ for (tso0=0;tso0<tsoSortAry.length;tso0++){ tsoTBody.appendChild(tsoSortAry[tso0][0]); } }
 if (tsoDir=='D'){ for (tso0=tsoSortAry.length-1;tso0>=0;tso0--){ tsoTBody.appendChild(tsoSortAry[tso0][0]); } }
}

function tsoSortAlpha(tso0,tso1){
 tsoA=tso0[1].toLowerCase();
 tsoB=tso1[1].toLowerCase();
 if (tsoA<tsoB){ return -1; }
 if (tsoA>tsoB){ return 1; }
 return 0;
}

// Mick White advised me to replace this with the code below:
// function tsoSortNumeric(tso0,tso1){
//  tsoA=tso0[1]; tsoB=tso1[1];
//  if (isNaN(tsoA)){ return 0;}
//  else {
//  if (isNaN(tsoB)){ return 0; }
//   else { return tsoA-tsoB; }
//  }
// }

function deComma(val){return val.replace(/[^\d]/g,"")}
function tsoSortNumeric(a,b){
return  deComma(a[1])-deComma(b[1]);
}
// End Mick Script

function tsoSortDate(tsoD){
 tsoSep='/';
 if (tsoDateFormat!='A'){
  for (tso0=0;tso0<tsoD.length;tso0++){
   if (isNaN(tsoD.charAt(tso0))){ tsoSep=tsoD.charAt(tso0);  }
  }
 }

 for (tso1=0;tso1<tsoSortAry.length;tso1++){
  tsoS=tsoSortAry[tso1][1].split(tsoSep);
  for (tso2=0;tso2<tsoS.length;tso2++){
   for (tso3=0;tso3<tsoMonthAry.length;tso3++){
    if (isNaN(tsoS[tso2])){ if (tsoS[tso2].toLowerCase().match(tsoMonthAry[tso3])){tsoS[tso2]=tso3; } }
   }
  }
  tsoSortAry[tso1][1]=tsoS[0]+tsoSep+tsoS[1]+tsoSep+tsoS[2];
 }
 tsoSortAry.sort(tsoSortDateDo);
}

function tsoSortDateDo(tso0,tso1){
 tsoA=tso0[1].split(tsoSep);
 tsoB=tso1[1].split(tsoSep);
 if (tsoDateFormat=='A'){
  tsoDA=new Date(tsoA[1],tsoA[2],tsoA[0]);
  tsoDB=new Date(tsoB[1],tsoB[2],tsoB[0]);
 }
 else {
  tsoDA=new Date(tsoA[2],tsoA[1],tsoA[0]);
  tsoDB=new Date(tsoB[2],tsoB[1],tsoB[0]);
 }
 if (tsoDA<tsoDB){ return -1; }
  else { if (tsoDA>tsoDB){ return 1; }
   else { return 0; }
 }
}


function tsogEBId(tso){
 return document.getElementById(tso);
}