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('bissurvey',mode,'525600','/','','');
	return false;
}

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

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

function surveyLoad(freq) {
  //document.write("<code>"+url+"</code>");
  
  var cookieinfo = getCookie("bissurvey");
  //document.write(cookieinfo);
  var ref = document.referrer;
  var berrmatch = ref.match(/berr.gov.uk/gi);
  var diusmatch = ref.match(/dius.gov.uk/gi);
  var govboxmatch = ref.match(/govbox.org.uk/gi);

  if ( (berrmatch || diusmatch || govboxmatch) && cookieinfo == null) {
    closeSurvey('referred');
    return null;
  }
  
  var randomnumber = Math.floor(Math.random()*11)
  
  if (randomnumber/10 <= freq) {

  var $now = new Date().getTime();
  var $end = 1255513010941; // survey expires roughly Wed 14 October ish
  var $end = 1256256001000; // survey expires roughly Fri 23 October ish
  
   if (cookieinfo == null && $now < $end) {
  	var html = "<div id='surveyform' style='width: 99%; position: absolute; top: 50px; margin: 0; height: 500px; text-align:center; font-family: arial, helvetica, sans-serif;'><div style='width: 500px; margin: 0 auto; border: 10px darkblue solid; padding: 20px; background: white; text-align: left;'><h2 style='padding: 0; margin: 0;'>Website visitor survey</h2><form action='http://dev.bis.gov.uk/visitorsurvey/submit.php' method='post'><p>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?</p><p><a onclick='postponeSurvey();' 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='#' 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 you:<br /><select name='q[visitortype]' style='border: 1px solid #aaa;'><option value=''>--Please select--</option><option value='business'>Business owner/senior manager</option><option value='advisor'>Business advisor (e.g. accountant, consultant, lawyer)</option><option value='policygovt'>Policy specialist/researcher - in government</option><option value='policynongovt'>Policy specialist/researcher - outside government</option><option value='academic'>Academic, lecturer or student</option><option value='media'>Journalist</option><option value='public'>Member of the public</option><option value='staff'>I work for BIS or one of its delivery partners</option><option value='other'>Other (please specify)</option></select></label></p><p style='margin: 5px auto;'><label>If other, please specify: <input type='text' name='q[othertype]' size='20' style='border: 1px solid #aaa;' /></label></p><p style='margin: 5px auto;'><label>How often do you visit this website? <br /><select name='q[visitfreq]' style='border: 1px solid #aaa;'><option value=''>--Please select--</option><option value='constantly'>Constantly/all the time</option><option value='weekly'>Weekly</option><option value='monthly'>Monthly</option><option value='nowagain'>Now and again</option><option value='firsttime'>First time here</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%; border: 1px solid #aaa;' /></p><p>We appreciate you may have only just landed on this site. But if you have any feedback or suggestions for improvement, please use the box below:<br /><textarea name='q[feedback]' cols='50' rows='3' style='width: 99%; border: 1px solid #aaa;'></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' style='border: 1px solid #aaa;' /><input type='hidden' name='q[sourceurl]' value='"+document.location.href+"' /> -- Thank you for your help!</p></form></div></div>";
  	
  	document.write(html);
   }
 
  }
   
    return null;
}

