// JavaScript Document - Survey Popup

/***********************************************
* Sticky Note script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
* Go to http://www.dynamicdrive.com/ for full source code
***********************************************/

//Specify display mode. 3 possible values are:
//1) "always"- This makes the fade-in box load each time the page is displayed
//2) "oncepersession"- This uses cookies to display the fade-in box only once per browser session
//3) integer (ie: 5)- Finally, you can specify an integer to display the box randomly via a frequency of 1/integer...
//4) "custom" - NEW - This makes the fade-in box load based on a time elapse and page views which ever comes first defaultPageViews and defaultTimeSpan 
// For example, 2 would display the box about (1/2) 50% of the time the page loads.

var displaymode="custom";

var defaultPageViews = 3; //number of page views survey will appear on the defaultPageViews + 1 view 
var defaultTimeSpan = 3; //min

var enablefade="no" //("yes" to enable fade in effect, "no" to disable)
var autohidebox=["no", 30] //Automatically hide box after x seconds? [yes/no, if_yes_hide_after_seconds]
var showonscroll="yes" //Should box remain visible even when user scrolls page? ("yes"/"no)
var IEfadelength=1 //fade in duration for IE, in seconds
var Mozfadedegree=0.1 //fade in degree for NS6+ (number between 0 and 1. Recommended max: 0.2)
var greyoutbackground="no" //"yes" to enable the background greying out, "no" to disable


////////No need to edit beyond here///////////

if (parseInt(displaymode)!=NaN)
var random_num=Math.floor(Math.random()*displaymode)


function fadeBackground() {
  //document.getElementById('opaque').style.display='block'
  var wra = document.getElementById('opaque'), opacity = 0;
  //setOpacity(wra, 0);
  wra.style.display = 'block';
  var int = window.setInterval(function() {
    opacity += 0.05;
    //setOpacity(wra, opacity);
    if (opacity >= 1) window.clearInterval(int);
  }, 50);
}


function displayfadeinbox()
{
	//grey out the background
	if (greyoutbackground == "yes")
		document.getElementById('opaque').style.display='block';	
	
	var ie=document.all && !window.opera
	var dom=document.getElementById
	
	iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
	objref=(dom)? document.getElementById("fadeinbox") : document.all.fadeinbox

	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
	var docwidth=(ie)? iebody.clientWidth : window.innerWidth

	docheight=(ie)? iebody.clientHeight: window.innerHeight
	
	var objwidth=objref.offsetWidth

	objheight=objref.offsetHeight
	
	objref.style.left=docwidth/2-objwidth/2+"px"
	objref.style.top=scroll_top+docheight/2-objheight/2+"px"

	if (showonscroll=="yes")
		showonscrollvar=setInterval("staticfadebox()", 50)

	if (enablefade=="yes" && objref.filters){
		objref.filters[0].duration=IEfadelength
		objref.filters[0].Apply()
		objref.filters[0].Play()
	}
	
	objref.style.visibility="visible"
	
	if (objref.style.MozOpacity){
		if (enablefade=="yes")
			mozfadevar=setInterval("mozfadefx()", 90)
		else{
			objref.style.MozOpacity=1
			controlledhidebox()
		}
	}
	else
		controlledhidebox()
}

function mozfadefx(){
	if (parseFloat(objref.style.MozOpacity)<1)
		objref.style.MozOpacity=parseFloat(objref.style.MozOpacity)+Mozfadedegree
	else{
		clearInterval(mozfadevar)
		controlledhidebox()
	}
}

function staticfadebox(){
	var ie=document.all && !window.opera
	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
	objref.style.top=scroll_top+docheight/2-objheight/2+"px"
}

function hidefadebox(){
	objref.style.visibility="hidden"
	
	if (typeof showonscrollvar!="undefined")
		clearInterval(showonscrollvar)

	if (greyoutbackground == "yes")
		document.getElementById('opaque').style.display='none';	
}

function controlledhidebox(){
	if (autohidebox[0]=="yes"){
		var delayvar=(enablefade=="yes" && objref.filters)? (autohidebox[1]+objref.filters[0].duration)*1000 : autohidebox[1]*1000
		setTimeout("hidefadebox()", delayvar)
	}
}

function p (i)
{
	return Math.floor(i / 10) + "" + i % 10;
}

function trunc (i)
{
	var j = Math.round(i * 100);
	return Math.floor(j / 100) + (j % 100 > 0 ? "." + p(j % 100) : "");
}

