// Copyright 2011 Coast Guard Auxiliary Association, Inc. All Rights Reserved.
// Cookie jar of general cookie client-side functions.

//-------------------------------------------------------------------------------
// Create a cookie given minimally its name and value. Other params are optional;
// a null placeholder is not required for trailing omitted arguments.
// inName - name
// inValue - value
// [inPath] - path for which the cookie is valid (default = path of calling document)
// [inExpMons] - 0=session expiration, else # of months in future

function setCookie(inName, inValue, inPath, inExpMons) {
  if (inValue == "") inValue = 0; //"NONE";

  var cookie = inName + "=" + escape(inValue);

  if (inPath != "") cookie += ";path=" + inPath;

  if (inExpMons > 0) {
    // Set expiration date to today plus inExpMons months.
    var dToday = new Date();
    dToday.setMonth(dToday.getMonth() + inExpMons);
    cookie += ";expires=" + dToday.toGMTString();
  }

  //cookie += ";domain=" + document.domain + ";"

  document.cookie = cookie;

  //window.alert("setCookie: " + cookie);
  //window.alert("setCookie: document.cookie='" + document.cookie + "'");
} // setCookie

//---------------------------------------------------------------
// Find a cookie. If found, return its value else the null value.
// inName - name of desired cookie

function getCookie(inName) {
  //window.alert("getCookie: inName="+inName);
  var dc = document.cookie;
  var prefix = inName + "=";
  var begin = dc.indexOf(prefix);

  //if (begin == -1) begin = dc.indexOf(prefix);
  if (begin == -1) return null; // not found, return null value

  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;

  return unescape(dc.substring(begin + prefix.length, end));
} // getCookie

//---------------------------------------------------------------------------------------
// If an existing cookie is found, delete it by setting its expiration date to 01-Jan-70.
// inName - name of the cookie
// [inPath] - path of the cookie (must be same as path used to create cookie)

function deleteCookie(inName, inPath) {
  if (getCookie(inName)) {
    // Cookie found, delete it.
    document.cookie = inName + "=" + ((inPath) ? ";path=" + inPath : "") + ";expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
} // deleteCookie
