﻿// Cookies Directive Disclosure Script Version 1.0 by Ollie Phillips
function cookiesDirective(privacyPolicyUri,cookieScripts){
	
	// Have we already been granted permission, if so lets not keep doing this
	if(!cdReadCookie('cookiesDirective')){
		// Going to use JQuery, if we've not got it we load it
		if(!(window.jQuery)){
			var s = document.createElement('script');
			s.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js');
			s.setAttribute('type', 'text/javascript');
			document.getElementsByTagName('head')[0].appendChild(s);
		}
		
		// Give the HTTP request time to complete
		if(cookieScripts){
			setTimeout("cdHandler('"+ privacyPolicyUri +"','" + cookieScripts + "')",1000);
		}else{
			setTimeout("cdHandler('"+ privacyPolicyUri +"')",1000);
		}
	
	}else{
		if(!(window.jQuery)){
			var s = document.createElement('script');
			s.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js');
			s.setAttribute('type', 'text/javascript');
			document.getElementsByTagName('head')[0].appendChild(s);
		}
		// Cookies have been accepted, call our wrapper function here so that enclosed cookie setting scripts fire 
		setTimeout("cookiesDirectiveScriptWrapper()",1000);
	}
	
}

function cdHandler(privacyPolicyUri,cookieScripts){
	
	// Our main disclosure script
	$(document).ready(function(){

		var epdApps;
		var epdAppsCount;
		var epdAppsDisclosure;
		var epdPrivacyPolicyUri;
		
		epdPrivacyPolicyUri = privacyPolicyUri;
		
		// What scripts must be declared, user passes these as comma delimited string
		if (cookieScripts){
			epdApps = cookieScripts.split(',');
			epdAppsCount = epdApps.length;
			var epdAppsDisclosureText='';
			if(epdAppsCount>1){
				for(var t=0; t < epdAppsCount - 1; t++){
					epdAppsDisclosureText += epdApps[t] + ', ';				
				}	
				epdAppsDisclosure = ' We also use ' + epdAppsDisclosureText.substring(0, epdAppsDisclosureText.length - 2) + ' and ' + epdApps[epdAppsCount - 1] + ' scripts, which all use cookies. ';
			}else{
				epdAppsDisclosure = ' We also use a ' + epdApps[0] + ' script which uses cookies for statistical purposes only.';		
			}	
		}else{
			epdAppsDisclosure = '';
		}
		
		// Create our overlay with message
		var divNode = document.createElement('div');
		divNode.setAttribute('id','epd');
		document.body.appendChild(divNode);
		
		// The disclosure narrative pretty much follows that on the Information Commissioners Office website		
		var disclosure = '<div id="cookiesdirective" style="position:absolute;top:-300px;left:0px;width:100%;height:auto;background:#000000;opacity:.80; -ms-filter: “alpha(opacity=80)”; filter: alpha(opacity=80);-khtml-opacity: .80; -moz-opacity: .80; color:#FFFFFF;font-family:arial;font-size:14px;text-align:center;z-index:1000;">';
		
		disclosure +='<div style="position:relative;height:auto;width:90%;padding:15px;margin-left:auto;margin-right:auto;">';	
		disclosure += 'On 26 May 2011, the rules about cookies on websites changed. This site uses cookies. Some of the cookies we ';
		disclosure += 'use are essential for parts of the site to operate (for example, our forms to work correctly) and these cookies have already been set.'+ epdAppsDisclosure +' You may delete and block all ';
		disclosure += 'cookies from this site, but parts of the site may not work and you may not be able to navigate our site correctly. To find out more about cookies on this ';
		disclosure += 'website, see our <a style="color:#ca0000;font-weight:bold;font-family:arial;font-size:14px;" href="'+ epdPrivacyPolicyUri + '">privacy policy</a>.<br/><br/>';
		disclosure += '<div id="epdnotick" style="color:#ca0000;display:none;margin:2px;"><span style="background:#cecece;padding:2px;">You must tick the "I accept cookies from this site" box to accept</span></div>'
		disclosure += 'I accept cookies from this site <input type="checkbox" name="epdagree" id="epdagree" />&nbsp;';
		disclosure += '<input type="submit" name="epdsubmit" id="epdsubmit" value="Continue"/><br/><br/></div></div>';
		document.getElementById("epd").innerHTML= disclosure;
	  	
		// Bring our overlay in
		$('#cookiesdirective').animate({
		    top: '0'
		 }, 1000, function() {
			// Overlay is displayed, set a listener on the button
			$('#epdsubmit').click(function() {
			  
				if(document.getElementById('epdagree').checked){
					// Set a cookie to prevent this being displayed again
					cdCreateCookie('cookiesDirective',1,365);	
					// Close the overlay
					$('#cookiesdirective').animate({
						top:'-300'
					},1000,function(){
						// Remove the elements from the DOM and reload page, which should now
						// fire our the scripts enclosed by our wrapper function
						$('#cookiesdirective').remove();
						location.reload(true);
					});
				}else{
					// We need the box checked we want "explicit consent", display message
					document.getElementById('epdnotick').style.display = 'block';	
				}
			});
		});
	});
		
}

function cdScriptAppend(scriptURI,mylocation){
	// location is 'head', 'body' or specific or after a specific html element identified by its id attribute
	var elementId = String(mylocation);
	var s = document.createElement('script');
	s.setAttribute('src',scriptURI);
	s.setAttribute('type', 'text/javascript');
	
	switch(mylocation){
	case 'head':
		document.getElementsByTagName('head')[0].appendChild(s);
	  	break;
	case 'body':
		document.getElementsByTagName('body')[0].appendChild(s);
	  	break;
	default: 
		document.getElementById(elementId).appendChild(s);
	}	
}


// Simple Cookie functions from https://www.quirksmode.org/js/cookies.html - thanks!
function cdReadCookie(name) {
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
	
}

function cdCreateCookie(name,value,days) {
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
}

