// Cookie Toolbox Javascript
// copyright 4th September 2002, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.
// For instructions on how to use these functions see "A Cookie Toolbox"
// in the Javascript section of our site at http://www.felgall.com/

var dbug = 0;

function d_a(ary) {
	var beg = next_entry(ary) - 1;
	for (var i = beg ; i > -1; i--)
	{
		ary[i] = null;
	}
}

function init_array() {
	if (dbug) alert('init_cookie');
	var ary = new Array(null);
	return ary;
}

/*
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=/';
}
*/
function set_cookie(name,value) {
	if (dbug) alert('set_cookie');
	document.cookie = name + '=' + escape(value) + '; path=/';
}

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;
}

function del_cookie(name) {
	if (dbug) alert('del_cookie');
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}

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);
		}
	}
}

/*
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);
}
*/
function set_array(name, ary) {
	if (dbug) alert('set_array');
	var value = '';
	for (var i = 1; ary[i]; i++)
	{
		value += ary[i] + '^';
	}
	set_cookie(name, value);
}

/*
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);
}
*/
function del_entry(name, ary, pos) {
	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);
}

function next_entry(ary) {
	if (dbug) alert('next_entry');
	var j = 0;
	for (var i=1; ary[i]; i++)
	{
		j = i;
	}
	return j + 1;
}

function debug_on() {
	dbug = 1;
}

function debug_off() {
	dbug = 0;
}

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 \/>');
		}
	}
}

// CVB Reps-specific functions

function showReps() {
	var cookieName = 'rfpreps';
	var myarray = init_array();
	get_array(cookieName, myarray);
	for (var i=1; i<next_entry(myarray); i++)
	{
		document.write('<p>rfp rep #' + i + ' = ' + myarray[i] + '<\/p>');
	}
}

function isInRFP(nID) {
	var cookieName = 'rfpreps';
	var myarray = init_array();
	get_array(cookieName, myarray);

	// now we have all the CVBs that are already in the array
	for (var i=1; i<next_entry(myarray); i++)
	{
		if (myarray[i] == nID)
		{
			//alert("already in here");
			return true;
		}
	}
	return false;
}

function _add(nID) {
	// adds a node ID to the cookie array
	var cookieName = 'rfpreps';
	var myarray = init_array();
	get_array(cookieName, myarray);

	if (isInRFP(nID))
	{
		return false;
	}
	else
	{
		var num = next_entry(myarray);
		myarray[num] = nID;
		set_array(cookieName, myarray);
		return true;
	}
}

function addToRFP(nID) {
	if (_add(nID))
	{
		alert ("Added to your RFP.");
	}
	else
	{
		alert ("This CVB Rep is already set to receive your RFP.");
	}
}

function addAndContinue(nID, strLocation) {
	// adds a node ID to the cookie array and continues to the RFP
	_add(nID);
	document.location=strLocation;
}

function getIndex(nID) {
	// returns the index of a given nID or -1 if not found
	var cookieName = 'rfpreps';
	var myarray = init_array();
	get_array(cookieName, myarray);

	// now we have all the CVBs that are already in the array
	// apparently this dude is working with 1-based arrays for some reason
	for (var i=1; i<next_entry(myarray); i++)
	{
		if (myarray[i] == nID)
		{
			return i;
		}
	}
	return -1;
}

function removeFromRFP(nID) {
	var proceed = confirm("Are you sure you want to remove this CVB Rep from your RFP?");
	if (proceed)
	{
		// removes a node ID from the RFP array
		var cookieName = 'rfpreps';
		var myarray = init_array();
		get_array(cookieName, myarray);
		var pos = getIndex(nID);
		if (pos > -1)
		{
			del_entry(cookieName, myarray, pos);
		}
		// nID is also the ID of the span to empty out
		//document.getElementById(nID).innerHTML = '';
		document.getElementById(nID).style.display = 'none';

		// refresh the array
		myarray = init_array();
		get_array(cookieName, myarray);
		document.getElementById('recipients').value = get_cookie(cookieName);
		// his weird code can give you an array full of null elements, i.e. not having a length of 0
		var isEmpty = true;
		for (var i=1; i<next_entry(myarray); i++)
		{
			if (myarray[i] != null)
			{
				isEmpty = false;
			}
		}
		if (isEmpty)
		{
			document.getElementById('frmRFP').innerHTML="<div style=\"color:red;font-weight:bold;\">You have removed all the CVB Reps from your RFP.<br />Please return to the <a href=\"/cvbs\">CVB Search</a> and try again.  Thank you.</div>";
		}
	}
}