// BEGIN HEADER ///////////////////////////////////////////////////////////////////////////////////////////// AUTHOR: jeremy at badlib.com// LANGUAGE: javascript1.3// You may incorporate this code into your own code without restriction.// This code has been provided "AS IS" and the responsibility for its operation is yours.// You may redistribute this code, as long as this HEADER stays intact.// Any modifications must be noted with a description and the date. Your name and contact info is optional.// Please submit any modifications (especially fixes!) to me.// END HEADER /////////////////////////////////////////////////////////////////////////////////////////////function cooky(name, content, expire_date, action)// PRE: 	'name' must be string.// POST: 	1) Returns the contents of cookie with name 'name' if only 'name' is provided.//			2) If name and content are provided a session cookie with those parameters will be created or changed.//			3) If name, content and expire_date are provided the same as (2) except the cookie will expire 'expire_date' days from today.//			4) If action is 'add' a cookie with name 'name' will have 'content' appended to it's current content//			5) If action is 'remove' the content ofcookie 'name' that matches 'content' will be removed// VERSION: 1.0{	var del = ','; //the content delimiter (default is ',')	var regex;		if(!navigator.cookieEnabled) return false;		//minimal debugging help	if(typeof name !== 'string')	{		Alert('"name" must be of type "string"\n\n');		return false;	}		var current = document.cookie.split(name + '='); //array	current = current.length < 2 ? '' : current[1]; //string - right side of array	current = current.split(';')[0]; //string - just content		if(typeof content === 'undefined')	{		return current;	}		//start off as a session cookie and go from there	expires = '';	if(expire_date)	{		//otherwise set offset by day		expires = new Date();		expires.setDate(expires.getDate() + expire_date);		expires = 'expires=' + expires; //string - for appending to cookie name and content	}		content = content.toString(); //make sure that content is a string	content = content.replace(/[=;]/g,''); //discard illegal characters	var new_content = content; //create new_content string	content = content.split(/[,]+/); //convert to an array for easy parsing later		if(action === 'add' || action === '+')	{		new_content = current + del + new_content;	}	else if(action === 'remove' || action === '-')	{		new_content = del + new_content + del;		while(content.length) {			regex = new RegExp([del + content.pop() + del],['g']);			new_content = new_content.replace(regex, del);		}	}		//cleanup by removing redundant content and excess commas	content = new_content.split(/[,]+/); //array	new_content = del + new_content + del;	while(content.length) {		regex = new RegExp([del + content[content.length-1] + del],['g']);		new_content = new_content.replace(regex, del);		new_content += del + content.pop() + del;	}	regex = new RegExp([del+'+'], ['g']);	new_content = new_content.replace(regex, del);	regex = new RegExp(['^'+del], ['g']);	new_content = new_content.replace(regex, '');	regex = new RegExp([del+'$'], ['g']);	new_content = new_content.replace(regex, '');		//finally update the cookie and return the new content	document.cookie = name + '=' + new_content + ';' + expires;	return new_content;		function Alert(text)	{		alert('cooky() reports:\n\n' + text + 'cooky() will now quit without finishing.');	}}function more_cooky(name, content, expire_date){	return cooky(name, content, expire_date, 'add');}function less_cooky(name, content, expire_date){	return cooky(name, content, expire_date, 'remove');}
