/*
Overlay+Entry Script
Modified by: 		MetrixLab
Description: 		Script 
Latest update on: 	14-10-2005
29-09-2005: 		changed VBscript to JavaScript, made variables unique, added variable metrixlab_privacyLanguage
						reduced usage of hardcoded projectNumber variables
30-09-2005: 		changed loading of overlay: image is now loaded before the overlay is shown.
14-10-2005: 		changed extra variables. Shorter list to edit.
18-10-2005:		changed extra variables. Cookie survive and ForceData
14-12-2005:		combine the entry-exit technique
17-01-2006:		Insert an extra layer to avoid the problem with forms attributes and Flash objects
04-08-2006:		fixed critical bug in metrixlab_checkStartSurvey: onload and onabort event where wrong
						added metrixlab_startOverlay which dynamically adds the iframe, imagemap and div to the page via DOM
							Iframe widht and height are now set in metrixlab_loadOverlayCreative
						added metrixlab_onready which executes the script when the DOM is loaded (not the page, but sooner)
*/

var metrixlab_inViteIntercept = 0;
//var metrixlab_myImage;


//change ONLY these variables according to the project!
//----------------------------------------------------------------------------------------------------------------------------
	//type overlay     	: 'entry', 'exit' or 'entryexit'
	var metrixlab_type_overlay = 'entry';

	//If survey is entryexit, please give the ProjectEntryFolderName and the PopupFolderName
	var metrixlab_ProjectEntryFolderName = 'p07401';
	var metrixlab_PopupFolderName = 'p07401';


	var metrixlab_projectNumber = 'p07401';
	var metrixlab_privacyLanguage = 'nl';

	//Position of the overlay, standard = 100,100
	var metrixlab_DivStartLeft = '100';
	var metrixlab_DivStartTop = '100';
	
	var metrixlab_overlay_width = 335;
	var metrixlab_overlay_height = 295;

	var metrixlab_coordinate_yesbutton = '80,242,121,275';
	var metrixlab_coordinate_nobutton = '215,242,256,275';
	var metrixlab_coordinate_closebutton = '313,3,337,25';
	var metrixlab_coordinate_privacybutton = '211,279,260,297';
	var metrixlab_coordinate_metrixbutton = '259,279,337,297';
	var metrixlab_coordinate_algemeenbutton = '188,166,275,179';

	//Counting days till cookie must expire, standard = 180 days
	var metrixlab_expire_date_cookie = 180;
	

	//ForceData is needed for the Channel Profiler surveys, if '0' then there is no ForceData, if '1,1' then ForceData is 1 
	//If '0' then there is no ForceData, if '1,1' then ForceData is 1 
	//var metrixlab_ForceData = '0';
	//metrixlab_ForceData = '1'


	//Define the intercept for the channels
	var metrixlab_intercept_channel_1 = 'interceptWidth_windowsvista'
	var metrixlab_intercept_channel_2 = 'interceptWidth_ondernemers'
	var metrixlab_intercept_channel_3 = 'interceptWidth_beveiliging'
	var metrixlab_intercept_channel_4 = 'interceptWidth_thuisgebruikers'
	var metrixlab_intercept_channel_5 = 'interceptWidth_publiekesector'
	var metrixlab_intercept_channel_6 = 'interceptWidth_onderwijs'
	var metrixlab_intercept_channel_7 = 'interceptWidth_itmanager'
	var metrixlab_intercept_channel_8 = 'interceptWidth_partners'
	var metrixlab_intercept_channel_9 = 'interceptWidth_technet'

	//If Overlay must move	: '0' = inactive
	//		      	: '1' = active
	var metrixlab_MoveOverlayActive = '0';

	//Number of pixels every move of the overlay
	var metrixlab_MovePixels = 1;

	//Number of pixels where the overlay must stop
	var metrixlab_MoveStop = 550;

	//Speed
	var metrixlab_MoveSpeed = 90;

	//Voor Domain gedeelte
	//var metrixlab_domain = "opinionbar.com"
	//var metrixlab_domain = document.domain;
	
//----------------------------------------------------------------------------------------------------------------------------


	if (metrixlab_ForceData == '1') 
	{
		metrixlab_inViteIntercept=40;
	}

	if (metrixlab_ForceData == '2') 
	{
		metrixlab_inViteIntercept=10;
	}

	if (metrixlab_ForceData == '3') 
	{
		metrixlab_inViteIntercept=15;
	}

	if (metrixlab_ForceData == '4') 
	{
		metrixlab_inViteIntercept=1;
	}

	if (metrixlab_ForceData == '5') 
	{
		metrixlab_inViteIntercept=1;
	}

	if (metrixlab_ForceData == '6') 
	{
		metrixlab_inViteIntercept=1;
	}

	if (metrixlab_ForceData == '7') 
	{
		metrixlab_inViteIntercept=1;
	}

	if (metrixlab_ForceData == '8') 
	{
		metrixlab_inViteIntercept=1;
	}

	if (metrixlab_ForceData == '9') 
	{
		metrixlab_inViteIntercept=1;
	}
	
