// buildScoreboards.js
// ===================

function buildGameAnchorCells(numGames)
{
    var mainAnchorElement;
    var anchorTable;
    var numFullRows;
    var numGamesOnPartialRow;
    var trRow;
    var tdCell;
    var tBody;
    var cc;

    // first, store currentCalendar (from HTML) in a variable, for reuse.
    if (currentCalendar == null)
        currentCalendar = (document.getElementById("myGamesCalendar")).cloneNode(true);
    cc = currentCalendar.cloneNode(true);

    mainAnchorElement = document.getElementById("scoreTable");
    mainAnchorElement.innerHTML = "";
    mainAnchorElement.appendChild(cc);

    ////log.write("PROCESSING STEP: buildGameAnchorCells(numGames).... numGames parameter = " + numGames);

    if (numGames <= 0) 
    {
        ////log.write("Error: buildAnchorTables() function... invalid paramater value passed");
        return;
    }

    numFullRows = getNumFullRows(numGames);
    ////log.write("getNumFullRows() function returned " + numFullRows);
    
    anchorTable = document.createElement("table");
    anchorTable.setAttribute("border","0");
    anchorTable.setAttribute("cellSpacing","4");
    anchorTable.className = "cnnncaaScore";

    tBody = document.createElement("tbody");
    
    for (var i = 0; i < numFullRows; i++)
    {
        trRow = document.createElement('tr');
    
        tdGameAnchor = document.createElement('td');
        tdGameAnchor.id = (i * 3) + 1;
        tdGameAnchor.setAttribute("vAlign","top");
        trRow.appendChild(tdGameAnchor);
        tdGameAnchor = document.createElement('td');
        tdGameAnchor.setAttribute("vAlign","top");
        tdGameAnchor.id = (i * 3) + 2;
        trRow.appendChild(tdGameAnchor);
        tdGameAnchor = document.createElement('td');
        tdGameAnchor.setAttribute("vAlign","top");
        tdGameAnchor.id = (i * 3) + 3;
        trRow.appendChild(tdGameAnchor);

        tBody.appendChild(trRow);
    }

    numGamesOnPartialRow = getNumGamesOnPartialRow(numGames);

    if (numGamesOnPartialRow > 0)
    {
        trRow = document.createElement('tr');

        switch(numGamesOnPartialRow)
        {
            case 1:
                tdGameAnchor = document.createElement('td');
        tdGameAnchor.setAttribute("vAlign","top");
                tdGameAnchor.id = (numFullRows * 3) + 1;
                trRow.appendChild(tdGameAnchor);
                break;
            case 2:
                tdGameAnchor = document.createElement('td');
        tdGameAnchor.setAttribute("vAlign","top");
                tdGameAnchor.id = (numFullRows * 3) + 1;
                trRow.appendChild(tdGameAnchor);
                tdGameAnchor = document.createElement('td');
                tdGameAnchor.id = (numFullRows * 3) + 2;
                trRow.appendChild(tdGameAnchor);
                break;
            case 3:
            default:
                ;
        }
        tBody.appendChild(trRow);
    }
    anchorTable.appendChild(tBody);


    if (mainAnchorElement != null)
        mainAnchorElement.appendChild(anchorTable);
    else {}
        //alert("Error: the 'gamesTable' DOM Id was not found on the page");
}

function buildViewCastGameAnchorCells(numGames)
{
    var mainAnchorElement;
    var anchorTable;
    var trRow;
    var tdCell;
    var tBody;

    mainAnchorElement = document.getElementById("topGames");
    //log.write("Found game placeholder: " + mainAnchorElement);
    mainAnchorElement.innerHTML = "";
    
    var scoreTitleDiv = document.getElementById("cnnLtTitle");
    scoreTitleDiv.innerHTML = "";
    scoreTitleDiv.innerHTML = VIEWCAST_NAME.toUpperCase() + " SCORES";    
    
    var dropdownDiv = document.getElementById("cnnDpDwn");
    dropdownDiv.innerHTML = "";

    // sample PATH: /football/ncaa/scoreboards/2007/top25/regularseason/1/
    var pathSplit = PATH.split("/");

    var urlArr = ["top25","acc","big12","bige","bsky","bsou","big10","caa","cusa","ia","iaa","gate","gwest",
                  "ivy","maac","midam","meac","mwest","nec","ovc","pac10","patr","pio","sec","south","sland",
                  "swac","sbelt","wac"];
    var nameArr = ["Top 25","ACC","Big 12","Big East","Big Sky","Big South","Big Ten","CAA",
                   "C-USA","I-A Ind.","I-AA Ind.","Gateway","Great West","Ivy","MAAC","MAC","MEAC","MWC","Northeast",
				   "Ohio Valley","Pac-10","Patriot","Pioneer","SEC","Southern","Southland","SWAC","Sun Belt","WAC"];

    var dropdownHTML = "";

    dropdownHTML = dropdownHTML +
    "<select name=\"conferences\" onChange=\"reloadViewCast(this.options[this.selectedIndex].value);\">" +
    "<option value=\"\" selected=\"1\">All Scoreboards</option>";

    for (var i = 0; i < urlArr.length; i++)
    {
        dropdownHTML = dropdownHTML +
        "<option value=\"/" + pathSplit[1] + "/" + pathSplit[2] +
          "/" + pathSplit[3] + "/" + pathSplit[4] + "/" + urlArr[i] + "/" + pathSplit[6] +
          "/" + pathSplit[7] + "/" + "|" + nameArr[i] + "\">" + nameArr[i] + "</option>";
    }
    dropdownHTML = dropdownHTML + "</select>";

    //log.write("Dropdown HTML: " + dropdownHTML);
    
    dropdownDiv.innerHTML = dropdownHTML;
    
    var colgroup = document.createElement("colgroup");
    var col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    col = document.createElement("col");
    colgroup.appendChild(col);

    anchorTable = document.createElement("table");
    anchorTable.id = "cnnScoreboard";

    var tbody = document.createElement("tbody");
    tbody.appendChild(colgroup);

    var rowCount = 1;
    var cellCount = 1;

    if (numGames <= 0)
    {
        ////log.write("Error: buildAnchorTables() function... invalid paramater value passed");
        return;
    }

    var tr = null;
    var td = null;

    for (var count = 0; count < numGames; count++)
    {
        if (cellCount == 1)
            tr = document.createElement("tr");

        td = document.createElement("td");
        td.id = count+1;
        td.setAttribute("onmouseover","this.bgColor='#eeeeee'");
        td.setAttribute("onmouseout","this.bgColor='#ffffff'");

        //log.write("Creating table cell: " + count);

        if (cellCount == 8 || count == (numGames - 1))
        {
           if (rowCount == 1 && count == (numGames - 1))
           {
               td.className = "cnnTbBotDwnLast";
               tr.appendChild(td);

               for (j = 0; j < (8 - cellCount); j++)
               {
                   td = document.createElement("td");
                   td.setAttribute("width","117");
                   td.appendChild(document.createTextNode(" "));
                   tr.appendChild(td);
               }
           }
           else if (rowCount == 1 && (numGames < 16 && numGames > 8))
           {
               td.className = "cnnTbBotDwnLast";
               tr.appendChild(td);
           }
           else if (rowCount == 1)
           {
               td.className = "cnnJustCCCBot";
               tr.appendChild(td);
           }
           else
           {
               td.className = "cnnJustCCCTop";
               tr.appendChild(td);
           }


           // wrap up the row
           tbody.appendChild(tr);

           // reset the count
           cellCount = 0;

           // increment the rowCount
           rowCount++;
        }
        else
        {
            if (rowCount == 1)
                td.className = "cnnTdGreyBotRgt";
            else
                td.className = "cnnTbBotDwn";

            tr.appendChild(td);
        }
        cellCount++;
    }
    anchorTable.appendChild(tbody);

    if (mainAnchorElement != null)
    {
        mainAnchorElement.appendChild(anchorTable);
    }
    else
    {
        alert("Can't find a place for my topScores!");
    }

}

