function UpdateCalendarBoxByGender( gender, linkId )
{
    SetLargeAjaxAnimation('calendarconsole');
    req = GetReq();
    
    var calendarRegion = document.getElementById( 'CalendarRegion' ).value;
    
    var url = '/ajax/ajax_calendarbox.php?region=' + calendarRegion + '&gender=' + gender + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulateCalendarBox(req.responseText); } } }
    req.open("GET", url, true);
    req.send(null);

    document.getElementById( 'Calendar_Men' ).style.color = "#000000";
    document.getElementById( 'Calendar_Ladies' ).style.color = "#000000";
    document.getElementById( linkId ).style.color = "#FFFFFF";
    
    document.getElementById( 'CalendarGender' ).value = gender;
}

function UpdateCalendarBoxByRegion( calendarRegion, linkId, isChainLoad )
{
    SetLargeAjaxAnimation('calendarconsole');
    req = GetReq();
    
    var gender = document.getElementById( 'CalendarGender' ).value;
    
    var url = '/ajax/ajax_calendarbox.php?region=' + calendarRegion + '&gender=' + gender + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulateCalendarBox(req.responseText, isChainLoad); } } }
    req.open("GET", url, true);
    req.send(null);

    document.getElementById( 'Calendar_North' ).style.color = "#000000";
    document.getElementById( 'Calendar_East' ).style.color = "#000000";
    document.getElementById( 'Calendar_SouthEast' ).style.color = "#000000";
    document.getElementById( 'Calendar_SouthWest' ).style.color = "#000000";
    document.getElementById( 'Calendar_West' ).style.color = "#000000";
    document.getElementById( 'Calendar_NorthWest' ).style.color = "#000000";
    document.getElementById( linkId ).style.color = "#FFFFFF";
    
    document.getElementById( 'CalendarRegion' ).value = calendarRegion;
}

function PopulateCalendarBox(json, isChainLoad)
{
    var records = eval('(' + json + ')');
    count = 0;
    var s = "";
    s += "<table style=\"margin-left:7px;margin-top:5px;width:250px\">";
    
    s += "<tr><td class=\"th2\">Date</td><td class=\"th2\">Daytime</td><td class=\"th2\">Evening</td></tr>";
    
    if (records.calendarplayer.length > 0)
    {
        for (i=0; i<records.calendarplayer.length; i++)
        {
            s += "<tr>";
            s += "<td nowrap>" + records.calendarplayer[i].calendardate + "</td>";
            s += "<td nowrap>" + records.calendarplayer[i].daytimeplayer + "</td>";
            s += "<td nowrap>" + records.calendarplayer[i].eveningplayer + "</td>";
            s += "</tr>";
            count ++;
        }
    }
    s += "</table>";
    document.getElementById( 'calendarconsole' ).innerHTML = s;
    if (isChainLoad == 1)
    {
        //UpdateCalendarBoxByRegion('0', 'Matches_All', 1);
    }    
}

function UpdateMatchResultsBoxByGender( gender, linkId )
{
    SetLargeAjaxAnimation('matchresultsconsole');
    req = GetReq();
    
    var matchType = document.getElementById( 'MatchType' ).value;
    
    var url = '/ajax/ajax_matches.php?type=' + matchType + '&gender=' + gender + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulateMatchResultsBox(req.responseText); } } }
    req.open("GET", url, true);
    req.send(null);

    document.getElementById( 'Matches_MenWomen' ).style.color = "#000000";
    document.getElementById( 'Matches_Men' ).style.color = "#000000";
    document.getElementById( 'Matches_Ladies' ).style.color = "#000000";
    document.getElementById( linkId ).style.color = "#FFFFFF";
    
    document.getElementById( 'MatchGender' ).value = gender;
}

function UpdateMatchResultsBoxByType( matchType, linkId, isChainLoad )
{
    SetLargeAjaxAnimation('matchresultsconsole');

    req = GetReq();
    
    var gender = document.getElementById( 'MatchGender' ).value;
    
    var url = '/ajax/ajax_matches.php?type=' + matchType + '&gender=' + gender + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulateMatchResultsBox(req.responseText, isChainLoad); } } }
    req.open("GET", url, true);
    req.send(null);

    document.getElementById( 'Matches_All' ).style.color = "#000000";
    document.getElementById( 'Matches_Friendly' ).style.color = "#000000";
    document.getElementById( 'Matches_League' ).style.color = "#000000";
    document.getElementById( 'Matches_Knockout' ).style.color = "#000000";
    document.getElementById( linkId ).style.color = "#FFFFFF";
    
    document.getElementById( 'MatchType' ).value = matchType;
}