//alert("!!!!!!!!!!!!!!!!!!!!!!!!!! \nintercept: 1 op "+metrixlab_inViteIntercept);





var metrixlab_name = "shownMetrixLabPopup"+metrixlab_projectNumber;
var metrixlab_popup_intercept = 1;
var metrixlab_continueLoading = true;
var metrixlab_userID = "";
var metrixlab_tryCount = 0;





var metrixlab_userid = getUserID();


function getUserID(){
	var userIDCode;
	var Today = new Date();
	userIDCode=''+Today.getFullYear()+'_'+(Today.getMonth()+1)+'_'+Today.getDate()+'_'+Today.getHours()+'_'+Today.getMinutes()+'_'+Today.getSeconds()+'_'+Today.getMilliseconds();
	return userIDCode;
}


function metrixlab_showInvite(){
	metrixlab_randomNumber = metrixlab_GetRandomNumber(1,metrixlab_inViteIntercept);
	
	//alert("!!!!!!!!!!!!!!!!!!!!!!!!!! \nrandom: "+metrixlab_randomNumber);

	if (metrixlab_randomNumber==1)
	{
		metrixlab_setCookie("shownMetrixLabPopup"+metrixlab_projectNumber,""+(1));
		metrixlab_userID = getUserID();
		metrixlab_loadOverlayCreative();
	}
}





function metrixlab_getCookie(metrixlab_name){
	var metrixlab_search = metrixlab_name + "=";
	var metrixlab_offset = 0;
	var metrixlab_end = 0;
	if (document.cookie.length > 0){
		metrixlab_offset = document.cookie.indexOf(metrixlab_search);
		if (metrixlab_offset != -1){
			metrixlab_offset += metrixlab_search.length;
			metrixlab_end = document.cookie.indexOf(";", metrixlab_offset) ;
			if (metrixlab_end == -1){
				metrixlab_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(metrixlab_offset, metrixlab_end));
		}
	}
}






function metrixlab_setCookie (metrixlab_name, metrixlab_value){
	var expDays = metrixlab_expire_date_cookie;
	var metrixlab_expires = new Date();
	metrixlab_expires.setTime(metrixlab_expires.getTime() + (expDays*24*60*60*1000));

	document.cookie = metrixlab_name + "=" + escape(metrixlab_value) + "; expires=" + metrixlab_expires.toGMTString() + "; path=/";

}

function metrixlab_GetRandomNumber(metrixlab_minValue,metrixlab_maxValue){
	var metrixlab_result=0;

	if (metrixlab_minValue<=metrixlab_maxValue){
		metrixlab_result=Math.round((Math.random()*(metrixlab_maxValue-metrixlab_minValue+1))+metrixlab_minValue-0.5);
	}
	return metrixlab_result;
}

function metrixlab_showPopup(){
    var metrixlab_shownPopupCount = metrixlab_getCookie("shownMetrixLabPopup"+metrixlab_projectNumber);
	
	if (metrixlab_shownPopupCount=='yes'){
		metrixlab_shownPopupCount = 1;
		CloseBanner();
	}
	if (!(metrixlab_shownPopupCount>0)){
		metrixlab_shownPopupCount = 0;
	}
//alert("shownPopupCount=" + shownPopupCount);
//alert("inViteIntercept=" + inViteIntercept);

	if (metrixlab_shownPopupCount<1){
		metrixlab_randomNumber = metrixlab_GetRandomNumber(1,metrixlab_popup_intercept);
		if (metrixlab_randomNumber==1){
			metrixlab_showInvite();
		}
	}
}

// Plaatje geladen?
function metrixlab_checkImageLoaded(){
	metrixlab_imageIsLoaded = metrixlab_myImage.complete;
	if (metrixlab_imageIsLoaded){
		//broken link image in IE = 25, in FF = 0. So inViteIntercept is OK
		if ((metrixlab_myImage.width<25)||(metrixlab_myImage.width>25)){
			metrixlab_inViteIntercept = metrixlab_myImage.width;
			metrixlab_showPopup();
		}
	}else{
		if ((metrixlab_continueLoading)&&(metrixlab_tryCount<5)){
			metrixlab_tryCount++;
			setTimeout('metrixlab_checkImageLoaded()',1000);
		}
	}
}


