﻿var surveyCookieExpireDays=365;
var surveyShowMilliSeconds = 120 * 1000;
// Check every checkSurveyShowSeconds to see if we have reached the time on site
var checkSurveyShowMilliSeconds = 2 * 1000;
var to;

// Code behind sets these variables
//var surveyUrl="/HVA/Survey.aspx";
//var siteName - used for cookie names

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 setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) +
    "; path=/";
    // Path set to / so same cookie for all directories/pages
}

function checkSurveyShow(parmCookieNameBase, parmSurveyUrl)
{
    var SurveyShowCookieName = parmCookieNameBase + 'SurveyShown';
    var SurveyShowTimeStartCookieName = parmCookieNameBase + 'SurveyShownTimeStart';
    var CheckSurveyShowString = "checkSurveyShow('" + parmCookieNameBase + "','" + parmSurveyUrl + "')";

    // Issues Log:  When survey manually shown, it may come up again from here if we do not test for SurveyShown in here 
    var SurveyShown = getCookie(SurveyShowCookieName);
    if (SurveyShown == "true") {
        clearTimeout(to);
    }
    
    var SurveyShownTimeStart = getCookie(SurveyShowTimeStartCookieName);
    if (SurveyShown != "true" && SurveyShownTimeStart != null && SurveyShownTimeStart != "")
    {
        var currentDate = new Date();
        var MilliSurveyShownTimeStart = Date.parse(SurveyShownTimeStart);
        //alert('checkSurveyShow SurveyShownTimeStart'+SurveyShownTimeStart+' MilliSurveyShownTimeStart '+MilliSurveyShownTimeStart);
        var MilliSecondsSinceStart = Date.parse(currentDate.toString()) - MilliSurveyShownTimeStart;
        //alert('checkSurveyShow MilliSecondsSinceStart '+MilliSecondsSinceStart+' surveyShowMilliSeconds '+surveyShowMilliSeconds);
        if (MilliSecondsSinceStart > surveyShowMilliSeconds)
        {
            setCookie(SurveyShowCookieName, "true", surveyCookieExpireDays);
            clearTimeout(to);
            //alert('showing Survey! '+MilliSecondsSinceStart+' '+surveyShowMilliSeconds);
            
            //function(){
			    //var theURL = $jq(this).attr('href');
			    jQuery('#js-content').append(
				    '<table class="overlay-wrapper" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
					    '<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
						    '<iframe src="'+parmSurveyUrl+'" width="710" height="450" frameborder="0"><\/iframe>'+
					    '<\/td><\/tr><\/table>'+
				    '<\/td><\/tr><\/table>'
			    );
			    jQuery('html').addClass('overlay-active');
			    jQuery('#js-overlay').fadeIn(animspeed);
			    jQuery('#js-content').fadeIn(animspeed);
			//return false;
		    //}
            
        }
        else 
        {
            // If false, check timer and show survey
            //alert('setting to');
            to = setTimeout(CheckSurveyShowString, checkSurveyShowMilliSeconds);
        }
    }
    else 
    {
        var exdate = new Date();
        //alert('date '+exdate.toString());
        setCookie(SurveyShowTimeStartCookieName, exdate.toString(), surveyCookieExpireDays);
        to = setTimeout(CheckSurveyShowString, checkSurveyShowMilliSeconds);
    }
}

function surveyCookieWithParms(parmCookieNameBase, parmSurveyUrl) {

    var SurveyShowCookieName = parmCookieNameBase + 'SurveyShown';
    var SurveyShowTimeStartCookieName = parmCookieNameBase + 'SurveyShownTimeStart';
    var CheckSurveyShowString = "checkSurveyShow('" + parmCookieNameBase + "','" + parmSurveyUrl + "')";

    var SurveyShown = getCookie(SurveyShowCookieName);
    //alert('surveyShownOnInit ' +SurveyShown);
    if (SurveyShown == null || SurveyShown == "" || SurveyShown != "true" )
    {
        //alert('setting SurveyShown to false '+SurveyShown);
        setCookie(SurveyShowCookieName, "false", surveyCookieExpireDays); 
                     
        // If false, check timer and show survey
        to = setTimeout(CheckSurveyShowString, checkSurveyShowMilliSeconds);
        //alert('surveyCookie ' + parmCookieNameBase + ' ' + parmSurveyUrl);

        // Not initial each browser visit since this happened on every page
        var SurveyShowTimeStart = getCookie(SurveyShowTimeStartCookieName);
        if (SurveyShowTimeStart == null || SurveyShowTimeStart == "") {
            var exdate = new Date();
            //alert('date ' + exdate.toString());
            setCookie(SurveyShowTimeStartCookieName, exdate.toString(), surveyCookieExpireDays);
            to = setTimeout(CheckSurveyShowString, checkSurveyShowMilliSeconds);
        }
    }
    else
    {
        //alert('surveyCookie:  Survey Shown'+SurveyShown);
    }
}

function surveyCookie() {
    //alert('site Name ' + siteName + ' ' + surveyUrl);
    return(surveyCookieWithParms(siteName,surveyUrl));
}

function setSurveyCookie() {
    var SurveyShowCookieName = siteName + 'SurveyShown';
    setCookie(SurveyShowCookieName, "true", surveyCookieExpireDays); 
}