//  080709 Philipp
// any questions please contact me on philipp.krasser@globalpark.com
// define the variables we need
 var survey_cookie = "surveycookie1";  // name of the cookie
 var when_to_pop = 60000;             // time after which the pop-up should be displayed (in milli seconds) so 60000 = 1 min
 var how_long = 7;                    // definies the days of how long the cookie should be stored on the users machine
 var who_sees = 0.30;                  // define the probability of how many visitors will see the pop-up 0.34 = 1/3; 0.5 = 1/2,...
 var where_to_go ="https://efs.globalpark.co.uk/uc/main/7e80/"; // defines the location URL of the survey 
 var open_like ="_blank";  // how to open the link html
 var open_dimensions = "width=650,height=550,location=no,resizable=yes,menubar=no,toolbar=no,scrollbars=yes"; // define the window attributes
 var layer_id_name = "surveypop";  // id name of the pop-up layer

//////////******************** DO NOT CHANGE ANYTHING BELOW THIS LINE **********************************//////////////////////////
//////////******************** DO NOT CHANGE ANYTHING BELOW THIS LINE **********************************//////////////////////////
//////////******************** DO NOT CHANGE ANYTHING BELOW THIS LINE **********************************//////////////////////////
//////////******************** DO NOT CHANGE ANYTHING BELOW THIS LINE **********************************//////////////////////////



function createSCookie(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=/";
}

function readSCookie(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 eraseSCookie(name) {
	createCookie(name,"",-1);
}



/// actually creating the cookie below

function createSurveyCookie(){
 var s_cookie_value = new Date().getTime();  // set the value to be either seen time now or not 0= do not show  set time now to 
  createSCookie(survey_cookie,s_cookie_value,how_long);
// console.log('created cookie');
  
}

function notshowSurveyCookie(){
  createSCookie(survey_cookie,0,how_long);
}

function notshowSurveyCookiethisSession(){
  createSCookie(survey_cookie,0,0);
}

// Part of the old script 

function checkSurveyCookie() {	
  // console.log('in checkSurveyCookie');
	var ck = readSCookie(survey_cookie);
	if (ck == null || ck == undefined || ck == "") {
		var num = Math.random();
 //console.log('math random= ', num );
		if (num < who_sees) {
			createSurveyCookie();
			window.surveyTimer = setTimeout("checkSurveyCookie()", 1000);
		} else {
			notshowSurveyCookiethisSession();//  write the survey cookie but with the value set to 0 so the pop up will never show
		//	console.log('will not have a pop up');
		}
	} else {
		ck = parseInt(ck);
		// console.log('this is the ck value=', ck);
		if (ck > 0) {
			var time_now = new Date().getTime();
			var time_then = ck;
			var difference = time_now - time_then;
			if (difference > when_to_pop) {
				showSurveyPopup();
			} else {
				window.surveyTimer = setTimeout("checkSurveyCookie()", 1000);
			}
		}
	}
}
			
function showSurveyPopup() {
	var lay = document.getElementById(layer_id_name);
	var layerwidth = 500;
	var layerheight = 300;
	var width = screen.width;
	var height = screen.height;
	var top = parseInt((height - layerheight) / 2) - 100;
	var left = parseInt((width - layerwidth) / 2);
	lay.style.left = left;
	lay.style.top = top;
	lay.style.display = "inline";
	lay.style.visibility = "visible";
}
			
function closeSurveyPopup() {
	var lay = document.getElementById(layer_id_name);
	lay.style.display = "none";
	lay.style.visibility = "hidden";
	notshowSurveyCookie();
}

function remindUserLater() {
	closeSurveyPopup();
	window.surveyTimer = setTimeout('checkSurveyCookie()', 1000);
	createSurveyCookie();
}

function activateSurvey() {
	closeSurveyPopup();
	window.open(where_to_go,open_like,open_dimensions);
}


window.surveyTimer = setTimeout("checkSurveyCookie()", 1000);
