/**
 * @author Leon Janzen <leon@novgroup.com>
 * @package TNGLibrary
 */

/**
 * Cookie handling class (static)
 */
var TNGCookie={

	/**
	 * Sets a browser cookie
	 * 
	 * @param {Object} strName
	 * @param {Object} strValue
	 * @param {Object} dtmExpires
	 * @param {Object} strPath
	 * @param {Object} strDomain
	 * @param {Object} blnSecure
	 */
	set:function(strName, strValue, dtmExpires, strPath, strDomain, blnSecure)
	{
		document.cookie= strName + "=" + encodeURIComponent(strValue) +
			((dtmExpires) ? "; expires=" + dtmExpires.toGMTString() : "") +
			((strPath) ? "; path=" + strPath : "") +
			((strDomain) ? "; domain=" + strDomain : "") +
			((blnSecure) ? "; secure" : "");
	},
	
	/**
	 * Gets a browser cookie
	 * @param {Object} strName
	 */
	get:function(strName)
	{
		//declare variables
		var strSearch = strName + "=";
		var strCookie="";
		var intOffset=0;
		var intEnd=0;
		
		if (document.cookie.length > 0)
		{ // if there are any cookies
			intOffset = document.cookie.indexOf(strSearch);
			if (intOffset != -1) // if cookie name exists
			{
				intOffset += strSearch.length // set index of beginning of value
				intEnd = document.cookie.indexOf(";", intOffset); // set index of end of cookie value
				if (intEnd == -1)
				{
					intEnd = document.cookie.length;
				}
				strCookie=decodeURIComponent(document.cookie.substring(intOffset, intEnd));
			}           
		}
		
		//for browsers that return "undefined" when a cookie is not found, return an empty string instead, for consistent return values across browsers
		if(strCookie=="undefined")
		{
			strCookie="";
		}
		return strCookie;
	},
	
	/**
	 * Deletes a browser cookie
	 * @param {Object} strName
	 */
	del:function(strName)
	{
		this.set(strName,"",new Date());
	}
}