//-- Foviance Survey Integration													 --//
//-- Copyright Foviance 2011																		 --//
//-- Please contact info@foviance.com for more information.	 --//

//---- Essential Settings ----//

//1. Change this to the relevant Survey details
var SurveyBaseURL = "https://ecustomeropinions.com/survey/survey.php?sid=374316350&data="+escape(document.URL);

//2. Display Survey as a Layer? Setting to false, will result in only a link being shown
var DisplayMethod = "layer"; //Options are: layer, corner-TL, corner-TR, corner-BL, corner-BR, link
var popUnderSurvey = true;

//3. Survey Layer Text (only used if DisplayLayer = true)
var SurveyTitle = "We need your help...";
var SurveyDescription = "<p>Please help us continue to improve this website by taking part in our survey, which should take no longer than 15 minutes to complete.</p><p>Simply click the 'Yes, please' button below and the survey will open in a new window (behind this one) ready for you to complete at the end of your visit.<p/><p>Please do not complete the survey until you have finished using the website.</p>";
var SurveyNoText = "&nbsp;&nbsp;No, thanks&nbsp;&nbsp;";
var SurveyYesText = "&nbsp;Yes, please&nbsp;";
var TopOffset = 180;
var maxWidth = 360;

//4. Survey Sample details
var MaxSurveyDisplayCount = 1;
var ApproxSampleSizePercent = 0.00;
var PageLevelSampling = true;
var PageLevelMatches = ["/player/p/lotterydrawgames/euromillions.ftl","/player/p/results/euromillions.ftl","/player/p/home.ftl","/player/p/instantwins.ftl","/player/p/lotterydrawgames/lotto.ftl","/player/p/results/lotto.ftl"];
var PageLevelSampleSize = [0.32,0.40,0.10,0.28,0.28,0.32];
//Note: Add 'SurveyReset=true' to the URL to reset the display count
//Note: Add 'SurveyTest=true' to the URL to test the display


//---- Advanced Settings ----//

//5. Set Site Domain + Cookie Duration
var cookieDomain = "";
var cookieLife = 60;

//6. Define the WebAnalytics Cookie ID for data integration.
var wa_cookieID = "SIVISITOR";


//7. Capture Usage Statistics
var CaptureDisplayStats = false;

//---- DO NOT EDIT BELOW THIS LINE ----//
var LinkID = readCookie(wa_cookieID);

if(document.URL.indexOf('SurveyReset=true')>-1)
	createCookie('SurveyDisplayCount',-1,cookieLife);
	
if(document.URL.indexOf('DisplayMethod=layer')>-1)
	DisplayMethod="layer";
if(document.URL.indexOf('DisplayMethod=corner-BR')>-1)
	DisplayMethod="corner-BR";
if(document.URL.indexOf('DisplayMethod=corner-BL')>-1)
	DisplayMethod="corner-BL";
if(document.URL.indexOf('DisplayMethod=corner-TR')>-1)
	DisplayMethod="corner-TR";
if(document.URL.indexOf('DisplayMethod=corner-TL')>-1)
	DisplayMethod="corner-TL";
if(document.URL.indexOf('DisplayMethod=link')>-1)
	DisplayMethod="link";
	
