/*
 * GfK NOP DHTML on-exit (c) 2007 GfK NOP Ltd
 */
	// *****  Use this var to enable/disable this script *****
    var active = false;
    // *****  Use this var to enable/disable this script *****
    
    // Check for active flag...
    if (active) {

        // The incidence rate... 100 = everyone, -1 = no one
    	var INCIDENCE = 100;

    	// --
    	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[0]="ikea.com/se/sv";
    	//Sites[0]="ikea.com/de/de";
    	
    	//Sites[2]="ikea.com/gb/en";
        //Sites[2]="ikea.com/us/en";
    	// test sites...
    	//Sites[4]="ikea.com/iw/cci/meta/no-injection/iw-mount/default/main/Modular_Site/Small_Dev_Site/master/WORKAREA/jbnn/ms/local/logout.html";   // Only TeamSite server...
        //Sites[5]="ikea.com/se/en";
    	
    	// Set incidence rates
    	var Incidence=new Array();
    	
    	Incidence[0]=100;
    	//Incidence[1]=60;
    	//Incidence[2]=30;
        //Incidence[2]=8;
    	
    	//Incidence[4]=100;
        //Incidence[5]=100;

    	var thissite = document.location.href.replace(/\/$/,"");
        var str = thissite.split(/ikea.com/);
        thissite = thissite.replace(str[0],"");     // remove everything in URL preceding ikea.com
        
        var myNum = -1;
    	var mySite = "";
        
    	for (i=0;i<Sites.length;i++) {
    		if (thissite == Sites[i]) {
    			myNum=i;
    			mySite=Sites[i];
    		}
    	}
        
        //alert('mySite=' + mySite);
        
    	if (myNum>=0) {
    		INCIDENCE = Incidence[myNum];
    	}
    	else {
    		INCIDENCE = 0
    	}
    	
    	if (INCIDENCE == undefined) {
    		INCIDENCE=0; // ie we just don't pop them
    	}
    	
        if (mySite=="ikea.com/gb/en") { 
            d.innerHTML="<p><img src=\"http://www.ikea.com/ms/img/header/logo.gif\" alt=\"Ikea logo\" /></p><p style=\"font-family:'Trebuchet MS',Verdana,Sans-Serif;font-size:14px;\">Have your say about the IKEA website!</p><p>Thank you for visiting the IKEA website today. After you have completed your visit to the site, we would like to ask you to fill in a short (10 minute) questionnaire. Your responses will help us improve our website and tailor its content to meet your needs.</p><p>The survey is being conducted by GfK NOP Ltd, an independent market research agency, on behalf of IKEA. All information given will be kept completely confidential and will not be passed on to any third parties.</p><p><b><a href=\"#\" onclick=\"run_survey('uk');return false;\" style=\"color:" + colorscheme + "\">Click here to do the survey at the end of your visit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Click here to close this invitation</a></b></p>";
        }
        else if (mySite=="ikea.com/us/en") { 
            d.innerHTML="<p><img src=\"http://www.ikea.com/ms/img/header/logo.gif\" alt=\"Ikea logo\" /></p><p style=\"font-family:'Trebuchet MS',Verdana,Sans-Serif;font-size:14px;\">Have your say about the IKEA website!</p><p>Thank you for visiting the IKEA website today. After you have completed your visit to the site, we would like to ask you to fill in a short (10 minute) questionnaire. Your responses will help us improve our website and tailor its content to meet your needs.</p><p>The survey is being conducted by GfK NOP Ltd, an independent market research agency, on behalf of IKEA. All information given will be kept completely confidential and will not be passed on to any third parties.</p><p><b><a href=\"#\" onclick=\"run_survey('us');return false;\" style=\"color:" + colorscheme + "\">Click here to do the survey at the end of your visit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Click here to close this invitation</a></b></p>";	
        }
        else if (mySite=="ikea.com/de/de") { 
            d.innerHTML="<p><img src=\"http://www.ikea.com/ms/img/header/logo.gif\" alt=\"Ikea logo\" /></p><p style=\"font-family:'Trebuchet MS',Verdana,Sans-Serif;font-size:14px;\">Teilen Sie uns Ihre Meinung über die IKEA-Website mit!</p><p>Vielen Dank für Ihren heutigen Besuch auf der IKEA-Website. Wir möchten Sie bitten, nach Ihrem Besuch einen kurzen Fragebogen (10 Minuten) auszufüllen. Ihre Antworten sollen uns dabei helfen, die Website zu verbessern und ihren Inhalt auf Ihre Bedürfnisse abzustimmen.</p><p>Diese Befragung wird von GfK NOP Ltd, einem unabhängigen Marktforschungsinstitut, im Auftrag von IKEA durchgeführt. All Ihre Antworten werden streng vertraulich behandelt und keinesfalls an Dritte weitergegeben.</p><p><b><a href=\"#\" onclick=\"run_survey('de');return false;\" style=\"color:" + colorscheme + "\">Klicken Sie hier, um den Fragebogen nach Ihrem Besuch auf der Website auszufüllen</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Klicken Sie hier, um diese Aufforderung zu schließen</a></b></p>";
        }
        else if (mySite=="ikea.com/se/sv") {
            d.innerHTML="<p><img src=\"http://www.ikea.com/ms/img/header/logo.gif\" alt=\"Ikea logo\" /></p><p style=\"font-family:'Trebuchet MS',Verdana,Sans-Serif;font-size:14px;\">Tyck till om IKEA.se!</p><p>Vad kul att du besöker IKEA.se idag. Vi vill gärna höra dina åsikter om hur hemsidan kan bli bättre och mer möta din önskemål och behov. Vi hoppas att du kan ta dig tid att svara på en kort enkät. Klicka på länken nedan för att komma dit. Att fylla i den tar inte lång tid.</p><p>Undersökningen görs av GfK NOP Ltd på uppdrag av IKEA. IKEA kommer inte att ge informationen vidare till någon annan.</p><p><b><a href=\"#\" onclick=\"run_survey('se');return false;\" style=\"color:" + colorscheme + "\">Klicka här om du vill delta i undersökningen.</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Klicka här om du vill stänga detta fönster.</a></b></p>";
        }
        else {
            d.style.display="none";
            //d.innerHTML="<p><img src=\"http://www.ikea.com/ms/img/header/logo.gif\" alt=\"Ikea logo\" /></p><p style=\"font-family:'Trebuchet MS',Verdana,Sans-Serif;font-size:14px;\">Have your say about the IKEA website!</p><p>Thank you for visiting the IKEA website today. After you have completed your visit to the site, we would like to ask you to fill in a short (10 minute) questionnaire. Your responses will help us improve our website and tailor its content to meet your needs.</p><p>The survey is being conducted by GfK NOP Ltd, an independent market research agency, on behalf of IKEA. All information given will be kept completely confidential and will not be passed on to any third parties.</p><p><b><a href=\"#\" onclick=\"run_survey('uk');return false;\" style=\"color:" + colorscheme + "\">Click here to do the survey at the end of your visit</a></p><p><a href=\"#\" onclick=\"close_popup();return false;\" style=\"color:" + colorscheme + "\">Click here to close this invitation</a></b></p>";
        }

        // this is the name of the cookie that will drop to stop people getting multiple invites
        survey_cookie="gfk_j581752_survey";

        // 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("heartbeat.htm?l=" + lang ,"_blank");  // 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) {
		document.cookie = cookiename + "=" + value +";expires=" + theDate.toGMTString() +";path=/";
		//alert("Cookie set");
	}

	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');
		if (ck==1) {
			//alert('This browser accepts cookies');
			return 1;
		}
		else {
			//alert('This browser is not cookie capable');
			return 0;
		}
		// erase it for good measure
		erase_cookie('testaccept');
	}

	function incidence_rate(incidence) {
		var percentage=incidence;
		var randNum = Math.round(Math.random() * 100);
        
		if (randNum<=percentage) {
			return 1; // success
		}
		else {
			return 0; // failed
		}
	}