/**
 * This file must be included to simplify cookie handles. See also
 * <a href='http://www.webreference.com/js/column8/functions.html'>Crispy JavaScript Cookies: JavaScript Cookie Functions - Doc JavaScript</a> or
 * <a href='http://techpatterns.com/downloads/javascript_cookies.php'>Javascript Cookie Script: Get Cookie, Set Cookie, Delete Cookie Functions</a>.
 * 
 * @version 09-11-2009
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=cookie.js">R. ten Napel, ing.</a>
 **/

/**
 * The static class that handles cookie functions.
 **/
Cookie = new function() {
	/**
	 * Sets the cookie variable.
	 * 
	 * @param name			The name of the cookie.
	 * @param value			The cookie value.
	 * @param expires		Null or the cookie expiration date (default is end of current session).
	 * @param path			Null or the path for which the cookie is valid (default to path of calling document).
	 * @param domain		Null or the the domain for which the cookie is valid (default to domain of calling document).
	 * @param secure		Null or True if the cookie transmission requires a secure transmission or false.
	 **/
	this.save = function(name, value, expires, path, domain, secure) {
		// Create cookie string:
		var cookie = name + "=" + escape(value) + 
				((expires) ? "; expires=" + expires.toGMTString() : "") + 
				((path) ? "; path=" + path : "") + 
				((domain) ? "; domain=" + domain : "") + 
				((secure) ? "; secure" : "");
		
		// Save the cookie:
		document.cookie = cookie;
	};
	
	/**
	 * Retrieve a cookie with a certain name.
	 * 
	 * @param name			The name of the cookie to retrieve.
	 * 
	 * @return				The right cookie.
	 **/
	this.get = function(name) {
		var allCookies = document.cookie.split(";");
		var tempCookie = "";
		var cookieName = "";
		var cookieValue = "";
		var cookieFound = false;
		
		for (i = 0; i < allCookies.length; i++) {
			// Split the cookie name and value:
			tempCookie = allCookies[i].split("=");
			
			// Trim the temp cookie name:
			cookieName = tempCookie[0].trim();
			
			// Check if the right name:
			if (cookieName == name) {
				cookieFound = true;
				
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if (tempCookie.length > 1) {
					cookieValue = unescape(tempCookie[1].trim());
				}
				
				// note that in cases where cookie is initialized but no value, null is returned
				return cookieValue;
			}
			
			tempCookie = null;
			cookieName = "";
		}
		
		// Return null if no cookie is found:
		return null;
	};
	
	/**
	 * Remove the cookie.
	 * 
	 * @param name			The name of the cookie to delete.
	 * @param path			Null or the path to the cookie to delete (must be the same as the creation path).
	 * @param domain		Null or the domain of the cookie.
	 **/
	this.remove = function(name, path, domain) {
		if (Cookie.get(name)) {
			document.cookie = name + "=" +
					((path) ? "; path=" + path : "") +
					((domain) ? "; domain=" + domain : "") +
					"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	};
};
