/*
   The Cookie Maximizer Utility
   Version: 1.1
   Netscape Developer's Guide

Notes: Utility functions to set, get and delete cookies.
            setExpire() - sets expiration date in valid date format
            cookieSet() - sets cookie
            cookieDelete() - deletes cookie
            cookieGet() - gets cookie
*/

/*
Setting expiration date for cookies: call setExpire with 2 parameters
                    1. Number of days cookie is valid for.
                    2. Number of hours cookie is valid for.
Use setExpire in conjunction with cookieSet, such as:
cookieSet("myCookie","This is cookie date",setExpire(1,5))
*/
function setExpire(day,hour) {
var dateExpire = new Date ();
count = (day * 24 * 3600 * 1000) + (hour * 3600 * 1000)
dateExpire.setTime (dateExpire.getTime() + count)
return (dateExpire)
}
/*
Setting cookies: call cookieSet() with up to 6 parameters
                       1. The name of the cookie (mandatory)
                       2. The data the cookie is storing (mandatory)
                       3. The expiration date for cookie (optional).
                           Do not set expiration date for temporary cookies
                       4. The path within domain cookie is valid for (optional)
                          Default is current path
                       5. The domain for the cookie (optional)
                          Default is current domain
                       6. Set to true to require secure connection (optional)
                          Default is false.
*/
function cookieSet (name,data,expires,path,domain,secure) {

   document.cookie = name + "=" + escape (data) + 
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "")

}

/*
Deleting cookies: call cookieDelete() with up to 3 parameters
                       1. The name of the cookie (mandatory)
                       2. The path within domain cookie is valid for (optional)
                          Default is current path
                       3. The domain for the cookie (optional)
                          Default is current domain
   Note: makes a cookie invalid by setting to already expired date
*/
function cookieDelete (name,path,domain) {

   if (cookieGet(name)) {
      document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Mon, 31-Dec-79 23:59:59 GMT"
   }

}

//used with cookieGet(); don't call directly
function cookieValue (index) {

   var stringEnd = document.cookie.indexOf (";", index);

   if (stringEnd == -1)
      stringEnd = document.cookie.length
   return unescape(document.cookie.substring(index, stringEnd))

}

/*
Getting cookies: call cookieGet() with name of cookie
*/
function cookieGet (name) {

   var checkString = name + "="
   var lengthCheckString = checkString.length
   var lengthCookie = document.cookie.length
   var position1 = 0

   while (position1 < lengthCookie) {

      var position2 = position1 + lengthCheckString

      if (document.cookie.substring(position1, position2) == checkString) {
         return cookieValue (position2)
      }

      position1 = document.cookie.indexOf(" ", position1) + 1

      if (position1 == 0) {
         break
      }
   }

   return null
}
