/*
Author: David Penty (The British Library)
Date:   11th September 2008
Why?:   Pop up a request to fill out a questionnaire
*/


//frequency: 0=never, 1=everytime, 5=one in five... I'm sure you get the idea...
var frequency = 1;

//use cookies to stop people seeing the request if they have already had a request
//and expire the cookie after the specified number of Days
//give the cookie a new name so it doesn't interfere with other surveys
var enableCookies=true;
/*var enableCookies=false;*/
var cookieDuration=30;
var nameOfCookie='cim2010survey';
/*var nameOfCookie='eblj2009survey';*/
/*var nameOfCookie='Dec2008WebSurvey';*/
/*var nameOfCookie='Nov2008BIPCWebSurvey';*/




function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-300;//150 is half popup's height
	popUpDiv.style.top = (popUpDiv_height ) + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function notAlwaysPopup(windowname)
{
	var cookieExists = getCookie(nameOfCookie);
 	
 	if (cookieExists==null) 
 	{
		if (frequency > 0)
		{
			var randomness = Math.floor(Math.random() * frequency)	+ 1;
			if(randomness == 1)
			{	
				popup(windowname);
				if (enableCookies)
				{
					setCookie(nameOfCookie, 'yes', cookieDuration)
				}
			}
		}
	}

}

function theySaidYes(windowname)
{
	var newWindow;	
	newWindow= window.open('http://www.bl.uk/surveys/catim/index.html');
	/*newWindow= window.open('http://www.bl.uk/surveys/eblj/survey.html');*/
	/*newWindow= window.open('http://www.bl.uk/surveys/bipc/febmar09.html');*/
	if (window.focus) {newWindow.focus()};
	popup(windowname);
}

function popup(windowname) {

	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}



function getCookie(NameOfCookie)
{
	if (document.cookie.length > 0) 
	{
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) 
		{
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		} 
	}
	return null; 
} 
 
 



function setCookie(NameOfCookie, value, expiredays) 
{
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}


