/*<![CDATA[*/
//<!--

	/* ********************************************************************************
	*
	* 		Modulo Cookie
	*
	* autore : Danilo "Dennis" Pederiva
	*          Matteo "mackey" Chessa - mackey@lycos.it
	*          William "standard" Verdolini
	* versione : 0.1
	* data : 19/08/2003
	*
	*
	* Modulo Cookie per la memorizzazioni dei dati in locale
	*
	* metodi: setta, leggi, cancella, modifica, recuperaTutti,
	* aggiuntivi: sintassiVar, serializza, deSerializza
	* proprietà: etichetta, attivi, recuperati
	*
	*
	* ESEMPIO: WDL.Cookie.setta("nome","mackey",10); //setta il cookie nome="mackey" con durata 10 minuti
	*
	*/

if(!window.WDL) WDL= {}; 

WDL.Cookie= {};
	WDL.Cookie.etichetta = "WDL_";
	WDL.Cookie.attivi = (document.cookie="WDLCOOKIE=ok;") && (document.cookie.length);

	WDL.Cookie.setta = function(nome, valore, durata, percorso, dominio, sicurezza) {
		if(this.attivi && nome && this.sintassiVar(nome)) {
			if(typeof(durata)!="undefined") {
				durata=(durata.constructor==Array)?durata[0]*86400000+durata[1]*3600000+durata[2]*60000+durata[3]*1000:durata*86400000;
           		var data_scadenza= new Date();
           		data_scadenza.setTime(data_scadenza.getTime()+durata);
			}
			document.cookie= this.etichetta+nome+"="+escape(this.serializza(valore))+(typeof(durata)!="undefined"?"; expires="+data_scadenza.toGMTString():"")+
			(percorso?"; path="+percorso:"")+(dominio?"; domain="+dominio:"")+(sicurezza?"; secure":"")+";";
			if(this.recuperati) window[nome] = valore;
			return true;
		}
		else return false;
   	};

	WDL.Cookie.leggi = function(nome) {
		var vettoreCookie= document.cookie.split("; "), i;
		if(vettoreCookie.length>0 && nome)
			for(i in vettoreCookie)
				if(vettoreCookie[i].substr(0,vettoreCookie[i].indexOf("="))==this.etichetta+nome)
					return this.deSerializza(unescape(vettoreCookie[i].slice(vettoreCookie[i].indexOf("=")+1)));
		return(undefined);
	};
	
	WDL.Cookie.cancella = function(nome, percorso, dominio, sicurezza) {
		if(this.leggi(nome)) {
			this.setta(nome,"",-1, percorso, dominio, sicurezza);
			return true;
		}
		else return false;
	};

	WDL.Cookie.modifica = function(nome, valore, durata, percorso, dominio, sicurezza) {
		return this.leggi(nome)?this.setta(nome, valore, durata, percorso, dominio, sicurezza):false;
	};

	WDL.Cookie.sintassiVar = function(nome) {
		var paroleChiave= ['ActiveXObject','Array','Boolean','break','case','catch','class','const',
		'continue','Date','debugger','default','delete','do','else','enum','Enumerator','Error','escape',
		'eval','export','extends','false','finally','for','function','Function','Global','if','import','in',
		'instanceof','Math','new','null','Number','Object','RegExp','return','String','super','switch',
		'this','throw','true','try','typeof','unescape','var','VBArray','void','WDL','while','with'], i;
		for(i in paroleChiave) if(nome==paroleChiave[i]) return false;
		return(/^[a-zA-Z\$_][\w\$_]*$/.test(nome));
	};

	WDL.Cookie.recuperaTutti = function() {
		var vettoreCookie= document.cookie.split("; "), i;
		for(i in vettoreCookie) {
			var vvar= unescape(vettoreCookie[i].substr(vettoreCookie[i].indexOf("=")+1));
			if(vettoreCookie[i].indexOf(this.etichetta)==0)
				window[vettoreCookie[i].substr(this.etichetta.length,vettoreCookie[i].indexOf("=")-this.etichetta.length)]=
				this.deSerializza(vvar);
		}
		this.recuperati= true;
	};

	WDL.Cookie.serializza = function(valore) {
		if(this.serializza.arguments.length!=1)
			return("");
		if(valore===null)
			return("N;");
		else if(typeof(valore)=="undefined")
			return("U;");
		else if(valore.constructor===Array) {
/*
			var ris;
			ris = "A:"+valore.length+":";
			for(i in valore)
				ris+=escape(this.serializza(i)+this.serializza(valore[i]));
			ris+=";";
			return ris;
*/
			var ris1=0, ris2="";
			for(i in valore) {
				ris1++;
				ris2 += escape(this.serializza(i)+this.serializza(valore[i]));
			}
			return "A:"+ris1+":"+ris2+";";

		}
		else if(valore.constructor===Date) {
			return "D:"+valore.getTime()+";";
		}
		else if(valore.constructor===Object||valore.toString()==="[object Object]") {
			var ris1=0, ris2="";
			for(i in valore) {
				ris1++;
				ris2 += escape(this.serializza(i)+this.serializza(valore[i]));
			}
			return "O:"+ris1+":"+ris2+";";
		}
		else switch(typeof(valore)) {
			case "boolean":	return("b:"+(valore?1:0)+";");
			case "number": return("n:"+valore+";");
			case "string": return("s:"+escape(valore)+";");
			case "function": {
				var args, corpo;
				valore = valore.toString();
				args = valore.substring(valore.indexOf("(")+1,valore.indexOf(")"));
				corpo = valore.substring(valore.indexOf("{")+1,valore.lastIndexOf("}"));
				return("f:"+escape(args)+":"+escape(corpo)+";");
			}
			default: return valore;
		}
	};

	WDL.Cookie.deSerializza = function(stringa) {
		var dA=/^([ADNUObnsf]):?([^:;]*):?([^:;]*);$/.exec(stringa);
		if(dA===null) return stringa;
		switch(dA[1]) {
			case "N": return null;
			case "U": return undefined;
			case "b": return typeof(dA[2])=="undefined"?stringa:Boolean(dA[2]);
			case "n": return typeof(dA[2])=="undefined"?stringa:Number(dA[2]);
			case "s": return typeof(dA[2])=="undefined"?stringa:unescape(dA[2]);
			case "f": return (typeof(dA[2])=="undefined"||typeof(dA[3])=="undefined")?stringa:new Function(unescape(dA[2]),unescape(dA[3]));
			case "D": return typeof(dA[2])=="undefined"?stringa:new Date(Number(dA[2]));
			case "A": {
				if(typeof(dA[2])=="undefined"||typeof(dA[3])=="undefined") return stringa;
				var arr1=new Array(),arr2=[],i;
				dA[2]=Number(dA[2])*2;
				arr2=unescape(dA[3]).split(";").slice(0,-1);
				if(arr2.length!=dA[2] || dA[2]%2!=0) return stringa;
				for(i=0; i<dA[2]; i+=2)
					arr1[this.deSerializza(arr2[i]+";")] = this.deSerializza(arr2[i+1]+";");
				return arr1;
			}
			case "O": {
				if(typeof(dA[2])=="undefined"||typeof(dA[3])=="undefined") return stringa;
				var arr1=new Object(),arr2=[],i;
				dA[2] = Number(dA[2])*2;
				arr2 = unescape(dA[3]).split(";").slice(0,-1);
				if(arr2.length!=dA[2] || dA[2]%2!=0) return stringa;
				for(i=0; i<dA[2]; i+=2)
					arr1[this.deSerializza(arr2[i]+";")] = this.deSerializza(arr2[i+1]+";");
				return arr1;
			}
			default: return stringa;
		}
	};

//-->
/*]]>*/