function paintViewCastBox(contestO, elem)
{
    var html = "";
    var linkViewcast = "";
    if (contestO.links.recapInd == "T" && contestO.isFinal)
        linkViewcast = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_recap.html";
    //else if (contestO.links.boxscoreInd == "T")
     else if (contestO.isPregame == false)
        linkViewcast = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_boxscore.html";
    else
        linkViewcast = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_preview.html";
    html = html + "<a href=\"javascript:void(0)\" onclick=\"window.location='" + linkViewcast + "'\">";
    if ( ! (contestO.isFinal || contestO.gameState.isHalftime) &&
        (contestO.gameState.isRedZone || contestO.homeTeam.isNewScore || contestO.visitingTeam.isNewScore))
    {
        html = html +  "<div class=\"cnnRedRound\">";
    }
    else
    {
        html = html +  "<div class=\"cnnTbTopTab\">";
    }

    html = html +  "<table align=\"center\">";

    var teamO = contestO.visitingTeam;

    html = html +  "<tr><td class=\"cnnRating\">";
    if (teamO.apRank == null || teamO.apRank == "" || teamO.apRank == "0")
        html = html +  " ";
    else
        html = html +  teamO.apRank;    
    html = html +  "</td><td class=\"cnnTeamName\"><nobr>";
    if (teamO.nickname == null || teamO.nickname == "")
        html = html +  teamO.name.toUpperCase();
    else
        html = html +  teamO.nickname.toUpperCase();

    html = html +  "</nobr></td>";
    if (contestO.isPregame)
        html = html +  "<td class=\"cnnScore\"> </td>";
    else if (teamO.isNewScore)
        html = html +  "<td class=\"cnnScoreWin\">" + teamO.totalScore + "</td>";
    else
        html = html +  "<td class=\"cnnScore\">" + teamO.totalScore + "</td>";
    html = html +  "</tr>";

    teamO = contestO.homeTeam;

    html = html +  "<tr><td class=\"cnnRating\">";
    if (teamO.apRank == null || teamO.apRank == "" || teamO.apRank == "0")
        html = html +  " ";
    else
        html = html +  teamO.apRank;
    html = html +  "</td><td class=\"cnnTeamName\"><nobr>";
    if (teamO.nickname == null || teamO.nickname == "")
        html = html +  teamO.name.toUpperCase();
    else
        html = html +  teamO.nickname.toUpperCase();

    html = html +  "</nobr></td>";
    if (contestO.isPregame)
        html = html +  "<td class=\"cnnScore\"> </td>";
    else if (teamO.isNewScore)
        html = html +  "<td class=\"cnnScoreWin\">" + teamO.totalScore + "</td>";
    else
        html = html +  "<td class=\"cnnScore\">" + teamO.totalScore + "</td>";
    html = html +  "</tr>";

    html = html +  "<tr><td colspan=\"3\" align=\"center\">";
    
    html = html +  "<table><tr>";
    
    if (contestO.isPregame || contestO.gameState.isHalftime || contestO.isFinal)
    {
        html = html +  "<td class=\"cnnSgleLine\">";
        var status;
        if (contestO.statusInd == "FINA")
        {
           status = "Final";
           if (contestO.gameState.period == "5" || contestO.gameState.period == "6")
               status = status + " - OT";
        }
        else if (contestO.statusInd == "DELA")
           status = "Delayed";
        else if (contestO.statusInd == "POST")
           status = "Postponed";
        else if (contestO.statusInd == "CANC")
           status = "Cancelled";
        else if (contestO.statusInd == "SUSP")
           status = "Suspended";
        else if (contestO.gameState.isHalftime)
           status = "Halftime";
        else
        {
           //status = contestO.gameDateStr;
           // Chopping off the long weekday Thursday->Thu.
           if (contestO.stageID == "2")
           {
				var splitDate = contestO.gameDateStr.split(",");
				status = contestO.gameDateUrl.substring(5,10) + " " + splitDate[1];
           }
           else
           {
				var splitDate = contestO.gameDateStr.split(",");
                var shortWeekDay = splitDate[0].substring(0,3);
                status = shortWeekDay.toUpperCase() + " " + splitDate[1];
           }
           if (status.indexOf("TBD") == -1)
           		status = status + " ET";
		}
        html = html +  status.toUpperCase();
    }
    else
    {
        html = html +  "<td class=\"cnnBotRow\">";
        var period;
        if (contestO.gameState.period == "1")
             period = "1st";
        else if (contestO.gameState.period == "2")
             period = "2nd";
        else if (contestO.gameState.period == "3")
             period = "3rd";
        else if (contestO.gameState.period == "4")
             period = "4th";
        else if (contestO.gameState.period == "5" || contestO.gameState.period == "6")
             period = "OT";
        html = html +  period;
        if (contestO.gameState.clock != "")
        {
            html = html +  "</td>";
            html = html +  "<td class=\"cnnLineCCCSep\">&nbsp;&nbsp;|&nbsp;</td>";
            html = html +  "<td class=\"cnnBotRow2\">";
            html = html +  contestO.gameState.clock;
        }
    }
    html = html +  "</td></tr></table></td></tr></table></div></a>";
    //log.write(html);
    elem.innerHTML = html;
    return elem;
}

function buildViewCastMiniscoreboard(domIdSequence,contestO)
{
    var gamePlaceholder;
    
    gamePlaceholder = document.getElementById(domIdSequence);
    if (gamePlaceholder != null)
    {
        gamePlaceholder.innerHTML = "";
        gamePlaceholder = paintViewCastBox(contestO,gamePlaceholder);
    }
    else
    {
        //alert("Error: attempted to create a scoreBox table in a game placeholder[" + domIdSequence + "]");
    }
}

