// jQuery.sortable
// Version 1.0
//NOTE: These functions require the jQuery library.

var sortTableVars=new Object();


	sortTableVars.noSortClass='noSort'; //Class applied to a header cell if that column should not be sortable
	sortTableVars.sortHeaderClass='sortHeader'; //Class applied to the header cell link
	sortTableVars.headerLinkTitle="Sort this column"; //The title of the header sort link
	sortTableVars.alreadySortedAscendingClass='sortedAscending';//Class applied to the header cell of a column that is already sorted ascending on page load
	sortTableVars.alreadySortedDescendingClass='sortedDescending';//Class applied to the header cell of a column that is already sorted descending on page load
	
	
	sortTableVars.sortArrowClass='sortArrow'; //Class applied to the span that holds the sort arrow.
	sortTableVars.ascSortArrow='&nbsp;&darr;'; //HTML shown when a column is sorted ascending - normally a down arrow
	sortTableVars.descSortArrow='&nbsp;&uarr;'; //HTML shown when a column is sorted descending - normally an up arrow
	sortTableVars.sortDirectionAscClass='sortDirectionAsc';// Additional class applied to the span that holds the sort arrow when sorted ascending;
	sortTableVars.sortDirectionDesClass='sortDirectionDes';// Additional class applied to the span that holds the sort arrow when sorted descending;

	sortTableVars.sortKeyClassStart="dataType_";
	sortTableVars.sortFunctionClassStart="sort_";
	sortTableVars.sortKeyClass="sortKey";
	
	sortTableVars.sortedColumnClass='sortedCol';
	sortTableVars.sortHeaderRowClass='sortHeaderRow'; //Class applied to tell the script what row is the header row.
	sortTableVars.secondarySortClass="secondarySort_";//Start of the class to determine secondary column sorting

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$.fn.alternateRowColors = function() { $('tbody tr:odd', this).removeClass('odd').addClass('even'); $('tbody tr:even', this).removeClass('even').addClass('odd'); return this;}; $.fn.setSortTable=function(){ var headerRow; if($(this).find('tr.'+sortTableVars.sortHeaderRowClass).length>0){ headerRow=$(this).find('tr.'+sortTableVars.sortHeaderRowClass).eq(0);}
else if($(this).find('thead').length>0){ headerRow=$(this).find('thead tr:last');}
else{ headerRow=$(this).find('tr:first');}
if($(headerRow).parents('thead').length==0){ $(this).prepend("<thead></thead>"); $(this).find('thead:first').append(headerRow);}
$(headerRow).children().each(function(headerCellIndex){ $(this).setSortLink(headerCellIndex);}); $(this).alternateRowColors(); return this;}; $.fn.setSortLink=function(index,direction){ if(!$(this).hasClass(sortTableVars.noSortClass)){ if(index==undefined||index==""){ var row= $(this).parents('tr').eq(0); var currentCell2; $(this).each(function(){currentCell2=this;}); $(row).find('th').each(function(i){ if(this==currentCell2) index=i;}); if(index==undefined||index==""){ $(row).find('td').each(function(i){ if(this==currentCell2) index=i;});}
}
var headerLink=this; var displayBlock=true; var direction=""; if($(headerLink).find('.'+sortTableVars.sortHeaderClass).length>0){ var sortHeaderContainer=$(headerLink).find('.'+sortTableVars.sortHeaderClass).eq(0); $(sortHeaderContainer).removeClass(sortTableVars.sortHeaderClass); headerLink=sortHeaderContainer; displayBlock=false;}
$(headerLink).wrapInner('<a href="javascript:void(0)"></a>'); var $a=$(headerLink).find('a:first'); $a.attr('title',sortTableVars.headerLinkTitle).addClass(sortTableVars.sortHeaderClass); if($(headerLink).hasClass(sortTableVars.alreadySortedAscendingClass))direction=1; if($(headerLink).hasClass(sortTableVars.alreadySortedDescendingClass))direction=-1; $a.setSortArrow(direction,false); if(displayBlock)$a.css('display','block'); $a.click(function(){$(this).sortTableColumn(index);});}
return this;}; $.fn.setSecondarySort=function(secondaryCellObj,direction){ $(this).removeSecondarySort(); var tableCell=$(this).sortGetParentCell(); var secondarySortString=sortTableVars.secondarySortClass; var lastobj=""; for(var i=0;i<arguments.length;i++){ if(typeof arguments[i]=="object"){ if(i!=0) secondarySortString+="_"; var index=$(arguments[i]).sortGetTableCellIndex(); secondarySortString+=index+"_"; if(arguments[i+1]!="object"&&arguments[i+1]!=undefined) secondarySortString+=arguments[i+1]; else secondarySortString+="0";}
}
$(tableCell).addClass(secondarySortString);}; $.fn.setSecondarySortByIndex=function(index,direction){ if(arguments.length%2!=0){ alert("Odd number of arguments sent to the setSecondarySortByIndex method")
return this;}
var tableCell=$(this).sortGetParentCell(); var secondarySortString=sortTableVars.secondarySortClass; for(var i=0;i<arguments.length;i++){ if(i!=0) secondarySortString+="_"; secondarySortString+=arguments[i];}
$(tableCell).addClass(secondarySortString);}; $.fn.removeSecondarySort=function(){ var thislink=$(this).sortCheckForClass(sortTableVars.secondarySortClass); if(thislink!=""){ $(this).removeClass(thislink); return this;}
var thistd=$(this).parent('th'); if(thistd.length!=0){ var tdSort=$(thistd).eq(0).sortCheckForClass(sortTableVars.secondarySortClass); if(tdSort!=""){ $(thistd).eq(0).removeClass(tdSort); return this;}
}
var thistd=$(this).parent('td'); if(thistd.length!=0){ var tdSort=$(thistd).eq(0).sortCheckForClass(sortTableVars.secondarySortClass); if(tdSort!=""){ $(thistd).eq(0).removeClass(tdSort); return this;}
}
return this;}; $.fn.setSortArrow=function(direction,removeEmpty){ if(removeEmpty!=false) removeEmpty=true; if($(this).find('.'+sortTableVars.sortArrowClass).length==0){ $(this).append("<span class='"+sortTableVars.sortArrowClass+"'></span>");}
if(removeEmpty)$(this).parents('table').eq(0).find('.'+sortTableVars.sortArrowClass).empty(); $(this).removeClass(sortTableVars.sortDirectionAscClass).removeClass(sortTableVars.sortDirectionDesClass).find('.'+sortTableVars.sortArrowClass).eq(0).empty(); if(direction==1){ $(this).addClass(sortTableVars.sortDirectionAscClass).find('.'+sortTableVars.sortArrowClass).eq(0).html(sortTableVars.ascSortArrow);}
else if(direction==-1){ $(this).addClass(sortTableVars.sortDirectionDesClass).find('.'+sortTableVars.sortArrowClass).eq(0).html(sortTableVars.descSortArrow);}
return this;}; $.fn.sortTableColumn=function(index,direction,sortKeyFunction,sortFunction){ dataType_alpha=function($cell) { if($.browser.msie){ var html=$cell.html().replace(/&nbsp;+/g," "); $cell.html(html);}
return $.trim($cell.find('.'+sortTableVars.sortKeyClass).text().toUpperCase()) + ' ' + $.trim($cell.text().toUpperCase());}; dataType_numeric= function($cell) { var num=$cell.text(); if($cell.find('.'+sortTableVars.sortKeyClass).length>0) num=$cell.find('.'+sortTableVars.sortKeyClass).eq(0); var key = parseFloat(num.replace(/^[^\d.]*/, '')); return isNaN(key) ? 0 : key;}; dataType_date = function($cell) { if($.browser.msie){ var html=$cell.html().replace(/&nbsp;+/g," "); $cell.html(html);}
var aa= $.trim($cell.text()); if($cell.find('.'+sortTableVars.sortKeyClass).length>0) aa=$.trim($cell.find('.'+sortTableVars.sortKeyClass).eq(0)); return aa.substr(6, 4) + aa.substr(0, 2) + aa.substr(3, 2);}; sort_default=function(a,b){ for(var i=0;i<a.sortKey.length;i++){ if (a.sortKey[i] > b.sortKey[i]){ return a.sortDirection[i];}
if (a.sortKey[i] < b.sortKey[i]){ return -a.sortDirection[i];}
}
return 0;}; checkSecondarySort=function(obj){ var sortClass=$(obj).sortCheckForClass(sortTableVars.secondarySortClass); if(sortClass=="")return ""; var tempArray=sortClass.split('_'); tempArray.shift(); if(tempArray.length%2!=0){ alert('incorrect number of items set in the secondarySort class'); return "";}
var itemArray=new Array(); for(var i=0;i<tempArray.length;i=i+2){ itemArray.push(new Array(tempArray[i],tempArray[i+1]));}
return itemArray;}; var checkForSecondarySort=false; var sortArray; if(typeof index!="object"){ sortArray=new Array(new Array(index,direction,sortKeyFunction)); checkForSecondarySort=true;}
else{ sortArray=index; sortFunction=direction;}
var primaryHeaderCell; var table; var headerLink; var primaryHeaderLink; var primaryIndex=sortArray[0][0]; var primaryDirection=sortArray[0][1]; var tag=$(this).getTagName(); if(tag=="table"||tag=="TABLE"){ table=this; if(primaryIndex==undefined){ alert("No column index was passed to the sortTableColumn function - sorting did not happen."); return this;}
if($(table).find('tr.'+sortTableVars.sortHeaderRowClass).length>0){ headerRow=$(table).find('tr.'+sortTableVars.sortHeaderRowClass).eq(0);}
else if($(table).find('thead').length>0){ headerRow=$(table).find('thead tr:last');}
else{ headerRow=$(table).find('tr:first');}
primaryHeaderCell=$(headerRow).children().eq(primaryIndex); primaryHeaderLink=$(primaryHeaderCell).find('.'+sortTableVars.sortHeaderClass).eq(0);}
else if(tag=="td"||tag=="TD"||tag=="th"||tag=="TH"){ primaryHeaderCell=this; headerRow=$(primaryHeaderCell).parents('tr').eq(0); table=$(this).parents('table').eq(0); primaryHeaderLink=$(this).find('.'+sortTableVars.sortHeaderClass).eq(0);}
else if((tag=="a"||tag=="A")&&$(this).hasClass(sortTableVars.sortHeaderClass)){ if($(this).parents('th').length>0)primaryHeaderCell=$(this).parents('th').eq(0); else primaryHeaderCell=$(this).parents('td').eq(0); headerRow=$(primaryHeaderCell).parents('tr').eq(0); table=$(this).parents('table').eq(0); primaryHeaderLink=this;}
else{ alert("Improper object daisy-chained to the sortTableColumn function - sorting did not happen."); return this;}
if(checkForSecondarySort){ var secondarySortArray=checkSecondarySort(primaryHeaderCell); if(secondarySortArray!=""&&secondarySortArray.length>0){ for(var i=0;i<secondarySortArray.length;i++){ sortArray.push(secondarySortArray[i]);}
}
}
if(primaryIndex==undefined||primaryIndex==""){ var row= $(primaryHeaderCell).parents('tr').eq(0); var currentCell2; $(primaryHeaderCell).each(function(){currentCell2=this;}); $(row).find('th').each(function(i){ if(this==currentCell2) sortArray[0][0]=i;}); if(index==undefined||index==""){ $(row).find('td').each(function(i){ if(this==currentCell2) sortArray[0][0]=i;});}
primaryIndex=sortArray[0][0];}
var rows = $(table).find('tbody > tr').get(); for(var j=0;j<sortArray.length;j++){ var index=sortArray[j][0]; var direction=sortArray[j][1]; var sortKeyFunc=sortArray[j][2]; if(direction==0){ direction=primaryDirection;}
if(direction==undefined||direction==""||(direction!=1&&direction!=-1)){ if($(headerRow).children().eq(index).find('.'+sortTableVars.sortHeaderClass).eq(0).hasClass(sortTableVars.sortDirectionAscClass)){direction=-1;}
else{direction=1;}
if(j==0) primaryDirection=direction;}
if(sortKeyFunc==undefined||sortKeyFunc==""){ sortKeyFunc=""; var sortKeyFromClass=$(headerRow).children().eq(index).sortCheckForClass(sortTableVars.sortKeyClassStart); if(sortKeyFromClass) sortKeyFunc=eval(sortKeyFromClass);}
if(sortKeyFunc==""){ sortKeyFunc=dataType_alpha; $(headerRow).children().eq(index).addClass('dataType_alpha'); var cellText=""; for(var i=0;cellText=="";i++){ var sortCell=$(table).find('tbody tr').eq(i).find('td').eq(index); if($.browser.msie){ var html=$(sortCell).html().replace(/&nbsp;+/g," "); $(sortCell).html(html);}
var currentCellText=$.trim($(table).find('tbody tr').eq(i).find('td').eq(index).text()); if(currentCellText!="")cellText=currentCellText;}
if ( cellText.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/) ){ sortKeyFunc=dataType_date; $(headerRow).children().eq(index).removeClass('dataType_alpha').addClass('dataType_date');}
if ( cellText.match(/^\d\d[\/-]\d\d[\/-]\d\d$/) ){ sortKeyFunc=dataType_date; $(headerRow).children().eq(index).removeClass('dataType_alpha').addClass('dataType_date');}
if ( cellText.match(/^[£$]/) ){ sortKeyFunc=dataType_numeric; $(headerRow).children().eq(index).removeClass('dataType_alpha').addClass('dataType_numeric');}
if ( cellText.match(/^[\d\.]+$/) ){ sortKeyFunc=dataType_numeric; $(headerRow).children().eq(index).removeClass('dataType_alpha').addClass('dataType_numeric');}
}
$.each(rows, function(p, row) { if(j==0){ row.sortKey=new Array(); row.sortDirection=new Array();}
row.sortKey[j] = sortKeyFunc($(row).children('td').eq(index)); row.sortDirection[j]=direction;});}
if(sortFunction==undefined||sortFunction==""){ sortFunction=""; var sortFunctionFromClass=$(primaryHeaderCell).sortCheckForClass(sortTableVars.sortFunctionClassStart); if(sortFunctionFromClass!="") sortKey=eval(sortFunctionFromClass);}
if(sortFunction==""){ sortFunction=sort_default; $(primaryHeaderCell).addClass('sort_default');}
rows.sort(sortFunction); $.each(rows, function(i, row) { $(table).children('tbody').append(row); $(row).find('.'+sortTableVars.sortedColumnClass).removeClass(sortTableVars.sortedColumnClass); $(row).find('td').eq(primaryIndex).addClass(sortTableVars.sortedColumnClass); row.sortKey = null; row.sortDirection=null;}); $(primaryHeaderCell).parents('tr').eq(0).find('.'+sortTableVars.sortedColumnClass).removeClass(sortTableVars.sortedColumnClass); $(primaryHeaderCell).addClass(sortTableVars.sortedColumnClass); $(table).alternateRowColors(); $(primaryHeaderLink).setSortArrow(primaryDirection); return this;}; $.fn.sortCheckForClass=function(regExp){ var classes=new Array(); var regExp=new RegExp(regExp); classes=$(this).attr('class').split(' '); for(var i=0;i<classes.length;i++){ if(classes[i].match(regExp)) return classes[i];}
return "";}; $.fn.getTagName=function(){ var thisTagName=""; $(this).each(function(){thisTagName=this.tagName;}); return thisTagName;}; $.fn.sortGetParentCell=function(){ var tagname=$(this).getTagName(); if(tagname=="th"||tagname=="TH"||tagname=="td"||tagname=="TD")return this; var parentCell=$(this).parent('th'); if(parentCell.length>0)return parentCell.eq(0); var parentCell=$(this).parent('td'); if(parentCell.length>0)return parentCell.eq(0); return this;}; $.fn.sortGetTableCellIndex=function(){ var tableCell=$(this).sortGetParentCell(); var row= $(tableCell).parents('tr').eq(0); var currentCell2; var index=""; $(tableCell).each(function(){currentCell2=this;}); $(row).find('th').each(function(i){ if(this==currentCell2){ index=i; alert('th '+i);}
}); if(index==undefined||index==""){ $(row).find('td').each(function(i){ if(this==currentCell2){ index=i;}
});}
return index;}; 