EXPIRE_DAYS = 365; 	// on garde les cookies pour autant de jours

function getCookie(NameOfCookie) {
	if (document.cookie.length > 0) { 
		// Second we check if the cookies name is stored in the
		// "document.cookie"-object for the page.
		
		// Since more than just one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie"-object
		// is not just an empty text.
		// If our cookiename is not present the value -1 is stored
		// in the variable called "begin".

	    begin = document.cookie.indexOf(NameOfCookie+"="); 
    	if (begin != -1) { 
			begin += NameOfCookie.length+1; 
      		end = document.cookie.indexOf(";", begin);
      		if (end == -1) {
				end = document.cookie.length;
			}
			
      		return unescape(document.cookie.substring(begin, end));       
		} 
  	}
	return null;  
}

function setCookie(NameOfCookie, value, expiredays) {
	// Three variables are used to set the new cookie. 
	// The name of the cookie, the value to be stored,
	// and finaly the number of days till the cookie expires.
	// The first lines in the function converts 
	// the number of days to a valid date.

	var ExpireDate = new Date ();
  	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	// The next line stores the cookie, simply by assigning 
	// the values to the "document.cookie"-object.
	// Note the date is converted to Greenwich Meantime using
	// the "toGMTstring()"-function.

  	document.cookie = NameOfCookie + "=" + escape(value) + 
  	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/";
}

// créer un cookie pour un nouveau utilisateur
function initCookie(user) {
	var value = "x";
	setCookie("user_" + user, value, EXPIRE_DAYS);
}

// enregistrer la visite d'une page
function addPage(user, page) {
	value = getCookie("user_" + user);
	if (value) {
		var parts = value.split("&");
		found = false;
		for (i = 1; i < parts.length; i++) {
			if (parts[i] == page) { // page déjà visitée
				found = true;
				break;
			}
		}
		value = parts.join("&");
		if (!found) {
			value += "&" + page;
		}
		setCookie("user_" + user, value, EXPIRE_DAYS);
	}
}

function showPages(user) {
	value = getCookie("user_" + user);
	s = "";
	if (value) {
		var parts = value.split("&");
		found = false;
		first = true;
		for (i = 1; i < parts.length; i++) {
			if (first) {
				first = false;
			}
			else {
				s += ", ";
			}
			s += parts[i];
		}
	}
	return s;
}

/////////////////////////////////////////
// cookie du système

// ce cookie identifie l'utilisateur actuel
/////////////////////////////////////////

function setTrackerCookie() {	
	value = this.user;
	setCookie("tracker", value, EXPIRE_DAYS);
}

function getTrackerCookie() {
	value = getCookie("tracker");
	if (value) {
		this.user = value;
		return;
	}
	this.user = null; // cookie est corrompu ou n'existe pas
}

var tracker = new Object;
tracker.user = null;
tracker.set = setTrackerCookie;
tracker.get = getTrackerCookie;