function calculate (date1,date2)
{
	var date1 = new Date(date1);
	var date2 = new Date(date2);
	var sec = date2.getTime() - date1.getTime();
	
	if (isNaN(sec))
	{
		alert("Input data is incorrect!");
		return;
	}
	if (sec < 0)
	{
		alert("The second date ocurred earlier than the first one!");
		return;
	}

	var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;

	var numhours = trunc(sec / hour);
	var numminute = trunc(sec / minute);
	var numsecond = trunc(sec / second);

	return numminute;	
}

function testCustomCondition(timespan,pagevisits,survey)
{
	var siteTimeSpan = readCookie(timespan);
	var sitePageVisits = readCookie(pagevisits);
	var siteSurvey = readCookie(survey);
		
	if (siteSurvey == null)
	{
		if (siteTimeSpan == null || sitePageVisits == null)
		{
			//erase all cookies to make sure values are reset
			eraseCookie(timespan);
			eraseCookie(pagevisits);
			eraseCookie(survey);
			
			//creat the cookies needed by the site - set default values.
			var currentTime = new Date()
			createCookie(timespan,currentTime,1);
			createCookie(pagevisits,1,1);
			
			siteTimeSpan = currentTime;
			sitePageVisits = 1;
		}	
		else
		{
			sitePageVisits = parseInt(sitePageVisits) + 1;
			createCookie(pagevisits,sitePageVisits,1);
		}
		
		var timeRightNow = new Date();	
		var visitDuration = calculate(siteTimeSpan,timeRightNow)
	 	
		if (sitePageVisits > defaultPageViews )
		{
			setTimeout("displayfadeinbox()", 1000);
		}
		else
		{
			var showIn = ((defaultTimeSpan - visitDuration)* 60)* 1000 + 1000;
			setTimeout("displayfadeinbox()", showIn);
		}
	}
	else if (siteSurvey == "startSurvey")
	{
		showSurvey('/cms/One.aspx?portalId=1377&pageId=45991','surveySrc');			
	}
}

function surveyVisible()
{
	var screen_id = document.getElementById("surveybox");

	if (screen_id.style.visibility != 'visible') 
	{	
		showSurvey('/cms/One.aspx?portalId=1377&pageId=45991','surveySrc');		
	}
}

function initfunction(){
	if (displaymode=="custom")		
		testCustomCondition("CCOSiteTimeSpan","CCOSitePageVisits","CCOSurvey")
	else
		setTimeout("displayfadeinbox()", 1000);
}

function createCookie(name,value,days,fulldate) {
	if (fulldate) {
		var date = new Date(fulldate);
		var expires = "; expires="+date.toGMTString();		
	}
	else if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
		
function showSurvey(url,frameid)
{
	frames[frameid].location.href = url;
	
	var ie=document.all && !window.opera
	var dom=document.getElementById
	
	iebody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
	objref=(dom)? document.getElementById("surveybox") : document.all.surveybox

	var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset
	var docwidth=(ie)? iebody.clientWidth : window.innerWidth

	docheight=(ie)? iebody.clientHeight: window.innerHeight
	
	var objwidth=objref.offsetWidth;
	
	objheight=objref.offsetHeight;
	
	objref.style.left=docwidth/2-objwidth/2+"px"
	objref.style.top=scroll_top+docheight/2-objheight/2+"px"
		
	screen_id = document.getElementById("surveybox");
	screen_id.style.visibility = 'visible';  // turn off HTML block	
}

function hideSurvey()
{
	screen_id = document.getElementById("surveybox");
	screen_id.style.visibility = 'hidden'; 
}

//var sitehostname = "cmsweb1prd.cco.ccods.cancercare.on.ca";
//if (location.hostname == sitehostname)
//{

if (isSurveyActive)
{
	var todaysDate  = new Date();
	
	if ((todaysDate<surveyExpiryDate) && (displaymode=="oncepersession" && readCookie("fadedin")=="" || displaymode=="always" || parseInt(displaymode)!=NaN && random_num==0 || displaymode=="custom"))
	{
		if (window.addEventListener)
			window.addEventListener("load", initfunction, false)
		else if (window.attachEvent)
			window.attachEvent("onload", initfunction)
		else if (document.getElementById)
			window.onload=initfunction
		
		createCookie("fadedin","yes")
	}
}
//}