

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookieDbcreator(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: one year)
 * [path]     Path where the cookie is valid (default: any document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookieDbcreator(name, value, expires, path, domain, secure)
{
    document.cookie=(name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "; expires=" + new Date(new Date().getTime()+31536000000).toGMTString()) +
        ((path) ? "; path=" + path : "; path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : ""));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookieDbcreator(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "; path=/") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/** 
* The following javascripts are copyrighted by ChemExper sprl (www.chemexper.com).
* You are not allowed to use them without written permission.
* Cookies are limited in number and size. Per server the limit in 20 and
* each cookie may have a length of 4k
*
*/


/** We delete the cookie "name" and we add the current value
*/
function setValuesInCookie(name, values, expires, path, domain, secure) {
	deleteCookie(name);
	putValuesInCookie(name, values, expires, path, domain, secure);
}

/** We will allow to store many "objects" (an array) in one cookie
* The first value of "values" should be unique.
*/
function putValuesInCookie(name, values, expires, path, domain, secure) {
	allValues=getArrayFromCookie(name);
	found=false;
	if ((! allValues) || (! allValues.length)) {
		allValues=new Array(0);
	}
	for (var i=0; i<allValues.length; i+=values.length) {
		if (allValues[i]==values[0]) {
			found=true;
			for (var j=0; j<values.length; j++) {
				allValues[j+i]=values[j];
			}
		}
	}
	if (! found) {  // we append the values
		allValues=allValues.concat(values);
	}
	putArrayInCookie(name, allValues, expires, path, domain, secure)
}

function getArrayFromCookie(name) {
	if (! getCookieDbcreator(name)) return null;
	allValues=getCookieDbcreator(name).split(",");
	for (var i=0; i<allValues.length; i++) {
		allValues[i]=unescape(allValues[i]);
	}
	return allValues;
}

function putArrayInCookie(name, values, expires, path, domain, secure) {
	currentValue="";
	for (var i=0; i<values.length; i++) {
		if (currentValue!="") currentValue+=",";
		currentValue+=escape(values[i]);
	}
	setCookieDbcreator(name, currentValue, expires, path, domain, secure);
}

function getValuesFromCookie(name, firstFieldValue, numberValues) {
	allValues=getArrayFromCookie(name);
	if (! allValues) return null;
	values=null;
	for (var i=0; i<allValues.length; i+=numberValues) {
		if (allValues[i]==firstFieldValue) {
			values=new Array(numberValues);
			for (var j=0; j<numberValues; j++) {
				values[j]=unescape(allValues[j+i]);
			}
		}
	}
	return values;
}

function keyExistsInCookie(name, firstFieldValue, numberValues) {
	allValues=getArrayFromCookie(name);
	if (! allValues) return false;
	for (var i=0; i<allValues.length; i+=numberValues) {
		if (allValues[i]==firstFieldValue) return true;
	}
	return false;
}

function getKeysFromCookie(name, numberValues) {
	allValues=getArrayFromCookie(name);
	if (! allValues) return null;
	values=new Array(0);
	position=0;
	for (var i=0; i<allValues.length; i+=numberValues) {
		values[position]=allValues[i];
		position++;
	}
	return values;
}



function deleteValuesFromCookie(name, firstFieldValue, numberValues, expires, path, domain, secure) {
	allValues=getArrayFromCookie(name);
	if ((! allValues) || (! allValues.length)) {
		allValues=new Array(0);
	}
	newValues=new Array(0);
	position=0;
	for (var i=0; i<allValues.length; i+=numberValues) {
		if (allValues[i]!=firstFieldValue) {
			for (var j=0; j<numberValues; j++) {
				newValues[j+position]=allValues[i+j];
			}
			position+=numberValues;
		}
	}
	putArrayInCookie(name, newValues, expires, path, domain, secure)
}


