Difference between revisions of "MediaWiki:Common.js"

m
m (Another round of shenanigans, I'm sure.)
Line 116: Line 116:
 
var downArrow = 'images/7/7a/Sort_down.gif';
 
var downArrow = 'images/7/7a/Sort_down.gif';
 
var noneArrow = 'images/e/e8/Sort_none.gif';
 
var noneArrow = 'images/e/e8/Sort_none.gif';
 +
var numberTransformTable = null;
 +
var numberRegex = null;
  
function sortTablesInit() {
+
function sortTableInit() {
 
     var idnum = 0;
 
     var idnum = 0;
     var tables = getElementsByClassName(document, 'table', 'sorts');
+
     var tables = getElementsByClassName(document, 'table', 'sortable');
 +
 
 
     for (var i = 0; i < tables.length; i++) {
 
     for (var i = 0; i < tables.length; i++) {
 
         if (!tables[i].id) {
 
         if (!tables[i].id) {
 
             tables[i].setAttribute('id', 'sortable_table_id_' + idnum);
 
             tables[i].setAttribute('id', 'sortable_table_id_' + idnum);
             idnum++;
+
             ++idnum;
 
         }
 
         }
 
         makeSortable(tables[i]);
 
         makeSortable(tables[i]);
Line 144: Line 147:
 
     }
 
     }
  
for (var i = 0; i < firstRow.cells.length; i++) {
+
    for (var i = 0; i < firstRow.cells.length; i++) {
 
         var cell = firstRow.cells[i];
 
         var cell = firstRow.cells[i];
 +
 
         if ((' ' + cell.className + ' ').indexOf(' unsortable ') == -1) {
 
         if ((' ' + cell.className + ' ').indexOf(' unsortable ') == -1) {
             var spacer = document.createElement('span');
+
             cell.innerHTML += '&nbsp;&nbsp;'
 
+
                + '<a href="#" class="sortheader" '
            spacer.style.cssFloat = 'left';
+
                + 'onclick="reSortTable(this);return false;">'
            spacer.style.styleFloat = 'left';
+
                + '<span class="sortarrow">'
            spacer.style.textAlign = 'left';
+
                + '<img src="' + noneArrow + '" alt="&darr;"/></span></a>';
            spacer.style.width = '12px';
+
            spacer.appendChild(document.createTextNode('\u00a0'));
+
            cell.insertBefore(spacer, cell.childNodes[0]);
+
 
+
            cell.innerHTML += '<a href="" class="sortheader" onclick="reSortTable(this);return false;"><span class="sortarrow"><img src="'+ noneArrow + '" alt="&darr;" /></span></a>';
+
 
         }
 
         }
 
     }
 
     }
 
    alternate(table);
 
 
}
 
}
  
function getInnerText(el) {
+
function ts_getInnerText(el) {
     if (typeof el == 'string' || typeof el == 'undefined') {
+
     return getInnerText(el);
        return el;
+
    }
+
    if (el.textContent) {
+
        return el.textContent;
+
    }
+
    if (el.innerText) {
+
        return el.innerText;
+
    }
+
 
+
    var innerText = '';
+
    var cs = el.childNodes;
+
    var l = cs.length;
+
 
+
    for (var i = 0; i < l; i++) {
+
        switch (cs[i].nodeType) {
+
            case 1: innerText += getInnerText(cs[i]); break;
+
            case 3: innerText += cs[i].nodeValue; break;
+
        }
+
    }
+
 
+
    return innerText;
+
 
}
 
}
  
Line 202: Line 178:
 
         return;
 
         return;
 
     }
 
     }
 +
 
     if (table.rows.length <= 1) {
 
     if (table.rows.length <= 1) {
 
         return;
 
         return;
 +
    }
 +
 +
    if (numberTransformTable == null) {
 +
        initTransformTable();
 
     }
 
     }
  
Line 211: Line 192:
 
     for (var i = rowStart; i < table.rows.length; i++) {
 
     for (var i = rowStart; i < table.rows.length; i++) {
 
         if (table.rows[i].cells.length > column) {
 
         if (table.rows[i].cells.length > column) {
             itm = getInnerText(table.rows[i].cells[column]);
+
             itm = ts_getInnerText(table.rows[i].cells[column]);
 
             itm = itm.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '');
 
             itm = itm.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '');
 +
 
             if (itm != '') {
 
             if (itm != '') {
 
                 break;
 
                 break;
Line 219: Line 201:
 
     }
 
     }
  
     sortfn = sortCaseInsensitive;
+
     var sortfn = sortGeneric;
 +
    var preprocessor = ts_toLowerCase;
  
     if (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/)) {
+
     if (/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test(itm)) {
         sortfn = sortDate;
+
         preprocessor = dateToSortKey;
     }
+
     } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test(itm)) {
    if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/)) {
+
         preprocessor = dateToSortKey;
         sortfn = sortDate;
+
     } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d$/.test(itm)) {
     }
