/*
 * HTML5 functions for Session Storage and Local Storage
 * If the browser doesn't support SessionStorage a cookies-based fallback is used.
 */

function GetSessionStorageItem(key) {
	if (window.sessionStorage) {
		// Check item expiracy
		var expires = window.sessionStorage[key + ".~expires~"];
		if (expires) {
			var now = new Date().getTime();
			if (now > expires) {
				// Item expired, remove it
				window.sessionStorage.removeItem(key);
				// Remove item expiracy
				if (window.sessionStorage[key + ".~expires~"]) {
					window.sessionStorage.removeItem(key + ".~expires~");
				}
				return null;
			}
		}
		return window.sessionStorage[key];
	}
	if (GetCookie) {
		return GetCookie(key);
	}
	return null;
}

function GetLocalStorageItem(key) {
	if (window.localStorage) {
		// Check item expiracy
		var expires = window.localStorage[key + ".~expires~"];
		if (expires) {
			var now = new Date().getTime();
			if (now > expires) {
				// Item expired, remove it
				window.localStorage.removeItem(key);
				// Remove item expiracy
				if (window.localStorage[key + ".~expires~"]) {
					window.localStorage.removeItem(key + ".~expires~");
				}
				return null;
			}
		}
		return window.localStorage[key];
	}
	if (GetCookie) {
		return GetCookie(key);
	}
	return null;
}

function SetSessionStorageItem(key, value, max_age){
	if (window.sessionStorage) {
		// Store item
		window.sessionStorage[key] = value; // Persistent item with expiracy
		if (max_age) {
			// Store item expiracy
			var expires = new Date();
			expires.setTime(expires.getTime() + max_age * 1000);
			window.sessionStorage[key + ".~expires~"] = expires.getTime();
		}
		else {
			// Remove item expiracy
			if (window.sessionStorage[key + ".~expires~"]){
				window.sessionStorage.removeItem(key + ".~expires~");
			}
		}
	}
	else if (SetCookie) {
		SetCookie(key, value, max_age);     // Volatile cookie
	}
}

function SetLocalStorageItem(key, value, max_age){
	if (window.localStorage) {
		// Store item
		window.localStorage[key] = value;   // Persistent item with expiracy
		if (max_age) {
			// Store item expiracy
			var expires = new Date();
			expires.setTime(expires.getTime() + max_age * 1000);
			window.localStorage[key + ".~expires~"] = expires.getTime();
		}
		else {
			// Remove item expiracy
			if (window.localStorage[key + ".~expires~"]){
				window.localStorage.removeItem(key + ".~expires~");
			}
		}
	}
	else if (SetCookie) {
		if (max_age){
			SetCookie(key, value, max_age);
		} else {
			SetCookie(key, value, 10*365*24*3600); // "Persistent" cookie, for 10 years
		}
	}
}

function DeleteSessionStorageItem(key){
	if (window.sessionStorage) {
		// Remove item
		if (window.sessionStorage[key]){
			window.sessionStorage.removeItem(key);
		}
		// Remove item expiracy
		if (window.sessionStorage[key + ".~expires~"]){
			window.sessionStorage.removeItem(key + ".~expires~");
		}
	}
	else if (DeleteCookie) {
		DeleteCookie(key);
	}
}

function DeleteLocalStorageItem(key){
	if (window.localStorage) {
		// Remove item
		if (window.localStorage[key]){
			window.localStorage.removeItem(key);
		}
		// Remove item expiracy
		if (window.localStorage[key + ".~expires~"]){
			window.localStorage.removeItem(key + ".~expires~");
		}
	}
	else if (DeleteCookie) {
		DeleteCookie(key);
	}
}

