/*
 * GfK NOP DHTML on-exit (c) 2011 GfK NOP Ltd
 */
	// *****  Use this var to enable/disable this script *****
    var active = true;
    // *****  Use this var to enable/disable this script *****
    // this is the name of the cookie that will drop to stop people getting multiple invites
 
 	survey_cookie="gfk_j438086_survey";// + Math.random();
 	
 	HeartBeatPath = "/custom/wtb/gfkcode/other/";
    
    
    // Check for active flag...
    if (active) {

        // The incidence rate... 100 = everyone, -1 = no one

	
	if (!(get_cookie(survey_cookie + "_referer"))) { 

		var gfk_referrer = "";
		
	
		if (typeof php_http_referer != "undefined") { 
			gfk_referrer = php_http_referer
		}
		else {
			gfk_referrer = document.referrer
		}
		
		gfk_referrer = gfk_referrer.replace(/\&/g,"%26");
		set_cookie(survey_cookie + "_referer",gfk_referrer,"0");
	
	}
	else { 
		
		gfk_referrer = get_cookie(survey_cookie + "_referer");
	}

	
	
    	// --
    	var theDate=new Date();
    	month=theDate.getMonth();
    	month=month+3;
    	theDate.setMonth(month);   // this is all for the cookie expiry
    	
    	var colorscheme="#3399fd;text-decoration:none;"; // for border and link colour
    	var d=document.createElement("div"); // create the invitation layer

        var winHeight = (document.compatMode=="CSS1Compat")?document.documentElement.clientHeight:document.body.clientHeight;
        if (self.innerHeight) { // for Safari (or all but Explorer)
        	winHeight = self.innerHeight;
        }
        
    	// now style it
        d.style.borderStyle="solid";
    	d.style.borderWidth="2px";
    	d.style.borderColor="#333333";
    	d.style.width="500px";
    	//d.style.height="220px";
    	d.style.fontFamily="verdana,sans-serif";
    	d.style.fontSize="10px";
    	d.style.textAlign="center";
    	d.style.zIndex="100";
    	d.style.padding="10px";
    	d.style.position="absolute";
    	d.style.left=((document.body.clientWidth/2) - 250)+"px";
    	d.style.top=((winHeight/2) - 110)+"px";
        d.style.backgroundColor="#ffffff";
        
    	// Set valid sites
        var Sites=new Array(); 
    	

	    Sites = new Array(
		//"www.visitwales.co.uk", 20, "en",
		//"five.surveys.com/projects/j438086", 100, "en-ca",
		"mrinterview2.gfknop.co.uk", 100, "en-na",
		"usa.visitwales.com", 100, "en-us",
		"canada.visitwales.com", 100, "en-ca",
		"americas.visitwales.com", 100, "en-na",
		"www.visitwales.nl", 100, "nl",
		"www.visitwales.de", 100, "de",
		"www.german.visitwales.com", 100, "de",
		"www.visitwales.fr", 100, "fr",
		"www.french.visitwales.com", 100, "fr"
	    );
   
  
    
		var thissite = document.location.href.replace(/\/$/,"");
        
    	var mySite = "";
    	var Template = "";

        INCIDENCE = -1
        
    	for (i=0;i<Sites.length;i+=3) {
    		if (thissite.indexOf(Sites[i]) > -1) {
    			mySite = Sites[i];
    			INCIDENCE = Sites[i+1];
    			Template = Sites[i+2];
    		}
    	}

	
		
        //if (Template=="en") { 
        //    d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Tell us your views and win 50 Dollars!</p><p>Thank you for visiting the Visit Wales website. To help us to improve the information we can provide you, we\u2019d like to ask you six very quick questions at the end of your visit. As a thank you we will enter you into a prize draw for a 50 Dollar cheque.</p><p>For more information about the prize draw please <a href=\"https://five.surveys.com/projects/j438086/prizedraw.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438086/prizedraw.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>click here</u></a></p></p><p><b><a href=\"#\" onclick=\"run_survey();return false;\" style=\"color:" + colorscheme + "\">Take survey on exit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">No thanks</a></b></p>";
        //}
	if (Template=="en-us") { 
            d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Tell us your views and win 50 Dollars!</p><p>Thank you for visiting the Visit Wales website. To help us to improve the information we can provide you, we\u2019d like to ask you six very quick questions at the end of your visit. As a thank you we will enter you into a prize draw for a 50 Dollar cheque.</p><p>For more information about the prize draw please <a href=\"https://five.surveys.com/projects/j438086/prizedraw.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438086/prizedraw.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>click here</u></a></p></p><p><b><a href=\"#\" onclick=\"run_survey('USA');return false;\" style=\"color:" + colorscheme + "\">Take survey on exit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">No thanks</a></b></p>";
        }
	if (Template=="en-ca") { 
            d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Tell us your views and win 50 Dollars!</p><p>Thank you for visiting the Visit Wales website. To help us to improve the information we can provide you, we\u2019d like to ask you six very quick questions at the end of your visit. As a thank you we will enter you into a prize draw for a 50 Dollar cheque.</p><p>For more information about the prize draw please <a href=\"https://five.surveys.com/projects/j438086/prizedraw.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438086/prizedraw.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>click here</u></a></p></p><p><b><a href=\"#\" onclick=\"run_survey('Canada');return false;\" style=\"color:" + colorscheme + "\">Take survey on exit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">No thanks</a></b></p>";
        }
        if (Template=="en-na") { 
	    d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Tell us your views and win 50 Dollars!</p><p>Thank you for visiting the Visit Wales website. To help us to improve the information we can provide you, we\u2019d like to ask you six very quick questions at the end of your visit. As a thank you we will enter you into a prize draw for a 50 Dollar cheque.</p><p>For more information about the prize draw please <a href=\"https://five.surveys.com/projects/j438086/prizedraw.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438086/prizedraw.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>click here</u></a></p></p><p><b><a href=\"#\" onclick=\"run_survey('North_America');return false;\" style=\"color:" + colorscheme + "\">Take survey on exit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">No thanks</a></b></p>";
        }
	if (Template=="nl") { 
	    d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Geef ons uw mening en win \u20AC 50!</p><p>Dank voor uw bezoke aan de Visit Wales website.  Om ons te helpen u nog betere informatie te bieden, willen we u na uw bezoek vijf korte vragen stellen. Als bedankje hiervoor doet u automatisch mee aan een prijzentrekking waarbij u een cheque van \u20AC 50 kunt winnen.</p><p><a href=\"https://five.surveys.com/projects/j438087/prizedraw_nl.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438087/prizedraw_nl.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>Klik hier</u></a> voor meer informatie over de prijzentrekking.</p></p><p><b><a href=\"#\" onclick=\"run_survey('Netherlands');return false;\" style=\"color:" + colorscheme + "\">Neem deel aan de enqu\u00EAte bij het verlaten van de website</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Nee, bedankt</a></b></p>";
        }
	if (Template=="de") { 
	    d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Teilen Sie uns Ihre Meinung mit und gewinnen Sie 50\u20AC!</p><p>Vielen Dank, dass Sie unsere Webseite \u2018Visit Wales\u2019 besucht haben.  Damit wir unsere Informationen noch besser auf Ihre Bed\u00FCrfnisse zuschneiden k\u00F6nnen, w\u00E4ren wir Ihnen dankbar, wenn Sie am Ende Ihres Besuches f\u00FCnf kurze Fragen beantworten k\u00F6nnten. Als Dankesch\u00F6n erhalten Sie die M\u00F6glichkeit, an einer Verlosung teilzunehmen, bei der Sie einen Scheck im Wert von 50\u20AC gewinnen k\u00F6nnen.</p><p>F\u00FCr weitere Informationen zum Gewinnspiel <a href=\"https://five.surveys.com/projects/j438087/prizedraw_de.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438087/prizedraw_de.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>klicken Sie bitte hier </u></a></p></p><p><b><a href=\"#\" onclick=\"run_survey('Germany');return false;\" style=\"color:" + colorscheme + "\">Am Ende an der Umfrage teilnehmen</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Nein, danke</a></b></p>";
        }
        if (Template=="fr") { 
	    d.innerHTML="<p><img src=\"http://www.visitwales.co.uk/custom/wtb/img/visitwales/wales-logo.png\" alt=\"Visit Wales\" /></p><p style=\"font-family:Tahoma,Verdana,Sans-Serif;font-size:14px;\"><p>Gagnez 50 euros en nous donnant votre avis !</p><p>Merci de votre visite sur le site \"Visit Wales\".  Afin de nous aider \u00E0 am\u00E9liorer les informations que nous sommes susceptibles vous communiquer, nous aimerions vous poser 6 questions tr\u00E8s rapides \u00E0 la fin de votre visite. Pour vous remercier, vous aurez la possibilit\u00E9 de participer \u00E0 un tirage au sort et gagner un ch\u00E8que d\u2019une valeur de 50 euros.</p><p>Pour plus de renseignements \u00E0 propos du tirage au sort, veuillez <a href=\"https://five.surveys.com/projects/j438087/prizedraw_fr.htm\" onclick=\"window.open('https://five.surveys.com/projects/j438087/prizedraw_fr.htm',null, 'scrollbars=yes, resizable=yes, width=650,height=775, left=0,top=0'); return false;\"  target=\"_blank\" ><u>cliquer ici </u></a></p></p><p><b><a href=\"#\" onclick=\"run_survey('France');return false;\" style=\"color:" + colorscheme + "\">Merci de r\u00E9pondre \u00E0 quelques questions avant de quitter notre site.</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Non merci</a></b></p>";
        }
	
	
        // check if mySite found
        if(mySite != ""){
            // check against incidence rate (100=everyone, -1 = no one)
            if (incidence_rate(INCIDENCE)) {
                // only serve if they haven't got a cookie
                if (!get_cookie(survey_cookie)) {
                    set_cookie(survey_cookie,"1");
                    window.setTimeout("document.body.appendChild(d)",1500); // pop up after 1.5 secs
                }
            }
        }
    }
    
	function close_popup() {
		d.style.display="none";
	}

	function run_survey(lang) {
		a=window.open(HeartBeatPath + "heartbeat.htm?ref=" + gfk_referrer + "&country="  + lang ,"_blank","modal=yes,resizable=yes");  // open the heartbeat window - this will check the opener until it can't any more - ie we've left the opening domain
		self.focus();
		close_popup();
	}

	// Functions to set, get and delete cookies
	// First, to set a cookie - set a 3 month expiry date. Year is handled automatically if the month spills over
	function set_cookie(cookiename,value,expires) {
		var theCookie = cookiename + "=" + value +";path=/;"
		if (expires != "0") { 
			theCookie += "expires=" + theDate.toGMTString() ;
		}
		document.cookie = theCookie;
		
	}

	function get_cookie(cookiename) {
		// document.cookie gives us a list of all the cookies from this domain
		// we need to find just the cookie we are interested in
		// take a substring between "cookiename=" and ";" - this is the value of the cookie
		// this function will return either the value of the cookie, or a null value if it's not there

  		var nameStr = cookiename + "=";
  		var maxLen = document.cookie.length
  		var i = 0
  		while (i < maxLen) {
            var j = i + nameStr.length
            if (document.cookie.substring(i,j) == nameStr) {
                var cookieEnd = document.cookie.indexOf(";",j);
                if (cookieEnd == -1) {
                    cookieEnd = document.cookie.length;
                }
                return unescape(document.cookie.substring(j,cookieEnd));
            }
    		i++
  		}
  		return "";
	}

	// simply erase the cookie by making it expire before now
	function erase_cookie(cookiename) {
		document.cookie = cookiename + "=0;expires=Sun, 26 Mar-2000 12:00:00 GMT;path=/";
		//alert("Cookie erased");
	}

	// do they accept cookies?
	// simply drop a test cookie and try to retrieve it. if it's not there, they don't take cookies
	function check_cookie_accept() {
		set_cookie('testaccept','1');
		var ck=get_cookie('testaccept');
		erase_cookie('testaccept');
		if (ck==1) {
			return 1;
		}
		else {
			//alert('This browser is not cookie capable');
			return 0;
		}

	}

	function incidence_rate(incidence) {
		var percentage=incidence;
		var randNum = Math.round(Math.random() * 100);
        
		if (randNum<=percentage) {
			return 1; // success
		}
		else {
			return 0; // failed
		}
	}

