//-------------------------------------------------------------------
function getCookieValue(cookieName) {
   var cookieValue = document.cookie;
   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

  if (cookieStartsAt == -1) {
      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
  }

   if (cookieStartsAt == -1) {
      cookieValue = null;
   } else {

      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
      if (cookieEndsAt == -1)
         cookieEndsAt = cookieValue.length;
      cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));

   }

   return cookieValue;
}

//-------------------------------------------------------------------
function setCookie(cookieName, cookieValue, cookiePath, cookieExpires) {
   cookieValue = escape(cookieValue);
   if (cookieExpires == "") {

// THIS DOES NOT WORK IN SAFARI
//      var nowDate = new Date();
//      nowDate.setMonth(nowDate.getMonth() + 3);
//      cookieExpires = nowDate.toGMTString();

        var duration = 6 * 30 * 24 * 60;  // 6 months, in minutes

        var expDate = new Date();
        var nowTime = new Date().getTime();
        expDate.setTime(nowTime + (1000 * 60 * duration));
        cookieExpires = expDate.toGMTString();

   }
   if (cookiePath != "") {
      cookiePath = ";Path=" + cookiePath;
   }
 
   document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + cookiePath;
}

//-------------------------------------------------------------------
function deleteCookie(cookieName) {
  setCookie(cookieName,"","","Thu, 01-Jan-70 00:00:01 GMT");
}
