Object.prototype.toString = function ()
{
	var str = "";
	for (var key in this)
		if (typeof this [key] != "function")
			str += key + " => " + this [key] + "\n";
	return str;
};

function mrObject ()
{}

mrObject.prototype.create = function (tagName, attributes)
{
	var element = document.createElement (tagName);
	for (var name in attributes)
		this.setAttribute (element, name, attributes [name]);
	return element;
};

mrObject.prototype.setAttribute = function (element, name, value)
{
	var attr = document.createAttribute (name);
	attr.value = value;
	element.setAttributeNode (attr);
	return element;
};

mrObject.prototype.appendChild = function (element, tagName, attributes)
{
	var child = this.create (tagName, attributes);
	element.appendChild (child);
	return child;
};

mrObject.prototype.removeChild = function (element)
{
	if (element.firstChild)
		element.removeChild (element.firstChild);
	return element;
};

mrObject.prototype.removeAllChildren = function (element)
{
	while (element.firstChild)
		element.removeChild (element.firstChild);
	return element;
};

mrObject.prototype.replaceChild = function (element, tagName, attributes)
{
	this.removeChild (element);
	return this.appendChild (element, tagName, attributes);
};

mrObject.prototype.setText = function (element, text)
{
	this.removeChild (element).appendChild (document.createTextNode (text));
	return element;
};

mrObject.prototype.focus = function (element)
{
	setTimeout (function () {element.focus ();}, 250);
	return element;
};

mrObject.prototype.isEmpty = function (input)
{
	for (var nchar = 0; nchar < input.value.length; ++nchar)
		if (input.value.charAt (nchar) != ' ')
			return false;
	return true;
};

mrObject.prototype.floatKey = function (input)
{
	if (input.value.length > 0)
	{
		var newPos = input.value.length - 1;
		var newChar = input.value.charAt (newPos);
		if (!(("0" <= newChar && newChar <= "9")
			  || (newChar == "." && input.value.indexOf (".") == newPos)))
			input.value = input.value.slice (0, newPos);
	}
};

mrObject.prototype.rtrim = function (input)
{
	for (var nchar = input.value.length - 1; nchar >= 0
		 && input.value.charAt (nchar) == ' '; --nchar)
		;
	if (++nchar < input.value.length)
		input.value = input.value.substr (0, nchar);
	return input;
};

mrObject.prototype.setCaption = function (table, caption)
{
	var cap = table.firstChild;
	if (!cap)
		cap = table.createCaption ();
	while (cap.firstChild)
		cap.removeChild (cap.firstChild);
	if (typeof caption == "string")
		cap.appendChild (document.createTextNode (caption));
	else
	{
		var capChild;
		for (var n = 0; n < caption.length; n += 2)
		{
			capChild = document.createElement (caption [n]);
			cap.appendChild (capChild);
			capChild.appendChild (document.createTextNode (caption [n + 1]));
		}
	}
	return table;
};

mrObject.prototype.selectEnable = function (select)
{
	select.disabled = false;
	select.selectedIndex = 0;
	select.removeChild (select.options [1]);
	return select;
};

mrObject.prototype.selectDisable = function (select)
{
	select.length = 2;
	select.removeChild (select.options [1]);
	select.appendChild (document.createElement ("option"));
	select.selectedIndex = 1;
	select.disabled = "disabled";
	return select;
};

mrObject.prototype.magicButton = function (button, visible)
{
	button.style.visibility = visible ? "visible" : "hidden";
	return button;
};

mrObject.prototype.magicTable = function (input, keyCells, newRow)
{
	row = input.parentNode.parentNode;
	table = row.parentNode;
	for (var firstRow = 0; firstRow < table.rows.length; ++firstRow)
		if (table.rows [firstRow].cells [0].tagName == "TD")
			break;
	var readyRows = 0;
	var disabledRows = 0;
	for (var nrow = firstRow; nrow < table.rows.length; ++nrow)
	{
		var readyCells = 0;
		var disabled = false;
		var cells = table.rows [nrow].cells;
		for (var nkey in keyCells)
		{
			var element = cells [keyCells [nkey]].firstChild;
			if (element.disabled)
			{
				disabled = true;
				break;
			}
			if (!this.isEmpty (element))
				++readyCells;
		}
		if (disabled)
			++disabledRows;
		else if (readyCells == keyCells.length)
			++readyRows;
	}
	var emptyRows = table.rows.length - (firstRow + readyRows + disabledRows);
	if (emptyRows == 0)
		newRow ();
	else
	{
		for (var nrow = table.rows.length - 1; nrow >= firstRow && emptyRows > 1; --nrow)
		{
			if (table.rows [nrow] != row)
			{
				var emptyRow = true;
				var cells = table.rows [nrow].cells;
				for (var ncell = 0; ncell < cells.length; ++ncell)
					if (!this.isEmpty (cells [ncell].firstChild))
					{
						emptyRow = false;
						break;
					}
				if (emptyRow)
				{
					table.deleteRow (nrow);
					--emptyRows;
				}
			}
		}
	}
	return readyRows != 0;
};

mrObject.prototype.ajax = function (url, xml, callback)
{
	var xmlhttp;
	if (window.XMLHttpRequest)
		xmlhttp = new XMLHttpRequest ();
	else if (window.ActiveXObject)
		xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
	if (!xmlhttp)
	{
		badBrowser ();
		return;
	}
	xmlhttp.onreadystatechange = function ()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status != 200)
				alert ("error: " + xmlhttp.statusText);
			else if (xmlhttp.responseText.substr (0, 1) != "<")
				alert (xmlhttp.responseText);
			else
				callback (mr.xmlRoot (xmlhttp.responseText));
		}
	};
	xmlhttp.open (xml == null ? "GET" : "POST", url);
	xmlhttp.send (xml);
};

mrObject.prototype.xmlRoot = function (xmlText)
{
	if (window.ActiveXObject)
	{
		var dom = new ActiveXObject ("Microsoft.XMLDOM");
		dom.loadXML (xmlText);
		return dom.documentElement;
	}
	if (DOMParser)
	{
		var parser = new DOMParser ();
		var dom = parser.parseFromString (xmlText, "text/xml");
		return dom.documentElement;
	}
	this.badBrowser ();
};

mrObject.prototype.xmlDoc = function (rootTagName)
{
	if (document.implementation && document.implementation.createDocument)
		return document.implementation.createDocument ("", rootTagName, null);
	if (window.ActiveXObject)
	{
		var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
		xmldoc.documentElement = xmldoc.createElement (rootTagName);
		return xmldoc;
	}
	this.badBrowser ();
};

mrObject.prototype.badBrowser = function ()
{
	alert ("This browser is not supported!");
};

var mr = new mrObject;