+
         preprocessor = dateToSortKey;
    if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/)) {
+
     } else if (/(^[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test(itm)) {
         sortfn = sortDate;
+
         preprocessor = currencyToSortKey;
     }
+
     } else if (ts_number_regex.test(itm)) {
    if (itm.match(/^[\u00a3$\u20ac]/)) {
+
         preprocessor = parseFloat;
         sortfn = sortCurrency;
+
     }
+
    if (itm.match(/^[\d.,]+\%?$/)) {
+
         sortfn = sortNumeric;
+
 
     }
 
     }
  
 
     var reverse = (span.getAttribute('sortdir') == 'down');
 
     var reverse = (span.getAttribute('sortdir') == 'down');
 
     var newRows = new Array();
 
     var newRows = new Array();
 +
    var staticRows = new Array();
  
 
     for (var j = rowStart; j < table.rows.length; j++) {
 
     for (var j = rowStart; j < table.rows.length; j++) {
 
         var row = table.rows[j];
 
         var row = table.rows[j];
         var keyText = getInnerText(row.cells[column]);
+
 
        var oldIndex = (reverse ? -j : j);
+
         if((' ' + row.className + ' ').indexOf(' unsortable ') < 0) {
        newRows[newRows.length] = new Array(row, keyText, oldIndex);
+
            var keyText = ts_getInnerText(row.cells[column]);
 +
 
 +
            if(keyText == undefined) {
 +
                keyText = '';
 +
            }
 +
 
 +
            var oldIndex = (reverse ? -j : j);
 +
            var preprocessed = preprocessor( keyText.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '') );
 +
 
 +
            newRows[newRows.length] = new Array(row, preprocessed, oldIndex);
 +
        } else {
 +
            staticRows[staticRows.length] = new Array(row, false, j - rowStart);
 +
        }
 
     }
 
     }
 +
 
     newRows.sort(sortfn);
 
     newRows.sort(sortfn);
  
Line 251: Line 244:
  
 
     if (reverse) {
 
     if (reverse) {
         arrowHTML = '<img src="' + downArrow + '" alt="&darr;"/>';
+
         arrowHTML = '<img src="'+ downArrow + '" alt="&darr;"/>';
 
         newRows.reverse();
 
         newRows.reverse();
 
         span.setAttribute('sortdir', 'up');
 
         span.setAttribute('sortdir', 'up');
 
     } else {
 
     } else {
         arrowHTML = '<img src="' + upArrow + '" alt="&uarr;"/>';
+
         arrowHTML = '<img src="'+ upArrow + '" alt="&uarr;"/>';
 
         span.setAttribute('sortdir', 'down');
 
         span.setAttribute('sortdir', 'down');
 +
    }
 +
 +
    for (var i = 0; i < staticRows.length; i++) {
 +
        var row = staticRows[i];
 +
 +
        newRows.splice(row[2], 0, row);
 
     }
 
     }
  
Line 274: Line 273:
  
 
     for (var i = 0; i < spans.length; i++) {
 
     for (var i = 0; i < spans.length; i++) {
         spans[i].innerHTML = '<img src="' + noneArrow + '" alt="&darr;"/>';
+
         spans[i].innerHTML = '<img src="'+ noneArrow + '" alt="&darr;"/>';
 
     }
 
     }
  
 
     span.innerHTML = arrowHTML;
 
     span.innerHTML = arrowHTML;
    alternate(table);
 
 
}
 
}
  
