/* save the cookie and its value */
function setCookie(name, value)
{
 var start;
 var end;
 var firstPart;
 var secondPart;
 var newName;
 var cookieValue;

 if( document.cookie.length == 0)
 {
  document.cookie = "cookie= " + name + "=" + value + ":;" + makeCookie();
  return;
 }

 cookieValue = document.cookie.substring(7)

 newName = name + "=";

 start = cookieValue.indexOf(newName);
 if(start > 0)
  start = cookieValue.indexOf(":"+newName)+1;
  
 if(start < 0)
 {
  newAttr = newName+value+":";
  document.cookie = "cookie=" + newAttr + cookieValue + "; " +
  makeCookie();
  return;
 }
 else
 {
  end = getEnd( cookieValue.substring(start) ) + start;
  firstPart = cookieValue.substring(0,start);
  secondPart = cookieValue.substring(end+1);

  newAttr = newName+value+":";
  document.cookie = "cookie=" + firstPart + newAttr + secondPart+";" +
  makeCookie();
 }
}

/* get the value of a cookie by its name */
function getCookie(name)
{
 var name;
 var start;
 var end;
 var value;
 var cookieValue = document.cookie.substring(7);

 name  = name+"=";
 start = cookieValue.indexOf(name);
   
 if (start < 0)
 return null;

 value = cookieValue.substring(start);

 end = getEnd(value);

 value = unescape(cookieValue.substring(start+name.length, end));

 return value;
}


/*** returns the pos of the ending ':' or ';' ***/
function getEnd(string)
{
 var end1 = string.indexOf(":");
 var end2 = string.indexOf(";");

 if (end1 < 0)
 return string.indexOf(";");

 end2 = string.indexOf(";");

 if (end2 < end1 && end2 >= 0)
  return end2;

 return end1;
}

/*** make a generic cookie ***/
function makeCookie()
{
 var endDate = new Date();
 var domain;
 var path;
 var newCookie = "";

 if( location.href.indexOf("dracos-lair") >= 0)
  domain = ".dracos-lair.net";

 if( location.href.indexOf("/") >= 0)
  path = "/";

 if(endDate)
 {
  endDate = endDate.setTime( endDate.getTime() + (1000*60*60*24*365) );
  newCookie = newCookie+"expires="+"Monday, 18-Jan-2038 00:00:00 GMT; ";
 }

 if(domain)
  newCookie = (newCookie+"domain="+domain+"; ");

 if(path)
  newCookie = (newCookie+"path="+path+"; ");

 return newCookie;
}

/*** delete cookie ***/
function deleteCookie()
{
/*
 var endDate = new Date();
 var domain;
 var path;
 var newCookie = "";

 if( location.href.indexOf("kixor") >= 0)
  domain = ".kixor.net";

 if( location.href.indexOf("draco") >= 0)
  path = "/~draco";

 if(endDate)
 {
  endDate = endDate.setTime( endDate.getTime() + (1000*60*60*24*365) );

  //document.debug.debug0.value="Normal: "+endDate;
  //document.debug.debug1.value="Locale: "+endDate.toLocaleString();
  //document.debug.debug2.value="String: "+endDate.toString();
  //document.debug.debug3.value="GMT: "+endDate.toGMTString();
 }

 if(domain)
  newCookie = (newCookie+"domain="+domain+"; ");

 if(path)
  newCookie = (newCookie+"path="+path+"; ");

//   document.debug.debug2.value = ("newCookie: "+newCookie);
*/
 var killDate = "Thursday, 1-Jan-1970 00:00:00 GMT; ";
 document.cookie = "goodbye=cookie; expires="+killDate;
}