/*
// Check Intercept
function metrixlab_checkStartSurvey(){
	metrixlab_myImage = new Image();

	if (metrixlab_ForceData == '1') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_1+".gif";
	}

	if (metrixlab_ForceData == '2') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_2+".gif";
	}

	if (metrixlab_ForceData == '3') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_3+".gif";
	}

	if (metrixlab_ForceData == '4') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_4+".gif";
	}

	if (metrixlab_ForceData == '5') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_5+".gif";
	}

	if (metrixlab_ForceData == '6') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_6+".gif";
	}

	if (metrixlab_ForceData == '7') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_7+".gif";
	}

	if (metrixlab_ForceData == '8') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_8+".gif";
	}

	if (metrixlab_ForceData == '9') 
	{
		metrixlab_myImage.src = "http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/"+metrixlab_intercept_channel_9+".gif";
	}

	metrixlab_myImage.onload = metrixlab_checkImageLoaded;
	metrixlab_myImage.onabort = metrixlab_checkImageLoaded;
}

*/

// Close Overlay
function CloseBanner()
{
	if (document.getElementById)
	{
		var the_layer = document.getElementById('metrixlab_overlay');
		the_layer.style.left = '-2000px';
		the_layer.style.top  = '-2000px';
		var IframeOverlay_close = document.getElementById('metrixlab_iframe');
		IframeOverlay_close.style.display = "none";
	}
}

// Go to Privacy 
function GoToPrivacy()
{
	PopupPrivacy = window.open('http://www.opinionbar.com/'+metrixlab_privacyLanguage+'/privacy.asp','PopupPrivacy','resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,width=750,height=450');
}

// Go to MetrixLab
function GoToMetrixLab()
{
	PopupPrivacy = window.open('http://www.metrixlab.com/','PopupPrivacy','resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,width=750,height=450');
}


// Go to Algemeen
function GoToAlgemeen()
{
	PopupPrivacy = window.open('http://invitation.opinionbar.com/popups/p07401_windowsvista/algemeen.htm','PopupPrivacy','resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,width=750,height=450');
}

// Go to Websurvey
function GoToWebSurvey()
{

	// EntrySurvey
	if (metrixlab_type_overlay=='entry')
	{
			if (metrixlab_ForceData=='0')
			{
					PopupWebSurvey = window.open('http://websurvey.opinionbar.com/go.asp?s='+metrixlab_projectNumber+'&t=4','PopupWebSurvey','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=750,height=450');
			}
			else
			{
					PopupWebSurvey = window.open('http://websurvey.opinionbar.com/go.asp?s='+metrixlab_projectNumber+'&t=4&f=;1,'+metrixlab_ForceData+'','PopupWebSurvey','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=750,height=450');
			}	
	}	

	// ExitSurvey
	if (metrixlab_type_overlay=='exit')
	{
			if (metrixlab_ForceData=='0')
			{
				PopupWebSurveyExit = window.open('http://www.zester.nl/invitation.html?u='+metrixlab_userid+'','PopupWebSurveyExit','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=366,height=300,copyhistory=yes');
			}
			else
			{
				PopupWebSurveyExit = window.open('http://www.zester.nl/invitation.html?u='+metrixlab_userid+'&f=;'+metrixlab_ForceData+',','PopupWebSurveyExit','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=366,height=300,copyhistory=yes');
			}
	}

	
	
	// EntryExitSurvey
	if (metrixlab_type_overlay=='entryexit')
	{
			PopupWebSurveyExit = window.open('./invitation.html?u='+metrixlab_userid+'','PopupWebSurveyExit','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=366,height=300,copyhistory=yes');
	
			if (metrixlab_ForceData=='0')
			{
		   			PopupWebSurvey = window.open('http://websurvey.opinionbar.com/go.asp?s='+metrixlab_ProjectEntryFolderName+'&u='+metrixlab_userid+'','PopupWebSurvey','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=750,height=450');
			}
			else
			{
					PopupWebSurvey = window.open('http://websurvey.opinionbar.com/go.asp?s='+metrixlab_ProjectEntryFolderName+'&f=;'+metrixlab_ForceData+'&u='+metrixlab_userid+'','PopupWebSurvey','resizable=yes,scrollbars=yes,toolbar=no,menubar=no,width=750,height=450');
			}	
	}
	
	CloseBanner();
}


// Show Overlay
function metrixlab_loadOverlayCreative()
{
	if (!(metrixlab_type_overlay == 'entry' || metrixlab_type_overlay == 'exit' || metrixlab_type_overlay == 'entryexit'))
	{
			alert('Variable: metrixlab_type_overlay is not an entry, exit or entryexit value!!');
			alert('Overlay cannot be created.');
	}
	else
	{
		var IframeOverlay = document.getElementById('metrixlab_iframe');
		IframeOverlay.style.display = "block";
		
		if (document.all) 
		{
			IframeOverlay.width = metrixlab_overlay_width;
			IframeOverlay.height = metrixlab_overlay_height;
		}
		else
		{
			IframeOverlay.width = metrixlab_overlay_width - 2;
			IframeOverlay.height = metrixlab_overlay_height - 2;
		}
		
		var overlay_gif = document.getElementById('metrixlab_overlay_gif');	
		overlay_gif.width = metrixlab_overlay_width;
		overlay_gif.height = metrixlab_overlay_height;
		
		var overlay = document.getElementById('metrixlab_overlay');
		overlay.style.visibility = 'visible';
		
		if (metrixlab_MoveOverlayActive=='1')
		{
			metrixlab_moveOverlay();
			setTimeout("CloseBanner();",15000);
		}
	}
}


