function randomselect(elementarray, cookiename, chosen) {
	var releasecook=0; /* release cookie mechanism ensures no repeat possible at end of loop by marking the last element, setting the capture bit and then releasing this element once the next element is determined - this ensures the last element is not statistically penalised*/
	var answer=0;
	var i=0;
	var ibin=0;
	if (elementarray=="exhibitArray") var nofels = numberOfExhibits; else var nofels = elementarray.length;
	var icook = getCookie(cookiename);
	icook = Math.floor(icook);
	if (nofels <= 1) {icook = 0;} /*otherwise 1 element array causes routine to thrash looking for non-repeating elements*/
	if (icook >= Math.pow(2,(nofels+1))-1) {icook=0;} /*cookie reset mechanism - number of array elements has changed*/
	if (icook == Math.pow(2,nofels)-1) {icook=0;} /*for some reason capture bit wasn't set*/
	if (icook >= Math.pow(2,nofels)) {icook = icook-Math.pow(2,nofels); releasecook=icook;}/*note that the previous final loop element is marked as having been used here, capture bit is turned off and release mechanism set to turn this mark off later*/
	var ibinstr = icook.toString(2);
	do		{
		if (chosen == -1) {i = Math.floor(Math.random()*nofels);} else {i = chosen; answer = 1;}
		ibin = ibinstr.charAt(ibinstr.length-i-1);
		if (i>ibinstr.length-1) {answer = 1;}
		else if (ibin == "0") {answer = 1;}
		}
	while (answer == 0)
	if (chosen == -1) {icook = icook+Math.pow(2,i);} else if (ibin != "1") {icook = icook+Math.pow(2,i);}
	if (releasecook >= 1) {icook = icook - releasecook;} /*now i is determined the previous cookie marking the element which was last in the previous loop can be released*/
	else {if (icook >= Math.pow(2,nofels)-1) {icook = Math.pow(2,nofels)+Math.pow(2,i);}/*The first thing added to icook is the capture bit then the last element of the loop just finished*/
	}
	setCookie(cookiename, icook , 365, "/");
	return i;
	}

function setCookie (name, value, lifespan, access_path) {
	var cookietext = name + "=" + escape(value) 
	if (lifespan != null) { 
	var today=new Date() 
	var expiredate = new Date() 
	expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan)
	cookietext += "; expires=" + expiredate.toGMTString()
	}
	if (access_path != null) { 
	cookietext += "; PATH="+access_path 
	}
	document.cookie = cookietext 
	return null 
	}

function getCookie(Name) {
	var search = Name + "=" 
	var CookieString = document.cookie 
	var result = null 
	if (CookieString.length > 0) { 
	offset = CookieString.indexOf(search) 
	if (offset != -1)
		{ 
		offset += search.length 
		end = CookieString.indexOf(";", offset) 
		if (end == -1) {
			end = CookieString.length }
			result = unescape(CookieString.substring(offset, end)) 
			} 
		}
	return result 
	}
