﻿/*
Please use ONLY closed-style commenting (like this one)
Don't use the open-ended two-slashes method
*/
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
var strWebtrendsSite = "Redesign2006"; 
var strCustomWTParams = "";
var strMainSection = "";
var strSubSection = "";
var str3rdLevel = "";
var strTvgServerName = "";


/* these were moved from the user control to prevent errors */
var gImages = new Array;
var gIndex = 0;
var DCS = new Object();
var WT = new Object();
var DCSext = new Object();
var gDomain = "sdc.tvguide.com";
var gDcsId = "";

var strSiteHostName = document.location.hostname.toLowerCase();

/* var gcCountImage = null; */

/* necessary domain setting !!! */
try
{
    switch(navigator.appName.toLowerCase())
    {
        case "netscape" :
            break;
        default:
            setDocDomain();
            break;
    }
}
catch(err)
{
}  

function setDocDomain()
{
    var domParts = document.domain.split('.');
    
    switch(domParts.length)
    {
        case 0:
            break;
        case 1:
            break;
        default:
            document.domain = domParts[domParts.length-2] + '.' + domParts[domParts.length-1];
            break;
    }
}

function collectWebtrendsData()
{
	/* deprecated stub function */ 
}

function dcsVar()
{
	/* deprecated stub function */ 
}

function makeNVP(N,V)
{
	/* deprecated stub function */ 
}

function dcsEscape(S)
{
	/* deprecated stub function */ 
}

function dcsCreateImage(dcsSrc)
{
	/* deprecated stub function */ 
}

function dcsTag()
{
	/* deprecated stub function */ 
}

function getWebtrendsBase()
{
	/* deprecated stub function */ 
}

function executeGridCounter(strDcsImgUrl)
{
	/* deprecated stub function */ 
}

function countHPGridClick(strLocation)
{
	/* deprecated stub function */ 
}    

function countGridAction(strAction, strValue)
{ 
    try {
		countOmniGridAction(strAction, strValue);
    } catch(e) { }
}

function countFlyover(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strAction)
{    
    try {
		countOmniFlyover(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strAction);
    } catch(e) { }
	srCountGridEvent(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strAction);
}

function countFlyoverEx(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strAction, iObjectID)
{    
	/* 2011-02-22 - using this one now */
    try {
		countOmniFlyover(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strAction);
    } catch(e) { }

	countTRAXCloseupView(strTitle, iObjectID, iProgramID);

    /* Re-Enabled by out by DAN 2010-02-01 */
	srCountGridEvent(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strAction);
}

function countGridPrefs(strPrefs)
{  
    try {
		countOmniGridPrefs(strPrefs);
    } catch(e) { }
}

function countGridView(strViewType, strFilter)
{    
	/* deprecated stub function */ 
}

function countListingsSetup(strPageDesc)
{    
	/* deprecated stub function */ 
}

function countDetailPage(strObjName, strObjType)
{
	/* deprecated stub function */ 
}

function countGridViewEx(strViewType, strFilter, strProviderType, nChannels, nMinutes, iAjaxLoadTime, iAjaxRender, iAjaxBytes)
{
    try {
		countOmniGridViewEx(strViewType, strFilter, strProviderType, nChannels, nMinutes, iAjaxLoadTime, iAjaxRender, iAjaxBytes);
    } catch(e){}
    
	if(isErrorPage())
	{ 
		countTraxErrorPage();
		return; 
	}

	collectTraxSL2GridView();
	    
    nChannels = parseInt(nChannels,10);
    var strChans = "";
    
    if(nChannels<=20)
    {
        strChans = "1-20"; 
    }
    else if(nChannels<=50)
    {
        strChans = "21-50"; 
    }
    else if(nChannels<=100)
    {
        strChans = "51-100"; 
    }
    else if(nChannels<=150)
    {
        strChans = "101-150"; 
    }
    else if(nChannels<=200)
    {
        strChans = "151-200"; 
    }
    else if(nChannels<=250)
    {
        strChans = "201-250"; 
    }
    else if(nChannels<=300)
    {
        strChans = "251-300"; 
    }
    else if(nChannels<=400)
    {
        strChans = "301-400"; 
    }
    else if(nChannels<=500)
    {
        strChans = "401-500"; 
    }
    else if(nChannels<=1000)
    {
        strChans = "501-1000"; 
    }
    else
    {
        strChans = "1001-xxxx"; 
    }
    
    var strTrax = getTraxUrlBase('listings');    
    /* pass back grid start time (local) */
    strTrax += "&gdt=" + escape(getGridTime());
    /* pass back grid duration (minutes) */
    strTrax += "&gmn=" + iGridSpanMinutes;
    /* number of channels */
    strTrax += "&chs=" + nChannels;
    /* number of programs */
    strTrax += "&prg=" + objRawData.length;
    /* size in bytes */
    strTrax += "&byt=" + iResponseBytes;
    /* load in milliseconds */
    strTrax += "&mil=" + iAjaxMillisecs;
    /* data server */
    strTrax += "&srv=" + strDataServer;
    /* number of grid ads */
    /* strTrax += "&ads=" + iGridAds; */
    /* cache buster */    
    strTrax += "&cb=" + parseInt(Math.random()*10000000,10);
    /* make the call back */
    loadImageURL(strTrax);
}

