/* ------------------------------------------------------ */
// Classe Cookie
// Class_Cookie.js v1.1 : JScript File
// Auteur : Frédéric REMISE / EXPONENSE.COM
// Créé le : 10/02/2003
// Modifié le : 04/09/2006
/* ------------------------------------------------------ */


// Constructeur : Crée un objet Cookie pour le document
function Cookie(document, name, hours, path, domain, secure)
{
	// Propriétés
	this._document = document;
	this._name = name;
	// 3600000 => 1 heure en millieme de secondes
	if(hours) this._expiration = new Date((new Date()).getTime() + hours * 3600000);
	else this._expiration = null;
	if(path) this._path = path;  else this._path = null;
	if(domain) this._domain = domain;  else this._domain = null;
	if(secure) this._secure = true;  else this._secure = false;
};


// Méthode setCookie() : Ecrit le Cookie
Cookie.prototype.setCookie = function() {
	// Boucle sur les propriétés et Assemblage de la valeur du cookie
	var cookieval = "";
	
	for(var property in this)

	{
		if((property.charAt(0) == '_') || ((typeof this[property]) == 'function')) continue;
		if(cookieval != "") cookieval += 'BBB';
		cookieval += property + 'AAA' + escape(this[property]);
	};
	
	// Assemblage de la chaîne complete du cookie
	var cookie = this._name + '=' + cookieval;
	if(this._expiration) cookie += '; expires=' + this._expiration.toGMTString();
	if(this._path) cookie += '; path=' + this._path;
	if(this._domain) cookie += '; domain=' + this._domain;
	if(this._secure) cookie += '; secure';
	
	this._document.cookie = cookie;
};


// Méthode getCookie() : Lit le Cookie
Cookie.prototype.getCookie = function() {
	// Liste tous les cookies du document
	var allcookies = this._document.cookie;
	if(allcookies == "") return false;
	

	var start = allcookies.indexOf(this._name + '=');
	if(start == -1) return false;
	start += this._name.length + 1;
	
	var end = allcookies.indexOf(';', start);
	if(end == -1) end = allcookies.length;
	
	var cookieval = allcookies.substring(start, end);
	
	
	// Parser les valeurs du cookie
	var array = cookieval.split('BBB');
	for(var i=0; i<array.length; i++) array[i] = array[i].split('AAA');
	
	// Récupération des variables d'état
	for(var i=0; i<array.length; i++)
	{
		this[array[i][0]] = unescape(array[i][1]);
	};
	
	// Si le cookie est lisible => il existe
	return true;
};


// Méthode removeCookie() : Supprime le Cookie
Cookie.prototype.removeCookie = function() {
	var cookie;
	cookie = this._name + '=';
	if(this._path) cookie += '; path=' + this._path;
	if(this._domain) cookie += '; domain=' + this._domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	
	this._document.cookie = cookie;
};
