﻿Type.registerNamespace('Com.Electrabel.Web.UI.WebControls');

Com.Electrabel.Web.UI.WebControls.Preferences = function()
{
	Com.Electrabel.Web.UI.WebControls.Preferences.initializeBase(this);
	this._cookieName = null;
	this._itemName = null;
	this._itemValue = null;
	this._cookiePath = null;
	this._cookieDomain = null;
	this._cookieSecure = null;
	this._cookieExpire = null;
}

Com.Electrabel.Web.UI.WebControls.Preferences.prototype =
{
	get_CookieName: function()
	{
		return this._cookieName;
	},

	set_CookieName: function(value)
	{
		if (this._cookieName != value) this._cookieName = value;
	},

	get_ItemName: function()
	{
		return this._itemName;
	},

	set_ItemName: function(value)
	{
		if (this._itemName != value) this._itemName = value;
	},

	get_ItemValue: function()
	{
		this._itemValue = this._getItem();
		return this._itemValue;
	},

	set_ItemValue: function(value)
	{
		if (this._itemValue != value)
		{
			this._itemValue = value;
			this._setItem(value);
		}
	},

	get_CookiePath: function()
	{
		return this._cookiePath;
	},

	set_CookiePath: function(value)
	{
		if (this._cookiePath != value) this._cookiePath = value;
	},

	get_CookieDomain: function()
	{
		return this._cookieDomain;
	},

	set_CookieDomain: function(value)
	{
		if (this._cookieDomain != value) this._cookieDomain = value;
	},

	get_CookieSecure: function()
	{
		return this._cookieSecure;
	},

	set_CookieSecure: function(value)
	{
		if (this._cookieSecure != value) this._cookieSecure = value;
	},

	get_CookieExpire: function()
	{
		return this._cookieExpire;
	},

	set_CookieExpire: function(value)
	{
		if (this._cookieExpire != value) this._cookieExpire = value;
	},

	clear: function()
	{
		var cookieValue = this._getCookie();
		if (cookieValue)
		{
			var name = this._cookieName;
			var cookiePath = this._cookiePath ? '; path=' + this._cookiePath : '';
			var cookieDomain = this._cookieDomain ? '; domain=' + this._cookieDomain : '';
			document.cookie = name + '=' + '' + cookiePath + cookieDomain + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
		}
	},
	
	_setCookie: function(value)
	{
		var name = this._cookieName;
		var cookiePath = this._cookiePath ? '; path=' + this._cookiePath : '';
		var cookieDomain = this._cookieDomain ? '; domain=' + this._cookieDomain : '';
		var cookieSecure = this._cookieSecure == true ? '; secure' + this._cookieSecure : '';
		var cookieExpire = this._cookieExpire ? '; expires=' + this._cookieExpire : '';
		document.cookie = name + '=' + value + cookiePath + cookieDomain + cookieSecure + cookieExpire;
	},

	_getCookie: function()
	{
		var name = this._cookieName;
		if (document.cookie && document.cookie != '')
		{
			var regex = new RegExp('.*\\b' + escape(name) + '=([^;]*)');
			var match = document.cookie.match(regex);
			if (match) return unescape(match[1]);
		}
		return;
	},

	_setItem: function(value)
	{
		var key = this._itemName;
		var newValue = key ? key + '=' + value : value;
		var cookieValue = this._getCookie();
		if (cookieValue) 
		{
			var itemValue = this._getItem();
			if (key && key != '')
			{
				if (itemValue)
				{
					newValue = cookieValue.replace(key + '=' + itemValue, newValue);
				}
				else
				{
					newValue = cookieValue + '&' + newValue;
				}
			}
			else
			{
				newValue = value;
			}
		}
		this._setCookie(newValue);
	},

	_getItem: function()
	{
		var item = null;
		var key = this._itemName;
		var cookieValue = this._getCookie();
		if (cookieValue)
		{
			if (key && key != '')
			{
				var regex = new RegExp('.*\\b' + escape(key) + '=([^&]*)');
				var match = cookieValue.match(regex);
				if (match) item = unescape(match[1]);
			}
			else
			{
				item = cookieValue;
			}
		}
		return item;
	}
}

Com.Electrabel.Web.UI.WebControls.Preferences.registerClass('Com.Electrabel.Web.UI.WebControls.Preferences', Sys.Component);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();