﻿// JScript File
var firstTime = true;


function UpdateScoreboard_callback(response){

    //retrieve the response property values
    var scoreboard_data  = response.value;
    
    var LeagueID = scoreboard_data.LeagueID;
    var League = scoreboard_data.League;
    var GameDate = scoreboard_data.GameDate;
    var Season = scoreboard_data.Season;
    var PageType = scoreboard_data.PageType;
    var Week = scoreboard_data.Week;
    var LastUpdateTime = scoreboard_data.LastUpdateTime;
    var RefreshStartTime = scoreboard_data.RefreshStartTime;
    var ConferenceTitle = scoreboard_data.ConferenceTitle;
    var ConferenceID = scoreboard_data.ConferenceID;
    var ScoreboardName = scoreboard_data.ScoreboardName;
    var ScoreboardID = scoreboard_data.ScoreboardID;
    var ScoreboardTypeID = scoreboard_data.ScoreboardTypeID;
    var refresh = false;
    //var DoUpdates = scoreboard_data.DoUpdates;
    
    //the property Refresh signifies to reload the entire scoreboard, not individual game boxes
    //game goes final, auto resh turned on, new date selected etc.
     var HiddenGameDateCtl = document.getElementById("gamedatetime");
     var HiddenGameDateCtlValue;
     if(HiddenGameDateCtl != null){
        HiddenGameDateCtlValue = HiddenGameDateCtl.value;
     }
     
   
    
    if (scoreboard_data.Refresh == true){   
          //if(HiddenGameDateCtlValue == GameDate)
          //{ 
                var sbDiv = document.getElementById("Scoreboard_" + LeagueID);
                if (sbDiv != null){
                    sbDiv.innerHTML = scoreboard_data.ScoreBoardHTML;
                }
          //}
    }
    else {
    
        //This is used to stop AJAX if this date is not today or yestersday (Only today and yesterday should get livescores updates)
        //var runAjax = document.getElementById("runAjax").value
        var runAjax = scoreboard_data.DoUpdates;
        //if runAjax is not turned on, kill this execution
        if (runAjax!= "True")
        {
            return;   
        }  
        
        //if the user has switched to a new date, kill off the old cycle
        if (LeagueID!= 1 && LeagueID!=2)
        {
            var HiddenGameDate = document.getElementById("gamedatetime").value
            if (HiddenGameDate!= GameDate)
            {
                return;   
            }
        }
    
        var GameResponseList = scoreboard_data.GameResponseList;

        for (var i=0; i<GameResponseList.length; i++){
        
  
            var EventID = GameResponseList[i].EventID;           
            var Total = GameResponseList[i].Total;           
            var Spread = GameResponseList[i].Spread;           
            var ScoreStatus = GameResponseList[i].ScoreStatus; 
           
            var OldHomeScore 
            var OldVisitScore 
            var NewHomeScore
            var NewVisitScore
            var NewScoreStatus
            var OldScoreStatus 
           

            var GameDivID = "Game_" + LeagueID +"_"+EventID; 
        
            var gameDiv = document.getElementById(GameDivID);
            if (gameDiv != null){
            
                OldHomeScore = GetSubElementById(gameDiv,"HomeScore_"+LeagueID +"_"+EventID);               
                 
                OldVisitScore = GetSubElementById(gameDiv,"VisitScore_"+LeagueID +"_"+EventID);         
                OldScoreStatus = GetSubElementById(gameDiv,"Status_"+LeagueID +"_"+EventID);
                
                if (OldHomeScore != null && OldVisitScore != null)
                {      
                    OldHomeScore = OldHomeScore.innerHTML;
                    OldVisitScore = OldVisitScore.innerHTML;   
                }
                
                gameDiv.parentNode.innerHTML = GameResponseList[i].gameHTML;
                
                NewScoreStatus = GetSubElementById(document.getElementById(GameDivID),"Status_"+LeagueID +"_"+EventID);          
                NewHomeScore = GetSubElementById(document.getElementById(GameDivID),"HomeScore_"+LeagueID +"_"+EventID); 
                NewVisitScore = GetSubElementById(document.getElementById(GameDivID),"VisitScore_"+LeagueID +"_"+EventID);
                
               if (NewHomeScore != null && NewVisitScore != null && GameDivID != null)
                {      
                    NewHomeScore = NewHomeScore.innerHTML;
                    NewVisitScore = NewVisitScore.innerHTML;                    
                }
                
                if (NewScoreStatus != null && OldScoreStatus != null)
                {   
                    //We are checking to see if a game just went final, if so we want to refresh the entire 
                    //scoreboard so that this game will move to the bottom
                    
                    OldScoreStatus = OldScoreStatus.innerHTML;
                    NewScoreStatus = NewScoreStatus.innerHTML;
                    var OldIsFinal = OldScoreStatus.indexOf('Final');
                    var NewIsFinal = NewScoreStatus.indexOf('Final');
                    if ( (OldIsFinal == -1) && (NewIsFinal != -1))
                    {
                        refresh = true;
                    }
                    //if it baseball and we are going to the top of the inning then we want it to flash
                    if (LeagueID == 5 && OldScoreStatus.indexOf('Bot') != -1 && NewScoreStatus.indexOf('Top') != -1)
                    {
                        FlashScorebox(GameDivID, flashes, "");
                    }
                }
                
         	                               
                                   
                    var flashes = 5
                    
                     //check for a change in score so that we can make it blink
                    if (OldHomeScore != NewHomeScore){
                        FlashScorebox(GameDivID, flashes, "HomeScore_"+LeagueID +"_"+GameResponseList[i].EventID);
                    }
                    if (OldVisitScore != NewVisitScore){
                        FlashScorebox(GameDivID, flashes, "VisitScore_"+LeagueID +"_"+GameResponseList[i].EventID);
                    }
                   
                }
            }
        }
        
        
          //This is used to stop old AJAX from looping once a user has moved on to another scoreboard
        var cookie_refresh = Get_Cookie("RefreshScores");
        if (RefreshStartTime!=cookie_refresh || cookie_refresh=="")
        {
             return;
        }
   
          
        var ctlConf = document.getElementById("conferenceSelect");
        var myConf="";
        if (ctlConf != null)
            myConf = ctlConf.value;
                    
        var ControlType = document.getElementById("ControlType");
        var myControlType="";
        if (ControlType != null)
            myControlType = ControlType.value;
        
        var sbColumns = "";
        var scoreboardColumns = document.getElementById("ScoreboardColumns");;
        if (scoreboardColumns != null)
            sbColumns = scoreboardColumns.value;
                   
            
    
          
        //if refresh is true then we want to start redraw the scoreboard and start a new cycle of refreshes   
        if (refresh==true)
        {
            NewScoresFeed(LeagueID, GameDate, Season, PageType, Week )
        }
        else
        {    
        
            //DO NOT CALL UPDATE IF THE CURRENT DATE SELECTION IS NOT TODAY OR YESTERDAY      
            
           
              
            window.setTimeout("Scoreboard.UpdateScoreboard("
            +LeagueID+", '"
            +GameDate+"', '"
            +Season+"', "+refresh+", '"
            +LastUpdateTime + "', '"
            +PageType+"','"+RefreshStartTime + "', '"+Week+"','"+myControlType+"','"+sbColumns+"', '"+ScoreboardName+"', '"+ScoreboardID+"', '"+ScoreboardTypeID+"', UpdateScoreboard_callback);", 15000 );         
        }
}

