var p = ['Up','Down']; for( var q = 0, r = []; q < 4; q++ ) { r[q] = new Image(); r[q].src = 'ArrowSort' + p[q%2] + ((q>1)?'Y':'') + '.gif'; }
var arraySort = [function (a,b) {
		//sort on date (use trip number as there may be more than one trip on a day)
		oA = parseInt( a[0].replace(/^.*trip\s*/i,'') );
		oB = parseInt( b[0].replace(/^.*trip\s*/i,'') );
		return window.RoundWay ? ( oB - oA ) : ( oA - oB );
	},function (a,b) {
		//sort on name - names must be identical in length for numeric comparison
		var c = a[1].replace(/<[^>]*>/g,'').toLowerCase(); var d = b[1].replace(/<[^>]*>/g,'').toLowerCase();
		while( c.length > d.length ) { d += '~'; } while( d.length > c.length ) { c += '~'; }
		return ( d == c ) ? arraySort[0](a,b) : ( ( window.RoundWay ? ( d > c ) : ( c > d ) ) ? 1 : -1 );
	},function (a,b) {
		//sort on length - convert to ( 60 * hr ) + min
		c = a[4].toLowerCase().replace(/\./g,'').replace(/\s/g,''); d = b[4].toLowerCase().replace(/\./g,'').replace(/\s/g,'');
		if( c.indexOf( 'min' ) + 1 ) { c = c.replace(/hr/,'hr+').replace(/min/g,''); }
		if( c.indexOf( 'hr' ) + 1 ) { c = '('+c.replace(/hr/,'*60)'); }
		if( d.indexOf( 'min' ) + 1 ) { d = d.replace(/hr/,'hr+').replace(/min/g,''); }
		if( d.indexOf( 'hr' ) + 1 ) { d = '('+d.replace(/hr/,'*60)'); }
		eval( 'c=' + c ); eval( 'd=' + d ); return ( d == c ) ? arraySort[0](window.RoundWay?b:a,window.RoundWay?a:b) : ( window.RoundWay ? ( d - c ) : ( c - d ) );
	},function (a,b) { return ( a[0] == b[0] ) ? arraySort[1](window.RoundWay?b:a,window.RoundWay?a:b) : ( window.RoundWay ? ( b[0] - a[0] ) : ( a[0] - b[0] ) ); }]; //sort on number of trips
function gI(a,b,c,d,e) {
	//setup images
	return ((a==c&&b==1)?'<\/em>':'')+'<a href="#" onclick="s'+(e?'U':'I')+'('+a+','+b+');return false;" style="padding-right:2px;padding-left:2px;"><img src="'+(b?'ArrowSortDown':'ArrowSortUp')+((a==c&&b==d)?'Y':'')+'.gif" height="9" width="7" alt="'+(b?'v':'^')+'"><\/a>'+((a==c&&b==0)?'<em>':'');
}
function sI(a,b) {
	if( !window.pageHasLoaded ) { alert('These buttons will not work until the page has loaded.'); return; }
	//setup trip table
	var myTable = document.getElementById('triptable'), theRows = [];
	for( var y = 1, x = myTable.getElementsByTagName('tr'); x[y]; y++ ) {
		var theCells = x[y].getElementsByTagName('td');
		theRows[y-1] = [theCells[0].innerHTML,theCells[1].innerHTML,theCells[2].innerHTML,theCells[3].innerHTML,theCells[4].innerHTML];
	} window.RoundWay = b; theRows.sort(arraySort[a]);
	var outLine = '<thead><tr><th>'+gI(0,0,a,b)+'Date'+gI(0,1,a,b)+'<\/th><th>'+gI(1,0,a,b)+'Cave'+gI(1,1,a,b)+
		'<\/th><th>Where<\/th><th>Who with<\/th><th>'+gI(2,0,a,b)+'Length'+gI(2,1,a,b)+'<\/th>'+((a==2)?'<th>n<\/th>':'')+'<\/tr><\/thead><tbody>';
	for( var y = 0; theRows[y]; y++ ) {
		outLine += '<tr><td>'+theRows[y][0]+'<\/td><td>'+theRows[y][1]+'<\/td><td>'+theRows[y][2]+'<\/td><td>'+theRows[y][3]+'<\/td><td>'+theRows[y][4]+'<\/td>'+((a==2)?('<td><var>'+(y+1)+'<\/var><\/td>'):'')+'<\/tr>';
	} myTable.parentNode.innerHTML = '<table id="triptable" class="triplist wider">'+outLine+'<\/tbody><\/table>';
}
function sU(a,b,c) {
	if( !window.pageHasLoaded && !c ) { alert('These buttons will not work until the page has loaded.'); return; }
	//setup summary table
	var myTable = document.getElementById('triptable'), myP = document.getElementById('theP'), theCaves = [], totTrips = 0;
	for( var y = 1, x = myTable.getElementsByTagName('tr'); x[y]; y++ ) {
		totTrips++; var theCells = x[y].getElementsByTagName('td')[1].innerHTML.replace(/<[^>]*>/g,'');
		if( theCaves[theCells] ) { theCaves[theCells][0]++; } else { theCaves[theCells] = [1,theCells]; theCaves[theCaves.length] = theCaves[theCells]; }
	} window.RoundWay = b; theCaves.sort(arraySort[a]);
	var outLine = '<thead><tr><th>'+gI(3,0,a,b,true)+'Number of trips'+gI(3,1,a,b,true)+'<\/th><th>'+gI(1,0,a,b,true)+'Cave'+gI(1,1,a,b,true)+'<\/th><\/tr><\/thead>'+
		'<tfoot><tr class="bl"><td><dfn>Total trips:<\/dfn> '+totTrips+'<\/td><td><dfn>Total caves:<\/dfn> '+theCaves.length+'<\/td><\/tr><\/tfoot><tbody>';
	for( var y = 0; theCaves[y]; y++ ) {
		outLine += '<tr><td>'+theCaves[y][0]+'<\/td><td>'+theCaves[y][1].replace(/<[^>]*>/g,'')+'<\/td><\/tr>';
	}
	myP.innerHTML = '<table class="triplist">'+outLine+'<\/tbody><\/table>';
}
function safelyWrite(newmark) {
	if( document.getElementsByTagName && document.childNodes && document.createElement ) {
		document.write(newmark);
	}
}
window.onload = function () {
	if( document.getElementsByTagName && document.childNodes && document.createElement ) {
		sU(1,0,true);
		window.pageHasLoaded = true;
	}
};