function PopulateMatchResultsBox(json, isChainLoad)
{
    var records = eval('(' + json + ')');
    count = 0;
    var s = "";
    s += "<table style=\"margin-left:7px;margin-top:5px;width:510px\">";
    
    s += "<tr><td class=\"th2\">Date</td><td class=\"th2\">Player 1</td><td class=\"th2\">Player 2</td><td class=\"th2\">Result</td><td class=\"th2\">Venue</td></tr>";
    
    if (records.tennismatch.length > 0)
    {
        for (i=0; i<records.tennismatch.length; i++)
        {
            s += "<tr>";
            s += "<td nowrap>" + records.tennismatch[i].matchdate + "</td>";
            s += "<td nowrap>" + records.tennismatch[i].player1link + "</td>";
            s += "<td nowrap>" + records.tennismatch[i].player2link + "</td>";
            s += "<td nowrap>" + records.tennismatch[i].score + "</td>";
            //s += "<td nowrap>" + records.tennismatch[i].matchlink + "</td>";
            s += "<td nowrap>" + records.tennismatch[i].venuelink + "</td>";
            s += "</tr>";
            count ++;
        }
    }
    s += "</table>";
    document.getElementById( 'matchresultsconsole' ).innerHTML = s;
    if (isChainLoad == 1)
    {
        UpdateCalendarBoxByRegion('SW', 'Calendar_SouthWest', 1);
    }    
}

function UpdatePlayerBoxByGender( gender, linkId )
{
    SetLargeAjaxAnimation('playertableconsole');

    req = GetReq();
    
    var direction = document.getElementById( 'PlayerSortDirection' ).value;
    var playerSortColumn = document.getElementById( 'PlayerSortColumn' ).value;
    
    var url = '/ajax/ajax_players.php?dir=' + direction + '&playersortcol=' + playerSortColumn + '&gender=' + gender + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulatePlayerBox(req.responseText); } } }
    
    req.open("GET", url, true);
    
    req.send(null);

    document.getElementById( 'Players_Men' ).style.color = "#000000";
    document.getElementById( 'Players_Ladies' ).style.color = "#000000";
    document.getElementById( linkId ).style.color = "#FFFFFF";
    
    document.getElementById( 'PlayerGender' ).value = gender;
}

function UpdatePlayerBoxBySortColumn( playerSortColumn, direction, linkId, isChainLoad )
{
    SetLargeAjaxAnimation('playertableconsole');
    req = GetReq();

    var gender = document.getElementById( 'PlayerGender' ).value;
    
    var url = '/ajax/ajax_players.php?gender=' + gender + '&dir=' + direction + '&playersortcol=' + playerSortColumn + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulatePlayerBox(req.responseText, isChainLoad); } } }
    
    req.open("GET", url, true);
    
    req.send(null);

    document.getElementById( 'Players_Rank' ).style.color = "#000000";
    document.getElementById( 'Players_Active' ).style.color = "#000000";
    document.getElementById( 'Players_Recent' ).style.color = "#000000";
    document.getElementById( 'Players_Joined' ).style.color = "#000000";
    document.getElementById( linkId ).style.color = "#FFFFFF";
    
    document.getElementById( 'PlayerSortDirection' ).value = direction;
    document.getElementById( 'PlayerSortColumn' ).value = playerSortColumn;
    
}

function PopulatePlayerBox(json, isChainLoad)
{
    var records = eval('(' + json + ')');
    count = 0;
    var s = "";
    s += "<table style=\"margin-left:7px;margin-top:5px;width:250px\">";
    
    if (records.sortorder == "createddate")
    {
        s += "<tr><td class=\"th2\">Name</td><td class=\"th2\">Pld</td><td class=\"th2\">Rank</td><td class=\"th2\">Joined</td></tr>";
    }
    else
    {
        s += "<tr><td class=\"th2\">Name</td><td class=\"th2\">Pld</td><td class=\"th2\">Rank</td><td class=\"th2\">Last Match</td></tr>";
    }
    
    if (records.user.length > 0)
    {
        for (i=0; i<records.user.length; i++)
        {
            s += "<tr>";
            s += "<td>" + records.user[i].userlink + "</td>";
            s += "<td>" + records.user[i].matchesplayed + "</td>";
            s += "<td>" + records.user[i].siterank + "</td>";
            if (records.sortorder == "createddate")
            {
                s += "<td>" + records.user[i].createddate + "</td>";
            }
            else
            {
                s += "<td>" + records.user[i].lastplayed + "</td>";
            }
            s += "</tr>";
            count ++;
        }
    }
    s += "</table>";
    document.getElementById( 'playertableconsole' ).innerHTML = s;
    if (isChainLoad == 1)
    {
        UpdateMatchResultsBoxByType('0', 'Matches_All', 1);
    }
}

