﻿function DOMStorage() { }
DOMStorage.UseCookiesIfNotSupported = true;

//==========================================
// Get the contents of local storage
//==========================================
DOMStorage.GetData = function GetData()
{
	if (!DOMStorage.Supported())
	{
		if (!DOMStorage.UseCookiesIfNotSupported)
			throw new Error("DOM Storage is not supported");
		else
		{
			//parse the collections of DOM storage cookies
			var arrCookies = document.cookie.split(';');
			var objKeys = new Object();

			for (var intIndex = 0; intIndex < arrCookies.length; intIndex++)
			{
				var strCookie = arrCookies[intIndex];

				//ltrim
				while (strCookie.charAt(0) == " ")
					strCookie = strCookie.substring(1, strCookie.length);

				if (strCookie.indexOf("DS_") == 0)
				{
					var intSplit = strCookie.indexOf("=");
					if (intSplit == -1)
						intSplit = strCookie.length;
					
					var strName = strCookie.substring(3, intSplit); //strip off the DS_ prefix
					var strValue = strCookie.substring(intSplit + 1, strCookie.length);

					objKeys[strName] = strValue;
				}
			}

			return objKeys;
		}
	}
	else
	{
		var objKeys = new Object();
		var intCount = window.localStorage.length;

		for (var intIndex = 0; intIndex < intCount; intIndex++)
		{
			var strKey = window.localStorage.key(intIndex);
			var strValue = DOMStorage.Get(strKey);

			objKeys[strKey] = strValue;
		}

		return objKeys;
	}
}

//==========================================
// Set a value into the DOM Storage collection
//==========================================
DOMStorage.Set = function Set(strKey, strValue)
{
	if (DOMStorage.Supported())
	{
		if ((strValue == "" || strValue == null) && window.localStorage.removeItem)
			window.localStorage.removeItem(strKey);
		else
			window.localStorage[strKey] = strValue;
	}
	else
	{
		if (DOMStorage.UseCookiesIfNotSupported)
		{
			//set the value in a cookie
			var date = new Date();
			date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000)); //cookie valie for 365 days
			var strExpires = "; expires=" + date.toGMTString();
			document.cookie = "DS_" + strKey + "=" + strValue + strExpires + "; path=/";
		}
		else
			throw new Error("DOM Storage is not supported");
	}
}

//==========================================
// Get a value from the DOM Storage collection
//==========================================
DOMStorage.Get = function Get(strKey)
{
	if (DOMStorage.Supported())
		return window.localStorage[strKey];
	else
	{
		if (DOMStorage.UseCookiesIfNotSupported)
		{
			//return the value from a cookie
			var nameEQ = "DS_" + strKey + "=";
			var ca = document.cookie.split(';');
			for (var i = 0; i < ca.length; i++)
			{
				var c = ca[i];
				while (c.charAt(0) == ' ') c = c.substring(1, c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
			}
		}
	}
		
	return null;
}

//==========================================
// Is dom storage supported by this browser?
//==========================================
DOMStorage.Supported = function Supported()
{
	try
	{
		if (window.localStorage)
			return true;

		return false;
	}
	catch (exc)
	{
		return false;
	}
}

//==========================================
// Clear all entries in DOM storage
//==========================================
DOMStorage.Clear = function DOMStorage_Clear()
{
	try
	{
		if (!DOMStorage.Supported())
			return false;

		var objData = DOMStorage.GetData();

		for (strKey in objData)
			DOMStorage.Set(strKey, null);

		return true;
	}
	catch (exc)
	{
		return false;
	}
}
