

/*****************************************************************************************************************
Div overlay v1
Code developed by TNS Global Interactive dept
Developer: Dominic Claxton
TNS October 2007


Div overlay JavaScript for dynamic serving of CAWI Survey Invites

Current setup allows for: 

delayed overlay, 
minimum pages visited before overlay, 
1 in x number of people overlay trigger

Currenlty tested to work in: 
IE 5,6,7
FireFox 2
Netscape 7,8
Safari 3 on the PC (apart from the close browser popup)
******************************************************************************************************************/
//PARAMETERS - TO EDIT
//x and y position where div will appear on the page
D_Div_xpos = 0;
D_Div_ypos = 15;

//Life of the cookie in days
D_cookieLife = 30;

//Sets the number range for the random num which only shows an overlay for x number of people
//It is worth noting that this random number generator is only mimicking a random number
//So the numbers below equal a 1 in 2 chance of the overlay being shown to the respondent
//D_NumberRange is the universe so D_NumberRange=10 means 1 in 10 gets the invite
//D_TriggerNum must always be set to 1

D_NumberRange = 1;
D_triggerNum = 1;


/*
These two parameters below control the timed delay overlay and the x number of pages visited overlay
If one is turned off then the other should be turned on - and viceversa
*/

//This turns on/off the time delay on=yes  off=no
D_turnOnTimeDelay = "yes";
//Turns on/off the page visited function. Will show the overlay when x number of pages have been visited on=yes off=no
D_turnOnPageVisit = "no";


//The delay time in seconds when the overlay will show
D_delayTime = 3;

//The number of pages you must visit in order to trigger the overlay
D_pages2visit = 4;

//Enter the domain where this overlaycode will be served, preceded by a |character
D_domainCheckers = "|tns-global.com";

//tnscookiemonster
//The cookie name
D_check_name = "tnsoverlaycookie";

//This turns on/off the onclose/on exit script on=yes off=no
//It is currently not recommended to use this function because of the different results it returns in different browsers. 
//However, please seek advise from Global interactive if interested
D_turnOnCloseBrowser = "no";

// Perform Check if cookies enabled
var Check_Cookies_enabled=true;

/*=========================================================================================================================================================================================================================================================================================================================================*/
var cssNode = document.createElement('link'); 
cssNode.setAttribute('rel', 'stylesheet'); 
cssNode.setAttribute('type', 'text/css');
cssNode.setAttribute('href', 'http://cdn.tns-global.com/Multimedia/UK/706700/overlay/Live/controlPublic.css'); 
document.getElementsByTagName('head')[0].appendChild(cssNode);

/*===================================================================================================================================================================

                                                                  DIV overlay text - TO EDIT
=========================================================================================================================================================================================*/

D_divText = "<p>Hello</p><BR><p>Your opinion is important to us!<br><br>Thank you for using the health.gsk website.  We are very interested to find out more about the way people use this website and would really appreciate it if you could answer a few questions for us - the survey will take 7 minutes to complete.<br><br>TNS, the market research company running the survey, guarantees that everything you say will be kept private and confidential.<br><br>If you would like to continue browsing the website, you can minimise the questions or they may appear in a separate tab, for you to return to when you are ready.<br><br><i>By clicking yes, you agree to being contacted by TNS in the event of any suspected side effect being reported.</i></p><BR><p class=\"IntroFooter\"></br></br></br></br></br></br>GEN/WEB/09/44344/1</p>";


//Button text
D_AcceptText = "Yes, I am happy to take part in the survey &nbsp;&nbsp;";
D_DeclineText = "No, I would not like to take part in the survey &nbsp;&nbsp; ";

//URL link to your survey
// QueryString Site=
// 1 = Public Home Page
// 2 = Public Non Home Page
// 3 = HC Home Page
// 4 = HC Non Home Page
//
D_AcceptLink = "http://online.tns-global.com/wix/p571081340.aspx?Site=1";

//survey image
//D_surveyWindow="MUYOM Invite small.jpg";
D_surveyWindow = "http://cdn.tns-global.com/Multimedia/UK/706700/overlay/Live/logo-gsk.gif";
/*=====================================================================================================================================================================
                                                             DO NOT EDIT THE CODE BELOW THIS 
=======================================================================================================================================================================*/
D_urlon = window.location.href;

