/*
  =====================================================================================
  Function to create a cookie.
  if no expire date is given the cookie will be set
  to expire 6 months from now.
  =====================================================================================
*/
function setcookie(cookiename,cookievalue,cookiepath,cookieexpires,cookiedomain)
{
  if(!cookieexpires || cookieexpires=="")
  {
    // var nowdate = new Date()
    // nowdate.setMonth(nowdate.getMonth() + 6);
    // cookieexpires = nowdate.toGMTString();
    
    // set default to expires when browser session ends
    cookieexpires = "";
  }

  document.cookie = cookiename + "=" + escape(cookievalue) +
                    ((cookiepath=="") ? "" : "; path=" + cookiepath) +
                    "; expires=" + cookieexpires +
										((cookiedomain=="") ? "" : "; domain=" + cookiedomain);
}

/*
  =====================================================================================
  Function to return a cookie's value(s)
  =====================================================================================
*/
function getcookie(cookiename)
{
  var cookievalue = document.cookie;
  var cookiestart = cookievalue.indexOf(" " + cookiename + "=");

  (cookiestart == -1) ? cookiestart = cookievalue.indexOf(cookiename + "=") : void(null);

  if(cookiestart == -1)
    cookievalue = null;
  else
  {
    cookiestart = cookievalue.indexOf("=", cookiestart) + 1;
    var cookieend = cookievalue.indexOf(";", cookiestart);

    (cookieend == -1) ? cookieend = cookievalue.length : void(null);
    cookievalue = unescape(cookievalue.substring(cookiestart,cookieend));
  }
  return cookievalue;
}

/*
  =====================================================================================
  Function to remove a cookie.
  Works by setting a cookie of the same name but
  with an expiry date of yesterday
  =====================================================================================
*/
function removecookie(cookiename)
{
  setcookie(cookiename,"","","0",""); // Mon 1 Jan 1990 00:00:00
}


/*
  =====================================================================================
  Function to test whether user's browser will
  allow cookies to be set and read
  NOTE - requires both setcookie() and getcookie() functions
  (returns boolean)
  =====================================================================================
*/

function allowcookies()
{
  setcookie("freekeesoda","allowed=yes;","/","","freekeesoda.com");
  if(getcookie("cookiesallowed")==null)
    return false;
  else
		return true;		
}

/*
	======================================================================
	function to test for the presence of a specific cookie
	returns boolean, takes cookie name as parameter
	======================================================================
*/
function cookieexists(cookiename)
{
	var cookiedata = getcookie(cookiename);
	if(cookiedata == null || cookiedata == "")
		return false; // cookie does not exist
	else
		return true;	// cookie exists
}

/*
	=================================================================
	function to get a cookie and return its contents split up into
	the elements of an array. returns an array object.
	=================================================================
*/
function getcookievalues(cookiename)
{
	var cookiedata = getcookie(cookiename);
  cookiedata = cookiedata.split(";");
	
	for(i=0; i<cookiedata.length; i++)
	{
		var valuestart = cookiedata[i].indexOf("=") + 1;		
		cookiedata[i] = cookiedata[i].substring(valuestart, cookiedata[i].length);
	}	
	return cookiedata;
}

// 	=================================================================

//  function to return the values stored in a cookie as a
//	hash table (useful when you know the name of the cookie
//	variables).
function getcookiepairs(cookiename)
{
  var cookiepairs = new Array();
	var key, value, command = null;
	
	var cookiedata = getcookie(cookiename);
	cookiedata = cookiedata.split(";");
	
	for(i=0; i<cookiedata.length; i++)
	{						
		key     = cookiedata[i].slice(0, cookiedata[i].indexOf("="));
		value   = cookiedata[i].slice(cookiedata[i].indexOf("=") + 1);				
		command = ("cookiepairs[\"" + key + "\"] = \"" + value + "\";");
		eval(command);
	}	
	// show the values stored 
	// for(var j in cookiepairs) 
	//  alert('key is: ' + j + ', value is: ' + cookiepairs[j]); 
		
	return cookiepairs;
}	

// 	=================================================================