/* ------------------------------------------------------
 Surrey County Council
 JavaScript for public website satisfaction survey

 last modified: 20/07/2009 by Matthew Evans
 website:       http://www.surreycc.gov.uk/
--------------------------------------------------------- */

/*
Create the invitation overlay:
    The first three lines below create some inline styling to set the overlay positioning
    for Internet Explorer 6, which doesn't support the 'fixed' property in the 'position' attribute.
*/

strDiv = '</form><!-- close erroneous Lotus Notes form -->'
strDiv+= '<!--[if gte IE 5.5]><![if lt IE 7]><style type="text/css">'
strDiv+= 'div#scc-overlay {left: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + \'px\' ) !important; top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + \'px\' ) !important;}'
strDiv+= '</style><![endif]><![endif]-->'
strDiv+= '<link rel="stylesheet" href="http://www1.surreycc.gov.uk/scctemplate/survey/scc-survey.css" type="text/css" />';
strDiv+= '<div id="scc-overlay"></div><div id="scc-survey-invitation"><div id="scc-survey-invitation-inner">';
strDiv+='<div id="scc-survey-banner"><h2>Website satisfaction survey</h2></div>';
strDiv+='<a href="#" id="scc-survey-close" title="Close survey" onClick="closeInvite()"><span>Close survey</span></a>';
strDiv+='<div id="scc-survey-content">'
strDiv+='<p><strong>Dear website visitor</strong></p>'
strDiv+='<p>We value your opinions and would like to receive your views about our website.</p>';
strDiv+='<p>The survey will only take a few minutes to complete, following your visit to our website. All the information collected is covered by our privacy policy.</p>';
strDiv+='<p>The survey will appear <strong>after your visit</strong>.</p>';
strDiv+='<form name="invite"><input name="buttonYes" value="Do the survey" onclick="window.childWindow();" type="button" class="button" /> <input name="buttonNo" value="No thanks" onclick="closeInvite();" type="button" class="button" /></form>';
strDiv+='</div></div></div>'
//strDiv+='<form action=""><!-- re-open erroneous Lotus Notes form (previously closed above) -->'

/* Write the overlay to the screen */
//document.write (strDiv) //- moved this into the 'If' condition below.

/* Reset the expiry date of the cookie */
nowDate = new Date()
nowDate.setMonth(nowDate.getMonth() +6)
cookieExpires = nowDate.toGMTString()

/* Function to randomly determine whether survey layer should be presented to user */
function random()
{
    random.seed = (random.seed*random.a + random.c) % random.m;
    return random.seed / random.m;
}

random.m=714025;
random.a=4096;
random.c=150889;
random.seed = (new Date()).getTime()%random.m;

//to make survey pop up every time set frequency to 1
//frequency=1
frequency=0.2

// If user has NOT declined survey previously then show invitation layer on random basis
if (!userDeclineSurvey())
	{
		if(random() <= frequency)
		 {
			document.write (strDiv);
			showBox();
			
		}
	}

function showBox()
{
	var strUserAgt = navigator.userAgent;
	if (strUserAgt.indexOf("Firefox") != -1 || strUserAgt.indexOf("Netscape") != -1 || strUserAgt.indexOf("Opera") != -1){
		document.getElementById('scc-survey-invitation').style.visibility='visible';
		document.getElementById('scc-overlay').style.visibility='visible';
	}
	
	if (strUserAgt.indexOf("MSIE") != -1){
		document.all("scc-survey-invitation").style.visibility='visible';
		document.all("scc-overlay").style.visibility='visible';
	}
if (document.layers) {document.layers['scc-overlay'].visibility='show';}
if (document.layers) {document.layers['scc-survey-invitation'].visibility='show';}

}

/* Function to create the pop-up child window */
function childWindow()
{
	document.cookie="surveyUserResponse=false;expires=" + cookieExpires + ";path=/;domain=.surreycc.gov.uk";

	var childWin = window.open("","childWin","width=800,height=600,toolbar=no,menubar=no,scrollbars=yes,resizable=0,location=no,directories=no,status=no","replace=true");
	childWin.document.open("text/html");

	str='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>';
	str+='<title>Surrey County Council - Website satisfaction survey</title>' ;
	str = str +  '<script language="javaScript">';
	str+= 'DO = document.domain;';
	str+='checkWindow();';
	str+='function checkWindow(){';
	if (!document.layers){str+='try{;';};
	str+='if (!window.opener || window.opener.closed || window.opener.location.href.indexOf(DO) ==-1 || (document.referrer =="" && document.layers)){';
	str+= 'resizeWindow();';
	str+='clearTimeout(stTimeout)};';
	str+='stTimeout = setTimeout("checkWindow()",1000);';
	if (!document.layers){str+='}';};
	if (!document.layers){str+='catch(e){resizeWindow()};';};
	str+='}';
	
	str+='function resizeWindow(){';
	str+='window.resizeTo(800,600);';

	str=str + 'window.location = "http://www.surveymonkey.com/s.aspx?sm=qZSLJVIXyfBVtcYcb6q4iw_3d_3d";';
	
//	str+='window.opener.document.blur();';
	str+='if (window.opener){window.opener.blur()};';
	str+='window.focus();';
	str+='}';
	str+='window.blur();';
	str=str + '</script>';
	str=str + '<link rel="stylesheet" href="http://www.surreycc.gov.uk/css/scc-master.css" type="text/css" /><link rel="stylesheet" href="http://www1.surreycc.gov.uk/scctemplate/survey/scc-survey.css" type="text/css" />';
	str+='</head><body><div id="scc-survey-window"><div id="scc-survey-banner"><h1><span class="offscreen">Surrey County Council - </span>Website satisfaction survey</h1></div><div id="scc-survey-content"><h2>What to do next</h2><p>Please minimise this window and complete this survey <strong>after your visit</strong> to our website.</p><p>The survey should automatically load when you leave our site, but if this does not happen you can <a href="http://www.surveymonkey.com/s.aspx?sm=qZSLJVIXyfBVtcYcb6q4iw_3d_3d">activate&nbsp;the&nbsp;survey&nbsp;manually</a>.</p><p>To report a problem, please <a href="http://www.surreycc.gov.uk/contactus" target="_blank">contact us</a>.</p></div></div>';
	str=str + '</body></html>';

	childWin.document.write(str);
	childWin.document.close();

	if (!document.layers)
	{
		document.getElementById('scc-survey-invitation').style.visibility = 'hidden';
		document.getElementById('scc-overlay').style.visibility = 'hidden';
	}
	else
	{
		document.layers['scc-survey-invitation'].visibility='hide';
		document.layers['scc-overlay'].visibility='hide';
	}
}

function closeInvite()
{
	document.cookie="surveyUserResponse=false;expires=" + cookieExpires + ";path=/;domain=.surreycc.gov.uk";

	if (!document.layers)
	{
		document.getElementById('scc-survey-invitation').style.visibility = 'hidden';
		document.getElementById('scc-overlay').style.visibility = 'hidden';
	}
	else
	{
		document.layers['scc-survey-invitation'].visibility='hide';
		document.layers['scc-overlay'].visibility='hide';
	}
}


function userDeclineSurvey()
{

		if (document.cookie)
		{
			var myCookie = unescape(document.cookie);

			if (myCookie.indexOf('surveyUserResponse') > -1)
			{
					return true;
			}
			else
			{
					return false;
			}

		}
}