function PopulateRecipients( )
{
    req = GetReq();

    var sendToAllLeaguePlayers = document.getElementById( 'sendtoallleagueplayers' ).checked ? 'Y' : 'N';
    var sendToAllLeaguePlayersPrevious = document.getElementById( 'sendtoallleagueplayersprevious' ).checked ? 'Y' : 'N';
    var koCode = 0;
    
    if (document.getElementById( 'sendtoallwinterknockoutplayers' ).checked)
    {
        koCode += 1;
    }
    if (document.getElementById( 'sendtoallsummerknockoutplayers' ).checked)
    {
        koCode += 2;
    }

    var url = '/ajax/ajax_messages.php?stalp_previous=' + sendToAllLeaguePlayersPrevious + '&stalp=' + sendToAllLeaguePlayers + '&kocode=' + koCode + '&r=' + Math.random();

    req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { PopulateMessageRecipients(req.responseText); } } }
    
    req.open("GET", url, true);
    
    req.send(null);
    
}

function PopulateMessageRecipients(json)
{
    var records = eval('(' + json + ')');
    count = 0;
    var s = "";
    if (records.user.length > 0)
    {
        for (i=0; i<records.user.length; i++)
        {
            if (count > 0)
            {
                s += ", ";
            }
            s += records.user[i].name;
            count ++;
        }
    }
    document.getElementById( 'recipientnamelist' ).value = s;
}

function SaveState(elementDate)
{
    req = GetReq();

    var m = document.getElementById( 'morning_' + elementDate ).checked ? 'Y' : 'N';
    var a = document.getElementById( 'afternoon_' + elementDate ).checked ? 'Y' : 'N';
    var e = document.getElementById( 'evening_' + elementDate ).checked ? 'Y' : 'N';
    var type = document.getElementById( 'type_' + elementDate ).value;
    
    var url = '/ajax/ajax_calendar.php?id=' + elementDate + '&m=' + m + '&a=' + a + '&e=' + e + '&t=' + type + '&r=' + Math.random();
    
    //document.getElementById( 'calendarmatches_' + elementDate ).innerHTML = url;;
    
    req.onreadystatechange = function() {
          if (req.readyState == 4) { 
            if (req.status == 200) { 
                if (m == 'N' && a == 'N' && e == 'N')
                {
                    nonefoundtext = "";
                }
                else
                {
                    nonefoundtext = "No players found yet...";
                }
                PopulateMatchedPlayersArea(req.responseText, elementDate, nonefoundtext, 10);
            } 
        } 
    }
    
    req.open("GET", url, true);
    
    req.send(null);
    
    //document.getElementById( 'calendarmatches_' + elementDate ).innerHTML = url;
}

function PopulateMatchedPlayersArea(json, elementDate, nonefoundtext, max)
{
    var records = eval('(' + json + ')');
    //alert(req.responseText);
    //alert(records.matchedplayers.length);
    count = 0;
    var s = "";
    if (records.matchedplayers.length == 0)
    {
        s = nonefoundtext;
    }
    else
    {
        for (i=0; i<records.matchedplayers.length; i++)
        {
            if (count > 0)
            {
                s += ", ";
            }
            //document.getElementById( 'calendarmatches_' + elementDate ).innerHTML = req.responseText;
            s += records.matchedplayers[i].playerlink;
            count ++;
            if (count == max && records.matchedplayers.length > max)
            {
                s += ', <a target="_blank" href="/calendar/viewallcalendarmatches.php?md=' + elementDate + '">More...</a>';
                break;
            }
        }
    }
    document.getElementById( 'calendarmatches_' + elementDate ).innerHTML = s;
    //alert(req.responseText);
    //var a = req.responseXML.getElementsByTagName("junk")[0];
}

function GetReq()
{
    try{
        // Opera 8.0+, Firefox, Safari
        req = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Unknown error - please contact us via site feedback.");
                return false;
            }
        }
    }
    
    return req;
}

function InitialiseHomePageBoxes()
{
    document.getElementById('Players_Men').style.color = "#FFFFFF";
    document.getElementById('Matches_MenWomen').style.color = "#FFFFFF";
    document.getElementById('Calendar_Men').style.color = "#FFFFFF";
    document.getElementById('Courts_All').style.color = "#FFFFFF";
    UpdatePlayerBoxBySortColumn('siterank', 'A', 'Players_Rank', 1);
}

function SetLargeAjaxAnimation(divId)
{
    var s = "";
    s += "<div style=\"width:100%;margin-top:30px;text-align:center\"><img style=\"text-align:center; vertical-align:middle;\" src=\"/img/ajax/ajax-bar.gif\"></div>";
    //document.getElementById( divId ).backgroundImage = '/img/ajax/ajax-bar.gif';
    document.getElementById( divId ).innerHTML = s;
}

