/*****************************************************************************************************************
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 = 30;
D_Div_ypos = 150;

//Life of the cookie in days
D_cookieLife = 60;

//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 = 30;
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 = 5;

//The number of pages you must visit in order to trigger the overlay
D_pages2visit = 1;

//Enter the domain where this overlaycode will be served, preceded by a |character
D_domainCheckers = "|www.nzpost.co.nz";

//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";




/*=============================================================================================================================*/
var cssNode = document.createElement('link'); 
cssNode.setAttribute('rel', 'stylesheet'); 
cssNode.setAttribute('type', 'text/css'); 
cssNode.setAttribute('href', 'http://cdn.tns-global.com/multimedia/NZ/NZPostMonitor/overlayinvites/control5.css'); 
document.getElementsByTagName('head')[0].appendChild(cssNode); 


/*===================================================================================================================================================================

                                                                  DIV overlay text - TO EDIT
=========================================================================================================================================================================================*/

D_divText = "Your feedback on our website is very important to us so we can make it work better for you.<BR/>We have asked TNS Conversa, an independent market research firm, to conduct an online survey on our behalf.<BR/>To reward you for spending about 10 minutes of your time, we will put you in the draw to win a $200 Prezzy Card.<BR/>The survey results are strictly confidential and will be used for statistical purposes only - individual responses will not be identified. This is purely market research - we are not trying to sell you anything.";


//Button text
D_AcceptText="Yes, I am happy to take part in the survey";
D_DeclineText="No, I would not like to take part in the survey";

//URL link to your survey
D_AcceptLink="http://online.tns-global.com/wix/p660707835.aspx?WebPage=7";
D_DeclineLink="http://online.tns-global.com/wix/p660707835.aspx?WebPage=7&Refused=1";

//survey image
D_surveyWindow="takeSurvey6.jpg";


/*=====================================================================================================================================================================
                                                             DO NOT EDIT THE CODE BELOW THIS 
=======================================================================================================================================================================*/
D_urlon = window.location.href;


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=\"http://cdn.tns-global.com/multimedia/NZ/NZPostMonitor/overlayinvites/"+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/><a href=\"javascript:D_hideDiv();\"><a href=\""+D_DeclineLink+"\" target=\"_blank\" onClick=\"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/NZ/NZPostMonitor/overlayinvites/butt.jpg\"></a></div><div id=\"GIS_InviteButton2\"><a href=\""+D_DeclineLink+"\" target=\"_blank\" onClick=\"D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/multimedia/NZ/NZPostMonitor/overlayinvites/butt.jpg\"></a></div><div id=\"GIS_closeButton\"><a href=\""+D_DeclineLink+"\" target=\"_blank\" onClick=\"D_hideDiv();\"><img class=\"GIS_img\" src=\"http://cdn.tns-global.com/multimedia/NZ/NZPostMonitor/overlayinvites/closebutt.png\"></a></div>");
}

function D_MoveDiv(){
D_element = document.getElementById("GIS_InviteImageClass");

D_element.style.top = (D_Div_ypos+"px");
D_element.style.left = (D_Div_xpos+"px");
//
var D_element2 = document.getElementById("GIS_InviteText");
var D_newypos = D_Div_ypos+90;
var D_newxpos = D_Div_xpos+33;
D_element2.style.top = (D_newypos+"px");
D_element2.style.left = (D_newxpos+"px");
//
var D_element2 = document.getElementById("GIS_InviteLink");
var D_newypos2 = D_newypos+140;
var D_newxpos2 = D_newxpos;
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+138;
var D_newxpos3 = D_newxpos+390;
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+165;
var D_newxpos4 = D_newxpos+390;
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-70;
var D_newxpos6 = D_newxpos+435;
D_element6.style.top = (D_newypos6+"px");
D_element6.style.left = (D_newxpos6+"px");
//
var D_element2 = document.getElementById("GIS_popupInvite");
D_element2.style.top = "0px";
D_element2.style.left = "0px";

}

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
	
	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();











