/***********************************************************************
Elsevier Cookie Utility Object

This javascript object provides wraps the reading/writing of a cookie.

When you construct the cookie you give it the name and other attributes. 

You get the cookie value with cookie.get().

You set the value in the cookie with cookie.set().  

The other functions here (load and save) are straight methods that do not
persist the cookie's current value in the javacript object.  They are helper
methods for get/set and should not be used directly.

Note:  The save/load functions are derived from code originally downloaded from: 
	http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm

************************************************************************/	
	
// Note:  path, domain, and secure are optional.	
function elsCookie(cookieName,expires,path,domain,secure) {
		
	// Cookie attributes:		
	this.name = cookieName;
	this.expires = expires;
	this.path = path;
	this.domain = domain;
	this.secure = secure;
	
	// Current value in the cookie:
	this.value = null;	
		
	// Cookie object methods:	
		
	// Get the value of the cookie	
	this.get = function () {
		// We only need to load the value once per page.
		if(!this.value) {
			this.value = this.load(this.name);
		} 
		
		return this.value;
	}; 
	
	// Set the value in the cookie
	this.set = function (value) {
		// There's nothing to be done if the value's no different:
		if(this.value != value) {
			this.value = value;
			this.save(this.name,this.value,this.expires,this.path,this.domain,this.secure);
		} 
	}; 
	
	// Delete the cookie
	this.remove = function () {
		this.value = "";
		this.save(this.name,this.value,-500,this.path,this.domain,this.secure);
	}; 	
	
	// Helper methods access the cookie directly:	
	
	// Load the cookie value directly and return it:
	this.load = function (name) { 
	   var start = document.cookie.indexOf(name+"="); 
	   var len = start+name.length+1; 
	   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
	   if (start == -1) return null; 
	   var end = document.cookie.indexOf(";",len); 
	   if (end == -1) end = document.cookie.length; 
	   return unescape(document.cookie.substring(len,end)); 
	}; 
	
	// Save the value directly to the cookie
	this.save = function (name,value,expires,path,domain,secure) {	 
		expires = expires * 60*60*24*1000;
		var today = new Date();
		var expires_date = new Date( today.getTime() + (expires) );
	    var cookieString = name + "=" +escape(value) + 
	       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	       ( (path) ? ";path=" + path : "") + 
	       ( (domain) ? ";domain=" + domain : "") + 
	       ( (secure) ? ";secure" : ""); 
	    document.cookie = cookieString;     
	}; 		
}
	