// **************************
// JavaScript Cookies Methods
// **************************

function setCookie(name, value, expires)
{
	// Set time in milliseconds
	var today = new Date();
	today.setTime(today.getTime());
	
	// If the expires variable is set, set the expiration time
	// The current script below will set it for x number of days
	// To make it for hours, delete * 24; for minutes, delete * 60 * 24
	if (expires)
		expires = expires * 1000 * 60 * 60 * 24;
	
	var expirationDate = new Date(today.getTime() + expires);
	
	document.cookie = name + "=" + escape(value) + (expires ? ";expires=" + expirationDate.toGMTString() : "") + ";path=/;";
}

function getCookie(search_name)
{
	// First split the cookie into name/value pairs
	// NOTE: [document.cookie] only returns name=value
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	
	for (i = 0; i < a_all_cookies.length; i++)
	{
		// Split apart each name/value pair
		a_temp_cookie = a_all_cookies[i].split('=');
		
		// Trim whitespace
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		// Check if the extracted name matches
		if (cookie_name == search_name)
		{
			b_cookie_found = true;
			
			// Handle case where cookie has no value but exists (no = sign, that is):
			if (a_temp_cookie.length > 1)
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			
			// NOTE: in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		
		a_temp_cookie = null;
		cookie_name = '';
	}
	
	if (!b_cookie_found)
		return null;
}

function deleteCookie(name)
{
	if (getCookie(name))
		document.cookie = name + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
