/**
 *Javascript cookieManager.js created on Tuesday 09-09-08
 *Contact: david.brown@brainingcamp.com
 *Purpose: External javascript to handle general HTTP cookie 
 *Notes: Users of this module should follow the following guidelines for managing data in cookies
		,		Delimiter for dataset's in cookies. Used to break up different values in a dataset.
 */
 
/**
*Sets a cookie.
 *******************Arguments************************
 * name			Name of the cookie
 * value		Value of the cookie
 * [ttl]		Number of DAYS a cookie will be valid.
 * [path]		Path where the cookie is valid. **Default: path of calling document
 * [domain]		Domain where the cookie is valid. **Default: domain of calling document
 * [secure]		Boolean specifying whether cookie must be sent via secure transmission (i.e. SSL)
 *
 * limitations: none
 * return value: none
*/
 function setCookie(name, value, ttl, path, domain, secure) {
	//Calculate expiration
	var dateSet = new Date();
	var dateExpires = new Date(dateSet.getTime() + ttl * 24 * 60 * 60 * 1000);
	
	//Set cookie. Use shorthand logical checks to determine values for possible null values ([ttl])
	document.cookie = name + "=" + escape(value) +
	( (ttl) ? "; expires=" + dateExpires.toGMTString() : "" ) +
	( (path) ? "; path=" + path : "" ) +
	( (domain)  ? "; domain=" + domain : "" ) + 
	( (secure) ? "; secure" : "" );	
 }
 
/**
*Gets a cookie
 *******************Arguments************************
 * name			Name of the cookie to be retrieved
 *
 * limitations: none
 * return value: String containing value of specified cookie or null if cookies does not exist.
*/
 function getCookie(name){
	 var dc = document.cookie;
	 //Retrieving cookie requires string searching. Parts are: term(string) start(int)......end(int)
	 var term = name + "=";
	 var start = dc.indexOf("; " + term);
	 //If start == -1, search failed for 1 to n cookies. Check first cookie
	 if(start == -1){
		 start = dc.indexOf(term);
		 //If start != 0, first cookie does not match search term either. Cookie does not exist, return null
		 if(start != 0){
			return null; 
		 }
	 }else{
		//cookie name found, add 2 to index location. New index location specifies the start of the data section of the cookie
		start += 2;
	 }
	 //Search for end of data section (specified by next encountered ;)
	 var end = document.cookie.indexOf(";", start);
	 //If end == -1, there is no ; marking end of data section, therefore the last character in the cookie marks the end of the data section
	 if (end == -1) end = dc.length;
	 
	 return unescape(dc.substring(start + term.length, end));
 }
 
/**
* Deletes the specified cookie.
 *******************Arguments************************
 * name      name of the cookie
 * [path]    path of the cookie 
 * [domain]  domain of the cookie
 *
 * limitations: path and domain MUST match the path and domain used to create the cookie EXACTLY
 * return value: none
*/
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
	
}

/**
*Checks if cookies are enabled
 *******************Arguments************************
 *
 *limitations: none
 *return value: Boolean specifying test success or failure
*/
function cookiesEnabled() 
{
	var testName = "bcTestCookies";
	var testData = "abcdef";
	
	setCookie(testName, testData);
	
	if( getCookie(testName) == null )
	{
		return false;
	}
	else
	{
		return true;
	}
}