function NewScoresFeed(LeagueID, GameDate, Season, PageType, Week ){
            
    //check to see if the cookie currently exists            
    var cookie_refresh = Get_Cookie("RefreshScores");
                    
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var time = d.getTime();
    var RefreshStartTime = curr_date+"-"+curr_month+"-"+curr_year+"-"+time
    
    //check if cookie exists
     var cookie_refresh = Get_Cookie("RefreshScores");
     
     if (cookie_refresh!=null){
        //kill it!
        //reset it!
        if (cookie_refresh != ""){
            Delete_Cookie('RefreshScores');
            Set_Cookie( 'RefreshScores', RefreshStartTime, '', '/', '', '' ); 
        }
     }
 
        
   //This function is used to update the conference title when a new option is selected in the ddlb
    var ctlConf = document.getElementById("confSelect");
    var myScoreboardName="";
    var myScoreboardID="";
    if (ctlConf != null)
    {   
        var confIndex = ctlConf.selectedIndex;       
          
        if (confIndex != null)
        {
             myScoreboardID = ctlConf.options[confIndex].value;     
             myScoreboardName = ctlConf.options[confIndex].text;       
        }
    }else{
        //if no conference selector, pull from hidden variable
         var ctlHiddenConf = document.getElementById("conferenceSelect");
         if (ctlHiddenConf != null){
            myScoreboardName = ctlHiddenConf.value;
            myScoreboardID = ctlHiddenConf.value;
         }
    }
        
        var ControlType = document.getElementById("ControlType");
    var myControlType="";
    if (ControlType != null)
        myControlType = ControlType.value;
        
        var sbColumns = "";
        var scoreboardColumns = document.getElementById("ScoreboardColumns");;
    if (scoreboardColumns != null)
        sbColumns = scoreboardColumns.value;
        
        
        
        
   
    //DO NOT CALL UPDATE IF THE CURRENT DATE SELECTION IS NOT TODAY OR YESTERDAY     
    
     //check to see if the cookie currently exists            
            var cookie_CurrentSelectedDate = Get_Cookie("LastDayViewed_" + LeagueID);
            
            var nowDate = new Date();
            var pastDate = new Date() ; with (pastDate) setDate(getDate()-1);
            var currentDateString = ( ((nowDate.getMonth()+1) < 9) ? ""+ (nowDate.getMonth()+1) : ""+(nowDate.getMonth()+1) ) + "-" + (nowDate.getDate() < 9 ? "" + nowDate.getDate() : nowDate.getDate()) + "-" + nowDate.getFullYear();
            var yesterDateString =  ( ((pastDate.getMonth()+1) < 9) ? ""+ (pastDate.getMonth()+1) : ""+(pastDate.getMonth()+1) ) + "-" + (pastDate.getDate() < 9 ? "" + pastDate.getDate() : pastDate.getDate() ) + "-" + pastDate.getFullYear();
          
        
        var hiddenScoreboardTypeID = document.getElementById("ScoreboardTypeId");
        
        
        if (cookie_CurrentSelectedDate != currentDateString || currentDateString != yesterDateString){       
            //start the new scores feed
            Scoreboard.UpdateScoreboard(LeagueID, GameDate, Season, 'true', '01-01-1900', PageType,RefreshStartTime,Week, myControlType,sbColumns, myScoreboardName, myScoreboardID, hiddenScoreboardTypeID.value,  UpdateScoreboard_callback);
             
        }
            
    //cookie didn't exist before so scores were turned off original, delete cookie to make sure the updates stay off      
    if (cookie_refresh=="")
    {   
        Delete_Cookie('RefreshScores');
    }       
}