function createCookie(name,value,days) { 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=/"; }

var RandomSeed = Math.random()*100;
var sampleSize = ApproxSampleSizePercent;

if(PageLevelSampling){
	for(i=0;i<PageLevelMatches.length;i++){
		if(PageLevelMatches[i] == document.location.pathname){
			sampleSize = PageLevelSampleSize[i];
		}
	}
}

if (parseFloat(RandomSeed.toFixed(2)) < parseFloat(sampleSize.toFixed(2)))
	var ShowSurveyFlag = true;
else
	var ShowSurveyFlag = false;
	
	//alert(document.location+"\n\nApproxSampleSizePercent: " + ApproxSampleSizePercent + "\n:sampleSize: " + sampleSize + "\nShowSurveyFlag: " +ShowSurveyFlag);

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 yesToSurvey(){
	
	var thisSurveyURL = SurveyBaseURL+"&data2="+LinkID;
	window.open(thisSurveyURL,'CustomerFeedback','width=825,height=520,location=no,resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no,copyhistory=yes')
	createCookie('SurveyDisplayCount',MaxSurveyDisplayCount,cookieLife);

	if(DisplayMethod== "layer"){
		document.getElementById('SurveyLayer').style.display='none';
		document.getElementById('SurveyLayerBackground').style.display='none';
	
		if(popUnderSurvey)
			self.focus();
	}
	
	if(CaptureDisplayStats){
		var feedbackForm_request = new Image();
		feedbackForm_requestString = "http://tag1.webabacus.com/surveyFeedback.gif?pageURL=" + escape(document.URL) + "&clientID="+LinkID + "&surveyFeedback=Accepted&DisplayMethod=" + DisplayMethod;
		feedbackForm_request.src = feedbackForm_requestString;
	}
}

function noToSurvey(){
	var DateNow = new Date();
	cexp = new Date(DateNow.getTime() + cookieLife);
	var content = document.getElementById('SurveyLayer');
	createCookie('SurveyDisplayCount',MaxSurveyDisplayCount,cookieLife);
	if(DisplayMethod== "layer"){
		document.getElementById('SurveyLayer').style.display='none';
		document.getElementById('SurveyLayerBackground').style.display='none';
	}
	if(CaptureDisplayStats){
		var feedbackForm_request = new Image();
		feedbackForm_requestString = "http://tag1.webabacus.com/surveyFeedback.gif?pageURL=" + escape(document.URL) + "&clientID="+LinkID + "&surveyFeedback=Declined&DisplayMethod=" + DisplayMethod;
		feedbackForm_request.src = feedbackForm_requestString;
	}
}

function updateTabIndex(){
	document.getElementById('SurveyClose').tabIndex = 1;
	document.getElementById('SurveyYesText').tabIndex = 2;
	document.getElementById('SurveyNoText').tabIndex = 3;
	document.getElementById('SurveyNoText').focus();
}

function updateLayerPosition(){
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	var dsocheight=document.all? iebody.clientHeight : window.innerHeight;
	var dsocwidth=document.all? iebody.clientWidth : window.innerWidth;

	SurveyLayerBG.style.height = ((dsoctop>1000) ? dsocheight + 1000 : dsocheight + dsoctop) + 'px';
	SurveyLayerBG.style.width  = '100%';

	var winW = document.all ? document.body.offsetWidth : window.innerWidth;
	var winH = document.all ? document.body.offsetHeight : window.innerHeight;

	SurveyLayer.style.position = 'Absolute';
	SurveyLayer.style.width = maxWidth + 'px';
	wide = (winW - maxWidth) / 2;
	SurveyLayer.style.left = wide + 'px';
	SurveyLayer.style.top = TopOffset + 'px';
}

function launchSite(siteURL){
	window.open(siteURL,'WebSite','width=1024,height=450,location=yes,resizable=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,copyhistory=yes');	
}

if(((document.URL.indexOf('SurveyTest=true')>-1)||((readCookie('SurveyDisplayCount') < MaxSurveyDisplayCount) && (ShowSurveyFlag))) && (DisplayMethod == "layer")){
	
	
	
		
	//Write out Style sheet
	document.writeln('<link type="text/css" media="screen" href="/player/i/survey/national-lottery_survey_layer.css" rel="stylesheet" />');
	
	//Write out DIV layers
	document.writeln('<div id="SurveyLayerBackground" onClick="noToSurvey();"></div>');
	document.writeln('<div id="SurveyLayer">');
	document.writeln('<a href="javascript:noToSurvey();" id="SurveyClose" tabindex="1">X</a>');
	document.writeln('<div id="SurveyTitle">' + SurveyTitle + '</div>');
	document.writeln('<div id="SurveyDescription">' + SurveyDescription + '</div>');
	document.writeln('<div id="SurveyButtons">');
	document.writeln('<a href="javascript:yesToSurvey();" id="SurveyYesText" tabindex="2">' + SurveyYesText + '</a>');
	document.writeln('<a href="javascript:noToSurvey();" id="SurveyNoText" tabindex="3">' + SurveyNoText + '</a></div>');
	document.writeln('<div id="SurveyFooter">&nbsp;</div>');
	document.writeln('</div>');
	
	

//Set Survey layer positioning
	var SurveyLayerBG = document.getElementById('SurveyLayerBackground');
	var SurveyLayer = document.getElementById('SurveyLayer');
	
	
// Set Tab Index for tab navigation. Also allows you to overvide current 'on page' tab sequence.

	addLoadEvent(function() {
		/* more code to run on page load */
		//document.getElementById('SurveyClose').focus();
		setTimeout("updateTabIndex();", 1000);
	});
	
	
		
//This automatically centres the Survey Layer
	var winW = document.all ? document.body.offsetWidth : window.innerWidth;
	var winH = document.all ? document.body.offsetHeight : window.innerHeight;
  
	SurveyLayer.style.position = 'Absolute';
	SurveyLayer.style.width = maxWidth + 'px';
	wide = (winW - maxWidth) / 2;
	SurveyLayer.style.left = wide + 'px';
	SurveyLayer.style.top = TopOffset + 'px';
  

//**** Set Survey Style options ****

	//Survey Layer Background
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	var dsocheight=document.all? iebody.clientHeight : window.innerHeight;
	var dsocwidth=document.all? iebody.clientWidth : window.innerWidth;
	SurveyLayerBG.style.height = ((dsoctop>750) ? dsocheight + 750 : dsocheight + dsoctop) + 'px';
	SurveyLayerBG.style.width  = ((dsocleft>100) ? dsocwidth + 250 : dsocwidth + dsocleft) + 'px';
			
	//Set 'SurveyDisplayCount' cookie so that Survey box is only displayed once.
	var SurveyDisplayCount = Math.floor(readCookie('SurveyDisplayCount')) + 1;
	createCookie('SurveyDisplayCount',SurveyDisplayCount,cookieLife);
	
	window.onscroll=function() {
		
		var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
		var dsoctop=document.all? iebody.scrollTop : pageYOffset;
		var dsocheight=document.all? iebody.clientHeight : window.innerHeight;
		var dsocwidth=document.all? iebody.clientWidth : window.innerWidth;

		SurveyLayerBG.style.height = ((dsoctop>750) ? dsocheight + 750 : dsocheight + dsoctop) + 'px';
		SurveyLayerBG.style.width  = ((document.all) ? (dsocwidth + dsocleft) : (dsocwidth + dsocleft)-17) + 'px';
		
		SurveyLayer.style.left = wide + 'px';
		SurveyLayer.style.top = dsoctop + TopOffset + 'px';
	}
	
	$(window).resize(function() {
		var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
		var dsoctop=document.all? iebody.scrollTop : pageYOffset;
		var dsocheight=document.all? iebody.clientHeight : window.innerHeight;
		var dsocwidth=document.all? iebody.clientWidth : window.innerWidth;

		SurveyLayerBG.style.height = ((dsoctop>1000) ? dsocheight + 1000 : dsocheight + dsoctop) + 'px';
		SurveyLayerBG.style.width  = '100%';
	
		var winW = document.all ? document.body.offsetWidth : window.innerWidth;
		var winH = document.all ? document.body.offsetHeight : window.innerHeight;
  
		SurveyLayer.style.position = 'Absolute';
		SurveyLayer.style.width = maxWidth + 'px';
		wide = (winW - maxWidth) / 2;
		SurveyLayer.style.left = wide + 'px';
		SurveyLayer.style.top = TopOffset + 'px';
	});
	
		
	document.onkeydown = function(evt) {
		evt = evt || window.event;
		if (evt.keyCode == 27) {
			noToSurvey();
		}
		if (evt.charCode == 27) {
			noToSurvey();
		}
	}
	
	if(CaptureDisplayStats){
		var feedbackForm_request = new Image();
		feedbackForm_requestString = "http://tag1.webabacus.com/surveyFeedback.gif?pageURL=" + escape(document.URL) + "&clientID="+LinkID + "&surveyFeedback=InviteDisplayed&DisplayMethod=" + DisplayMethod;
		feedbackForm_request.src = feedbackForm_requestString;
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