function dateToSortKey(date) {
+
function initTransformTable() {
 +
    if (typeof wgSeparatorTransformTable == 'undefined' || (wgSeparatorTransformTable[0] == '' && wgDigitTransformTable[2] == '')) {
 +
        digitClass = '[0-9,.]';
 +
        numberTransformTable = false;
 +
    } else {
 +
        numberTransformTable = {};
 +
        ascii = wgSeparatorTransformTable[0].split('\t');
 +
        localised = wgSeparatorTransformTable[1].split('\t');
 +
 
 +
        for (var i = 0; i < ascii.length; i++) {
 +
            numberTransformTable[localised[i]] = ascii[i];
 +
        }
 +
 
 +
        ascii = wgDigitTransformTable[0].split('\t');
 +
        localised = wgDigitTransformTable[1].split('\t');
 +
 
 +
        for ( var i = 0; i < ascii.length; i++ ) {
 +
            numberTransformTable[localised[i]] = ascii[i];
 +
        }
 +
 
 +
        digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
 +
        maxDigitLength = 1;
 +
 
 +
        for (var digit in numberTransformTable ) {
 +
            digits.push(
 +
                digit.replace( /[\\\\$\*\+\?\.\(\)\|\{\}\[\]\-]/,
 +
                function( s ) { return '\\' + s; } )
 +
            );
 +
 
 +
            if (digit.length > maxDigitLength) {
 +
                maxDigitLength = digit.length;
 +
            }
 +
        }
 +
 
 +
        if (maxDigitLength > 1) {
 +
            digitClass = '[' + digits.join('', digits) + ']';
 +
        } else {
 +
            digitClass = '(' + digits.join('|', digits) + ')';
 +
        }
 +
    }
 +
 
 +
    numberRegex = new RegExp(
 +
        '^(' +
 +
        '[+-]?[0-9][0-9,]*(\\.[0-9,]*)?(E[+-]?[0-9][0-9,]*)?' +
 +
        '|' +
 +
        '[+-]?' + digitClass + '+%?' +
 +
        ')$', 'i'
 +
    );
 +
}
 +
 
 +
function ts_toLowerCase(s) {
 +
    return s.toLowerCase();
 +
}
 +
 
 +
function dateToSortKey(date) {
 
     if (date.length == 11) {
 
     if (date.length == 11) {
 
         switch (date.substr(3, 3).toLowerCase()) {
 
         switch (date.substr(3, 3).toLowerCase()) {
Line 297: Line 349:
 
             case 'dec': var month = '12'; break;
 
             case 'dec': var month = '12'; break;
 
         }
 
         }
 +
 
         return date.substr(7, 4) + month + date.substr(0, 2);
 
         return date.substr(7, 4) + month + date.substr(0, 2);
 
     } else if (date.length == 10) {
 
     } else if (date.length == 10) {
Line 302: Line 355:
 
     } else if (date.length == 8) {
 
     } else if (date.length == 8) {
 
         year = date.substr(6, 2);
 
         year = date.substr(6, 2);
         if (parseInt(year) < 50) {  
+
 
             year = '20' + year;  
+
         if (parseInt(year) < 50) {
         } else {  
+
             yr = '20' + year;
             year = '19' + year;  
+
         } else {
 +
             yr = '19' + year;
 
         }
 
         }
 +
 
         return year + date.substr(0, 2) + date.substr(3, 2);
 
         return year + date.substr(0, 2) + date.substr(3, 2);
 
     }
 
     }
Line 313: Line 368:
 
}
 
}
  
function parseFloat(num) {
+
function ts_parseFloat(s) {
     if (!num) {
+
     if (!s) {
 
         return 0;
 
         return 0;
 
     }
 
     }
  
     num = parseFloat(num.replace(/,/g, ''));
+
     if (numberTransformTable != false) {
 +
        var newNum = '', c;
  
    return (isNaN(num) ? 0 : num);
+
        for ( var p = 0; p < s.length; p++ ) {
}
+
            c = s.charAt( p );
  
function sortDate(a, b) {
+
            if (c in numberTransformTable) {
    var aa = dateToSortKey(a[1]);
+
                newNum += numberTransformTable[c];
    var bb = dateToSortKey(b[1]);
+
            } else {
    return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]);
+
                newNum += c;
}
+
            }
 +
        }
 +
        s = newNum;
 +
    }
  
function sortCurrency(a, b) {
+
     num = parseFloat(s.replace(/,/g, ''));
     var aa = parseFloat(a[1].replace(/[^0-9.]/g, ''));
+
    var bb = parseFloat(b[1].replace(/[^0-9.]/g, ''));
+
    return (aa != bb ? aa - bb : a[2] - b[2]);
+
}
+
  
function sortNumeric(a, b) {
+
     return (isNaN(num) ? 0 : num);
     var aa = parseFloat(a[1]);
+
    var bb = parseFloat(b[1]);
+
    return (aa != bb ? aa - bb : a[2] - b[2]);
+
 
}
 
}
  