function AutoRefreshOn(LeagueID, GameDate, Season, Week)
{
   //This function is fired when the Scores Update On link is click
    
   // Change the HTML to show the new refresh state
   var RefreshDiv = document.getElementById('scores-note');
   if (RefreshDiv!=null)
   {
        if (displayLanguage == 'fr-CA')
        {
            RefreshDiv.innerHTML = "Les points mises à jour: <a href=\"javascript:AutoRefreshOff('"+LeagueID+"','"+GameDate+"','"+Season+"','"+ Week+"')\">Pas</a> | <strong>Sur</strong>";
        }
        else
        {
            RefreshDiv.innerHTML = "Scores Refresh: <a href=\"javascript:AutoRefreshOff('"+LeagueID+"','"+GameDate+"','"+Season+"','"+ Week+"')\">Off</a> | <strong>On</strong>";
        }
   }
   
   //Start-up the updates again
    Set_Cookie( 'RefreshScores', 'On', '', '/', '', '' ); 
    NewScoresFeed(LeagueID, GameDate, Season, 'Matchups', Week )
}


function AutoRefreshOff(LeagueID, GameDate, Season, Week)
{
    //This function is fired when the Scores Update Off link is click
   Delete_Cookie('RefreshScores', '/', '');

   // Change the HTML to show the new refresh state
   var RefreshDiv = document.getElementById('scores-note');
   if (RefreshDiv!=null)
   {
        if (displayLanguage == 'fr-CA')
        {
            RefreshDiv.innerHTML = "Les points mises à jour: <strong>Pas</strong> | <a href=\"javascript:AutoRefreshOn('"+LeagueID+"','"+GameDate+"','"+Season+"','"+ Week+"')\">Sur</a>"
        }
        else
        {
            RefreshDiv.innerHTML = "Scores Refresh: <strong>Off</strong> | <a href=\"javascript:AutoRefreshOn('"+LeagueID+"','"+GameDate+"','"+Season+"','"+ Week+"')\">On</a>"
        }
   }
}


function UpdateConference( leagueID )
{
    //This function is used to update the conference title when a new option is selected in the ddlb
    var ctlConf = document.getElementById("confSelect");
    var myConf="";
    if (ctlConf != null)
    {   
        myConf = ctlConf.selectedIndex;
        var conferenceTitle = document.getElementById("ConfTitle");
          
        if (conferenceTitle != null)
        {
            var SelValue = ctlConf.options[myConf].text;
            conferenceTitle.innerHTML = SelValue;
        }
        
         //sets the conference cookie
        Delete_Cookie('SelectedConf_'+leagueID);
        myConfValue = ctlConf.value;
        Set_Cookie('SelectedConf_'+leagueID,myConfValue,'','/','','');
    }
    
       
}

function writeHistory(message){ 
     historyDiv = document.getElementById('HolmesBlows'); 
     historyDiv.appendChild(document.createTextNode(message)); 
     historyDiv.appendChild(document.createElement('BR'));  
     historyDiv.scrollTop += 50; 
    }