﻿// ======================================================================
// Para usar este archivo:
// <script type="text/javascript" src="Cookie.js"></script>
//
//
//
//
//
// ======================================================================


var dbug = 0;

function d_a(ary)
{
	var beg = next_entry(ary) - 1;
	for (var i = beg ; i > -1; i--)
	{
		ary[i] = null;
	}
}

/*
    This initializes an array called myarray that will be used with the cookie
    array functions. You will load the values that you want to save in your cookie 
    into this array before saving the cookie and when you retrieve the cookie the 
    array will be cleared and then loaded with the values from the cookie.
    Provided that you are only dealing with one cookie at a time, you will only 
    need one array as it can be reused. Note that you need to initialize your array 
    before you call any of the following functions.
*/
function init_array()
{
	if (dbug) alert('init_cookie');
	var ary = new Array(null);
	return ary;
}

/*
    This function allows you to create a cookie called name and store value in it.
    All cookies have an expiry date to stop them hanging around forever. You specify 
    the expiry date for this cookie in the expires field. If you don't specify an 
    expiry date then the function will use the current date and time and the cookie 
    will expire at the end of your visitor's browser session.
*/
function set_cookie(name, value, expires)
{
	if (dbug) alert('set_cookie');
	if (!expires) expires = new Date();
	document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString() + '; path=/';
}

/*
    This function allows you to retrieve the content of a previously saved cookie 
    called name into a field called value.
*/
function get_cookie(name)
{
	if (dbug) alert('get_cookie');
	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;
}

/*
    This function deletes the cookie called name.
    As I mentioned at the start of this discussion, I came up with a way of storing 
    arrays in cookies and the following functions will allow you to do the same.
*/
function del_cookie(name)
{
	if (dbug) alert('del_cookie');
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}

/*
    This function will retrieve the cookie called name then convert the value field 
    back into the array myarray.
*/
function get_array(name, ary)
{
	if (dbug) alert('get_array');
	d_a(ary);
	var ent = get_cookie(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);
		}
	}
}

/*
    This function works the same as the set_cookie function except that it first 
    converts the array into a single value field before storing it in the cookie.
    Note that this function uses the caret (^) as a field separator in this conversion 
    so you will need to ensure that your array entries do not contain that character.
*/
function set_array(name, ary, expires)
{
	if (dbug) alert('set_array');
	var value = '';
	for (var i = 1; ary[i]; i++)
	{
		value += ary[i] + '^';
	}
	set_cookie(name, value, expires);
}

/*
    This function allows you to delete an array entry from within the cookie called name.
    The content of the cookie will be retrieved into myarray and then the cookie will be 
    recreated without the pos entry. A new expiry date of expires will be set.
*/
function del_entry(name, ary, pos, expires)
{
	if (dbug) alert('del_entry');
	var value = '';
	get_array(name, ary);
	for (var i = 1; i < pos; i++)
	{
		value += ary[i] + '^';
	}
	for (var j = pos + 1; ary[j]; j++)
	{
		value += ary[j] + '^';
	}
	set_cookie(name, value, expires);
}

/*
    The array that the toolbox uses dynamically grows and shrinks to meet your current 
    requirements. This function lets you know that the myarray[size] entry is the first 
    empty entry. That's the one you'll want to use when adding more entries into the array.
    Given all of the trouble that I had in testing my javascript, with cookies 
    disappearing unexpectedly, etc. I also built a debugging option into these functions 
    as well as a way to check all of your saved cookies.
*/
function next_entry(ary)
{
	if (dbug) alert('next_entry');
	var j = 0;
	for (var i = 1; ary[i]; i++)
	{
		j = i
	}
	return j + 1;
}

/*
    If you place this statement anywhere in your javascript then any subsequent calls 
    to any of the above cookie functions will first produce an alert telling you which 
    function is to be executed.
*/
function debug_on()
{
	dbug = 1;
}

/*
    You can use this to turn the alerts off again at the end of the section of code that
    you are debugging so that subsequent calls to the cookie functions will no longer 
    produce alerts.
*/
function debug_off()
{
	dbug = 0;
}

/*
    This function can be used to place a raw copy of all of your cookies at the current 
    location on your page so that you can see exactly what you currently have stored in
    cookies. This function can only be used in the body section of your page.
*/
function dump_cookies()
{
	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 \/>');
		}
	}
}