function isErrorPage()
{
	try {
		var str = document.title;
		if(str.match(/page not found/i)!=null) { return true; }
	} 
	catch(e) { }

	try {
		if(typeof(tvgPageStatus)!="undefined")
		{
			tvgPageStatus = parseInt(tvgPageStatus, 10);
			
			switch(tvgPageStatus)
			{
				case 404: return true;
				case 500: return true;
			}
		}
	}
	catch(e) { }
	
	return false;
}

function countTraxPage()
{
	if(isErrorPage())
	{ 
		countTraxErrorPage();
		return; 
	}	
	var traxSection = getTraxSection();
	if(traxSection.length<=0) { return; }
	var strTrax = getTraxUrlBase(traxSection);
	strTrax +=  getTraxContentParams();
	strTrax +=  getTraxObjectParams();
	strTrax += getTraxReferrer();
    loadImageURL(strTrax);
}

function countTraxCheckIn(iObjID, iProgID)
{
	if(isErrorPage())
	{ 
		countTraxErrorPage();
		return; 
	}
	var strTrax = getTraxUrlBase("");
	strTrax = strTrax.replace("trax.gif","checkin.gif");
	strTrax += "&chkinObjID=" + iObjID;
	strTrax += "&chkinProgID=" + iProgID;
    loadImageURL(strTrax);	
}

function countTraxErrorPage()
{
    var strTrax = "http://trax.tvguide.com/error.gif";
    strTrax += "?uid=" + readCookie("TVGID"); 
    strTrax += "&sect=" + getTraxSection();  

    var tmp = readCookie("serviceid");
    if(tmp===null) {tmp = "";}
    strTrax += "&sid=" + tmp;
    
	tmp = readCookie("zip");    
    if(tmp===null) {tmp = "";}
    strTrax += "&zip=" + tmp;

	try {
		strTrax += "&error=" + tvgPageStatus;
	}
	catch(e) { 
		strTrax += "&error=n/a";
	}	
	
	strTrax += "&badpage=" + document.location.pathname;
	
	/*
	try {
		tmp = document.location.search;
		if(tmp.length>0){strTrax + "?" + tmp;}
	}
	catch(e) { }
	*/
	
    loadImageURL(strTrax);	
}


function getTraxUrlBase(strSection)
{
    var strTrax = "http://trax.tvguide.com/trax.gif";
    /* pass tvgid */
    strTrax += "?uid=" + readCookie("TVGID"); 
    strTrax += "&sect=" + strSection;  
    /* pass back service ID and zip code */
    var strID = readCookie("serviceid");
    if(strID===null){strID = 80001;}
    strTrax += "&sid=" + strID + "&zip=" + readCookie("zip");
	/* is the user logged in ? */
	if(readCookie("MAPCookie")) { strTrax += "&login=1"; }
	else { strTrax += "&login=0"; } 
	/* new trax ad counter */
	try { strTrax += "&ads=" + traxAdCount; } catch(e){}	
	/* pass back string */
	return strTrax;       
}

function getTraxSection()
{
	/* process host names first, for sports, movies, and ovg */
	if(strSiteHostName.indexOf('sports')>=0) { return "sports"; }
	if(strSiteHostName.indexOf('movies')>=0) { return "movies"; }
	if(strSiteHostName.indexOf('video')>=0)  { return "video"; }
	/* extract relative path */
	var strRelativePath = document.location.pathname.toLowerCase();	
	/* special conditions for home page */
	if((strRelativePath=='\/home.aspx')||(strRelativePath=='\/')) { return "homepage"; }
	/* regex extract the first-level folder */
	var str = strRelativePath.match(/^\/([^\/]+)/);
	/* return the foldername if found */
	if(str.length>=2) { return str[1]; }		
	/* catch-all: return 'other' */
	return "other";
}

function getTraxObjectParams()
{
	try 
	{
		var strTrax = "&objid=" + iTvObjectID;
		strTrax += "&obtyp=" + strTvObjectType;
		return strTrax;
	}
	catch(e) { }
	return "";
}