function buildMiniscoreboard(domIdSequence, contestO)
{
    var gamePlaceholder;    
    
    var outerDiv = document.createElement("div");
    if (contestO.isFinal == false && contestO.gameState.isHalftime == false &&
        contestO.gameState.isRedZone || contestO.homeTeam.isNewScore || contestO.visitingTeam.isNewScore)
        outerDiv.className = "cnnboxcontactive";
    else
        outerDiv.className = "cnnboxcont";

    if (contestO.venue != null && contestO.stageID == "2" && contestO.venue.bowlName != "")
    {
        var tt = document.createElement("table");
        var tb = document.createElement("tbody");
        var tr = document.createElement("tr");
        var td = document.createElement("td");

        tt.setAttribute("cellPadding","0");
        tt.setAttribute("cellSpacing","0");
        tt.setAttribute("border","0");
        tt.className = "cnnsivenue";
        tt.setAttribute("align","center");

        td.appendChild(document.createTextNode(contestO.venue.bowlName));

        tr.appendChild(td);
        tb.appendChild(tr);
        tt.appendChild(tb);

        outerDiv.appendChild(tt);
    }

    var innerDiv = document.createElement("div");
    innerDiv.className = "cnnscorebox";

    var thirdDiv = document.createElement("div");
    thirdDiv.className = "cnntopCont";
    
    // Pre Game - structure is unique
    if (contestO.isPregame)
    {
        thirdDiv.appendChild(createPregameHeader(contestO));
        thirdDiv.appendChild(createPregameTable(contestO));
        innerDiv.appendChild(thirdDiv);
        innerDiv.appendChild(createTeamStats(contestO));
        //log.write("Upon passing to createTeamStats(H:" + contestO.homeTeam.ptsPerGame + ", V:" + contestO.visitingTeam.ptsPerGame + ")");
    }
    // Game In-progress or Final - structure is same
    else
    {
        thirdDiv.appendChild(createGameTable(contestO));
        thirdDiv.appendChild(createTeamLinescores(contestO));
        innerDiv.appendChild(thirdDiv);
        innerDiv.appendChild(createGameLeaders(contestO));
    }
    var linksArray = createGameLinks(contestO);
    if (linksArray != null)
    {
        for (var i = 0; i < linksArray.length; i++)
        {
            innerDiv.appendChild(linksArray[i]);
        }
    } 
    outerDiv.appendChild(innerDiv);
    
    // Find game placeholder on the page and insert newly built miniscoreboard
    gamePlaceholder = document.getElementById(domIdSequence);
    if (gamePlaceholder != null)
    {
       gamePlaceholder.innerHTML = "";
       gamePlaceholder.appendChild(outerDiv);
    }
    else  {}
        //alert("Error: attempted to create a scoreBox table in a game placeholder[" + domIdSequence + "]");
}


function createGameTable(contestO)
{
    var table = document.createElement("table");
    table.className = "cnnscoretbl";

    var tr, td, tbody, div, b, span, telem;
 
    tbody = document.createElement("tbody");
    tr = document.createElement("tr");
    tr.className = "cnntoprow";

    td = document.createElement("td");
    td.setAttribute("colSpan", "5");
    td.className = "noltbor";
    div = document.createElement("div");
    div.className = "cnn2pxBmar";
    var idiv = document.createElement("div");
    idiv.className = "cnntabtop"; 
    b = document.createElement("b");
    var text; 
    var clock = ""; 
    var situation = "";
    if (contestO.statusInd == "FINA")
    {
       b.appendChild(document.createTextNode("Final"));
       if (contestO.gameState.period == "5" || contestO.gameState.period == "6")
           b.appendChild(document.createTextNode(" - OT"));

       idiv.appendChild(b);
    }
    else if (contestO.statusInd == "DELA")
    {
       b.appendChild(document.createTextNode("Delayed"));
       idiv.appendChild(b);
    }
    else if (contestO.statusInd == "POST")
    {
       b.appendChild(document.createTextNode("Postponed"));
       idiv.appendChild(b);
    }
    else if (contestO.statusInd == "CANC")
    {
       b.appendChild(document.createTextNode("Cancelled"));
       idiv.appendChild(b);
    }
    else if (contestO.statusInd == "SUSP")
    {
       b.appendChild(document.createTextNode("Suspended"));
       idiv.appendChild(b);
    }
    else if (contestO.gameState.isHalftime)
    {
       b.appendChild(document.createTextNode("Halftime"));
       idiv.appendChild(b);
    }
    else
    {
        if (contestO.gameState.period == "1")
             b.appendChild(document.createTextNode("1st"));
        else if (contestO.gameState.period == "2")
             b.appendChild(document.createTextNode("2nd"));
        else if (contestO.gameState.period == "3")
             b.appendChild(document.createTextNode("3rd"));
        else if (contestO.gameState.period == "4")
             b.appendChild(document.createTextNode("4th"));
        else if (contestO.gameState.period == "5" || contestO.gameState.period == "6")
             b.appendChild(document.createTextNode("OT"));
  
        clock = contestO.gameState.clock;
        var downSt = "";

        if (contestO.gameState.down == "1")
            downSt = "1st";
        else if (contestO.gameState.down == "2")
            downSt = "2nd";
        else if (contestO.gameState.down == "3")
            downSt = "3rd";
        else if (contestO.gameState.down == "4")
            downSt = "4th";

        var distance = contestO.gameState.distance;
        ////log.write("Down and distance: " + downSt + ", " + contestO.gameState.distance);
        if (! (downSt == "" || distance == "" || distance == "0"))
            situation = downSt + " & " + distance;

        ////log.write("Situation:" + situation);

        idiv.appendChild(b);

        if (clock != "")
        {
            span = document.createElement("span");
            span.className = "line";
            span.appendChild(document.createTextNode(" | "));
            idiv.appendChild(span);
            idiv.appendChild(document.createTextNode(clock));
        }
        if (situation != "")
        {
            span = document.createElement("span");
            span.className = "line";
            span.appendChild(document.createTextNode(" | "));
            idiv.appendChild(span);
         
            if (contestO.gameState.isRedZone)
            {
                var red = document.createElement("span");
                red.className = "cnnredscore";
                red.appendChild(document.createTextNode(situation));
                idiv.appendChild(red);
                idiv.appendChild(document.createTextNode(" "));
                idiv.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/redzone_17x11.gif","11","17","0","",null,"cnnrzimg"));
            }
            else
            {
                idiv.appendChild(document.createTextNode(situation));
            }
        }
    }
    div.appendChild(idiv);
    td.appendChild(div);
    tr.appendChild(td);

/**
    td = document.createElement("td");
    td.appendChild(document.createTextNode("1"));
    tr.appendChild(td);

    td = document.createElement("td");
    td.appendChild(document.createTextNode("2"));
    tr.appendChild(td);

    td = document.createElement("td");
    td.appendChild(document.createTextNode("3"));
    tr.appendChild(td);

    td = document.createElement("td");
    td.appendChild(document.createTextNode("4"));
    tr.appendChild(td);

    if (contestO.gameState.period > 4)
    {
        td = document.createElement("td");
        td.appendChild(document.createTextNode("OT"));
        tr.appendChild(td);
    }
*/
    //td = document.createElement("td");
    //td.appendChild(document.createTextNode("T"));
    //tr.appendChild(td);

    tbody.appendChild(tr);
    table.appendChild(tbody);
    return table;

}

function createTeamLinescores(contestO)
{
    var table = document.createElement("table");
    table.className = "cnnscoretbl";

    var tbody = document.createElement("tbody");
    tbody.appendChild(createTeamLinescore(contestO.visitingTeam,false,contestO.gameState.possInd, contestO.isFinal,contestO.gameState.isRedZone));
    tbody.appendChild(createTeamLinescore(contestO.homeTeam,true,contestO.gameState.possInd, contestO.isFinal,contestO.gameState.isRedZone));

    table.appendChild(tbody);

    return table; 
}

