// Cookie Support
// Copyright (c) 1999,2000 Randy Tamura

// appendCookie: Append a name/value pair to a CookieVal string
// cCookieVal - The CookieVal string to which the name/value pair should be appended.
// cName - The name of the name/value pair. Duplicate name checking not performed.
// cValue - The value of the name/value pair.
// returns - new CookieVal
function appendCookie(cCookieVal, cName, cValue) {
   // We concatenate a new name/value pair to the Cookie value.
   // This is stored in string format
   return cCookieVal + "&" + cName + ":" + escape(cValue);
}

// extractCookie: Given the name, extract a value from a CookieVal string
// cCookieVal - The CookieVal string from which the value should be extracted.
// cName - The name of the name/value pair. 
// returns - value or empty string if not found.
function extractCookie(cCookieVal, cName) {
   // We look for the specified name and return its value
   // The CookieVal is stored in string format
   var cReturn;   // The return value
   if ("" == cCookieVal) return "";   // No values
   
   // Now find the specific cStoreName cookie in the string
   var nStart = cCookieVal.indexOf("&" + cName + ":");
   if (-1 == nStart) return "";  // cName not found
   nStart += cName.length + 2;   // Point at first data char
   var nEnd = cCookieVal.indexOf("&", nStart); // Find the end
   if (-1 == nEnd) {
      nEnd = cCookieVal.length; // Point at last char
   }
   cReturn = cCookieVal.substring(nStart, nEnd); // Extract the Cookie
   return cReturn;  // and return it
}

// saveCookie: Save a CookieVal - Save to Web browser. It may have several name/value pairs
// doc - The document object that should be used for the save.
// cStoreName - The actual cookie name that will be known by the browser.
// cCookieVal - The CookieVal string to save.
// nSeconds - (optional) (integer) The duration that the cookie should live, in seconds.
// cDomain - (optional) The domain name.
// cPath - (optional) The path associated with the cookie.
// bSecure - (optional) (boolean) The secure option
// returns - void
function saveCookie(doc, cStoreName, cCookieVal, nSeconds, cDomain, cPath, bSecure ) {
   var cCooking;  // Used to store intermediate strings
   var dtExpires; // Date this cookie expires
   cCooking = cStoreName + "=" + cCookieVal // The name=value
   if (nSeconds) {
      // Calculate expiration date
      dtExpires = new Date((new Date()).getTime() + 1000 * nSeconds);
	  cCooking += "; expires=" + dtExpires.toGMTString();
   }
   if (cDomain) { // If the domain was supplied
      cCooking += "; domain=" + cDomain;
   }
   if (cPath) {   // If the path was supplied
      cCooking += "; path=" + cPath;
   }
   if (bSecure) { // If the secure parameter was supplied
      cCooking += "; secure";
   }
   // Done cooking, now assign the cookie
   doc.cookie = cCooking;
}

// loadCookie: Load a CookieVal - It may contain several name/value pairs.
// doc - The document object that should be used for the load
// cStoreName - The actual cookie name that will be known by the browser.
// returns - The CookieVal string or empty string if not found.
function loadCookie(doc, cStoreName) {
   var cCooking;  // Used to store intermediate strings
   var cReturn;   // The return value
   cCooking = doc.cookie; // Get all the Cookie name/values
   if ("" == cCooking) return "";   // No values
   
   // Now find the specific cStoreName cookie in the string
   var nStart = cCooking.indexOf(cStoreName + "=");
   if (-1 == nStart) return "";  // cStoreName not found
   nStart += cStoreName.length + 1; // Point at first data char
   var nEnd = cCooking.indexOf(";", nStart); // Find the end
   if (-1 == nEnd) {
      nEnd = cCooking.length; // Point at last char
   }
   cReturn = cCooking.substring(nStart, nEnd); // Extract the CookieVal
   return cReturn;  // and return it
}
