
// FUNZIONI PER COOKIES (per gestione degli acquisti)
// prima dell'utilizzo, verificare se cookies abilitati, altrimenti avvisare

// data tra 3 giorni (durata del cookie)
var today = new Date();
var expire3g = new Date(today.getTime() + 1000*60*60*24*3 ); // 1/1000 sec -> 3 giorni
var expire30g = new Date(today.getTime() + 1000*60*60*24*30 ); // 1/1000 sec -> 30 giorni

// funzione interna usata da GetCookie()
function getCookieVal (offset) {

	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }

	return unescape(document.cookie.substring(offset, endstr));
}

// salva un cookie, esempio (per 3 giorni):
// utente = document.myForm.user.value;
// SetCookie('ck_utente', utente, expire3g);
//
function SetCookie (name,value,expires,path,domain,secure) {

	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

// richiede un cookie, esempio:
// var utente = GetCookie('ck_utente');
//
function GetCookie (name) {

	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;

	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
		}

		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

// elimina un cookie, esempio:
// DeleteCooke('ck_utente');
//
function DeleteCookie (name,path,domain) {

	if (GetCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}


