/**
 * FILE:		cookies2.js
 * DESCR:		JSLibCookies $Revision: 2.00 $
 * 					JS document cookies managment routines and classes.
 * NOTES:		(C) Marco Lamberto <lm@sunnyspot.org>
 *					http://the.sunnyspot.org/
**/


// 
function LibCookiesVersion() {
	return "JSLibCookies v" +
		"$Revision: 2.00 $".substring(11, "$Revision: 2.00 $".length - 2) +
		"\n(C) Marco Lamberto <lm@sunnyspot.org>";
}


//
function CookiesShow() {
	alert(document.cookie);
}

//
function CookiesClear(prefix, path) {
	var i=0, j=0;

	while(true) {
		if ((i = document.cookie.indexOf(prefix)) == -1) break;
		i += prefix.length + 1;
		if ((j = document.cookie.indexOf("=", i)) == -1) break;
		(new MCookie(prefix, document.cookie.substring(i, j), path, true)).remove();
	}
}

//
function CookiesInspector(path) {
	window.open((path ? path : "") + "cookies2.html?CookieLib", "CookiesDebugger",
		"toolbar=0,location=0,directories=0," +
		"status=0,menubar=0,scrollbars=1,resizable=1,width=300,height=660");
}




/*
DESCR:		Cookie class.
PARAMS:		prefix						prefix.
					name							cookie name.
					path							document path.
					temp							true if is a temporary cookie.
RETURNS:
NOTES:
*/
function MCookie(prefix, name, path, temp) {
	this.prefix = prefix;
	this.name = name;
	this.temp = temp;
	this.expires = temp ? "" : MCookie_getExpires2Store();
	this.path = path;

	this.getPrefix = MCookie_getPrefix;
	this.containsFragment = MCookie_containsFragment;
	this.getFragment = MCookie_getFragment;
	this.setFragment = MCookie_setFragment;
	this.removeFragment = MCookie_removeFragment;
	this.setTemp = MCookie_setTemp;
	this.isTemp = MCookie_isTemp;
	this.getExpires = MCookie_getExpires;
	this.setExpires = MCookie_setExpires;
	this.save = MCookie_save;
	this.remove = MCookie_remove;
	this.getBlock = MCookie_getBlock;
	this.getExpires2Store = MCookie_getExpires2Store;
	this.getExpires2Remove = MCookie_getExpires2Remove;
}

	function MCookie_getPrefix() {
		return this.prefix;
	}

	function MCookie_containsFragment(fname) {
		var block = this.getBlock(this.prefix, this.name);
		
		return block ? block.indexOf("@" + fname + "{") != -1 : false;
	}

	function MCookie_getFragment(fname) {
		var val = "";
		var block = this.getBlock(this.prefix, this.name);

		if (block) {
			var i = block.indexOf("@" + fname + "{");
			var j = i + fname.length + 2;
		
			val = (i == -1) ? "" : block.substring(j, block.indexOf('}', j));
		}
		return new MCookieFrag(fname, unescape(val));
	}

	function MCookie_setFragment(frag) {
		var block = this.getBlock(this.prefix, this.name);
		var c;
		var i;

		if (!block) {
			c = '@' + frag.getName() + '{' + escape(frag.getValue()) + '}';
		} else if ((i = block.indexOf('@' + frag.getName() + '{')) != -1) {
			i += frag.getName().length + 2;
			c = block.substring(0, i) + escape(frag.getValue()) + 
				block.substring(block.indexOf('}', i));
		} else
			c = block + '@' + frag.getName() + '{' + escape(frag.getValue()) + '}';
		this.save(c);
	}

	function MCookie_removeFragment(frag) {
		var block = this.getBlock(this.prefix, this.name);
		var i;

		if (block && (i = block.indexOf('@' + frag.getName() + '{')) != -1) {
			var c = block.substring(0, i) +
				block.substring(block.indexOf('}', i + frag.getName().length + 2) + 1);
			this.save(c);
		}
	}

	function MCookie_setTemp(temp) {
		this.temp = temp;
		this.expires = temp ? "" : this.getExpires2Store();
	}

	function MCookie_isTemp() {
		return this.temp;
	}

	function MCookie_getExpires() {
		return this.expires;
	}

	function MCookie_setExpires(gmtdate) {
		this.expires = gmtdate;
	}

	function MCookie_remove() {
		this.expires = this.getExpires2Remove();
		this.save(this.getBlock(this.prefix, this.name));
	}

	function MCookie_save(c) {
  	document.cookie = this.prefix + "_" + this.name + "=" + c +
			(this.path ? "; path=" + this.path : "") +
			((this.expires != "") ? "; expires=" + this.expires : "");
	}

	function MCookie_getBlock(prefix, name) {
  	var i, j;

	  if (document.cookie.length == 0 ||
			(i = document.cookie.indexOf(prefix + "_" + name + "=")) == -1)
			return null;
	
		i += prefix.length + name.length + 2;
		return ((j = document.cookie.indexOf(";", i)) == -1) ?
	   	  document.cookie.substring(i) : document.cookie.substring(i, j);
	}

	function MCookie_getExpires2Remove() {
		var d = new Date();

		return (new Date(d.getYear(), d.getMonth(), d.getDate(), d.getHours(),
			d.getMinutes(), d.getSeconds() + 1)).toGMTString();
	}

	function MCookie_getExpires2Store() {
		var d = new Date();
		var y = (d.getYear() < 100) ? 2010 : y + 10;

		return (new Date(y, d.getMonth(), d.getDate())).toGMTString();
	}
// MCookie class END




/*
DESCR:		MCookie class.
PARAMS:		name							name of fragment
					val								value of fragment
RETURNS:
NOTES:		Allows working on a single cookie for storing multiple values.
*/
function MCookieFrag(name, val) {
	this.name = name;
	this.val = val;

	this.setValue = MCookieFrag_setValue;
	this.getValue = MCookieFrag_getValue;
	this.getName = MCookieFrag_getName;
}

	function MCookieFrag_setValue(val) {
		this.val = val;
		return this.val;
	}

	function MCookieFrag_getValue() {
		return this.val;
	}

	function MCookieFrag_getName() {
		return this.name;
	}
// MCookieFrag class END


