/*
 * copy from http://www.json.org/json.js
 * modified by hobo at 2007-4-19 to fix ie5
 */

if (!Object.prototype.toJSONString)
{
	Boolean.prototype.toJSONString = Date.prototype.toJSONString
		= Number.prototype.toJSONString = function()
	{
		return this.toString();
	};

	Array.prototype.toJSONString = function()
	{
		var a = ['['], b, i, l = this.length, v;

		function p(s)
		{
			if (b)
			{
				a.push(',');
			}
			a.push(s);
			b = true;
		}

		for (i = 0; i < l; i += 1)
		{
			v = this[i];
			switch (typeof v)
			{
				case 'object':
					if (v)
					{
						if (typeof v.toJSONString === 'function')
						{
							p(v.toJSONString());
						}
					}
					else
					{
						p("null");
					}
					break;

				case 'string':
				case 'number':
				case 'boolean':
					p(v.toJSONString());
			}
		}

		a.push(']');
		return a.join('');
	};

	Object.prototype.toJSONString = function()
	{
		var a = ['{'], b, k, v;

		function p(s)
		{
			if (b)
			{
				a.push(',');
			}
			a.push(k.toJSONString(), ':', s);
			b = true;
		}

		for (k in this)
		{
			v = this[k];
			switch (typeof v)
			{
				case 'object':
					if (v) {
						if (typeof v.toJSONString === 'function')
						{
							p(v.toJSONString());
						}
					}
					else
					{
						p("null");
					}
					break;

				case 'string':
				case 'number':
				case 'boolean':
					p(v.toJSONString());
			}
		}

		a.push('}');
		return a.join('');
	};

	String.prototype.toJSONString = function()
	{
		var m = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'};

		if (/["\\\x00-\x1f]/.test(this))
		{
			return '"' + this.replace(/([\x00-\x1f\\"])/g, function (a, b) {
				var c = m[b];
				if (c)
				{
					return c;
				}
				c = b.charCodeAt();
				return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
			}) + '"';
		}

		return '"' + this + '"';
	};

	String.prototype.parseJSON = function ()
	{
		try
		{
			return eval('(' + this + ')');
		}
		catch (e)
		{
			return null;
		}
	};

	Array.prototype.jsonSort = function(field, order)
	{
		var _property = field;
		var _desc = order && order.toLowerCase()=="desc";

		return this.sort(function(a, b)
		{
			if (a[_property] < b[_property])
			{
				return _desc ? +1 : -1;
			}
			else if (a[_property] > b[_property])
			{
				return _desc ? -1 : +1;
			}
			else
			{
				return 0;
			}
		});
	};
}