var now=new Date(); //the current date of the day
var hitcounter = '_hitcounter'; //This cookie stores the clicks on the website. Rename this field to anything you like
var constLaunched = '_launched'; //Stores wheter or not the popup was shown
var constBreak = new Date( now.getTime()+(3600000)); //time in milliseconds for hitcounter cookie
var constExpire = new Date( now.getTime()+(15552000000)); //time in milliSec for constLaunched cookie (6 month)
var clicks = 3; //quantity of clicks
var probability = 0; //randomizing for execution "umfrage" 25% = 0.25   100% = 1


/* Popup settings */
var url = "https://www.nlr-surveys.de/uc/admin/3ecb/";
var height 				= 570;
var width 				= 760;
var toolbar 			= 'no';
var statusbar 		= 'no';
var menubar 			= 'no';
var resizable 		= 'no';
var scrollbars 		= 'yes';
var positionLeft 	= 0
var positionTop 	= 0;



// don't do anything if the survey has already been shown.
//This is based on a cookie which has to be present
if ((document.cookie.indexOf(constLaunched + '=true') == -1))
{
	if (document.cookie.indexOf(hitcounter) == -1)
	{
		// create hit-count cookie and set hits to 1
		document.cookie= hitcounter + '=1;expires=' + constBreak.toGMTString() + ';path=/';
	}
	else
	{
		// cookie parsing
		KeyAnfang 	= document.cookie.indexOf(hitcounter + '=') + 1;
		KeyLen 		= hitcounter.length;
		WertEnde  	= document.cookie.indexOf(";", KeyAnfang);
		if(WertEnde == -1) WertEnde = document.cookie.length;
		hitcounterValue = document.cookie.substring( parseInt(KeyAnfang, 10) + parseInt(KeyLen, 10),WertEnde);
		hitcounterValue = parseInt(hitcounterValue, 10) + 1;
		
		// update the hitcounter cookie
		document.cookie = hitcounter + '=' + hitcounterValue + ';expires=' + constBreak.toGMTString() + ';path=/';
		
		if(hitcounterValue >= clicks)
		{
			//probability of the popup beeing shown
			rand = Math.random();
			if (rand <= probability)
			{
				linkmodifier();
			}
		}
	}
}



/**
*	Open popup showing the first page of the survey
*/
function openPopupNLR()
{
	targetWin = window.open(url,"NLRsurvey","toolbar="+toolbar+",status="+statusbar+",menubar="+menubar+",width="+width+",height="+height+",resizable="+resizable+",scrollbars="+scrollbars+",left="+positionLeft+",top="+positionTop);	

}

/**
* Sets the cookie to remember that the survey has already been started
*/
function setCookie()
{
	document.cookie= constLaunched + '=true;expires=' + constExpire.toGMTString() + ';path=/';
}


/**
*Modifying the DOM Tree. Change all anchor tags.
*/
function linkmodifier()
{

	if (!document.getElementsByTagName) return;

	//get all anchors
	var anchors = document.getElementsByTagName('a');	

	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];

		//search for attribute "href" in that anchor
		if (anchor.getAttribute("href"))
		{
		
			var value_href = anchor.getAttribute("href");
			
			//if onclick or something similiar already exist
			if (value_href.indexOf('javascript') == 0 ||  anchor.getAttribute("onclick"))
			{
			
			}	else	{
			
				anchor.onclick=function()
				{
					
					setCookie();
					openPopupNLR();
				}
				 
			}
		}
	}
}


