// Last modification : 31/01/2007.

/* Functions in this file :
	 f_checkCookie(name)
	 f_getCookie(name)
	 f_setCookie(name, value[, expires, path, domain, secure])
	 f_deleteCookie(name)
	 f_isCookieEnabled()
*/

// --------------------------------------------------------------------------------
// CONSTANTS
// --------------------------------------------------------------------------------

var expDays = 365;
var expDate = new Date();
expDate.setTime(expDate.getTime() + (expDays*24*60*60*1000));


// --------------------------------------------------------------------------------
// f_checkCookie(name)
// --------------------------------------------------------------------------------
// Returns -1 when cookie does not exist.

function f_checkCookie(cookieName) {
	var search = cookieName + "=";

	if (document.cookie.length > 0) {                   // at least 1 cookie exists
		offset = document.cookie.indexOf(search);

		if (offset != -1) {                               // the cookie exists
			return offset;
		} else {                                          // the cookie does not exist
			return -1;
		}
	}
}


// --------------------------------------------------------------------------------
// f_getCookie(name)
// --------------------------------------------------------------------------------
// Returns the value of the cookie.

function f_getCookie(cookieName) {
	var search = cookieName + "=";

	if (document.cookie.length > 0) {                   // at least 1 cookie exists
		offset = document.cookie.indexOf(search);

		if (offset != -1) {                               // the cookie exists
			offset += search.length;                        // start position of cookie value
			end = document.cookie.indexOf(";", offset);     // end position of cookie value

			if (end == -1) end = document.cookie.length;

			return unescape(document.cookie.substring(offset, end));
		}
	}
}


// --------------------------------------------------------------------------------
// f_setCookie(name, value[, expires, path, domain, secure])
// --------------------------------------------------------------------------------
// Sets the cookie.

function f_setCookie(name, value) {
	var argv = f_setCookie.arguments;
	var argc = f_setCookie.arguments.length;
	var expires = new Date();

	if(argc > 2) {
		expires.setTime(expires.getTime() + argv[2]*24*60*60*1000);
	} else {
		expires.setTime(expDate.getTime());
	}

	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;

	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}


// --------------------------------------------------------------------------------
// f_deleteCookie(name)
// --------------------------------------------------------------------------------
// Delete cookie.

function f_deleteCookie(name) {
	var expDate = new Date();
	expDate.setTime(expDate.getTime()-1);
	var cval = f_getCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + expDate.toGMTString();
}


// --------------------------------------------------------------------------------
// f_isCookieEnabled()
// --------------------------------------------------------------------------------
// Returns true when cookies are enabled.
// Returns false when cookies are disabled.

function f_isCookieEnabled() {
	if (document.all) {
		if (!navigator.cookieEnabled) {
			// alert('Cookies disabled');
			return false;
		}
		else return true;
	}
	else {
		f_setCookie('temp', 'temp');
		var temp = f_getCookie('temp');
		if (!temp) {
			// alert('Cookies disabled');
			return false;
		}
		else return true;
	}
}
