/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.0.0
Last Update: 30 May 2004

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
*/

function Set_Cookie(name,value,days,path,domain,secure) {
	if (days) {
		var thisdate = new Date();
		thisdate.setTime(thisdate.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+thisdate.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+
			( ( path ) ? ";path=" + path : "" ) + 
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
} // function

function Get_Cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
} // function

function Delete_Cookie(name,path,domain) {
	Set_Cookie(name,"",-1);
} // function

// Cookie Toolbox
function d_a(ary) {
	var beg = nextCookieEntry(ary) - 1;
	for (var i = beg ; i > -1; i--) {
		ary[i] = null;
	}
}
		
function initCookieArray() {
	var ary = new Array(null);
	return ary;
}

function setCookie(name, value, expires) {
	if (!expires) expires = new Date();
	document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString() + '; path=/';
}

function getCookie(name) {
	var dcookie = document.cookie;
	var cname = name + "=";
	var clen = dcookie.length;
	var cbegin = 0;
	while (cbegin < clen) {
		var vbegin = cbegin + cname.length;
		if (dcookie.substring(cbegin, vbegin) == cname) {
			var vend = dcookie.indexOf (";", vbegin);
			if (vend == -1) vend = clen;
			return unescape(dcookie.substring(vbegin, vend));
		}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) break;
	}
	return null;
}

function deleteCookie(name) {
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}

function getCookieArray(name, ary) {
	d_a(ary);
	var ent = getCookie(name);
	if (ent) {
		i = 1;
		while (ent.indexOf('^') != '-1') {
			ary[i] = ent.substring(0,ent.indexOf('^'));
			i++;
			ent = ent.substring(ent.indexOf('^')+1, ent.length);
		}
	}
}

function setCookieArray(name, ary, expires) {
	var value = '';
	for (var i = 0; ary[i]; i++) {
		value += ary[i] + '^';
	}
	setCookie(name, value, expires);
}

function deleteCookieEntry(name, ary, pos, expires) {
	var value = '';
	getCookieArray(name, ary);
	for (var i = 0; i < pos; i++) {
		value += ary[i] + '^';
	}
	for (var j = pos + 1; ary[j]; j++) {
		value += ary[j] + '^';
	}
	setCookie(name, value, expires);
}

function nextCookieEntry(ary) {
	var j = 0;
	for (var i = 0; ary[i]; i++) {
		j = i
	}
	return j + 1;
}

function dumpCookies() {
	if (document.cookie == '')
		document.write('No cookies found.');
	else {
		thisCookie = document.cookie.split('; ');
	
		for (i=0; i<thisCookie.length; i++) {
			document.write(thisCookie[i] + '<br \/>');
		}
	}
}