function createTeamLinescore(teamO, isHomeTeam, isInPos, isFinal, isRedZone)
{
    var tr = document.createElement("tr");

    // first or second row
    var htStyle = "cnnscorerow";
    if (isHomeTeam)
        htStyle  = htStyle + "2";
    
    tr.className = htStyle;

    ////log.write("Style: " + htStyle);
    var td,div,span;

    // team record cell
    td = document.createElement("td");
    td.setAttribute("width","17");
    if (teamO.apRank != "" && teamO.apRank != "0")
        td.appendChild(document.createTextNode(teamO.apRank));
    else
        td.appendChild(document.createTextNode(" "));
    tr.appendChild(td);

    // team logo cell
    td = document.createElement("td");
    td.className = "ncaalogo";
    td.setAttribute("width", "32");

    if (teamO.linkable)
        td.appendChild(createLinkElement(createImageElement(IMG_SERVER + "/images/football/ncaa/logos/" + teamO.urlName + "_30.gif","30","30","0", teamO.name,null,"cnnhlmt"),"/football/ncaa/teams/" + teamO.urlName + "/",null,true));
    else
        td.appendChild(createImageElement(BLANK_IMG, "30","30","0",teamO.name,null,"cnnhlmt"));

    tr.appendChild(td);

    // team name and record cell
    td = document.createElement("td");
    td.className = "lefttalign";
    td.setAttribute ("width","147");
    div = document.createElement("div");
    if (teamO.linkable)
        div.appendChild(createLinkElement((String)(teamO.name).toUpperCase(),"/football/ncaa/teams/" + teamO.urlName + "/",null,false)); 
    else
        div.appendChild(document.createTextNode((String)(teamO.name).toUpperCase()));

    div.appendChild(document.createElement("br"));
    span = document.createElement("span");
    span.appendChild(document.createTextNode(createTeamRecord(teamO.wins, teamO.losses, true)));

    // add next game if game is final and next game is available
    if (isFinal && teamO.nextGame != null && teamO.nextGame.dateStr != "")
    {
        span.appendChild(document.createElement("br"));
        if (teamO.nextGame.homeAwayInd == "H")
            span.appendChild(document.createTextNode("vs. "));
        else if (teamO.nextGame.homeAwayInd == "V")
            span.appendChild(document.createTextNode("at "));
           
        if (teamO.nextGame.oppUrl != "")
            span.appendChild(createLinkElement((String)(teamO.nextGame.oppAbrv).toUpperCase(),"/football/ncaa/teams/"+teamO.nextGame.oppUrlName+"/",null,false));
        else
            span.appendChild(document.createTextNode((String)(teamO.nextGame.oppAbrv).toUpperCase()));

        span.appendChild(document.createTextNode(createTeamRecord(teamO.nextGame.oppWins,teamO.nextGame.oppLosses,true)));
        span.appendChild(document.createTextNode(" "+ teamO.nextGame.dateStr));
    }
   
    div.appendChild(span);
    td.appendChild(div);
    tr.appendChild(td);

    // add possession indicator
    td = document.createElement("td");
    td.setAttribute("width","22");
    td.className = "noltbor";
    if ((isInPos == "H" && isHomeTeam) || (isInPos == "V" && ! isHomeTeam))
    {
        td.appendChild(createImageElement(IMG_SERVER+IMG_PATH+"/scoreboards/football_16x11.gif","11","16","0","",null, null));
    }
    else
    {
        td.appendChild(document.createTextNode(" "));
    }

    tr.appendChild(td);

    // print scores by period
    /*
    if (teamO.linescore != null)
    {
        for (var i=0; i < teamO.linescore.length; i++)
        {
            td = document.createElement("td");
            td.setAttribute("width","21");
            td.appendChild(document.createTextNode(teamO.linescore[i]));
            tr.appendChild(td);
        } 
    }
    */
    // total score here
    td = document.createElement("td");
    td.setAttribute("width","36");
    td.className = "cnnfinalscore";
    span = document.createElement("span");
    if (teamO.isNewScore)
    {
        var redspan = document.createElement("span");
        redspan.className="cnnredscore";
        redspan.appendChild(document.createTextNode(teamO.totalScore));
        span.appendChild(redspan);
    }
    else
    {
        span.appendChild(document.createTextNode(teamO.totalScore));
    }
    td.appendChild(span);
    tr.appendChild(td);
    
    td = null;
    div = null;
    span = null;

    return tr;
}

