

function sitestat(ns_l) {
	ns_l += "&ns__t=" + new Date().getTime();
	ns_pixelUrl = ns_l;
	var ns_0 = location.search.substring(1);
	var ns_1 = ns_0.indexOf("ns_or=");
	if (ns_1 > -1)
		ns_0 = unescape(ns_0.substring(ns_1 + 6, ns_0.length));
	if (ns_0.length == 0)
		ns_0 = document.referrer;
	ns_0 = (ns_0.lastIndexOf("/") == ns_0.length - 1) ? ns_0.substring(ns_0
			.lastIndexOf("/"), 0) : ns_0;
	if (ns_0.length > 0)
		ns_l += "&ns_referrer=" + ns_0;
	if (document.images) {
		ns_1 = new Image();
		ns_1.src = ns_l;
	} else
		document.write("<img src=" + ns_l + " width=1 height=1>");
}
sitestat("http://bupa.s0.nl/s?bupa&homepage");

var expDays = 1; // number of days the cookie should last

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

function SetCookie(name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = "/" // (argc > 3) ? argv[3] : null; ### path set to / so that
					// cookie applies to whole domain - ie once one page has had
					// the popup, no others will
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape(value)
			+ ((expires == null) ? "" : ("; expires=" + expires))
			+ ((path == null) ? "" : ("; path=" + path))
			+ ((domain == null) ? "" : ("; domain=" + domain))
			+ ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = GetCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


//var exp = new Date();
//exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));

 var exp = new Date();
 exp.setMonth(exp.getMonth() + 6);
 exp = exp.toGMTString();

function amt() {
	var count = GetCookie('count')
	if (count == null) {
		SetCookie('count', '1')
		return 1
	} else {
		var newcount = parseInt(count) + 1;
		DeleteCookie('count')
		SetCookie('count', newcount, exp)
		return count
	}
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

var count = GetCookie('count');
//GRP 17.0
var siteName = document.invite.elements['siteName'].value

// Invitation layer
if (count == null) {
	count = 1;
	SetCookie('count', count, exp);
	setTimeout("document.getElementById('floatdiv').style.visibility = 'visible'",5000);
} else {
	document.getElementById('floatdiv').style.visibility = 'hidden';
	//count++;
	//SetCookie('count', count, exp);
}
	
	nowDate = new Date();
	nowDate.setMonth(nowDate.getMonth() + 6);
	cookieExpires = nowDate.toGMTString();

	function userDeclineSurvey() {

		if (document.cookie) {
			var myCookie = unescape(document.cookie);
			if (myCookie.indexOf('surveyUserResponse') >= -1) {
				return true;
			} else {
				return false;
			}

		}
	}

	function showBox() {
		var strUserAgt = navigator.userAgent;
		if (strUserAgt.indexOf("Firefox") != -1
				|| strUserAgt.indexOf("Netscape") != -1
				|| strUserAgt.indexOf("Opera") != -1) {
			document.getElementById('floatdiv').style.visibility = 'visible';
		}

		if (strUserAgt.indexOf("MSIE") != -1) {
			document.all("floatdiv").style.visibility = 'visible';
		}
		if (document.layers) {
			document.layers['floatdiv'].visibility = 'show';
		}

	}

	function random() {
		random.seed = (random.seed * random.a + random.c) % random.m;
		return random.seed / random.m;
	}

	random.m = 714025;
	random.a = 4096;
	random.c = 150889;
	random.seed = (new Date()).getTime() % random.m;

	frequency = 1

	if (!userDeclineSurvey()) {
		if (random() <= frequency) {
			alert('showBox()');
			showBox();
		}
	}

	function childWindow() {
		document.cookie = "surveyUserResponse=false;expires=" + cookieExpires
				+ ";path=/;domain=.bupa.gov.uk";

		var childWin = window
				.open(
						"",
						"childWin",
						"width=50,height=50,toolbar=no,menubar=no,scrollbars=yes,resizable=0,location=no,directories=no,status=no",
						"replace=true");
		childWin.document.open("text/html");

		str = '<html>';
		str += '<head><title>Bupa 2011 survey</title></head>';
		str += '<script language="' + 'javaScript">';
		str += 'var DO = document.domain;';
		str += 'checkWindow();';
		str += 'function checkWindow(){';
		if (!document.layers) {
			str += 'try{;';
		}
		;
		str += 'if (!window.opener || window.opener.closed || window.opener.location.href.indexOf(DO) ==-1 || (document.referrer =="" && document.layers)){';
		str += 'resizeWindow();';
		str += 'clearTimeout(stTimeout)};';
		str += 'stTimeout = setTimeout("checkWindow()",1000);';
		if (!document.layers) {
			str += '}';
		}
		;
		if (!document.layers) {
			str += 'catch(e){resizeWindow()};';
		}
		;
		str += '}';

		str += 'function resizeWindow(){';
		str += 'window.resizeTo(800,600);';

		if (siteName == "bupacouk"){		
		str = str + 'window.location =   "http://surveys.bupa.co.uk/surveys/wsb.dll/s/21g8e7";';
		}
		else {		
		str = str + 'window.location =   "http://surveys.bupa.co.uk/surveys/wsb.dll/s/21g8b0";';

		}
		// str+='window.opener.document.blur();';
		str += 'if (window.opener){window.opener.blur()};';
		str += 'window.focus();';
		str += '}';
		str += 'window.blur();';
		str = str + '</script>';
		str += '<body>';
		str = str + '</body></html>';

		childWin.document.write(str);
		childWin.document.close();

		if (!document.layers) {
			document.getElementById('floatdiv').style.visibility = 'hidden';
		} else {
			document.layers['floatdiv'].visibility = 'hide';
		}
	}

	function closeInvite() {
		document.cookie = "surveyUserResponse=false;expires=" + cookieExpires
				+ ";path=/;domain=.bupa.co.uk";

		if (!document.layers) {
			document.getElementById('floatdiv').style.visibility = 'hidden';
		} else {
			document.layers['floatdiv'].visibility = 'hide';
		}
	}



