
// --------------------------------------------------------------------
// Set a cookie of the given name to the given value, with appropriate
// expiration etc.
// --------------------------------------------------------------------
function setCookie(name, value, expires, path, domain, secure) {
  // The time, in milliseconds
  var today = new Date();
  today.setTime(today.getTime());

  // If the expires variable is set, make the correct
  // expires time, the current script below will set
  // it for x number of days, to make it for hours,
  // delete * 24, for minutes, delete * 60 * 24
  if (expires) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expiresDate = new Date(today.getTime() + (expires));
  //alert('(dbg) Cookie string: [' +
  //  name + "=" +escape(value) +
  //  ( ( expires ) ? "; expires=" + expiresDate.toGMTString() : "" ) +
  //  ( ( path ) ? "; path=" + path : "" ) +
  //  ( ( domain ) ? "; domain=" + domain : "" ) +
  //  ( ( secure ) ? "; secure" : "" ) + ']');

  document.cookie = name + "=" +escape(value) +
  ( ( expires ) ? "; expires=" + expiresDate.toGMTString() : "" ) +
  ( ( path ) ? "; path=" + path : "" ) +
  ( ( domain ) ? "; domain=" + domain : "" ) +
  ( ( secure ) ? "; secure" : "" );
}
	
// --------------------------------------------------------------------
// Get the cookie of the given name.
// --------------------------------------------------------------------
function getCookie(checkName) {
  // Split cookies up into name/value pairs
  // Note: document.cookie only returns name=value, not the other components
  var allCookies = document.cookie.split( ';' );
  var tempCookie = '';
  var cookieName = '';
  var cookieValue = '';
  var cookieFound = false;

  for (i=0; i < allCookies.length; i++ ) {
    // Split apart name=value pairs
    tempCookie = allCookies[i].split('=');

    // Trim left/right whitespace
    cookieName = tempCookie[0].replace(/^\s+|\s+$/g, '');

    // Check if extracted name matches passed checkName
    if (cookieName == checkName) {
      cookieFound = true;
      // Handle the case where cookie has no value but exists (IE. no = sign):
      if (tempCookie.length > 1) {
        cookieValue = unescape(tempCookie[1].replace(/^\s+|\s+$/g, ''));
      }
      // In cases where cookie is initialized but has no value, null is returned
      return cookieValue;
      break;
    }
    tempCookie = null;
    cookieName = '';
  }
  if (!cookieFound) {
    return null;
  }
}

// --------------------------------------------------------------------
// Delete the cookie of the given name.
// --------------------------------------------------------------------
function deleteCookie(name, path, domain) {
  if (getCookie(name))
    document.cookie = name + "=" +
                      ( ( path ) ? ";path=" + path : "") +
                      ( ( domain ) ? ";domain=" + domain : "" ) +
                      ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