function createGameLeaders(contestO)
{
    var div = document.createElement("div");
    div.className = "playerstats";
    var table, tbody, tr, td;

    table = document.createElement("table");
    tbody = document.createElement("tbody");

   if (contestO.visitingTeam.passingLeader != null ||
       contestO.homeTeam.passingLeader != null)
   {
        var tr = document.createElement("tr");
        tr.setAttribute("vAlign", "top");

        var td = document.createElement("td");
        td.className = "statsCag";
        var b = document.createElement("b");

        b.appendChild(document.createTextNode("PASS:"));
        td.appendChild(b);
        tr.appendChild(td);

        td = document.createElement("td");
        if (contestO.visitingTeam.passingLeader != null)
        {
            var leader = contestO.visitingTeam.passingLeader;
            var b = document.createElement("b");
            b.appendChild(document.createTextNode(leader.name));
            if (leader.linkable == "T" && leader.id != "0")
            {
               var link = createLinkElement("","/football/ncaa/players/" + leader.id + "/", null, false);
               link.appendChild(b);
               td.appendChild(link);
            }
            else
                td.appendChild(b);
            //if (leader.position != null && leader.position != "")
                //td.appendChild(document.createTextNode(" (" + leader.position + ")")); 
            td.appendChild(document.createTextNode(" (" + contestO.visitingTeam.abrv.toUpperCase() + "):")); 
            td.appendChild(document.createTextNode(" " + leader.made + "-" + leader.attempted));
            if (leader.yards != "")
                td.appendChild(document.createTextNode(", " + leader.yards));
            if (leader.touchdowns != "" && leader.touchdowns != "0")
            {
                if (leader.touchdowns == "1")
                    td.appendChild(document.createTextNode(", TD"));
                else
                    td.appendChild(document.createTextNode(", " + leader.touchdowns + " TD"));
            }
            td.appendChild(document.createElement("br"));
        }
        if (contestO.homeTeam.passingLeader != null)
        {
            var leader = contestO.homeTeam.passingLeader;
            var b = document.createElement("b");
            b.appendChild(document.createTextNode(leader.name));
            if (leader.linkable == "T" && leader.id != "0")
            {
               var link = createLinkElement("","/football/ncaa/players/" + leader.id + "/", null, false);
               link.appendChild(b);
               td.appendChild(link);
            }
            else
                td.appendChild(b);
            //if (leader.position != null && leader.position != "")
                //td.appendChild(document.createTextNode(" (" + leader.position + ")")); 
            td.appendChild(document.createTextNode(" (" + contestO.homeTeam.abrv.toUpperCase() + "):")); 
            td.appendChild(document.createTextNode(" " + leader.made + "-" + leader.attempted));
            if (leader.yards != "")
                td.appendChild(document.createTextNode(", " + leader.yards));
            if (leader.touchdowns != "" && leader.touchdowns != "0")
            {
                if (leader.touchdowns == "1")
                    td.appendChild(document.createTextNode(", TD"));
                else
                    td.appendChild(document.createTextNode(", " + leader.touchdowns + " TD"));
            }
            td.appendChild(document.createElement("br"));
        }

        tr.appendChild(td);
        tbody.appendChild(tr);
    }
    else
    {
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.appendChild(document.createElement("br"));
        td.appendChild(document.createElement("br"));
        tr.appendChild(td);
        tbody.appendChild(tr);
    }

    if (contestO.visitingTeam.rushingLeader != null ||
        contestO.homeTeam.rushingLeader != null)
    {
        var tr = document.createElement("tr");
        tr.setAttribute("vAlign", "top");

        var td = document.createElement("td");
        td.className = "statsCag";
        var b = document.createElement("b");

        b.appendChild(document.createTextNode("RUSH:"));
        td.appendChild(b);
        tr.appendChild(td);

        td = document.createElement("td");
        if (contestO.visitingTeam.rushingLeader != null)
        {
            var leader = contestO.visitingTeam.rushingLeader;
            var b = document.createElement("b");
            b.appendChild(document.createTextNode(leader.name));
            if (leader.linkable == "T" && leader.id != "0")
            {
               var link = createLinkElement("","/football/ncaa/players/" + leader.id + "/", null, false);
               link.appendChild(b);
               td.appendChild(link);
            }
            else
                td.appendChild(b);
            //if (leader.position != null && leader.position != "")
                //td.appendChild(document.createTextNode(" (" + leader.position + ")")); 
            td.appendChild(document.createTextNode(" (" + contestO.visitingTeam.abrv.toUpperCase() + "):")); 
            td.appendChild(document.createTextNode(" " + leader.rushes + "-" + leader.yards));
            if (leader.touchdowns != "" && leader.touchdowns != "0")
            {
                if (leader.touchdowns == "1")
                    td.appendChild(document.createTextNode(", TD"));
                else
                    td.appendChild(document.createTextNode(", " + leader.touchdowns + " TD"));
            }
            td.appendChild(document.createElement("br"));
        }
        if (contestO.homeTeam.rushingLeader != null)
        {
            var leader = contestO.homeTeam.rushingLeader;
            var b = document.createElement("b");
            b.appendChild(document.createTextNode(leader.name));
            if (leader.linkable == "T" && leader.id != "0")
            {
               var link = createLinkElement("","/football/ncaa/players/" + leader.id + "/", null, false);
               link.appendChild(b);
               td.appendChild(link);
            }
            else
                td.appendChild(b);
            //if (leader.position != null && leader.position != "")
                //td.appendChild(document.createTextNode(" (" + leader.position + ")")); 
            td.appendChild(document.createTextNode(" (" + contestO.homeTeam.abrv.toUpperCase() + "):")); 
            td.appendChild(document.createTextNode(" " + leader.rushes + "-" + leader.yards));
            if (leader.touchdowns != "" && leader.touchdowns != "0")
            {
                if (leader.touchdowns == "1")
                    td.appendChild(document.createTextNode(", TD"));
                else
                    td.appendChild(document.createTextNode(", " + leader.touchdowns + " TD"));
            }
            td.appendChild(document.createElement("br"));
        }

        tr.appendChild(td);
        tbody.appendChild(tr);
    }
    else
    {
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.appendChild(document.createElement("br"));
        td.appendChild(document.createElement("br"));
        tr.appendChild(td);
        tbody.appendChild(tr);
    }
    if (contestO.visitingTeam.receivingLeader != null ||
        contestO.homeTeam.receivingLeader != null)
    {
        var tr = document.createElement("tr");
        tr.setAttribute("vAlign", "top");

        var td = document.createElement("td");
        td.className = "statsCag";
        var b = document.createElement("b");

        b.appendChild(document.createTextNode("REC:"));
        td.appendChild(b);
        tr.appendChild(td);

        td = document.createElement("td");
        if (contestO.visitingTeam.receivingLeader != null)
        {
            var leader = contestO.visitingTeam.receivingLeader;
            var b = document.createElement("b");
            b.appendChild(document.createTextNode(leader.name));
            if (leader.linkable == "T" && leader.id != "0")
            {
               var link = createLinkElement("","/football/ncaa/players/" + leader.id + "/", null, false);
               link.appendChild(b);
               td.appendChild(link);
            }
            else
                td.appendChild(b);
           
            //if (leader.position != null && leader.position != "")
                //td.appendChild(document.createTextNode(" (" + leader.position + ")")); 
            td.appendChild(document.createTextNode(" (" + contestO.visitingTeam.abrv.toUpperCase() + "):")); 
            td.appendChild(document.createTextNode(" " + leader.receptions + "-" + leader.yards));
            if (leader.touchdowns != "" && leader.touchdowns != "0")
            {
                if (leader.touchdowns == "1")
                    td.appendChild(document.createTextNode(", TD"));
                else
                    td.appendChild(document.createTextNode(", " + leader.touchdowns + " TD"));
            }
            td.appendChild(document.createElement("br"));
        }
        if (contestO.homeTeam.receivingLeader != null)
        {
            var leader = contestO.homeTeam.receivingLeader;
            var b = document.createElement("b");
            b.appendChild(document.createTextNode(leader.name));
            if (leader.linkable == "T" && leader.id != "0")
            {
               var link = createLinkElement("","/football/ncaa/players/" + leader.id + "/", null, false);
               link.appendChild(b);
               td.appendChild(link);
            }
            else
                td.appendChild(b);
           
            //if (leader.position != null && leader.position != "")
                //td.appendChild(document.createTextNode(" (" + leader.position + ")")); 
            td.appendChild(document.createTextNode(" (" + contestO.homeTeam.abrv.toUpperCase() + "):")); 
            td.appendChild(document.createTextNode(" " + leader.receptions + "-" + leader.yards));
            if (leader.touchdowns != "" && leader.touchdowns != "0")
            {
                if (leader.touchdowns == "1")
                    td.appendChild(document.createTextNode(", TD"));
                else
                    td.appendChild(document.createTextNode(", " + leader.touchdowns + " TD"));
            }
        }

        tr.appendChild(td);
        tbody.appendChild(tr);
    }
    else
    {
        tr = document.createElement("tr");
        td = document.createElement("td");
        td.appendChild(document.createElement("br"));
        td.appendChild(document.createElement("br"));
        tr.appendChild(td);
        tbody.appendChild(tr);
    }

    table.appendChild(tbody);
    div.appendChild(table);
    if (contestO.stageID == "1")
        div.appendChild(document.createElement("br"));

    return div;
}

