// Creates a cookie like 'authorized user@~~local~~[1].txt or in memory'



//************************************************
// Generate an Expiration Date using the number of days
// passed in the duration parameter
//************************************************

function GnrtExprDt(duration)
{
    // duration = number of days cookie lives; expire immediately = -1; 
    var today = new Date();
    var expr = new Date();
    //  times 24 hours * 60 seconds, times 60 minutes, times 1000 milliseconds
    expr.setTime(today.getTime() + duration*24*60*60*1000);
    return  expr.toGMTString();
}




//************************************************
// Get the 'index' component of the 'name' cookie
//************************************************

function getCookieComp(name,index)
{
  var dc = document.cookie;
  var prefix = name + "=";
  var ck = dc.substring((dc.indexOf(prefix) + prefix.length));
  var comp = "";

  if (ck.length < 1) 
     return "";

  numKP = 1;
  ck = unescape(ck);
  while (ck.indexOf('|') > -1) 
  {
    comp = ck.substring(0,ck.indexOf('|'));
    if (numKP == index)
    {
        break;
    }
    ck = ck.substring((ck.indexOf('|')) + 1);
    numKP++;
  }
  return unescape(comp);
}




//************************************************
// Gets the cookie associated with the name
//************************************************

function GetCookie(name)  
{
    // Retrieves Value from Name Value pair in memory or text file 
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;

    // Find the name cookie in the string of cookies
    while (i < clen)  
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
	{

            // Found the name, now parse out he cookie & return it
	    var endstr = document.cookie.indexOf (";", j);
	    if (endstr == -1)
	    	endstr = document.cookie.length;
    	    return unescape(document.cookie.substring(j, endstr));
	}
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) 
          break; 
    }
    return null;
}



//************************************************
// Generate the cookie string and set the cookie
//************************************************

function SetCookie(name, value)  //optional values are expires, path, domain, and secure
{
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;

    //Generate the cookie string in "name=value" format
    var curCookie = name + "=" + escape (value) +
      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
      ((path == null) ? "" : ("; path=" + path)) +
      ((domain == null) ? "" : ("; domain=" + domain)) +
      ((secure == true) ? "; secure" : "");

    // Saves Name Value pair in memory or text file 
    document.cookie = curCookie;
}




//************************************************
// Delete the named cookie
//************************************************

function DeleteCookie(name) 
{  
    //This routine sets the value to null and the expires date to the past.
    var expdt = new Date(GnrtExprDt(-1));
    var curCookie = name + "=; expires=" + expdt.toGMTString()

    // This cookie is history  
    document.cookie = curCookie;
}

//-- eof --
