/**
 * Sets a Cookie with the given name and value.
 *
 * strName      The name of the cookie.
 * value        Value of the cookie.
 * [expires]    Expiration date of the cookie
 *             (default: end of current session)
 * [strPath]    Path where the cookie is valid
 *              (default: path of calling document)
 * [strDomain]  Domain where the cookie is valid
 *              (default: domain of calling document)
 * [boolSecure] Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 *
 * @return Nothing.
 */
function setCookie(strName, value, expires, strPath, strDomain, boolSecure) {
  document.cookie = "" +
    strName + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((strPath) ? "; path=" + path : "") +
    ((strDomain) ? "; domain=" + domain : "") +
    ((boolSecure) ? "; secure" : "");

  return;
}

/**
 * @description Gets the value of the specified cookie.
 *
 * @author Sami Keskinen
 * @since 12.08.2006
 *
 * @param strName Name of the desired cookie.
 *
 * @return A string, containing value of specified cookie;
 *         null if cookie does not exist.
 */
function getCookie(strName) {
  /*
    12.08.2006 SK:
      Check where the cookie begins.
  */
  var strCookies = document.cookie;
  var strPrefix = strName + "=";
  var intBegin = strCookies.indexOf("; " + strPrefix);

  /*
    12.08.2006 SK:
      Check if searched cookie is the first in cookies list.
  */
  if (intBegin == -1) {
    intBegin = strCookies.indexOf(strPrefix);

    if (intBegin != 0) {
      return;
    }
  }

  else {
    intBegin += 2;
  }

  /*
    12.08.2006 SK:
      Check cookie end.
  */
  var intEnd = strCookies.indexOf(";", intBegin);
  if (intEnd == -1) {
      intEnd = strCookies.length;
  }

  /*
    12.08.2006 SK:
      Reform cookie.
  */
  var strCookie = strCookies.substring(intBegin + strPrefix.length, intEnd);
  return (unescape(strCookie));
}

/**
 * @description Deletes the specified cookie.
 *
 * @author Sami Keskinen
 * @since 12.08.2006
 *
 * @param strName     Name of the cookie
 * @param [strPath]   Path of the cookie
 *                    (Must equal to the path given when the cookie was
 *                    created.)
 * @param [strDomain] Domain of the cookie
 *                    (Must equal to the domain given when the cookie was
 *                    created.)
 *
 * @return Nothing.
 */
function deleteCookie(strName, strPath, strDomain) {
  /*
    12.08.2006 SK:
      Check if cookie exists.
  */
  if (!(getCookie(strName))) {
    return;
  }

  /*
    12.08.2006 SK:
      Set the cookie's value to nothing; i.e. delete the cookie.
  */
  document.cookie = "" +
    strName + "=" +
    ((strPath) ? "; path=" + strPath : "") +
    ((strDomain) ? "; domain=" + strDomain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";

  return;
}

/**
 * @description Checks if a cookie exists.
 *
 * @author Sami Keskinen
 * @since 12.08.2006
 *
 * @param strName Name of the cookie to be checked.
 *
 * @return Boolean true if a cookie exists, false otherwise.
 */
function isCookieSet(strName) {
  return ((typeof getCookie(strName)) != "undefined");
}