function createGameLinks(contestO)
{
    if (contestO.links == null)
        return null;
    var divArray = new Array();
    var div = document.createElement("div");
    div.className = "cnngreylnks";

    if (contestO.isPregame == false)
    {
        if (contestO.isFinal)
        {
            if (VIEWCAST_ACTIVE && contestO.isFullGame)
            {
                var a = document.createElement("a");
                a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_boxscore.html";
                a.className = "viewcast";
                a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"greyarrow"));
                a.appendChild(document.createTextNode("BOX SCORE"));
                div.appendChild(a);
            }
            //else if (contestO.links.boxscoreInd == "T")
            else
            {
                var a = document.createElement("a");
                a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_boxscore.html";
                a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
                a.appendChild(document.createTextNode(" BOX SCORE"));
                div.appendChild(a);
            }
        }
        else
        {
            if (VIEWCAST_ACTIVE && contestO.isFullGame)
            {
                var a = document.createElement("a");
                a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_boxscore.html";
                a.className = "viewcast";
                a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"greyarrow"));
                a.appendChild(document.createTextNode("BOX SCORE"));
                div.appendChild(a);
            }
            //else if (contestO.links.boxscoreInd == "T")
            else
            {
                var a = document.createElement("a");
                a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_boxscore.html";
                a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
                a.appendChild(document.createTextNode(" BOX SCORE"));
                div.appendChild(a);
            }
        }
        if (VIEWCAST_ACTIVE && contestO.links.gameblogInd == "T")
        {
            var a = document.createElement("a");
            a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_gameblog.html";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" GAME BLOG"));
            div.appendChild(a);
         }
    }
    if (contestO.isFinal || contestO.isInprogress)
    { }
    else
    {        
        if (VIEWCAST_ACTIVE && contestO.isFullGame)
        {
            var a = document.createElement("a");
            a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_preview.html";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" GAME PREVIEW"));
            div.appendChild(a);
        }
        else if (contestO.links.previewInd == "T")
        {
            var a = document.createElement("a");
            a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_preview.html";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" GAME PREVIEW"));
            div.appendChild(a);

        }
    }
   divArray[0] = div;

    div = document.createElement("div");        
    div.className = "cnngreylnks";

    if (contestO.isPregame)
    {
        if (contestO.visitingTeam.linkable)
        {
            var a = document.createElement("a");
            a.href = "/football/ncaa/teams/" + contestO.visitingTeam.urlName + "/";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" MORE " + (contestO.visitingTeam.nickname).toUpperCase()));
            div.appendChild(a);
        }

        if (contestO.homeTeam.linkable)
        {
            a = document.createElement("a");
            a.href = "/football/ncaa/teams/" + contestO.homeTeam.urlName + "/";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" MORE " + (contestO.homeTeam.nickname).toUpperCase()));
            div.appendChild(a);
        }
    }
    else
    {
        if (contestO.links.recapInd == "T" && contestO.isFinal)
        {
            if (VIEWCAST_ACTIVE && contestO.isFullGame)
            {
                var a = document.createElement("a");
                a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_recap.html";
                a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
                a.appendChild(document.createTextNode(" RECAP"));
                div.appendChild(a);
            }
            else
            {
                var a = document.createElement("a");
                a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_recap.html";
                a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
                a.appendChild(document.createTextNode(" RECAP"));
                div.appendChild(a);
            }
        }
        if (VIEWCAST_ACTIVE && contestO.isFullGame)
        {
            var a = document.createElement("a");
            a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_gamephotos.html";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" PHOTOS"));
            div.appendChild(a);

            var a = document.createElement("a");
            a.href = "/football/ncaa/gameflash/" + contestO.gameDateUrl + "/" + contestO.contestID + "_fancomment.html";
            a.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/arrow_transp.gif","7","6","0","",null,"whitearrow"));
            a.appendChild(document.createTextNode(" FAN COMMENTS"));
            div.appendChild(a);
        }
    }
    divArray[1] = div;
    
    return divArray;
}


