// UKTTTables.js

function orderBy(whatToOrderBy) {
    var raceByAge = document.getElementById("raceByAge");
    var raceByTime = document.getElementById("raceByTime");

    if (whatToOrderBy == "age")
    {
        raceByTime.style.display = "none";
        raceByAge.style.display = "block";
    }
    else
    {
        raceByTime.style.display = "block";
        raceByAge.style.display = "none";
    }
}


function stripHTML(oldString) {

   var newString = oldString.replace( /<.+?>/, "" ) ;

   return newString;
}

function getInnerText(element)
{
    var innerText = "";
    if (element.text != undefined)
    {
        innerText = element.text;
    }
    else  if (element.innerText != undefined)
    {
        innerText = element.innerText;
    }
    for (var i=0; i<element.childNodes.length; i++)
    {
        innerText += getInnerText(element.childNodes[i]);
    }
    return innerText;
}

 
// uses filter code based on www.ukresults.net
function filter (phrase, _id, colsToFilter)
{
    var visibleRowCount = 0;
    
    var search = phrase.value.toLowerCase();
    if (search == "<type filter here>")
    {
        search = "";
    }
    
    var table = document.getElementById(_id);
    if (table == null || table == undefined)
    {
        return visibleRowCount;
    }
    
    if (table.rows == undefined)
    {
        return visibleRowCount;
    }
    if (table.rows == null)
    {
        return visibleRowCount;
    }
    
    var firstTime = true;

    for (var r = 1; r < table.rows.length; r++)
    {
        var showCurrentRow = false;
        var currentRow = table.rows[r];
        
        for (var c = 0; c < colsToFilter.length; c++)
        {
             var elemnt = stripHTML(currentRow.cells[colsToFilter[c]].innerHTML);

             if (elemnt.toLowerCase().indexOf(search)>=0 )
             {
                showCurrentRow = true;
                break;
             }
         }
         
         if (showCurrentRow)
         {
             currentRow.style.display = ''; 
             visibleRowCount++;
         }
         else
         {
             currentRow.style.display = 'none'; 
         }
    }
    return visibleRowCount;
}

function shouldFilterByName()
{
    var applyFilter = document.getElementById("applyFilterName");
    
    if (applyFilter.checked)
    {
        return true;    
    }
    
    return false;
}

function getColsToFilterForTime()
{
    var colsToFilter = new Array();
    if (shouldFilterByName())
    {
        colsToFilter.push(1)
    }
    else
    {
        colsToFilter.push(6)
    }    
    
    return colsToFilter;
}

function updateFilter()
{
    var theFilter = document.getElementById("theFilter")
    if (theFilter == null)
    {
        alert("Filter not found!");
    }
    
    var visibleRowCount=0;

    visibleRowCount += filter(theFilter, 'eventByTimeTable', getColsToFilterForTime());
    
    if (visibleRowCount == 0)
    {
        // no need to display this any more...
        //alert("No athletes match the current filter criteria");
    }
}

function theFilter_onFocus()
{
    var theFilter = document.getElementById("theFilter")
    if (theFilter.value == "<type filter here>")
    {
        theFilter.value = "";
        theFilter.style.color = "WindowText";
    }
}

