// Extract the value from the cookie at the given offset.

function GetValue(Offset){
  var End = document.cookie.indexOf (";", Offset);
  if( End == -1 )
    End = document.cookie.length;

  // Return the portion of the cookie beginning with the offset
  // and ending with the ";".

  return unescape( document.cookie.substring( Offset, End) );
}

function GetCookie(Name){
  var Len = Name.length;

// Look at each substring that's the same length as the cookie name
// for a match.  If found, look up the value and return it.

  var i = 0;
  while(i < document.cookie.length){
    var j = i + Len + 1;
    if(document.cookie.substring( i, j) == (Name + "="))
      return GetValue( j );
    i = document.cookie.indexOf( " ", i ) + 1;
    if(i == 0) break;
  }
  var a = "";
  return a;
}

// Create or change a cookie given its name and value.  The name and value
// are required, but the expiration date isn't.  Note that if you don't specify
// an expiration date, the cookie only exists for the current session.
function SetCookie(Name, Value, Expire){
  if(!Name) return;
  var str = Name + "=" + escape( Value );
  if(Expire) str += ";expires=" + Expire;;
  document.cookie = str;
}

// Load the form with the values in the cookie

function GetCookies(){
  with(document.forms.LOGON){
    var v = GetCookie("username");
    if(v) auth_user_name.value = v;
    v = GetCookie("password");
    if(v) auth_password.value = v;

    if(auth_remember_login.value == "on"){
      auth_remember_login.checked = true;
    }
  }
}

function FixCookieDate (date){
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if(skew > 0)  // Except on the Mac - ahead of its time
    date.setTime(date.getTime() - skew);
}

var expdate = new Date ();
FixCookieDate(expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365)); // 365 days from now