function createPregameTable(contestO)
{
    var tElem = document.createElement("table");
    var tbody = document.createElement("tbody");

    // visiting team
    var tRow = document.createElement("tr");
    tRow.className = "cnnscorerow";
	var teamO = contestO.visitingTeam;
	
	// rank
    var rankTd = document.createElement("td");
    rankTd.setAttribute("width","17");
    if (teamO.apRank != "" && teamO.apRank != "0")
       rankTd.appendChild(document.createTextNode(teamO.apRank));       
    else
       rankTd.appendChild(document.createTextNode(" "));
    tRow.appendChild(rankTd);

	// logo
    var tTd = document.createElement("td");
    tTd.setAttribute("width","32");
    tTd.className = "ncaalogo";    
    var linkTo = "/football/ncaa/teams/" + teamO.urlName ;
    var teamLogo = createImageElement(IMG_SERVER + "/images/football/ncaa/logos/" + teamO.urlName + "_30.gif", "30", "30", "0", teamO.name, "left","cnnhlmt");

    if (teamO.linkable)
        tTd.appendChild(createLinkElement(teamLogo, linkTo, null, true));
    else
        tTd.appendChild(createImageElement(BLANK_IMG, "30","30","0",teamO.name,null,"cnnhlmt"));
    tRow.appendChild(tTd);
    
    // name
    tTd = document.createElement("td");
    tTd.className = "lefttalign";
    tTd.setAttribute("width","100");
    var tempE = document.createElement("div");

    if (teamO.linkable)
        tempE.appendChild(createLinkElement((String)(teamO.name).toUpperCase(), linkTo,null,false));
    else
        tempE.appendChild(document.createTextNode((String)(teamO.name).toUpperCase()));
    tTd.appendChild(tempE);
    
    // record
    tempE.appendChild(document.createElement("br"));
    var tempS = document.createElement("span");
    tempS.appendChild(document.createTextNode(createTeamRecord(teamO.wins, teamO.losses, true)));
    tempE.appendChild(tempS);
    tTd.appendChild(tempE);

    tRow.appendChild(tTd);

    // venue
    tTd = document.createElement("td");
    tTd.className = "cnnsipreloc";
    tTd.setAttribute("width","109");
    
    if (contestO.venue != null)
    {
        tTd.appendChild(document.createTextNode(contestO.venue.stadium));
        if (contestO.venue.city != "" && contestO.venue.state != "")
        {
        	tTd.appendChild(document.createElement("br"));
        	tTd.appendChild(document.createTextNode(contestO.venue.city + ", " + contestO.venue.state));
    	}
    }
    tRow.appendChild(tTd);
    
    tbody.appendChild(tRow);
    
    // home team
    teamO = contestO.homeTeam;
	tRow = document.createElement("tr");
	tRow.className = "cnnscorerow2";
	
	// rank
    var rankTd = document.createElement("td");
    if (teamO.apRank != "" && teamO.apRank != "0")
       rankTd.appendChild(document.createTextNode(teamO.apRank));       
    else
       rankTd.appendChild(document.createTextNode(" "));
    tRow.appendChild(rankTd);

	// logo
    var tTd = document.createElement("td");
    tTd.className = "ncaalogo";    
    var linkTo = "/football/ncaa/teams/" + teamO.urlName ;
    var teamLogo = createImageElement(IMG_SERVER + "/images/football/ncaa/logos/" + teamO.urlName + "_30.gif", "30", "30", "0", teamO.name, "left","cnnhlmt");

    if (teamO.linkable)
        tTd.appendChild(createLinkElement(teamLogo, linkTo, null, true));
    else
        tTd.appendChild(createImageElement(BLANK_IMG, "30","30","0",teamO.name,null,"cnnhlmt"));
    tRow.appendChild(tTd);
    
    // name
    tTd = document.createElement("td");
    tTd.className = "lefttalign";
    var tempE = document.createElement("div");

    if (teamO.linkable)
        tempE.appendChild(createLinkElement((String)(teamO.name).toUpperCase(), linkTo,null,false));
    else
        tempE.appendChild(document.createTextNode((String)(teamO.name).toUpperCase()));
    tTd.appendChild(tempE);
    
    // record
    tempE.appendChild(document.createElement("br"));
    var tempS = document.createElement("span");
    tempS.appendChild(document.createTextNode(createTeamRecord(teamO.wins, teamO.losses, true)));
    tempE.appendChild(tempS);
    tTd.appendChild(tempE);

    tRow.appendChild(tTd);
    // weather
    tTd = document.createElement("td");
    tTd.className = "nortbor";
    
    if (contestO.weather != null)
    {
        tempE = document.createElement("div");
        tempE.className = "weather";
        iD = document.createElement("div");
        iD.className = "forecast";
        iD.appendChild(createLinkElement(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/kickoff_95x13.gif", "13","95","0","Get detailed forecast for this city",null,null), "http://weather.cnn.com/weather/forecast.jsp?zipCode="+contestO.weather.zipCode,"_new",true));        
        tempE.appendChild(iD);
    
        iD = document.createElement("div");   
        var nobr = document.createElement("nobr");
        var a = createLinkElement("", "http://weather.cnn.com/weather/forecast.jsp?zipCode="+contestO.weather.zipCode,"_new",false);
        nobr.appendChild(createImageElement(WEATHER_IMAGES + contestO.weather.weatherCode + ".gif", "17", "21", "0", "", null,"cnnweathicn"));
         
        nobr.appendChild(document.createTextNode(trimString(contestO.weather.forecast)));
        //a.appendChild(document.createElement("br"));
        nobr.appendChild(document.createTextNode(" " + contestO.weather.temperature + "°"));
        a.appendChild(nobr);
        iD.appendChild(a);
        tempE.appendChild(iD);
        
        tTd.appendChild(tempE);
    }
    tRow.appendChild(tTd);
    tbody.appendChild(tRow);
    tElem.appendChild(tbody); 
        
    return tElem;
}

function createTeamStats (contestO)
{
    var pElem = document.createElement("div");
    pElem.className = "teamstats";

/*
    if (contestO.visitingTeam.hasStats == true)
    {
        var team = contestO.visitingTeam;
        //log.write("VISITING TEAM:  Pts/Game: " + team.ptsPerGame + ", Allowed/Game: " + team.ptsAllowedPerGame);
        var b = document.createElement("b");
       	b.appendChild(document.createTextNode(team.abrv.toUpperCase() + ":"));
        pElem.appendChild(b);
	pElem.appendChild(document.createTextNode(" Pts/Game: " + team.ptsPerGame + ", Allowed/Game: " + team.ptsAllowedPerGame));
        pElem.appendChild(document.createElement("br"));
    }
    if (contestO.homeTeam.hasStats == true)
    {
        var team = contestO.homeTeam;
        //log.write("HOME TEAM HELLO");
        //log.write(team.ptsPerGame);
        //log.write("HOME TEAM:  Pts/Game: " + team.ptsPerGame + ", Allowed/Game: " + team.ptsAllowedPerGame);
        var b = document.createElement("b");
        b.appendChild(document.createTextNode(team.abrv.toUpperCase() + ":"));
        pElem.appendChild(b);
	pElem.appendChild(document.createTextNode(" Pts/Game: " + team.ptsPerGame + ", Allowed/Game: " + team.ptsAllowedPerGame));
	pElem.appendChild(document.createElement("br"));
    }
    if (contestO.lastGame != null)
    {
        var b = document.createElement("b");
        b.appendChild(document.createTextNode("LAST:"));
        pElem.appendChild(b);

        pElem.appendChild(document.createTextNode(" " + contestO.lastGame.dateStr + " :: "));
        if (parseInt(contestO.lastGame.homeScore) > parseInt(contestO.lastGame.visitorScore))
            pElem.appendChild(document.createTextNode(contestO.homeTeam.nickname + " " + contestO.lastGame.homeScore + ", " + contestO.visitingTeam.nickname + " " + contestO.lastGame.visitorScore));
        else
            pElem.appendChild(document.createTextNode(contestO.visitingTeam.nickname + " " + contestO.lastGame.visitorScore + ", " + contestO.homeTeam.nickname + " " + contestO.lastGame.homeScore));
    } 
*/

    if (contestO.homeTeam.hasStats || contestO.visitingTeam.hasStats)
    {
        var tempT = document.createElement("table");
        var tempB = document.createElement("tbody");
        var tempR, tempTd;

        tempR = document.createElement("tr");
        tempR.className = "tablehead";
        tempTd = document.createElement("td");
        tempR.appendChild(tempTd);

        tempTd = document.createElement("td");
        var b = document.createElement("b");
        b.appendChild(document.createTextNode("Scored/Game"));
        tempTd.appendChild(b);
        tempR.appendChild(tempTd);

        tempTd = document.createElement("td");
        b = document.createElement("b");
        b.appendChild(document.createTextNode("Allowed/Game"));
        tempTd.appendChild(b);
        tempR.appendChild(tempTd);

        tempB.appendChild(tempR);

        tempTd =  null;
        tempR = null;
        b = null;

        if (contestO.visitingTeam.hasStats)
            tempB.appendChild(createTeamStatsRow(contestO.visitingTeam));

        if (contestO.homeTeam.hasStats)
            tempB.appendChild(createTeamStatsRow(contestO.homeTeam));

        tempT.appendChild(tempB);
        tempB = null;

        pElem.appendChild(tempT);
        pElem.appendChild(document.createElement("br"));
        tempT = null;
     }

    if (contestO.lastGame != null)
    {
        var b = document.createElement("b");
        b.appendChild(document.createTextNode("Last Meeting: "));
        pElem.appendChild(b);

        pElem.appendChild(document.createTextNode(contestO.lastGame.dateStr));
        pElem.appendChild(document.createElement("br"));
        var tempD = document.createElement("div");
        tempD.className = "cnnindent";
        if (parseInt(contestO.lastGame.homeScore) > parseInt(contestO.lastGame.visitorScore))
            tempD.appendChild(document.createTextNode(contestO.homeTeam.nickname + " " + contestO.lastGame.homeScore + ", " + contestO.visitingTeam.nickname + " " + contestO.lastGame.visitorScore));
        else
            tempD.appendChild(document.createTextNode(contestO.visitingTeam.nickname + " " + contestO.lastGame.visitorScore + ", " + contestO.homeTeam.nickname + " " + contestO.lastGame.homeScore));
        pElem.appendChild(tempD);
    }

    if (! contestO.homeTeam.hasStats)
        pElem.appendChild(document.createElement("br"));
    if (! contestO.visitingTeam.hasStats)
        pElem.appendChild(document.createElement("br"));
    if (contestO.lastGame == null)
    {
        pElem.appendChild(document.createElement("br"));
        pElem.appendChild(document.createElement("br"));
    }
   
    if (contestO.stageID == "1")
    {
        pElem.appendChild(document.createElement("br"));
    }

    return pElem;
}

function createTeamStatsRow(teamO)
{
    var tempR = document.createElement("tr");
    var tempTd = document.createElement("td");

    tempTd.className = "cnnteamname";
    tempB = document.createElement("b");

    tempB.appendChild(document.createTextNode((String)(teamO.abrv).toUpperCase()));
    tempTd.appendChild(tempB);

    tempR.appendChild(tempTd);

    tempTd = document.createElement("td");
    tempTd.className = "cnncntr";
    tempTd.appendChild(document.createTextNode(teamO.ptsPerGame));
    tempR.appendChild(tempTd);

    tempTd = document.createElement("td");
    tempTd.className = "cnncntr";
    tempTd.appendChild(document.createTextNode(teamO.ptsAllowedPerGame));
    tempR.appendChild(tempTd);

    tempTd = null;
    tempB = null;

    return tempR;
}

function createPregameHeader(contestO)
{
    var pElem = document.createElement("div");
    pElem.className =  "gamedate";

    var iD = document.createElement("div");       
    var t = document.createElement("table");

    t.setAttribute("cellpadding","0");
    t.setAttribute("cellspacing","0");
    t.setAttribute("border","0");

    var tB = document.createElement("tbody");
    var r = document.createElement("tr");
    var td = document.createElement("td");
    td.className = "lefttalign";

    var bT = document.createElement("b");
    bT.appendChild(document.createTextNode(contestO.gameDateStr));
    if (contestO.gameDateStr.indexOf("TBD") == -1)
        bT.appendChild(document.createTextNode(" ET"));
    td.appendChild(bT);
    bT = null;
    r.appendChild(td);
 
    td = document.createElement("td");
    td.className = "rightalign";

    var searchString = (String)(contestO.homeTeam.name).toLowerCase();
    searchString = searchString.replace(/ /g, '-');
    searchString = searchString.replace(/\./g, '');

    var ticketLink = createLinkElement(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/buytickets_99x14.gif", "14","99","0","StubHub: Buy Tickets Now", null,null), "http://www.stubhub.com/" + searchString + "football-tickets/?gcid=C12289x159","_new",true);
    
    td.appendChild(ticketLink);
    r.appendChild(td);

    ticketLink = null;
    td = null;

    tB.appendChild(r);
    r = null;
    t.appendChild(tB);
    tB = null;

    iD.appendChild(t);
    t = null;

    pElem.appendChild(iD);

    return pElem;
}


function createPicks(contestO)
{
    var pElem = null;
    var tempElem;
    var outerDiv;
    var innerDiv;
    var tempT, tempR, tempTd, tempB;
    
    if (contestO.picks != null)
    {
        pElem = document.createElement("div");
        pElem.className = "cnnbluebox";
        pElem.id = "cnnNoBtn";
        
        outerDiv = document.createElement("div");
        outerDiv.className = "left";        
        outerDiv.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/left_bg_nobtn.gif", "50","8","0","",null,null));
        pElem.appendChild(outerDiv);
        outerDiv = null;
        
        outerDiv = document.createElement("div");
        outerDiv.className = "inside";
        
        // peter king's pick row
        innerDiv = document.createElement("div");
        innerDiv.className = "cnnkingpick";
        
        tempT = document.createElement("table");
        tempT.setAttribute("align","center");
        tempB = document.createElement("tbody");
        tempR = document.createElement("tr");
        tempTd = document.createElement("td");
        
        tempTd.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/pk's_pick_116x15.gif", "15","116","0","",null,null));
        tempR.appendChild(tempTd);
        
        tempTd = null;
    
        tempTd = document.createElement("td");
         
         var teamLink = null;
         var teamName = null;
         if (contestO.picks.pickInd == "H")
         {
             teamLink = contestO.homeTeam.urlName;
             teamName = contestO.homeTeam.name.toUpperCase();
         }
         else 
         {
             teamLink = contestO.visitingTeam.urlName;
             teamName = contestO.visitingTeam.name.toUpperCase();
         }
             
         if (teamLink != null)
         {
             tempTd.appendChild(
             createLinkElement(teamName,"/football/ncaa/teams/" + teamLink + "/", null,false));
         }
         else
         {    
             tempTd.createTextNode(teamName);
         }
         tempR.appendChild(tempTd);
         tempElem = null;
         tempTd = null;
         tempB.appendChild(tempR);
         tempT.appendChild(tempB);
         innerDiv.appendChild(tempT);
         tempR = null;
         tempB = null;
         tempT = null;
         outerDiv.appendChild(innerDiv);
         innerDiv = null;
         
         // consensus row
        innerDiv = document.createElement("div");
        innerDiv.className = "cnnconsensus";
        
        tempT = document.createElement("table");
        tempB = document.createElement("tbody");

        tempT.setAttribute("align","center");
        tempR = document.createElement("tr");
        tempTd = document.createElement("td");
        
        tempTd.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/consensus_76x15.gif", "15","76","0","",null,null));
        tempR.appendChild(tempTd);
       
        tempElem = null;
        tempTd = null;
   
        tempTd = document.createElement("td");
        
        teamLink = null;
        teamName = null;
        if (contestO.picks.consensusInd == "H")
        {
            teamLink = contestO.homeTeam.urlName;
            teamName = contestO.homeTeam.name.toUpperCase();
        }
        else 
        {
            teamLink = contestO.visitingTeam.urlName;
            teamName = contestO.visitingTeam.name.toUpperCase();
        }
            
        if (teamLink != null)
        {
            tempTd.appendChild(createLinkElement(teamName,"/football/ncaa/teams/" + teamLink + "/", null,false));
            tempTd.appendChild(document.createTextNode(" (" + contestO.picks.percentage + "%)"));
        }
        else
        {    
            tempTd.appendChild(document.createTextNode(teamName + " (" + contestO.picks.percentage + "%)"));
        }
        tempR.appendChild(tempTd);
        tempElem = null;
        tempTd = null;
        tempB.appendChild(tempR);
        tempT.appendChild(tempB);
        innerDiv.appendChild(tempT);
        tempR = null;
        tempB = null;
        tempT = null;
        outerDiv.appendChild(innerDiv);
        innerDiv = null;
         
        pElem.appendChild(outerDiv);
        outerDiv = null;
         
        outerDiv = document.createElement("div");
        outerDiv.className = "right";
        outerDiv.appendChild(createImageElement(IMG_SERVER + IMG_PATH + "/scoreboards/right_bg_nobtn.gif", "50","8","0","",null,null));
        tempElem = null;
        pElem.appendChild(outerDiv);
        outerDiv = null;        
    }
    return pElem;
}


function getNumFullRows(numGames)
{    
    // this value is a UI design constant, and will not change often
    var gamesPerRow = GAMES_IN_ROW;
    var numFullRows;

    // The simple division will return a floating point value (ex: 3.1, if there are 10 games).
    // We have to use the floor() function to pull out the integer part of that number.  The 
    // round() function will not give us what we want - we always want to round down.
    //
    numFullRows = Math.floor(numGames / gamesPerRow);
    return numFullRows;
}

function getNumGamesOnPartialRow(numGames)
{
    // this value is a UI design constant, and will not change often
        var gamesPerRow = GAMES_IN_ROW;
    
        return (numGames % gamesPerRow);
}    
    
