/******************************************************************* 
 *  File:     Scores.js
 *  Created:  2003/10/03
 *  Author:   Brian Kilgore  (disckilgore@hotmail.com) 
 *  Purpose:  To create a more dynamic resutls page
 *  History
 *  Date         Version        Description 
 *  2003-10-07   1.0            First version
 *
 *  © Brian Kilgore 2003
 *  Do not use without permission.
 ***********************************************************************/

 var  FftSortCol = 0;
 var  FftScores = 0;
 
// Draw table from 'Scores' array of objects
function  FftDrawTable ( bodyname, scoresname )
{
    var  tr;
    var  td;
    var  cols;
    var  divisions;
    var  players;
    var  i;
    var  j;
    var  k;

    Scores = eval ( scoresname );
    tbody = document.getElementById ( bodyname );

    // remove existing rows, if any
    FftClearTable ( tbody );

    cols = Scores [ 0 ].length;
    divisions = Scores [ 1 ].length;

    //  Go through all divisions
    for ( i = 0; i < divisions; i++ )
    {
        //  Insert the division name.
        tr = tbody.insertRow ( tbody.rows.length );
        tr.className = "entrydivision";
        td = tr.insertCell ( tr.cells.length );
        td.colSpan = 100;
        td.innerHTML = "<BR>" + Scores [ 1 ][ i ];

        //  Insert the column headers.
        tr = tbody.insertRow ( tbody.rows.length );
        tr.className = "entrytitlerow";
        for ( k = 0; k < cols; k++ )
        {
            td = tr.insertCell ( tr.cells.length );
            td.className = "entrycell";
            if ( ( Scores [ 0 ][ k ] == "Name" ) || ( Scores [ 0 ][ k ] == "Tournament" ) || ( Scores [ 0 ][ k ] == "Division" ) ) 
            {
                //  Align the names to the left.
                td.setAttribute ( "align", "left" );
            }
            td.innerHTML = "<A style='cursor: hand;' onclick='FftSortTable ( \"" + bodyname + "\", \"" + scoresname + "\", " + k + " )'>" + Scores [ 0 ][ k ] + "</A>";
        }

        //  Insert the table data.
        for ( j = 0; j < Scores [ 2 + i ].length; j++ )
        {
            tr = tbody.insertRow ( tbody.rows.length );
            //  Use different styles for odd and even rows.
            if ( j & 1 )
            {
                tr.className = "entryoddrow";
            }
            else
            {
                tr.className = "entryevenrow" ;
            }
            for ( k = 0; k < cols; k++ )
            {
                td = tr.insertCell ( tr.cells.length );
                td.className = "entrycell";
                if ( ( Scores [ 0 ][ k ] == "Name" ) || ( Scores [ 0 ][ k ] == "Tournament" ) || ( Scores [ 0 ][ k ] == "Division" ) ) 
                {
                    //  Align the names to the left.
                    td.setAttribute ( "align", "left" );
                }
                td.innerHTML = Scores [ 2 + i ][ j ] [ k ];
            }
        }
    }
}


// Remove existing table rows
function  FftClearTable ( tbody )
{
    while ( tbody.rows.length > 0 )
    {
        tbody.deleteRow ( 0 );
    }
}


// Sorting functions
function  FftSortTable ( bodyname, scoresname, sortcol )
{
    var  cols;
    var  divisions;
    var  i;

    Scores = eval ( scoresname );

    cols = Scores [ 0 ].length;
    divisions = Scores [ 1 ].length;

    if ( sortcol >= cols )
    {
        FftSortCol = cols - 1;
    }
    else
    {
        FftSortCol = sortcol;
    }

    FftScores = Scores;

    for ( i = 0; i < divisions; i++ )
    {
        Scores [ 2 + i ].sort ( FftGetCol );
    }

    FftDrawTable ( bodyname, scoresname )
    return ( false );
}


// Compare function.
function FftGetCol ( a, b )
{
    switch ( FftScores [ 0 ][ FftSortCol ] )
    {
        case  "Place":
            return ( FftGetPlace ( a, b ) );
            break;

        case  "Name":
            return ( FftGetName ( a, b ) );
            break;

        case  "PDGA #":
            return ( FftGetPdga ( a, b ) );
            break;

        case  "Prize":
            return ( FftGetPrize ( a, b ) );
            break;

        case  "FFT Points":
            return ( FftGetPoints ( a, b ) );
            break;

        default:
            return ( FftGetRound ( a, b ) );
            break;
    }
}


