// Returns the current end-user's id, or creates one if it doesn't exist //
function getEndUserID() {
	var endUserID = getCookie("userid");
	if (endUserID == null) {
		endUserID = generateUniqueID();
		// Create a date far into the future
		var today = new Date();
		today.setTime(today.getTime()+(86400000*18000));
		setCookie("userid", endUserID, today);
	}
	return endUserID;
}

// Generates a new unique end-user id on the fly //
function generateUniqueID() {
	var strId = "";
	for (i = 0; i < 5; i++) {
		strId += String.fromCharCode(getRandomValue(90, 65));
	}
	var today = new Date();
	strId += today.getTime()
	return strId;
}

// Generates a random number between 'min' and 'max' //
function getRandomValue(max, min) {
	var span = max-min;
	var d = span*Math.random();
	return d+min;
}

// Sets cookie values. Expiration date is optional // 
function setCookie(name, value, expire) {
	document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

// Returns the value of the cookie with the given name //
function getCookie(name) {
	var search = name + "=";
	if (document.cookie.length > 0) { 
		// if there are any cookies
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			// if cookie exists
			offset += search.length;
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
	// If we reach here, then the cookie was not found
	return null;
}