function metrixlab_moveOverlay()
{
 var overlay = document.getElementById('metrixlab_overlay');
 var overlay_left = new String(overlay.style.left);
 
 if (parseInt(overlay_left)<parseInt(metrixlab_MoveStop))
 {
  overlay.style.left = (parseInt(overlay.style.left)+parseInt(metrixlab_MovePixels)) + "px";
  setTimeout("metrixlab_moveOverlay();",parseInt(metrixlab_MoveSpeed));
 }
}

function metrixlab_onready(el, func){
	this.args = new Array(el, func)
	this.doTry = function(){
			try{
					var el = eval(this.args[0])
					el.onloading = this.args[1]
					el.onloading()
					clearInterval(this.args[2])

			}
			catch(e){}
	}
	this.doTry.bind = function(object){
			var method = this;
			return function(){
					method.apply(object);
			}
	}
	this.args[2] = setInterval(this.doTry.bind(this), 250);
	return this
}

function metrixlab_startOverlay()
{
	var MLbody = document.getElementsByTagName('body').item(0);
	
	var MLmap = document.createElement('map');
	MLmap.id = 'metrixlab_popup_map';
	MLmap.name = 'metrixlab_popup_map';
	MLmap.innerHTML = "<AREA shape='rect' coords='"+metrixlab_coordinate_yesbutton+"' href='JavaScript:GoToWebSurvey();' target='_self'>";
	MLmap.innerHTML += "<AREA shape='rect' coords='"+metrixlab_coordinate_nobutton+"' href='JavaScript:CloseBanner();' target='_self'>";
	MLmap.innerHTML += "<AREA shape='rect' coords='"+metrixlab_coordinate_closebutton+"' href='JavaScript:CloseBanner();' target='_self'>";
	MLmap.innerHTML += "<AREA shape='rect' coords='"+metrixlab_coordinate_privacybutton+"' href='JavaScript:GoToPrivacy();' target='_self'>";
	MLmap.innerHTML += "<AREA shape='rect' coords='"+metrixlab_coordinate_metrixbutton+"' href='JavaScript:GoToMetrixLab();' target='_self'>";
	MLmap.innerHTML += "<AREA shape='rect' coords='"+metrixlab_coordinate_algemeenbutton+"' href='JavaScript:GoToAlgemeen();' target='_self'>";
	MLbody.insertBefore(MLmap, MLbody.childNodes[0]);
	
	var MLiframe = document.createElement('iframe');
	MLiframe.id = 'metrixlab_iframe';
	MLiframe.src = 'javascript:false;';
	MLiframe.scrolling='no';
	MLiframe.width='0';
	MLiframe.height='0';
	MLiframe.frameborder='0';
	MLiframe.style.zIndex = '98';
	MLiframe.style.position = 'absolute';
	MLiframe.style.top = metrixlab_DivStartTop + "px";
	MLiframe.style.left = metrixlab_DivStartLeft + "px";
	MLiframe.style.display = 'none';
	MLbody.insertBefore(MLiframe, MLbody.childNodes[0]);
	 
	var IframeOverlay = document.getElementById('metrixlab_iframe');
	IframeOverlay.style.zIndex = "98";
	IframeOverlay.style.position = "absolute";
	IframeOverlay.style.top = metrixlab_DivStartTop+"px";
	IframeOverlay.style.left = metrixlab_DivStartLeft+"px";
	 
	var MLoverlay = document.createElement('div');
	MLoverlay.id = 'metrixlab_overlay';
	MLoverlay.innerHTML = "<img src='http://invitation.opinionbar.com/popups/"+metrixlab_projectNumber+"/overlay.gif' USEMAP='#metrixlab_popup_map' alt='' border='0' width='0' height='0' name='metrixlab_overlay_gif' id='metrixlab_overlay_gif'>";
	MLoverlay.style.zIndex = "99";
	MLoverlay.style.position = "absolute";
	MLoverlay.style.visibility = "hidden";
	MLoverlay.style.top = metrixlab_DivStartTop+"px";
	MLoverlay.style.left = metrixlab_DivStartLeft+"px"; 
	MLbody.insertBefore(MLoverlay, MLbody.childNodes[0]);
	metrixlab_showPopup();
}

new metrixlab_onready("document.body", function(){metrixlab_startOverlay()});