// Definizione della classe Cookie
function Cookie(document, name, path, days){

	this.$document = document;

	this.$name = name;

	if(!path)path = "";
	this.$path = path;

	if(!days)days = 1;
	this.$expiration = new Date(new Date().getTime() + days * 86400000);

	this.nCampi = 0;
}

function _SalvaCookie(){

	var cookieval = "";
	for(var prop in this){
		if((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || (this[prop] == null))continue;
		if(cookieval != "")cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}

	var cookie = this.$name + '=' + cookieval;
	if(this.$expiration)cookie += '; expires=' + this.$expiration.toGMTString();
	if(this.$path)cookie += '; path=' + this.$path;

	this.$document.cookie = cookie;
}

function _CaricaCookie(){

	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);

	var a = cookieval.split('&');
	for(var i = 0; i < a.length; i++){
		a[i] = a[i].split(':');
	}
	for(var i = 0; i < a.length; i++){
		this[a[i][0]] = unescape(a[i][1]);
	}

	return true;
}

function _SvuotaCookie(){
	var cookie;
	cookie = this.$name + '=';
	cookie += '; path=' + this.$path;
	this.nCampi = 0;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

	this.$document.cookie = cookie;
	this.salva();
}

function _InserisciCampoCookie(percorso){
	this.nCampi++;
	this['campo' + this.nCampi] = percorso;
	this.salva();
}

function _RimuoviCampoCookie(ID){
	for(var i = ID; i < this.nCampi; i++){
		this['campo' + i] = this['campo' + (i + 1)];
	}
	this['campo' + this.nCampi] = null;
	this.nCampi--;
	this.salva();
}

new Cookie();
Cookie.prototype.salva = _SalvaCookie;
Cookie.prototype.carica = _CaricaCookie;
Cookie.prototype.svuota = _SvuotaCookie;
Cookie.prototype.inserisci = _InserisciCampoCookie;
Cookie.prototype.rimuovi = _RimuoviCampoCookie;

// Fine definizione della classe Cookie

var album = new Cookie(document, "albumBiglietti", "/fiera", 2);
album.carica();
