﻿Vehix.Web.Consumer.Portal.QueryString = function(s)
{
	if (s && s.length)
	{
		this._queryString = s.substring(s.indexOf("?") + 1);
	}
	else
	{
		this._queryString = window.location.search.substring(1);
	}
}

Vehix.Web.Consumer.Portal.QueryString.prototype =
{
	Clear: function()
	{
		this._queryString = "";
	},

	GetValue: function(key)
	{
		var result;
		var q = this._queryString;
		if (q)
		{
			var a = q.split("&");
			for (i = 0; i < a.length; i++)
			{
				o = a[i].split("=");
				if (o[0].toLowerCase() == key.toLowerCase())
				{
					result = decodeURIComponent(o[1]);
				}
			}
		}
		return result;
	},

	ContainsKey: function(key)
	{
		var result = false;
		var q = "&" + this._queryString;
		if (q && key)
		{
			if (q.toLowerCase().search("&" + key.toLowerCase() + "=") != -1)
				result = true;
		}
		return result;
	},

	SetValue: function(key, value)
	{
		var q = this._queryString;
		var s = '';
		if (this.ContainsKey(key))
		{
			var a = q.split("&");
			for (i = 0; i < a.length; i++)
			{
				o = a[i].split("=");
				if (o[0].toLowerCase() == key.toLowerCase())
				{
					s += '&' + o[0] + '=' + encodeURIComponent(value);
				}
				else
				{
					if (o[0] && o[1])
						s += '&' + o[0] + '=' + o[1];
				}
			}
			this._queryString = s.substring(1);
		}
		else
		{
			this._queryString = q + '&' + key + '=' + encodeURIComponent(value);
		}
	},

	Add: function(key, value)
	{
		this.SetValue(key, value);
	},

	Remove: function(key)
	{
		var q = this._queryString;
		var s = '';
		if (this.ContainsKey(key))
		{
			var a = q.split("&");
			for (i = 0; i < a.length; i++)
			{
				o = a[i].split("=");
				if (o[0].toLowerCase() != key.toLowerCase())
				{
					if (o[0] && o[1])
						s += '&' + o[0] + '=' + o[1];
				}
			}
			this._queryString = s.substring(1);
		}
	},

	ToString: function()
	{
		return this._queryString;
	}
}