function sortCaseInsensitive(a, b) {
+
function currencyToSortKey(s) {
     var aa = a[1].toLowerCase();
+
     return parseFloat(s.replace(/[^0-9.,]/g, ''));
    var bb = b[1].toLowerCase();
+
    return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]);
+
 
}
 
}
  
function sortDefault(a, b) {
+
function sortGeneric(a, b) {
     return (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2]);
+
     return a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2];
}
+
 
+
function alternate(table) {
+
    var tableBodies = table.getElementsByTagName('tbody');
+
    for (var i = 0; i < tableBodies.length; i++) {
+
        var tableRows = tableBodies[i].getElementsByTagName('tr');
+
 
+
        for (var j = 0; j < tableRows.length; j++) {
+
            var oldClasses = tableRows[j].className.split(' ');
+
            var newClassName = '';
+
 
+
            for (var k = 0; k < oldClasses.length; k++) {
+
                if (oldClasses[k] != '' && oldClasses[k] != 'even' && oldClasses[k] != 'odd') {
+
                    newClassName += oldClasses[k] + ' ';
+
                }
+
            }
+
 
+
            tableRows[j].className = newClassName + (j % 2 == 0 ? 'even' : 'odd');
+
        }
+
    }
+
 
}
 
}
  
 
addOnloadHook(sortTablesInit);
 
addOnloadHook(sortTablesInit);

Revision as of 22:19, 2 January 2010

/* Any JavaScript here will be loaded for all users on every page load. */

/**
 * Tests if a particular element possesses a class.
 */
var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp('(?:\\s|^)' + className + '(?:\\s|$)'))).test(element.className);
    };
})();


/**
 * Code for collapsible tables.
 */
var collapseCaption = 'hide';
var expandCaption = 'show';

function collapseTable(tableIndex) {
    var button = document.getElementById('collapseButton' + tableIndex);
    var table = document.getElementById('collapsibleTable' + tableIndex);

    if (!table || !button) {
        return false;
    }

    var rows = table.rows;

    if (button.firstChild.data == collapseCaption) {
        for (var i = 1; i < rows.length; i++) {
            rows[i].style.display = 'none';
        }
        button.firstChild.data = expandCaption;
    } else {
        for (var i = 1; i < rows.length; i++) {
            rows[i].style.display = rows[0].style.display;
        }
        button.firstChild.data = collapseCaption;
    }
}

function createCollapseButtons() {
    var tableIndex = 0;
    var navigationBoxes = new Object();
    var tables = getElementsByClassName(document, 'table', 'collapses');

    for (var i = 0; i < tables.length; i++) {
        var headerRow = tables[i].getElementsByTagName('tr')[0];
        if (!headerRow) {
            continue;
        }

        var header = headerRow.getElementsByTagName('th')[0];
        if (!header) {
            continue;
        }

        navigationBoxes[tableIndex] = tables[i];
        tables[i].setAttribute('id', 'collapsibleTable' + tableIndex);

        var spacer = document.createElement('div');
        var button = document.createElement('span');
        var buttonLink = document.createElement('a');
        var buttonText = document.createTextNode(collapseCaption);

        spacer.style.cssFloat = 'left';
        spacer.style.styleFloat = 'left';
        spacer.style.textAlign = 'left';
        spacer.style.width = '35px';
        spacer.appendChild(document.createTextNode('\u00a0'));
        header.insertBefore(spacer, header.childNodes[0]);

        button.style.cssFloat = 'right';
        button.style.styleFloat = 'right';
        button.style.fontSize = '11px';
        button.style.fontWeight = 'normal';
        button.style.textAlign = 'right';
        button.style.width = '35px';

        buttonLink.style.color = '#FFFFFF';
        buttonLink.id = 'collapseButton' + tableIndex;
        buttonLink.href = 'javascript:collapseTable(' + tableIndex + ');';
        buttonLink.appendChild(buttonText);

        button.appendChild(document.createTextNode('['));
        button.appendChild(buttonLink);
        button.appendChild(document.createTextNode(']'));
        header.insertBefore(button, header.childNodes[0]);

        tableIndex++;
    }

    for (var i = 0; i < tableIndex; i++) {
        if (hasClass(navigationBoxes[i], 'collapsed') || (tableIndex >= 2 && hasClass(navigationBoxes[i], 'autocollapse'))) {
            collapseTable(i);
        } 
        else if (hasClass(navigationBoxes[i], 'innercollapse')) {
            var element = navigationBoxes[i];
            while (element = element.parentNode) {
                if (hasClass(element, 'outercollapse')) {
                    collapseTable(i);
                    break;
                }
            }
        }
    }
}

