/*
 * GfK NOP DHTML on-exit (c) 2007 GfK NOP Ltd
 */
	// *****  Use this var to enable/disable this script *****
    var active = true;
    
    // *****  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]="/gb/en";
    	
    	// Set incidence rates
    	var Incidence=new Array();
		Incidence[0]=20;
		
    	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++) {
			mySplitSite = "/" + Sites[i].split("/")[1];
    		if (thissite == "ikea.com" + Sites[i] || thissite == "ikea.com" + mySplitSite || thissite.substring(0,21) == ("ikea.com" + Sites[i] + "/store/"))
			{
    			myNum=i;
    			mySite=Sites[i];
				break;
    		}
    	}
        
    	if (myNum>=0) {
    		INCIDENCE = Incidence[myNum];
    	}
    	else
		{
    		INCIDENCE = 0;
    	}
    	
    	if (INCIDENCE == undefined)
		{
    		INCIDENCE=0; // ie we just don't pop them
    	}
    	var popStart = "<p><img src=\"http://www.ikea.com/ms/img/logos/logo92x33.gif\" alt=\"Ikea logo\" /></p><p>";
        if (mySite=="/gb/en")
		{
			d.innerHTML = popStart + "<b>IKEA values your feedback!</b><p>You are invited to participate in an online survey about your recent experience of IKEA, including our Website.</p><p>The survey will take approximately 10 - 20 minutes and will start when you leave www.ikea.com.</p><br/><p>Please click the \"<b>YES</b>\" button to participate.</p><input type=\"button\" onclick=\"run_survey('us');return false;\" value=\"Yes\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" onclick=\"close_popup();return false;\" value=\"No\">";
			survey_cookie="641067_survey";
        }
        else
		{
            d.style.display="none";
        }

        // this is the name of the cookie that will drop to stop people getting multiple invites
        

        // check if mySite found
        if(mySite != "")
		{
            // check against incidence rate (100=everyone, -1 = no one)
			var thisCookie = parseInt(get_cookie(survey_cookie));
			var surveyDisplayCount = 1; //The number of times a user must enter a valid survey page before it display
			
			if (thisCookie)
			{
				if(mySite == "/gb/en" || mySite.substring(0,13) == "/gb/en/store/")
				{
					if(thisCookie >= 1 && thisCookie < surveyDisplayCount) //If the user has visited the homepage 1 or 2 times
					{
						thisCookie++;
						set_cookie(survey_cookie,thisCookie); //Sum up visits to the homepage
						if(thisCookie == surveyDisplayCount) 
						{
							window.setTimeout("document.body.appendChild(d)",1500); // pop up question after 1.5 secs
							set_cookie(survey_cookie,maxCookieCount);
						}
					}
				}
			}
			else
			{
				if(incidence_rate(INCIDENCE))
				{
					set_cookie(survey_cookie,"1");
					if(surveyDisplayCount == 1)
					{
						window.setTimeout("document.body.appendChild(d)",1500); // pop up after 1.5 secs
					}
				}
				else
				{
					set_cookie(survey_cookie,"-1");
				}
			}
        }
    }
    
	function close_popup() {
		d.style.display="none";
	}

	var surveyPopupWindow;
	var looper;
	
	function run_survey()
	{
		a=window.open(mySite + "/heartbeat.htm","_blank",'resizable=yes, menubar=no, scrollbars=yes, toolbar=no, height=400, width=400, left=50, top=50');  // open the heartbeat window - this will check the opener until it can't any more - ie we've left the opening domain
		window.focus();
		close_popup();
	}
	
	function checker() {
		try {
			myopener = surveyPopupWindow.location.href;
		}
		catch(err) {
			window.clearInterval(looper);
			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=/";
	}

	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=/";
	}

	// 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) {
			return 1;
		}
		else {
			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
		}
	}
