function toggleRow(tr) {
    // Get the numeric part of the row id.
    var fixtureId = tr.id;
    var _position = fixtureId.indexOf("_") + 1;
    fixtureId = fixtureId.substr(_position, fixtureId.length - _position);

    // Get the expand/collapse image by id.
    var image = document.getElementById("ExpandCollapse_" + fixtureId);

    var helpTextRow = document.getElementById("HelpTextRow_" + fixtureId);

    // Expand or collapse the next row and switch the expand/collapse image.
    if (tr.className == "ToggleOff") {
        tr.className = "ToggleOn";
        image.src = "/images/collapse.gif";
        image.title = "Hide details";

        // Get the next row under this and expand it.
        var nextTr = getNextRow(tr);
        nextTr.style.display = '';
        var innerDiv = document.getElementById("InnerFixtureId_" + fixtureId);
        if (innerDiv != null) {
            var status = innerDiv.getAttribute("status");
            if (status == null || status == "0") {
                // get the match details
                TheFA.CountyFA.Web.UserControls.Interaction.GetCountyCupMatchDetailsDataHandler.GetCountyCupMatchDetails(fixtureId, SucceededFixtureDetailsCallback);
            }
        }
        if (helpTextRow != null) {
            helpTextRow.style.display = 'none';
        }        
    }
    else {
        tr.className = "ToggleOff";
        image.src = "/images/expand.gif";
        image.title = "Show details";

        // Get the next row under this and collapse it.
        var nextTr = getNextRow(tr);
        nextTr.style.display = 'none';
        if (helpTextRow != null) {
            helpTextRow.style.display = '';
        }        
    }
}

function toggleByeRow(tr) {
    // Get the numeric part of the row id.
    var byeIdRow = tr.id;
    var _position1 = byeIdRow.indexOf("_") + 1;
    var _position2 = byeIdRow.indexOf("_", _position1) + 1;
    var competitionId = byeIdRow.substr(_position1, _position2 - _position1 - 1);
    var tournamentId = byeIdRow.substr(_position2, byeIdRow.length - _position2);

    // Get the expand/collapse image by id.
    var image = document.getElementById("ExpandCollapse_" + competitionId + "_" + tournamentId);

    // Expand or collapse the next row and switch the expand/collapse image.
    if (tr.className == "ToggleOff") {
        tr.className = "ToggleOn";
        image.src = "/images/collapse.gif";
        image.title = "Hide bye details";

        // Get the next row under this and expand it.
        var nextTr = getNextRow(tr);
        nextTr.style.display = '';
        var innerDiv = document.getElementById("InnerCupId_" + competitionId + "_" + tournamentId);
        if (innerDiv != null) {
            var status = innerDiv.getAttribute("status");
            if (status == null || status == "0") {
                // get the match details
                TheFA.CountyFA.Web.UserControls.Interaction.GetCountyCupMatchDetailsDataHandler.GetCountyCupByeMatchDetails(competitionId, tournamentId, SucceededByeCallback);
            }
        }
    }
    else {
        tr.className = "ToggleOff";
        image.src = "/images/expand.gif";
        image.title = "Show bye details";

        // Get the next row under this and collapse it.
        var nextTr = getNextRow(tr);
        nextTr.style.display = 'none';
    }
}

// This is the callback function that
// processes the Web Service return value.
function SucceededFixtureDetailsCallback(result) {
    if (result.length > 0) {
        var fixtureIdStartPos = result.indexOf("fixtureId=");
        if (fixtureIdStartPos != -1) {
            fixtureIdStartPos = fixtureIdStartPos + 11;
            var fixtureIdEndPos = result.indexOf("\"", fixtureIdStartPos);
            if (fixtureIdEndPos > fixtureIdStartPos) {
                var fixtureId = result.substr(fixtureIdStartPos, fixtureIdEndPos - fixtureIdStartPos);
                var innerDiv = document.getElementById("InnerFixtureId_" + fixtureId);
                if (innerDiv != null) {
                    innerDiv.setAttribute("status", "1");
                    innerDiv.innerHTML = result;
                }
            }
        }
    }
}

// This is the callback function that
// processes the Web Service return value.
function SucceededByeCallback(result) {
    if (result.length > 0) {
        var compIdStartPos = result.indexOf("competitionId=");
        if (compIdStartPos != -1) {
            compIdStartPos = compIdStartPos + 15;
            var compIdEndPos = result.indexOf("\"", compIdStartPos);
            if (compIdEndPos > compIdStartPos) {
                var tournIdStartPos = result.indexOf("tournamentId=");
                if (tournIdStartPos != -1) {
                    tournIdStartPos = tournIdStartPos + 14;
                    var tournIdEndPos = result.indexOf("\"", tournIdStartPos);
                    if (tournIdEndPos > tournIdStartPos) {

                        var compId = result.substr(compIdStartPos, compIdEndPos - compIdStartPos);

                        var tournId = result.substr(tournIdStartPos, tournIdEndPos - tournIdStartPos);

                        var innerDiv = document.getElementById("InnerCupId_" + compId + "_" + tournId);
                        if (innerDiv != null) {
                            innerDiv.setAttribute("status", "1");
                            innerDiv.innerHTML = result;
                        }
                    }
                }
            }
        }
    }
}

function getNextRow(currentRow) {
    if (currentRow.parentElement) {
        var row = currentRow.parentElement.rows[currentRow.rowIndex + 1];
    }
    else {
        var row = currentRow.parentNode.rows[currentRow.rowIndex + 1];
    }
    return row;
}
