


//Global variables
var surveyW = 260;
var surveyH = 200;



//Setup surveys
var initSurveys = function(){
	
	
	
	//Survey for the pensions section
	var TMSurvey = LGSurvey({
		
		name : "TMSurvey",
		section : "/findamortgageadviser.html",
		trigger : 1,
		path : "path=/;",
		expiry : "expires=1 Jan 2012 21:00:00 UTC;",
		url : "http://www.clicktools.com/survey?iv=b5120e6a59514d2",
		text : "Take part in our short survey to be in with a chance to win 150 pounds of Ticketmaster vouchers.",
		exclusions : []

});
	
};






























var LGSurvey = function(o){


	var __trigger = o.trigger-1;
	var __section = o.section;
	var __url = document.location.href;
	var __path = o.path;	
	var __expires = o.expires; 	
	var __exclusions = o.exclusions;
	var __exclude = false;
	
	//If the path of our survey matches any part of the current url, then setup/update the cookie and open the layer at the correct trigger point
	if(__url.indexOf(__section)!=-1){

		//If there is a cookie, then get the current value, increment it
		if(document.cookie.indexOf(o.name+"Count")!=-1){
			
			current = readCookie(o.name+"Count");
			if(current=="NaN"){current="1";}
			current = parseInt(current);
			done = readCookie(o.name+"completed");
			
			for(i=0;i<__exclusions.length;i++){
				if(__url.indexOf(__exclusions[i])!==-1){
					__exclude=true;
				}	
			}
			
			if(__exclude==false){
				if(current==__trigger && done=="false"){
					buildSurvey({
						survey : o,
						text : o.text,
						url : o.url
					});
					surveyPopup()
				}
				next = current+1;
				text = o.name+"Count="+next+";"+__path;
				document.cookie = text;				
			}
			
			

		}
		//If there isn't a cookie, then create one based on the name of the survey running
		else{
			document.cookie = o.name+"completed=false;"+__path+__expires;
			document.cookie = o.name+"Count=1;"+__path;
		}
	
	
	}
	//If the path of our survey does not match the current url, and there is a relevant cookie set then open the layer 
	else{
		
		if(document.cookie.indexOf(o.name+"Count")!=-1){
			done = readCookie(o.name+"completed");
			if(done=="false"){
				buildSurvey({
					survey : o,
					text : o.text,
					url : o.url
				});
				surveyPopup();
			}	

		}

	}
	
}




















//Read cookie
var readCookie= function(targ){
	var ckie = document.cookie.split(";");
	var pCount = new Array();
	var curr;
	for(i=0;i<ckie.length;i++){
		if(ckie[i].indexOf(targ)!=-1){
			pCount = ckie[i].split("=");
			curr = pCount[1];
		}
	}
	return curr;
}






//----------------------------------
// Functions to create the HTML for the survey
//----------------------------------

//Builds the HTML for the layer
var buildSurvey = function(o){
	
	var sText = o.text;
	var sLink = o.url;
	
	var targ = document.getElementById("container");
	var sBg = document.createElement("div");
	sBg.innerHTML = "&nbsp;";
	sBg.id = "survey-bg";

	var s = document.createElement("div");
	s.id = "survey";

	var sInn = document.createElement("div");
	sInn.id = "survey-inner";
	
	var sPadd = document.createElement("div");
	sPadd.className = "padding";
	
	var sPara = document.createElement("p");
	sPara.appendChild(document.createTextNode(sText));
	sPara.className = "question";
	
	var sUl = document.createElement("ul");
	var sLi1 = document.createElement("li");
	var sLi2 = document.createElement("li");
	
	var sImg1 = document.createElement("img");
	sImg1.src = "/images/survey-yes.gif";
	sImg1.alt = "Yes, begin the survey";
	sImg1.width = 194;
	sImg1.height = 22;
	
	var sImg2 = document.createElement("img");
	sImg2.src = "/images/survey-no.gif";
	sImg2.alt = "No thank you";
	sImg2.width = 194;
	sImg2.height = 22;
	
	var sA1 = document.createElement("a");
	sA1.href = sLink;
	sA1.onclick = function(){openWin(this,o.survey);return false;}
	
	var sA2 = document.createElement("a");
	sA2.href = "#";
	sA2.onclick = function(){surveyPopDown(o.survey);}	
	
	sA1.appendChild(sImg1);
	sA2.appendChild(sImg2);
	
	sLi1.appendChild(sA1);
	sLi2.appendChild(sA2);
	
	sUl.appendChild(sLi1);
	sUl.appendChild(sLi2);
	sPadd.appendChild(sPara);
	sPadd.appendChild(sUl);
	
	sInn.appendChild(sPadd);
	s.appendChild(sInn);
	
	targ.parentNode.appendChild(sBg);
	targ.parentNode.appendChild(s);
	
};
	
	
	
	
	
//Popup survey
var surveyPopup	= function(){
	currURL = document.location.href;
	selects("hidden");
	var lyr = document.getElementById("survey-bg");
	lyr.style.display="block";
	var lyr2 = document.getElementById("survey");
	lyr2.style.display="block";
	var lyr3 = document.getElementById("survey-inner");
	lyr3.style.display="block";
	w=getBrowserWidth();
	h=getBrowserHeight();
	newtop = (h-surveyH)/2;
	newleft = (w-surveyW)/2;
	lyr3.style.top=newtop+"px";
	lyr3.style.left=newleft+"px";
}
	
//Open survey window
var openWin = function(url,survey){
	document.cookie = survey.name+"completed=true;"+survey.path+survey.expires;
	var lyr = document.getElementById("survey-bg");
	lyr.style.display="none";
	var lyr2 = document.getElementById("survey");
	lyr2.style.display="none";
	var lyr3 = document.getElementById("survey-inner");
	lyr3.style.display="none";
	selects("visible");
	window.open(url,"Survey","width=525,height=500,scrollbars=yes");	
}
	
//PopDown survey
var surveyPopDown = function (survey){
	document.cookie = survey.name+"completed=true;"+survey.path+survey.expires;
	var lyr = document.getElementById("survey-bg");
	lyr.style.display="none";
	var lyr2 = document.getElementById("survey");
	lyr2.style.display="none";
	var lyr3 = document.getElementById("survey-inner");
	lyr3.style.display="none";
	selects("visible");
}

//Hides all selects on the page (IE issue)
var selects = function(sty){
	var sels = document.getElementsByTagName("select");
	for(i=0;i<sels.length;i++){
		sels[i].style.visibility = sty;
	}
}
	
//Functions to get the current screen width and height - to center the layer
var getBrowserWidth = function(){
	if (window.innerWidth){return window.innerWidth;}
	else if (document.documentElement && document.documentElement.clientWidth != 0){return document.documentElement.clientWidth;}
	else if (document.body){return document.body.clientWidth;}
	return 0;
}
	
var getBrowserHeight = function(){
	if (window.innerHeight){return window.innerHeight;}
	else if (document.documentElement && document.documentElement.clientHeight != 0){return document.documentElement.clientHeight;}
	else if (document.body){return document.body.clientHeight;}
	return 0;
}