function FftGetPlace ( a, b )
{
    var  a1 = a [ FftSortCol ];
    var  b1 = b [ FftSortCol ];

    //  Remove 'T' for ties before sorting.
    if ( a1.substring ( 0, 1 ) == "T" )
    {
        a1 = a1.substr ( 1 );
    }

    //  Remove 'T' for ties before sorting.
    if ( b1.substring ( 0, 1 ) == "T" )
    {
        b1 = b1.substr ( 1 );
    }

    //  If the place is the same go to last column to decide.
    if ( a1 == b1 )
    {
        oldcol = FftSortCol;
        FftSortCol = FftScores [ 0 ].length;
        a1 = FftGetCol ( a, b );
        FftSortCol = oldcol;
        return ( a1 );
    }
    else
    {
        return ( a1 - b1 );
    }
}


function FftGetName ( a, b )
{
    var  a1 = a [ FftSortCol ];
    var  b1 = b [ FftSortCol ];

    if ( a1 < b1 )
    {
        return ( -1 );
    }
    else
    {
        if ( a1 > b1 )
        {
            return ( 1 );
        }
        else
        {
            return ( 0 );
        }
    }
}


function FftGetPdga ( a, b )
{
    var  a1 = a [ FftSortCol ];
    var  b1 = b [ FftSortCol ];
    var  oldcol;

    //  Sort blank PDGA #s last.
    if ( a1 == "" )
    {
        a1 = 1000000;
    }

    //  Sort blank PDGA #s last.
    if ( b1 == "" )
    {
        b1 = 1000000;
    }

    //  If both are blank, then sort by previous column (should be name).
    if ( ( a1 == 1000000 ) &&
         ( b1 == 1000000 ) )
    {
        oldcol = FftSortCol;
        FftSortCol -= 1;
        a1 += FftGetCol ( a, b );
        FftSortCol = oldcol;
    }

    return ( a1 - b1 );
}


function FftGetPrize ( a, b )
{
    var  a1 = a [ FftSortCol ];
    var  b1 = b [ FftSortCol ];
    var  oldcol;

    //  If prize is blank for both sort by previous column (should be total score).
    if ( ( a1 == "" ) &&
         ( b1 == "" ) )
    {
        oldcol = FftSortCol;
        FftSortCol -= 1;
        a1 = FftGetCol ( a, b );
        FftSortCol = oldcol;
        return ( a1 );
    }
    else
    {
        return ( b1 - a1 );
    }
}


function FftGetPoints ( a, b )
{
    var  a1 = a [ FftSortCol ];
    var  b1 = b [ FftSortCol ];
    var  oldcol;

    //  If the points is the same go to previous column to decide.
    if ( a1 == b1 )
    {
        oldcol = FftSortCol;
        FftSortCol -= 1;
        a1 = FftGetCol ( a, b );
        FftSortCol = oldcol;
        return ( a1 );
    }
    else
    {
        return ( b1 - a1 );
    }
}


function FftGetRound ( a, b )
{
    var  a1 = a [ FftSortCol ];
    var  b1 = b [ FftSortCol ];
    var  oldcol;

    //  Put DNF and DNP to high score to sort to bottom.
    if ( ( a1 == "DNF" ) ||
         ( a1 == "DNP" ) ||
         ( a1 == "" ) )
    {
        a1 = 32767;
    }
    if ( a1 == "Trophy" )
    {
        a1 = 1;
    }

    //  Put DNF and DNP to high score to sort to bottom.
    if ( ( b1 == "DNF" ) ||
         ( b1 == "DNP" ) ||
         ( b1 == "" ) )
    {
        b1 = 32767;
    }
    if ( b1 == "Trophy" )
    {
        b1 = 1;
    }

    //  If previous column is not the first score column...
    if ( FftScores [ 0 ][ FftSortCol ] != "R1" )
    {
        //  If the previous column is blank then add a large amount to sort the current score to the bottom (handle final 9 like cases).
        if ( a [ FftSortCol - 1 ] == "" )
        {
            a1 = eval ( a1 ) + 1000;
        }
    }

    //  If previous column is not the first score column...
    if ( FftScores [ 0 ][ FftSortCol ] != "R1" )
    {
        //  If the previous column is blank then add a large amount to sort the current score to the bottom (handle final 9 like cases).
        if ( b [ FftSortCol - 1 ] == "" )
        {
            b1 = eval ( b1 ) + 1000;
        }
    }

    //  If the score is the same go to previous column to decide.
    if ( a1 == b1 )
    {
        oldcol = FftSortCol;
        FftSortCol -= 1;
        a1 = FftGetCol ( a, b );
        FftSortCol = oldcol;
        return ( a1 );
    }
    else
    {
        return ( a1 - b1 );
    }
}


