function caSurveyGetSurveyVoidedAbility()
{
	var insightCAVoidedAbilityValue = ((getCookie("insightCAVoidedAbility") == null) ? 0 : parseInt(getCookie("insightCAVoidedAbility")));
	return ((insightCAVoidedAbilityValue == 0) ? false : true);
}

function caSurveyInvalidateSurveyAbility()
{
	//NOTE: This cookie should persist for the user's session only
	setCookie("insightCAVoidedAbility", 1, "", "/", "", "");
}

function caSurveyIsInValidPage()
{ // BUSINESS RULE: User must visit at least four bose.ca pages without creating a cart
	//if (location.href.indexOf("/shop/") == -1)
	if (location.href.indexOf("TO_CART_EVENT") == -1)
    { // As long as we did not create a cart
    	return true;
    }
    else
    { // Otherwise we are detecting being on a shop page, invalidate survey for session
		caSurveyInvalidateSurveyAbility();
        return false;
    }
}

function caSurveySetPageViewCount()
{
	if (caSurveyIsInValidPage())
	{
		//NOTE: This cookie should persist for the user's session only
		setCookie("insightCA", ((getCookie("insightCA") == null) ? 1 : parseInt(getCookie("insightCA")) + 1), "", "/", "", "");
	}
}

function caSurveyGetPageViewCount() 
{
	return ((getCookie("insightCA") == null) ? 0 : parseInt(getCookie("insightCA")));
}

function caSurveyIsPageViewCountMet() 
{  // BUSINESS RULE: User must visit at least four bose.ca pages without creating a cart
	return ((parseInt(caSurveyGetPageViewCount()) >= 3) ? true : false);
}

function caSurveyShowSurvey() 
{
	var strEnglishSurveyURL = "https://www.consumerinsightsforum.com/R.aspx?a=256";
	var strFrenchSurveyURL = "https://www.consumerinsightsforum.com/R.aspx?a=264";
	var strSurveyURL = strEnglishSurveyURL;
    var strWinName = "InsightCASurvey";
    var strWinOptions = "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,height=680,width=800";
    var iSamplePercentage = 0; // Sampling percentage... ie: 0 = none, 100 = always

	// If we detect French version of CA site, alternate survey URL
	if (location.href.indexOf("/enfr/") > 0) strSurveyURL = strFrenchSurveyURL;

    // Henerate a random integer from 0-99
    var ranNum = Math.floor(Math.random()*100);

    if (ranNum < iSamplePercentage) 
	{
        // NOTE: survey window is a popunder!
        var objInsightCAPopWin = window.open(strSurveyURL, strWinName, strWinOptions);

        // If a pop-up blocker is on, the window is not created and referencing objInsightCAPopWin throws an error.
        if (!objInsightCAPopWin) // If objInsightCAPopWin does not exist set the Survey Show cookie and exit the function
        {        
	        caSurveySetSurveyShownCookie();
	        return;
        }
        else
		{
			objInsightCAPopWin.blur();
			window.focus();
			caSurveySetSurveyShownCookie();       
		}
    }
	else
	{
		caSurveyInvalidateSurveyAbility();
	}
}

function caSurveyProcessInsightSurvey() 
{
    //OCG BUSINESS RULE: Do NOT show survey if ANY survey has been served to this user within one year.
    //NOTE: ALL surveys now use a generic survey shown cookie that persists for 365 days.

	caSurveySetPageViewCount();

	if ((caSurveyIsInValidPage() == true) && (caSurveyGetSurveyVoidedAbility() == false))
	{	
		// CA SURVEY SWITCH
		var blnRunCASurvey = true;
		var blnPageViewCountMet = caSurveyIsPageViewCountMet();
		var blnSurveyShown = caSurveyIsSurveyShown();
	
		if (blnRunCASurvey && (!blnSurveyShown) && blnPageViewCountMet) 
		{
			caSurveyShowSurvey();
		}
	}
}

function caSurveyIsSurveyShown() 
{
    var blnSurveyShown = false;
    var surveyShownCookieName = "insightCASurveyShown";

    if (getCookie(surveyShownCookieName) != null) blnSurveyShown = true;

	return blnSurveyShown;
}

function caSurveySetSurveyShownCookie() 
{
    // BUSINESS RULE: Do NOT show this survey if survey has been served to user within one year.
    // NOTE: This survey will be treated seperately from any other survey on US/CA.
    
    //lifespan = one year (expressed in milliseconds)
    var lifeSpan = 365 * 24 * 60 * 60 * 1000;
    
    //get current time in milliseconds
    var now = new Date();
    now.setTime(now.getTime());
    
    //set expiration date
    var expiryDate = new Date(now.getTime()+lifeSpan);
    
    setCookie("insightCASurveyShown", "true", expiryDate, "/", "", "");
}