﻿// ~/Scripts/_WebService/UpcomingGames.js
// This file will perform all functions used to display the games in the dialog box
// on the main page of the website.
//===========================================================================
//===========================================================================
// This method accepts a result set and populates the control specified
// within the sControl variable.  sControl is set via the InitRequest method
function PopulateUpcomingGames(sXML) {
    var xmlObject = StringtoXML(sXML);
    var _XML = xmlObject.getElementsByTagName("Table");

    // Clear out the object before loading
    ClearRows("tblGames", "tbody");

    // Gets the table type content present in the Response XML and gets the data into a variable tbl
    var tbl = document.getElementById("tblGames").getElementsByTagName("tbody")[0];
    
    // Make sure that there are rows to add
    if (_XML.length > 0) {
        // Iterate through the table and add HTML Rows & contents into it.
        for (var i = 0; i < _XML.length; i++) {
            // Create the Row variables
            var _Row1 = document.createElement("TR");
            var _Row2 = document.createElement("TR");
            var _Row3 = document.createElement("TR");
            var rainedout = (getNodeText(_XML[i], "IsRainedOut") === 'true');
            
            // TEAM A
            var _TeamA = document.createElement("TD");
            _TeamA.setAttribute("class", "TodaysGameLeft");
            _TeamA.setAttribute("className", "TodaysGameLeft");
            _TeamA.innerHTML = getNodeText(_XML[i], "TeamA");

            // TEAM B
            var _TeamB = document.createElement("TD");
            _TeamB.setAttribute("class", "TodaysGameLeft");
            _TeamB.setAttribute("className", "TodaysGameLeft");
            _TeamB.innerHTML = getNodeText(_XML[i], "TeamB");

            // GAME TIME
            var _GameTime = document.createElement("TD");
            _GameTime.setAttribute("class", "TodaysGameRight");
            _GameTime.setAttribute("className", "TodaysGameRight");
            _GameTime.innerHTML = getNodeText(_XML[i], "GameTime");

            // RAINED OUT
            var _RainOut = document.createElement("TD");
            _RainOut.setAttribute("class", "TodaysGameRight");
            _RainOut.setAttribute("className", "TodaysGameRight");
            _RainOut.setAttribute("className", "TodaysGameRight");
            _RainOut.setAttribute("rowspan", "2");
            _RainOut.innerHTML = "Rained Out";
            
            // DIAMOND
            var _Diamond = document.createElement("TD");
            _Diamond.setAttribute("class", "TodaysGameRight");
            _Diamond.setAttribute("className", "TodaysGameRight");
            _Diamond.innerHTML = getNodeText(_XML[i], "Diamond");

            // SEPARATOR
            var _Separator = document.createElement("TD");
            _Separator.setAttribute("class", "TodaysGameSeparator");
            _Separator.setAttribute("className", "TodaysGameSeparator");
            _Separator.setAttribute("colSpan", "2");
            _Separator.setAttribute("colspan", "2");
            _Separator.innerHTML = "<HR />";

            // Append cells to Rows
            // Add the teams to their respective Rows
            _Row1.appendChild(_TeamA);
            _Row2.appendChild(_TeamB);

            // If rained out, display the rained out msg
            // otherwise display the diamond and game time
            if (rainedout == true) {
                _Row1.appendChild(_RainOut);
            }
            else {
                _Row1.appendChild(_GameTime);
                _Row2.appendChild(_Diamond);
            }

            // Add the separator
            _Row3.appendChild(_Separator);

            // Add rows to table
            if (i > 0)
                tbl.appendChild(_Row3);

            tbl.appendChild(_Row1);
            tbl.appendChild(_Row2);
        }
    }
    else {
        // Seeing as there are no games to display, Report this to the users
        // Create the Row / Cell variables
        var _Row1 = document.createElement("TR");
        var _NoGames = document.createElement("TD");
        _NoGames.innerHTML = "No Games to Display";
        
        // Add the row to the table
        _Row1.appendChild(_NoGames);
        tbl.appendChild(_Row1);
    }
}
