/* Filename: cookies.js
 * This file is part of the gRaDF Semantic Web browsing tool.
 * Copyright (C) 2006  Marcus Cobden
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * (or see the file gpl.txt)
 * 
 * 	Description
 * This file contains a set of utility functions for setting and retrieving cookies from browsers.
 * 
 * 	Classes:
 * CookieMngr
 *
 * 	Version information:
 * $HeadURL: svn+ssh://mc1204@svn.ugforge.ecs.soton.ac.uk/projects/grdf/code/branches/serverside/js/cookies.js $
 * $Date: 2007-10-04 04:17:48 -0700 (Thu, 04 Oct 2007) $
 * $Author: mc1204 $
 * $Rev: 140 $
 */

/**
 * Cookie interface utility methods.
 * These are all static, so there's no need for it to be class based.
 */
window.CookieMngr = [];

/**
 *  set
 * Sets a cookie
 * 
 * @param key Index key to the cookie
 * @param value Value of the cookie
 * @param expires Time from now that the cookie should expire
 */
CookieMngr.set = function (name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/";
};

/**
 *  read
 * Reads a cookie
 * 
 * @param key Index to the cookie
 * @return value of the cookie
 */
CookieMngr.read = function (name) {
	this.set('test','test',1);
	this.erase('test');
	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;
};

/**
 *  matchPrefix
 * Returns all cookies matching a given prefix.
 * 
 * @param key Prefix to match
 * @return array of keys and values. Value indexed by key.
 *//*
CookieMngr.matchPrefix = function(key) {
	// FIXME WebKit doesn't do cookies?
	if(document.cookie == null)
		return null;
	
	var re = new RegExp("^ ?(" + key + "[^=]+)=(.*)$");
	var cookies = document.cookie.split(';');
	var out = [];	
	for (id in cookies){
		var test = re.exec(cookies[id]);
		if(test != null)
			out[test[1]] = test[2];
	}

	return out;
}; */

/**
 *  erase
 * Erase a cookie, by setting the time to be in the past
 * 
 * @param key Cookie to erase
 */
CookieMngr.erase = function(key) {
	this.set(key,'',-1);
};