addOnloadHook(createCollapseButtons);

/**
 * Code for sortable tables.
 */
var upArrow = 'images/5/5b/Sort_up.gif';
var downArrow = 'images/7/7a/Sort_down.gif';
var noneArrow = 'images/e/e8/Sort_none.gif';
var numberTransformTable = null;
var numberRegex = null;

function sortTableInit() {
    var idnum = 0;
    var tables = getElementsByClassName(document, 'table', 'sortable');

    for (var i = 0; i < tables.length; i++) {
        if (!tables[i].id) {
            tables[i].setAttribute('id', 'sortable_table_id_' + idnum);
            ++idnum;
        }
        makeSortable(tables[i]);
    }
}

function makeSortable(table) {
    var firstRow;

    if (table.rows && table.rows.length > 0) {
        if (table.tHead && table.tHead.rows.length > 0) {
            firstRow = table.tHead.rows[table.tHead.rows.length - 1];
        } else {
            firstRow = table.rows[0];
        }
    }

    if (!firstRow) {
        return;
    }

    for (var i = 0; i < firstRow.cells.length; i++) {
        var cell = firstRow.cells[i];

        if ((' ' + cell.className + ' ').indexOf(' unsortable ') == -1) {
            cell.innerHTML += '&nbsp;&nbsp;'
                + '<a href="#" class="sortheader" '
                + 'onclick="reSortTable(this);return false;">'
                + '<span class="sortarrow">'
                + '<img src="' + noneArrow + '" alt="&darr;"/></span></a>';
        }
    }
}

function ts_getInnerText(el) {
    return getInnerText(el);
}

function reSortTable(lnk) {
    var span = lnk.getElementsByTagName('span')[0];
    var td = lnk.parentNode;
    var tr = td.parentNode;
    var column = td.cellIndex;
    var table = tr.parentNode;

    while (table && !(table.tagName && table.tagName.toLowerCase() == 'table')) {
        table = table.parentNode;
    }

    if (!table) {
        return;
    }

    if (table.rows.length <= 1) {
        return;
    }

    if (numberTransformTable == null) {
        initTransformTable();
    }

    var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1);
    var itm = '';

    for (var i = rowStart; i < table.rows.length; i++) {
        if (table.rows[i].cells.length > column) {
            itm = ts_getInnerText(table.rows[i].cells[column]);
            itm = itm.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '');

            if (itm != '') {
                break;
            }
        }
    }

    var sortfn = sortGeneric;
    var preprocessor = ts_toLowerCase;

    if (/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test(itm)) {
        preprocessor = dateToSortKey;
    } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test(itm)) {
        preprocessor = dateToSortKey;
    } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d$/.test(itm)) {
        preprocessor = dateToSortKey;
    } else if (/(^[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test(itm)) {
        preprocessor = currencyToSortKey;
    } else if (ts_number_regex.test(itm)) {
        preprocessor = parseFloat;
    }

    var reverse = (span.getAttribute('sortdir') == 'down');
    var newRows = new Array();
    var staticRows = new Array();

    for (var j = rowStart; j < table.rows.length; j++) {
        var row = table.rows[j];

        if((' ' + row.className + ' ').indexOf(' unsortable ') < 0) {
            var keyText = ts_getInnerText(row.cells[column]);

            if(keyText == undefined) {
                keyText = ''; 
            }

            var oldIndex = (reverse ? -j : j);
            var preprocessed = preprocessor( keyText.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '') );

            newRows[newRows.length] = new Array(row, preprocessed, oldIndex);
        } else {
            staticRows[staticRows.length] = new Array(row, false, j - rowStart);
        }
    }

    newRows.sort(sortfn);

    var arrowHTML;

    if (reverse) {
        arrowHTML = '<img src="'+ downArrow + '" alt="&darr;"/>';
        newRows.reverse();
        span.setAttribute('sortdir', 'up');
    } else {
        arrowHTML = '<img src="'+ upArrow + '" alt="&uarr;"/>';
        span.setAttribute('sortdir', 'down');
    }

    for (var i = 0; i < staticRows.length; i++) {
        var row = staticRows[i];

        newRows.splice(row[2], 0, row);
    }

    for (var i = 0; i < newRows.length; i++) {
        if ((' ' + newRows[i][0].className + ' ').indexOf(' sortbottom ') == -1) {
            table.tBodies[0].appendChild(newRows[i][0]);
        }
    }

    for (var i = 0; i < newRows.length; i++) {
        if ((' ' + newRows[i][0].className + ' ').indexOf(' sortbottom ') != -1) {
            table.tBodies[0].appendChild(newRows[i][0]);
        }
    }

    var spans = getElementsByClassName(tr, 'span', 'sortarrow');

    for (var i = 0; i < spans.length; i++) {
        spans[i].innerHTML = '<img src="'+ noneArrow + '" alt="&darr;"/>';
    }

    span.innerHTML = arrowHTML;
}

