//-------------------------------------------------------------------------------------------
// Set a cookie from our Flex app.
// Refer to O'Reilly's "JavaScript: The Definitive Guide" especially for path, domain and secure values.
// Refer also to http://www.faqs.org/rfcs/rfc2965.html
// If lifetime_in_days is not specified, the cookie only exists for the duration of the browser session.
//-------------------------------------------------------------------------------------------
function setCookie( cookie_name, value, lifetime_in_days, path, domain, port, secure )
{
	if ( lifetime_in_days ) 
	{
		var date = new Date();
		date.setTime( date.getTime() + ( lifetime_in_days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
	}
	else 
	{
		var expires = "";
	}


	var newString = cookie_name + "=" + escape( value ) 
					+ expires
					+ ( ( path == null ) ? "" : ( "; path=" + path ) )	// if "/", cookie is visible to any page on www.pulse.com

//
// In some cases, the domain causes the cookie to not be written, for example if I use .some-domain or some-domain
//
					+ ( ( domain == null ) ? "" : ( "; domain=" + domain ) )	// For us, maybe ".pulse.com" to allow any sub-domain on pulse.com

// We have been advised to not write the port #
//					+ ( ( port == null || ( port.length == 0 ) ) ? "" : ( "; port=" + port ) )	// Port
					+ ( ( secure == null ) ? "" : ( "; secure" ) );	// If true, requires a secure connection in order for it to be sent to server



	document.cookie = newString;

}


//-------------------------------------------------------------------------------------------
// Get the value of the specified cookie.
//-------------------------------------------------------------------------------------------
function getCookie( cookie_name )
{
	if ( document.cookie.length > 0 )
	{
		// Look for the start of the cookie whose name is specified by cookie_name param
		var c_start = document.cookie.indexOf( cookie_name + "=" );
		
		// If we found a cookie by the name, extract and use its value
		if ( c_start != -1 )	
		{ 
			c_start = c_start + cookie_name.length + 1; 		// Seek to start of the cookie's value
			c_end = document.cookie.indexOf( ";", c_start );	// Find the end of the value
			
			if ( c_end == -1 ) 	// If semicolon not found, the value extends to the end of the cookie
				c_end = document.cookie.length;	
		    
		    return unescape( document.cookie.substring( c_start, c_end ) );	// Unencode and return the value substring
		} 
	}

	return "";
}


//-------------------------------------------------------------------------------------------
// Forces a cookie to expire.
// "path" and "domain" need to be what was used when the cookies was set (via setCookie).
//-------------------------------------------------------------------------------------------
function expireCookie( cookie_name, path, domain, port ) 
{
	if ( getCookie( cookie_name ) )
	{
		document.cookie = cookie_name + "=" +
		( ( path == null ) ? "" : "; path=" + path ) +

//
// In some cases, the domain causes the cookie to not be written, for example if I use .allany-dell-690 or allany-dell-690
//
		( ( domain == null ) ? "" : "; domain=" + domain ) +

// We have been advised to not write the port #
//		( ( port == null || ( port.length == 0 ) ) ? "" : ( "; port=" + port ) ) +
		"; expires=Fri, 02-Jan-1970 00:00:00 GMT";
	}
}


