// Get the cookie with the supplied name
function getCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) 
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }

  return null;
}

// Determine if a cookie exists
function existsCookie (name)
{
	var flag = false;
  if (getCookie (name) != null)
		flag = true;

  return flag;
}

// Display login or logout URL
function showLoginLogoutUrl()
{
	if (existsCookie("CAMS_SID_CSCAMSCLUSTER_CAMSUSERS"))
		document.write("<a href='/cams/logout?cams_security_domain=camsusers' class='marginLink'>LOGOUT</a>")
	else
		document.write("<a href='/login.html' class='marginLink'>LOGIN</a>")
}

// Return substring containing an individual cookie 
function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
      endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// Set cookie with referrer value only if cookie does not exist
function SetCookie (name) 
{
  if (!existsCookie (name))
  {
	  var value = document.URL + "|" + document.referrer;
    var expires = new Date (); 
    expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000 * 365)); 
	  var path = '/';
	  var domain;
	  var secure = false;
    document.cookie = name + "=" + value +
      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
      ((path == null) ? "/" : ("; path=" + path)) +
      ((domain == null) ? "" : ("; domain=" + domain)) +
      ((secure == true) ? "; secure" : "");
  }
}