//Added by Swalsh for GSK check if cookies actually enabled
function are_cookies_enabled()
{
	var cookieEnabled = (navigator.cookieEnabled) ? true : false;

	if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
	{ 
		document.cookie="testcookie";
		cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
	}
	return (cookieEnabled);
}


function D_submitButton(){
window.onbeforeunload = D_voidedFunction;	
}


function D_setWindowrefresh(){	
	//Need to do domain checking to see if the link you have clicked on is with the same domain or not
    var D_domainSplit = D_domainCheckers.split("|");
    var D_getDomain = this.href.split("/");
	var D_JavaScriptMatch = this.href.match("java");
	var D_HashMatch = this.href.match("#");
    var D_getDomainOn = D_getDomain[2]; 
	var D_showpopup = true;
	//
	if(D_getDomain.length > 1){
      for(var n = 0; n < D_domainSplit.length; n++){
		if( D_getDomainOn == D_domainSplit[n] ){
			D_getDomain = "";//make the getDomain equal nothing
			var D_showpopup = false;
			break;
		}
	  }
	}
	
	if(D_JavaScriptMatch != null){
		var D_showpopup = false;
	}
	if(D_HashMatch != null){
		var D_showpopup = false;
	}
	
	if(D_showpopup == true && D_turnOnCloseBrowser == "yes"){
		//If you want the popup to appear then do this
		D_turnOnCloseBrowser = "no";
		//If the link is going outside and isn't part of the list of domains you specify then it will call the Div overlay to appear
		var D_cookie_value = ">"+D_domainCheckers+">no>"+D_pages2visit+">no>"+D_delayTime+">"+D_urlon+">"+D_Addone+">no"; 
		D_Set_Cookie( D_check_name, D_cookie_value, D_cookieLife, '/', '', '' );
	}else{
		window.onbeforeunload = D_voidedFunction;
	}
	
}

function D_voidedFunction(){
//over-rides the original window.onbeforeunload so that it doesn't prompt on link click!
}


function D_confirmExit(){
	D_MoveDiv();
	return D_popupText; 
}
  
function D_CreateDiv(){
    //document.write("<div id=\"GIS_popupInvite\"></div><div  id=\"GIS_InviteImageClass\"><img class=\"GIS_img\"  src=\"" + D_surveyWindow + "\"></div><div id=\"GIS_InviteText\">" + D_divText + "<br/></div><div id=\"GIS_InviteLink\"><a href=\"" + D_AcceptLink + "\" target=\"_self\" onClick=\"D_hideDiv();\">" + D_AcceptText + "</a><br/><br/><a href=\"javascript:D_hideDiv();\">" + D_DeclineText + "</a></div><div id=\"GIS_InviteButton1\"><a href=\"" + D_AcceptLink + "\" target=\"_self\" onClick=\"D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/butt.gif\"></a></div><div id=\"GIS_InviteButton2\"><a href=\"javascript:D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/butt.jpg\"></a></div><div id=\"GIS_closeButton\"><a href=\"javascript:D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/closebutt.png\"></a></div>");
    document.write("<div id=\"GIS_popupInvite\"></div><div  id=\"GIS_InviteImageClass\"><img class=\"GIS_img\"  src=\"" + D_surveyWindow + "\"></div><div id=\"GIS_InviteText\">" + D_divText + "<br/></div><div id=\"GIS_InviteLink\"><a href=\"" + D_AcceptLink + "\" target=\"_blank\" onClick=\"D_hideDiv();\">" + D_AcceptText + "</a><br/><br/><a href=\"javascript:D_hideDiv();\">" + D_DeclineText + "</a></div><div id=\"GIS_InviteButton1\"><a href=\"" + D_AcceptLink + "\" target=\"_blank\" onClick=\"D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/butt.gif\"></a></div><div id=\"GIS_InviteButton2\"><a href=\"javascript:D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/butt.gif\"></a></div><div id=\"GIS_closeButton\"><a href=\"javascript:D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/closebutt.png\"></a></div>");
    //var HTML = "<div 
    //HTML =+ "<div id=\"outer\"><div id=\"main\"><img height=\"60\" hspace=\"5\" src=\"http://cdn.tns-global.com/Multimedia/UK/706700/overlay/test/bigmlogo.gif\" width=\"80\" vspace=\"5\"/> <div> </div>"
    //document.write();

}



