function getCookie(name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return null;
}

function setCookie(name,value,expires,path,domain,secure) {
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires ) {
		expires = expires * 1000 * 60; // time in minutes
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );

    return null;
}

function closeSurvey(mode) {
	var surveyform = document.getElementById('surveyform');
	if (surveyform) surveyform.style.display = 'none';	
	setCookie('lsrreview',mode,'525600','/','','');
	return false;
}

function postponeSurvey(timeout) {
	var surveyform = document.getElementById('surveyform');
	if (surveyform) surveyform.style.display = 'none';	
	setCookie('lsrreview','postponed',timeout,'/','','');
	return false;
}

function completedSurvey() {
	var surveyform = document.getElementById('surveyform');
	if (surveyform) surveyform.style.display = 'none';	
	setCookie('lsrreview','completed','525600','/','',''); // 12 months
	return true; // process submit
}

function clearCookies() {
	setCookie('lsrreview','','-60','/','','');
	setCookie('lsrvisit','','-60','/','','');
	return false;
}

function surveyLoad(freq,delay) {
  //document.write("<code>"+url+"</code>");
  
  var visitcookie = getCookie("lsrvisit");
  
  var surveylive = 0; // set to 0 to turn off survey remotely
  var cookieinfo = getCookie("lsrreview");
  var randomnumber = Math.floor(Math.random()*11)

  if (visitcookie == null && cookieinfo == null) { // not been here before
	var today = new Date();
  	setCookie("lsrvisit",today.getTime(),'60','/','','');
    if (delay >= 0.1) {
      postponeSurvey(delay);
      return null;
    }
  }
      
  if (randomnumber/10 <= freq) {
   if (cookieinfo == null && surveylive == 1) {
		var html = "<div id='surveyform' style='width: 99%; z-index: 1000; position: absolute; top: 50px; margin: 0; height: 500px; font-family: arial, helvetica, sans-serif;'><div style='width: 600px; margin: 0 auto; border: 10px #5E2093 solid; box-shadow: 0 0 20px #aaa; padding: 20px; background: white;'><img src='http://visitors.helpfultechnology.com/lsrreview/lsrlogo.png' alt='Leicestershire Statistics and Research Online' style='float:left;' /><h2 style='padding: 7px 0 0 0; margin: 0; text-align:right;'>Visitor survey</h2><form action='http://visitors.helpfultechnology.com/lsrreview/submit.php' method='post'><p style='clear:both; padding-top: 10px;'>We'd like to find out what kinds of people visit this website and what they're looking for, so we can improve it. Can you help answer these short questions?</p><p><a onclick='postponeSurvey(10);' href='"+document.location.href+"' style='border: 1px solid #eeb; background: #ffc; padding: 3px; margin-right: 10px;'>Not right now, ask me later</a><a onclick='closeSurvey(\"rejected\");' href='"+document.location.href+"' style='border: 1px solid #fcc; background: #fee; padding: 3px; margin-right: 10px;'>No, please don't ask me again</a></p><p><label>Which of the following best describe your occupation:<br /><select name='q[visitortype]'><option value='' selected='selected'>--Please select--</option><option value='centralgovernment'>Central Government</option><option value='staff'>Leicestershire County Council</option><option value='localauthority'>Other Local Authority (e.g. City/District/Parish Council)</option><option value='localother'>Local public service organisation (e.g. Health, Emergency Services, LEP)</option><option value='privatesector'>Private Sector</option><option value='political'>MP, Councillor or political researcher</option><option value='academic'>Student or academic</option><option value='media'>Journalist</option><option value='public'>Member of the public</option><option value='other'>None of the above</option></select></label></p><p>In a few words, what are you looking for on this site today?<br /><input type='text' name='q[infosought]' size='50' style='width: 99%;' /></p><p><label>How easy to find using this website?</label><br /><select name='q[usefulness]'><option value=''>--Please select--</option><option value='very'>Very easy</option><option value='fairly'>Fairly easy</option><option value='notvery'>Not very easy</option><option value='notatall'>Not at all easy</option><option value='noopinion'>Don't know/no comment</option></select></p><p>Based on any previous visits, what suggestions, if any, do you have for how we might improve this website?<br /><textarea name='q[feedback]' cols='50' rows='3' style='width: 99%;'></textarea><p>If you would be willing to help in further research to  improve this website, please enter your email address below - you won't be added to any other lists or have your details passed on:<br /><input type='text' name='q[followup]' size='50' style='width: 99%;' /> </p><p><input type='submit' value='Send feedback' onclick='completedSurvey();' /><input type='hidden' name='q[sourceurl]' value='"+document.location.href+"' /> -- Thank you for your help!</p></form></div></div>";

		document.write(html);
   } 
  }
   
  return null;
}