function initTransformTable() {
    if (typeof wgSeparatorTransformTable == 'undefined' || (wgSeparatorTransformTable[0] == '' && wgDigitTransformTable[2] == '')) {
        digitClass = '[0-9,.]';
        numberTransformTable = false;
    } else {
        numberTransformTable = {};
        ascii = wgSeparatorTransformTable[0].split('\t');
        localised = wgSeparatorTransformTable[1].split('\t');

        for (var i = 0; i < ascii.length; i++) {
            numberTransformTable[localised[i]] = ascii[i];
        }

        ascii = wgDigitTransformTable[0].split('\t');
        localised = wgDigitTransformTable[1].split('\t');

        for ( var i = 0; i < ascii.length; i++ ) {
            numberTransformTable[localised[i]] = ascii[i];
        }

        digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
        maxDigitLength = 1;

        for (var digit in numberTransformTable ) {
            digits.push(
                digit.replace( /[\\\\$\*\+\?\.\(\)\|\{\}\[\]\-]/,
                function( s ) { return '\\' + s; } )
            );

            if (digit.length > maxDigitLength) {
                maxDigitLength = digit.length;
            }
        }

        if (maxDigitLength > 1) {
            digitClass = '[' + digits.join('', digits) + ']';
        } else {
            digitClass = '(' + digits.join('|', digits) + ')';
        }
    }

    numberRegex = new RegExp(
        '^(' +
        '[+-]?[0-9][0-9,]*(\\.[0-9,]*)?(E[+-]?[0-9][0-9,]*)?' +
        '|' +
        '[+-]?' + digitClass + '+%?' +
        ')$', 'i'
    );
}

function ts_toLowerCase(s) {
    return s.toLowerCase();
}

function dateToSortKey(date) {
    if (date.length == 11) {
        switch (date.substr(3, 3).toLowerCase()) {
            case 'jan': var month = '01'; break;
            case 'feb': var month = '02'; break;
            case 'mar': var month = '03'; break;
            case 'apr': var month = '04'; break;
            case 'may': var month = '05'; break;
            case 'jun': var month = '06'; break;
            case 'jul': var month = '07'; break;
            case 'aug': var month = '08'; break;
            case 'sep': var month = '09'; break;
            case 'oct': var month = '10'; break;
            case 'nov': var month = '11'; break;
            case 'dec': var month = '12'; break;
        }

        return date.substr(7, 4) + month + date.substr(0, 2);
    } else if (date.length == 10) {
        return date.substr(6, 4) + date.substr(0, 2) + date.substr(3, 2);
    } else if (date.length == 8) {
        year = date.substr(6, 2);

        if (parseInt(year) < 50) {
            yr = '20' + year;
        } else {
            yr = '19' + year;
        }

        return year + date.substr(0, 2) + date.substr(3, 2);
    }

    return '00000000';
}

function ts_parseFloat(s) {
    if (!s) {
        return 0;
    }

    if (numberTransformTable != false) {
        var newNum = '', c;

        for ( var p = 0; p < s.length; p++ ) {
            c = s.charAt( p );

            if (c in numberTransformTable) {
                newNum += numberTransformTable[c];
            } else {
                newNum += c;
            }
        }
        s = newNum;
    }

    num = parseFloat(s.replace(/,/g, ''));

    return (isNaN(num) ? 0 : num);
}

function currencyToSortKey(s) {
    return parseFloat(s.replace(/[^0-9.,]/g, ''));
}

function sortGeneric(a, b) {
    return a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2];
}

addOnloadHook(sortTablesInit);