﻿// JScript File
var Cookies = 
{
	// Loads all existing cookies and adds to hash table
	Load: function() 
	{
		// Load each cookie into an array
		var existingCookies = document.cookie.split('; ');

		// Go through each cookie
		for(var i = 0; i < existingCookies.length; i++)
		{
			// Split into key-value pairs
			var keyValuePair = existingCookies[i].split('=');
			
			// Add to the hash table
			this[keyValuePair[0]] = keyValuePair[1];
		}
	}, 

	// Creates a new cookie with the given expiration time
	Create: function(Name, Value, ExpirationDays)
	{
		var expires = "";
		
		// If given an expiration, set it to now + the number of days
		if(ExpirationDays)
		{
			// Convert days to ms and set as date
			var date = new Date();
			date.setTime(date.getTime() + (ExpirationDays * 24 * 60 * 60 * 1000));
			
			// Expiry must be GMT
			expires = "; expires=" + date.toGMTString();
		}
		
		// Set cookie and add to hash table
		document.cookie = Name + "=" + Value + expires + "; path=/";
		this[Name] = Value;
	}, 

	Delete: function(Name)
	{
		// Overwrite  the cookie with nothing and remove from hash table
		this.Create(Name, '', -1);
		this[Name] = undefined;
	}
};

Cookies.Load();