/* ------------------------------ Daily Listings ------------------------------ */


function FetchFullListings( pDate )
{
    // Concatenate the URL of the script we need to hit
 /* The first URL shuould be filled in with the www.sitename.tdl URL
  * The second URL should be filled in with the sitename.tdl (no www.) URL
  * so the script can hit either without generating access errors */
    var fullListingsURL = "/universalInclude/code/modules/programListingsExtraction/ProgramListingsExtractor.asp?date=" + pDate + "&table=achannelvictorialistingsVerified&site=citytv&eventid=16&hdtv=0";
    var fullListingsURL2 = "/universalInclude/code/modules/programListingsExtraction/ProgramListingsExtractor.asp?date=" + pDate + "&table=achannelvictorialistingsVerified&site=citytv&eventid=16&hdtv=0";

    // Check to see which listings URL we should be using
    var usedListingsURL = fullListingsURL;
    if ( location.hostname.indexOf( "www" ) == -1 )
    { usedListingsURL = fullListingsURL2; }

    // Instantiate the Ajax Executor and have it get the data from the other end
    var fullListingsAjaxExec = new AjaxExecutor( usedListingsURL, DisplayFullListings  );
}


// When the AJAX call is done it returns the data here for display
function DisplayFullListings( pFullListingsData )
{
    fullListingData = "Program listings are not available at this time.";
    if ( pFullListingsData != "" )
    { fullListingData = pFullListingsData; }
    document.getElementById( "listings-block" ).innerHTML = fullListingData;
}


// Returns the date used to get the listings. If no date is passed in via the querystring then today is used
function GetListingsDate()
{
    // Default date is the one to use if one wasn't passed in the querystring
    var theToday = new Date();
    var defaultDate = ( theToday.getMonth() + 1 ) + "-" + theToday.getDate() + "-" + theToday.getFullYear();
        
    var queryStr = new Querystring();
    var theDateStr = queryStr.get( "date", defaultDate );
        
    return theDateStr;
}


/* ------------------------------ Whats On Now ------------------------------ */


// Executes an AJAX call to get the latest Whats On data
function FetchWhatsOnNow()
{
    // Concatenate the URL of the script we need to hit
    var fullWhatsOnURL= "/universalInclude/code/modules/programListingsExtraction/WhatsOnNowExtractor.asp?table=achannelvictorialistingsVerified&offset=-3&hdtv=0";
    // Instantiate the Ajax Executor and have it get the data from the other end
    var whatsOnAjaxExec = new AjaxExecutor( fullWhatsOnURL, DisplayWhatsOnNow );
}


// When the AJAX call is done it calls back to here and returns the data for display
function DisplayWhatsOnNow( pWhatsOnData )
{
    whatsOnData = "Program listings are not available at this time.";
    if ( pWhatsOnData != "" )
    { whatsOnData = pWhatsOnData; }
    document.getElementById( "whatson-block" ).innerHTML = whatsOnData;
}


/* ------------------------------ Prime Time Listings ------------------------------ */


// Executes an AJAX call to get the latest Prime Time Listings
function FetchPrimeTimeListings()
{
    // Concatenate the URL of the script we need to hit
    var fullPrimeTimeListingsURL= "/universalInclude/code/modules/programListingsExtraction/PrimeTimeListingExtractor.asp?table=achannelvictorialistingsVerified&hdtv=0";

    // Instantiate the Ajax Executor and have it get the data from the other end
    var primeTimeListingsAjaxExec = new AjaxExecutor( fullPrimeTimeListingsURL, DisplayPrimeTimeListings );
}


// When the AJAX call is done it calls back to here and returns the data for display
function DisplayPrimeTimeListings ( pPrimeTimeListingsData )
{
    primeTimeListings= "Program listings are not available at this time.";
    if ( pPrimeTimeListingsData != "" )
    { primeTimeListings= pPrimeTimeListingsData ; }
    document.getElementById( "primetime-block" ).innerHTML = primeTimeListings;
}


/* ------------------------------ Date functions ------------------------------ */
    

function DisplayListingsDate( pElementName, pDate )
{
    if ( pElementName != "" )
    {
        // Display the formatted date of the data we're fetching
        var months = new makeArray('January','February','March',
            'April','May','June','July','August','September',
            'October','November','December');
    
        var theDate = GetRealDateFromQueryStringDate( pDate );
        var day = theDate.getDate();
        var month = theDate.getMonth() + 1;
        var yy = theDate.getYear();
        var year = (yy < 1000) ? yy + 1900 : yy;
        document.getElementById( pElementName ).innerHTML = months[month] + " " + day + ", " + year;
    }
}


// It is expected that the date will be passed via the querystring in format: mm-dd-yyyy
function GetRealDateFromQueryStringDate( pDate )
{
    var dateArray = pDate.split( "-" );
    var theDate = new Date( dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2] );
    return theDate;
}


/* ------------------------------ Toolbox functions ------------------------------ */


// Takes a bunch of comma-separated cruft and returns the items as an array
function makeArray() {
     for (i = 0; i<makeArray.arguments.length; i++)
          this[i + 1] = makeArray.arguments[i];
}