function fixCookieDate (date)
{
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
  {
    date.setTime (date.getTime() - skew);
  }
}

function setCookie (name,value,expires,path,domain,secure)
{
  document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

function getCookie (name)
{
  var cookieFound = false;
  var start = 0;
  var end = 0;
  var cookieString = document.cookie;
  var cookieValue;
  var i = 0;

  while (i <= cookieString.length)
  {
    start = i;
    end = start + name.length;
    if (cookieString.substring(start, end) == name)
    {
      cookieFound = true;
      break;
    }
    i++;
  }

  if (cookieFound)
  {
    start = end + 1;
    end = document.cookie.indexOf(";", start);
    if (end < start)
    {
      end = document.cookie.length;
    }

    cookieValue = document.cookie.substring(start, end);
    start = 0;
    end = cookieValue.indexOf(" ", start);
    if (end < start)
    {
      end = cookieValue.length;
    }

    return cookieValue.substring(start, end);
  }

  return "";
}

function deleteCookie (name, path, domain)
{
  if (getCookie(name))
  {
    document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function setRedirect(dbKey)
{
  deleteCookie("pclsRedirect", "/", "pascolibraries.org");

  var expdate = new Date();
  fixCookieDate(expdate);
  expdate.setTime(expdate.getTime() + (5*60*1000)); // 5 minutes

  var redirectPath = "http://pascolibraries.org/dbremote.shtml";
//  var redirectPath = "/dbremote.shtml";

  setCookie("pclsRedirect", dbKey, expdate, "/", "pascolibraries.org");

  window.location = redirectPath;
}

function testLoggedIn()
{

  var storedID = getCookie("pclsPatronID");
  var idLength = getCookie("pclsPatronAuth");
  var loggedin = 0;

  if( (storedID.length < 13) || (storedID.indexOf(" ") > -1) )
  {

    // Patron came to page without valid ID
    //alert("no match 1");
    loggedin = 0;

  }

  else
  {
    if (storedID.length == idLength)
    {
      //alert("match");
      loggedin = 1;
    }
    else
    {
      //alert("no match 2");
      loggedin = 0;
    }
  }

  return loggedin;

}
