/*
 * SRSA Website and Race Management Software
 *
 * Pete Calvert <pete@skiresults.co.uk>
 * Copyright 2008/09
 *
 * HTML Table Sorting Functions
 * javascript/tables.js
 */

var gMonths = [
  'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august',
  'september', 'october', 'november', 'december'
];

// Comparison of values as floats
function Compare_Numeric(aA, aB) {
  var lA = parseFloat(aA[0].replace(/[^0-9.-]/g,''));
  var lB = parseFloat(aB[0].replace(/[^0-9.-]/g,''));
	
  if(isNaN(lA)) return 1;
  if(isNaN(lB)) return -1;

	return lA - lB;
}

// Comparison of values as dates (DDth Month YYYY)
function Compare_Date(aA, aB) {
	var lArrA = aA[0].split(' ');
	var lArrB = aB[0].split(' ');
	var lMonthA, lMonthB;
	var lDateA, lDateB;
	
	// Convert Month A
  for(lMonthA = 1; lMonthA <= 12; lMonthA++)
    if(lArrA[1] == gMonths[lMonthA - 1]) break;
	
	// Convert Month B
  for(lMonthB = 1; lMonthB <= 12; lMonthB++)
    if(lArrB[1] == gMonths[lMonthB - 1]) break;
		
	// Dates
  if(lArrA[0].length == 3)
		lArrA[0] = '0' + lArrA[0];
	
  if(lArrB[0].length == 3)
		lArrB[0] = '0' + lArrB[0];
	
	lDateA = lArrA[2] + ((lMonthA < 10) ? '0':'')+lMonthA + lArrA[0].slice(0, -2);
	lDateB = lArrB[2] + ((lMonthB < 10) ? '0':'')+lMonthB + lArrB[0].slice(0, -2);
	
	return parseInt(lDateA) - parseInt(lDateB);
}

function Tables_Contents(aTable, aI, aJ) {
	var lCell = aTable.tBodies[0].rows[aI].cells[aJ]

	if(typeof lCell.textContent != 'undefined')
		return lCell.textContent.replace(/^\s+|\s+$/g, '').toLowerCase();
	else if(typeof lCell.innerText != 'undefined')
		return lCell.innerText.replace(/^\s+|\s+$/g, '').toLowerCase();
	else if(typeof lCell.text != 'undefined')
		return lCell.text.replace(/^\s+|\s+$/g, '').toLowerCase();
	else
		return "";
}

function Tables_Sort(aID, aColumn, aType, aLink) {
	var lTable	= document.getElementById(aID);
	var lBody	= lTable.tBodies[0];
	var lRows	= [];
	
	// If already sorted by column, just reverse
	if(lTable.Sorted == aColumn) {
		for(var lI = 0; lI < lBody.rows.length; lI++)
			lRows[lI] = lBody.rows[lI];
		
		for(var lI = lRows.length - 1; lI >= 0; lI--)
			lBody.appendChild(lRows[lI]);
		
		// Change Link
		if(aLink.className.indexOf("sort_asc") != -1)
			aLink.className = aLink.className.replace(/sort_asc/, "sort_desc");
		else
			aLink.className = aLink.className.replace(/sort_desc/, "sort_asc");
		
		return;
	}
	
	// Create array of rows along with sort value
	for(var lI = 0; lI < lBody.rows.length; lI++)
		lRows[lI] = [Tables_Contents(lTable, lI, aColumn), lBody.rows[lI]];
	
	// Sort by correct method
	if(aType == 'numeric')
    lRows.sort(Compare_Numeric);
  else if(aType == 'date')
    lRows.sort(Compare_Date);
  else
    lRows.sort();
	
	// Change Link
	if(typeof lTable.SortLink != 'undefined')
		lTable.SortLink.className = lTable.SortLink.className.replace(/sort_(desc|asc)/, "");
	
	aLink.className	+= " sort_asc";
	lTable.SortLink	= aLink;
	
	// Add rows in new order
	for(var lI = 0; lI < lRows.length; lI++)
		lBody.appendChild(lRows[lI][1]);
	
	// Store sorted column
	lTable.Sorted = aColumn;
}


