﻿
var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer(surveyURL)
{
    // Set the length of the timer, in seconds
    secs = 0;
    StopTheClock();
    StartTheTimer(surveyURL);
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer(surveyURL)
{
    if (secs==0)
    {
        StopTheClock()

        var url = '/LightBoxVideo/Survey.aspx?s=' + surveyURL;

        // Open the lightbox (player, url, height, width)
        //openShadowbox('iframe', url, '367', '450');

        // Enlarging the vertical space in the lightbox to account for people using 'grandma-font' sizes. 
        openShadowbox('iframe', url, '435', '450');
    }
    else
    {
        self.status = secs
        secs = secs - 1
		
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function doRandom() 
{
	var randomnumber=Math.floor(Math.random()*20);
	//var randomnumber = 2;
	if (randomnumber == 2)
		return true;
	else
	{
		return false;
	}
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setTime(exdate.getTime()+(expiredays*24*60*60*1000));

    var expires =  "; expires="+exdate.toGMTString();
    document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : expires+"; path=/");
}

function getCookie(c_name)
{
    if (document.cookie.length > 0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }

    return "";
}

function getTraffic(testMode, surveyURL)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate());

    if (testMode == null) {
        testMode = false;
    }

    if (testMode) {
        InitializeTimer(surveyURL);
    }
    else {
        if (doRandom() == true) {
            username = getCookie('name');
            if (username != null && username != "" && username == "dataCookie") {
                //Do nothing, just wait for the cookie to expire
            }
            else {
                //If user cookie expired, or user does not have cookie, then make user view popup
                //setCookie(name, cookieName, #of days untill expire)
                setCookie('name', 'dataCookie', 365);
                InitializeTimer(surveyURL);
            }
        }
    }
}

function deleteCookie()
{
	c_start=document.cookie.indexOf("name=");
	if (unescape(document.cookie.substring(c_start+5,c_start+15)) == 'dataCookie')
	{
		//alert('Going to Delete Cookie');
	    //Delete Cookie
        document.cookie = escape("name=dataCookie;expires=Thu, 01-Jan-70 00:00:01 GMT");
	}
}

function takeSurvey() 
{
    var surveyURL = frames["sb-player"].document.getElementById("hiddenSurveyURL").value;
    var win2 = window.open(surveyURL, "", "width=800,height=710,scrollbars=1,resizable=1,toolbar=0,location=0,menubar=0,status=0,directories=0");
    win2.blur();
    window.focus();
    
    closeShadowbox();
}