function getTraxReferrer()
{
	try 
	{
		var strRefer = document.referrer;
		if(strRefer.length>1) 
		{
			strRefer = strRefer.toLowerCase().replace(/^http\:\/\//i,"");
			var i = strRefer.indexOf("/");
			if(i > 0) {
				return "&refer=" + strRefer.substring(0,i);				
			}
			return "&refer=" + strRefer;
		}
	}
	catch(e) { }
	return "";
}

function getTraxContentParams()
{
	var strRelativePath = document.location.pathname;
	var rgx = /\-(\d+)\/(\d+)\.aspx$/i;
	var mth = rgx.exec(strRelativePath);
	if(mth) { return ("&cid=" + mth[1] + "&idx=" + mth[2]); }	
	rgx = /(\d+)\.aspx$/i;
	mth = rgx.exec(strRelativePath);
	if(mth) { return ("&cid=" + mth[1]); }
	return "";
}

function collectTraxSL2GridView()
{
    var strID = readCookie("serviceid");
    if(strID===null){strID = 80001;}

    var query  = "http://trax.tvguide.com/syndicated.gif";    
    query += "?partner=0&profile=0&width=760&height=1050";
	query += "&srvid=" + strID;
    query += "&uid=" + readCookie("TVGID");
    query += "&doc=" + document.location.pathname;

    /* safely attempt referrer access */
    try { query += "&refer=" + document.referrer;
	} catch(e) {  }
        	
	var img = new Image();
	img.src = query;
	var dmy = img.width + img.height;
}


function countTRAXCloseupView(strProgTitle, piObjectID, piProgramID)
{
	var query = "http://trax.tvguide.com/closeup.gif";

	query += "?partner=0";
	query += "&profile=0";
	query += "&objid=" + piObjectID;
	query += "&progid=" + piProgramID;
	query += "&cutype=inline";
    query += "&uid=" + readCookie("TVGID"); 
	query += "&title=" + escape(strProgTitle);

	var img = new Image();
	img.src = query;
	var dmy = img.width + img.height;
}


function getGridTime()
{
    /* date and time of grid shown LOCAL TIME!!! */
    var strDate = dtGridLocalStartTime.getFullYear();    
    /* months are 0 based */
    if(dtGridLocalStartTime.getMonth()<9) { strDate += "0"; }
    strDate += (dtGridLocalStartTime.getMonth()+1);
    /* dates are 1 based */
    if(dtGridLocalStartTime.getDate()<10) { strDate += "0"; }
    strDate += dtGridLocalStartTime.getDate();
    /* add the 24 hour time */
    if(dtGridLocalStartTime.getHours()<10) { strDate += "0"; }
    strDate += dtGridLocalStartTime.getHours();
    /* add minutes */
    if(dtGridLocalStartTime.getMinutes()<10) { strDate += "0"; }
    strDate += dtGridLocalStartTime.getMinutes();
    return strDate;
}

function getFavChannels()
{
    var fvch = "";
    try
    {
        if(arSelectedChannels)
        {
            if(arSelectedChannels.length>0)
            {
                for(var fv=0; fv<arSelectedChannels.length; fv++)
                {
                    if(fv>0) { fvch+=";"; }                    
                    fvch += arSelectedChannels[fv];
                }
                fvch = "&GRID.favchan=" + fvch;
            }
        }
    }
    catch(e)
    {
        return "";
    }
    return fvch;   
}

var srImage;

function getSmartRecUrlBase()
{
    var srURL = "http://trax.tvguide.com/clickstream.gif?UserID=";    
    srURL += readCookie("TVGID");
    return srURL;
}

function srCountGridEvent(strCat, strSubCat, strTitle, iProgramID, iSeriesID, strUsageType)
{
    var srURL = getSmartRecUrlBase();

	/* 2-1-2010 added service ID to the data */
	var strSID = readCookie("ServiceID");
	if(strSID){srURL+=("&ServiceID="+strSID);}
	else {srURL+="&ServiceID=0";}
        
    srURL += "&SeriesID=" + iSeriesID;
    srURL += "&ProgramID=" + iProgramID;
    /* srURL += "&ProgramCategory=" + escape(strCat); */
    /* srURL += "&ProgramSubCategory=" + escape(strSubCat); */
    /* srURL += "&ProgramTitle=" + escape(strTitle); */
    srURL += "&UsageDataType=" + strUsageType;
    srURL += "&Locale=en_us";
    
    callSmartRecs(srURL);       
}

function srAddFav(strRetURL, strUsageType)
{
    var srURL = getSmartRecUrlBase();
    if(strTvObjectName.length>0)
    {
        if(strTvObjectType!="celebrity")
        {
            srURL += "&TvObjectID=" + iTvObjectID;
            srURL += "&TvObjectType=" + escape(strTvObjectType);
            srURL += "&TvObjectName=" + escape(strTvObjectName);
            srURL += "&UsageDataType=" + strUsageType;
            callSmartRecs(srURL);
        }
    } 
    document.location = strRetURL;  
}

function loadImageURL(srURL)
{
	srImage = new Image();
	srImage.src = srURL;
	var xS = srImage.width;
	var yS = srImage.height;
}


function callSmartRecs(srURL)
{
	srImage = new Image();
	srImage.src = srURL;
	var xS = srImage.width;
	var yS = srImage.height;
}

function executeSiteCounter(strDcsImgUrl, strSite, strProfile)
{
	/* deprecated stub function */ 
}

function countSportsGuidePage(strSport, strLeague, strKeyword, strHdFilter, strEventFilter)
{
	omni_countSportsGuidePage(strSport, strLeague, strKeyword, strHdFilter, strEventFilter);
}