function D_MoveDiv(){
    var windowX = (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth;
    var windowY = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight;
    var scrollX = (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft;
    var scrollY = (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;
    var pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
    var pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;

    var viewportwidth;
    var viewportheight;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE

    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }

    // Positioning code

    D_element = document.getElementById("GIS_InviteImageClass");
    var popupH = D_element.offsetHeight;
    var popupW = D_element.offsetWidth;
    D_Div_ypos = (viewportheight / 2) - (parseInt(popupH) / 2);
    D_Div_xpos = (viewportwidth / 2) - (parseInt(popupW) / 2);
    //alert("win : " + viewportwidth + " : " + viewportheight + "  Calc : " + D_Div_xpos + " : " + D_Div_ypos);
    D_element.style.top = (D_Div_ypos + "px");
    D_element.style.left = (D_Div_xpos + "px");

    var D_element1 = document.getElementById("GIS_InviteText");
    var D_newypos = D_Div_ypos + 70;
    var D_newxpos = D_Div_xpos + 20;
    D_element1.style.top = (D_newypos + "px");
    D_element1.style.left = (D_newxpos + "px");
    //
    var D_element2 = document.getElementById("GIS_InviteLink");
    var D_newypos2 = D_newypos + 248;
    var D_newxpos2 = D_newxpos + 110;
    D_element2.style.top = (D_newypos2 + "px");
    D_element2.style.left = (D_newxpos2 + "px");
    //
    var D_element3 = document.getElementById("GIS_InviteButton1");
    var D_newypos3 = D_newypos + 248;
    var D_newxpos3 = D_newxpos + 500;
    D_element3.style.top = (D_newypos3 + "px");
    D_element3.style.left = (D_newxpos3 + "px");
    //
    var D_element4 = document.getElementById("GIS_InviteButton2");
    var D_newypos4 = D_newypos + 280;
    var D_newxpos4 = D_newxpos + 500;
    D_element4.style.top = (D_newypos4 + "px");
    D_element4.style.left = (D_newxpos4 + "px");
    //
    var D_element6 = document.getElementById("GIS_closeButton");
    var D_newypos6 = D_newypos - 60;
    var D_newxpos6 = D_newxpos + 549;
    D_element6.style.top = (D_newypos6 + "px");
    D_element6.style.left = (D_newxpos6 + "px");
    //


    var D_element2 = document.getElementById("GIS_popupInvite");
    //var dimen = getScrollXY();
    //D_element2.style.height = dimen[0] + " px";
    //D_element2.style.width = dimen[1] + " px";

    D_element2.style.height = pageY + "px";
    D_element2.style.top = 0;
    D_element2.style.left = 0;

}




function D_hideDiv(){
	var D_element = document.getElementById("GIS_InviteImageClass");
    D_element.style.top = "-2000px";
	var D_element1 = document.getElementById("GIS_popupInvite");
    D_element1.style.top = "-3000px";
	var D_element2 = document.getElementById("GIS_InviteText");
    D_element2.style.top = "-2000px";
	var D_element3 = document.getElementById("GIS_InviteLink");
    D_element3.style.top = "-2000px";
	var D_element4 = document.getElementById("GIS_InviteButton1");
    D_element4.style.top = "-2000px";
	var D_element5 = document.getElementById("GIS_InviteButton2");
    D_element5.style.top = "-2000px";
	var D_element6 = document.getElementById("GIS_closeButton");
    D_element6.style.top = "-2000px";
}

//====================================================================================================================================================

function D_showTimeOutDiv(){
	D_MoveDiv();
}

function D_Set_Cookie( D_name, D_value, D_expires, D_path, D_domain, D_secure ) {
// set time, it's in milliseconds
var D_today = new Date();
D_today.setTime( D_today.getTime() );
//if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, 
//delete * 24, for minutes, delete * 60 * 24
 if ( D_expires ){
	 D_expires = D_expires * 1000 * 60 * 60 * 24; 
	 }
  var D_expires_date = new Date( D_today.getTime() + (D_expires) );
  document.cookie = D_name + "=" +escape( D_value ) +( ( D_expires ) ? ";expires=" + D_expires_date.toGMTString() : "" ) + ( ( D_path ) ? ";path=" + D_path : "" ) + ( ( D_domain ) ? ";domain=" + D_domain : "" ) +( ( D_secure ) ? ";secure" : "" );

}


function D_Get_Cookie() {
	//D_check_name, D_domainCheckers, D_pages2visit, D_turnOnPageVisit, D_turnOnTimeDelay, D_delayTime, D_turnOnCloseBrowser
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var D_a_all_cookies = document.cookie.split( ';' );
	var D_a_temp_cookie = '';
	var D_cookie_name = '';
	var D_cookie_value = '';
	var D_b_cookie_found = "none"; // set boolean t/f default f
	// Added by Swalsh check if cookies enabled
	if ( are_cookies_enabled() != Check_Cookies_enabled ) {
	   return;
	}
	
	for ( i = 0; i < D_a_all_cookies.length; i++ ){
		// now we'll split apart each name=value pair
		D_a_temp_cookie = D_a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		D_cookie_name = D_a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		//==========================================================================================================================================
		// if the extracted name matches passed check_name
		
		if ( D_cookie_name == D_check_name ){
			var D_b_cookie_found = "present";
			var D_cookie_value = unescape(D_a_temp_cookie[1]);
			break;
		}
	    
	}
	//===========================================================================================================================================
	//Here is where the cookie is first set. So all the variables must be set here.
	if ( D_b_cookie_found == "none" ){
		var D_cookie_value = ">"+D_domainCheckers+">"+D_turnOnPageVisit+">"+D_pages2visit+">"+D_turnOnTimeDelay+">"+D_delayTime+">"+D_urlon+">1>"+D_turnOnCloseBrowser;
		D_Set_Cookie( D_check_name, D_cookie_value, D_cookieLife, '/', '', '' );
	}
	//===========================================================================================================================================
        D_CreateDiv();
	//Extract the variables
		    var D_temp = D_cookie_value.split(">");
			D_domainCheckers = D_temp[1];
			D_turnOnPageVisit = D_temp[2];
			D_pages2visit = D_temp[3];
			D_turnOnTimeDelay = D_temp[4];
			D_delayTime = D_temp[5];
			D_cookieUrlOn = D_temp[6];
			D_IncrementNum = D_temp[7];
			D_turnOnCloseBrowser = D_temp[8];
			//We do another split on the domaincheckers variable
			D_domainSplit = D_domainCheckers.split("|");
			
			//if the url is different then you have gone to a different page so increment the cookie by one
			D_Addone = parseInt(D_IncrementNum)+1;
			if(D_urlon != D_cookieUrlOn && D_turnOnPageVisit == "yes"){
			 var D_cookie_value = ">"+D_domainCheckers+">"+D_turnOnPageVisit+">"+D_pages2visit+">"+D_turnOnTimeDelay+">"+D_delayTime+">"+D_urlon+">"+D_Addone+">"+D_turnOnCloseBrowser;
			  D_Set_Cookie( D_check_name, D_cookie_value, D_cookieLife, '/', '', '' );
			 //If the person has visited three different pages then do this
		
			    if(D_Addone==D_pages2visit){
			      var D_cookie_value = ">"+D_domainCheckers+">no>"+D_pages2visit+">no>"+D_delayTime+">"+D_urlon+">"+D_Addone+">no"; 
				  D_Set_Cookie( D_check_name, D_cookie_value, D_cookieLife, '/', '', '' );
				 D_MoveDiv();
			    }
			
			}
		   var D_ran_unrounded=Math.random()*D_NumberRange;
           var D_ran_number=Math.ceil(D_ran_unrounded); 
          
         if(D_ran_number == D_triggerNum){
			//if D_turnOnTimeDelay =  "yes"
            D_refreshpage();
          }
}	

var D_incrm = 0;
function D_refreshpage(){
  if(D_turnOnTimeDelay == "yes"){ 
  //set the randomNum function which only lets x number of people in
    D_incrm =D_incrm+1;
    if(D_incrm == D_delayTime){
       D_MoveDiv();
       var D_cookie_value = ">"+D_domainCheckers+">no>"+D_pages2visit+">no>"+D_delayTime+">"+D_urlon+">"+D_Addone+">no"; 
       D_Set_Cookie( D_check_name, D_cookie_value, D_cookieLife, '/', '', '' );
    }else{
    window.setTimeout('D_refreshpage()', 1000);	
    }
 }
}

//=============================================================================================
//Start things off!
D_Get_Cookie();











