function BwMessage (name)
{
	this.dom = null;
	
	if (!name) {
		name = "message";
	}

	this.init (name);
}

BwMessage.initFromXML =function (dom)
{
	var msg = new BwMessage ();
	msg.dom = dom;
	msg.root = dom.documentElement;
	return msg;
};

BwMessage.prototype.init = function (name)
{
	var i = document.implementation;
	var d = (i && i.createDocument) ? i.createDocument ("","",null) : new ActiveXObject ("MSXML.DOMDocument");
	
	var r = d.createElement (name);
	d.appendChild (r);
	
	this.dom = d;
	this.root = r;
};

BwMessage.prototype.getName = function ()
{
	return this.dom.documentElement.nodeName;
};

BwMessage.prototype.changeRoot = function (obj)
{
	if (typeof obj == 'string') {
		obj = this.lookup (obj);
	}
	this.root = (obj == null) ? this.dom.documentElement : obj;
};

BwMessage.prototype.put = function (path, value)
{
	var n = this.create (path);
	return BwXml.setNodeValue (n, value);
};

BwMessage.prototype.get = function (path)
{
	var n = this.lookup (path);
	if (n == null) return null;
	return BwXml.getNodeValue(n);
};

BwMessage.prototype.getCasted = function (path)
{
	var v = this.get (path);
	if (v == null) return null;
	var n = new Number (v);
	if (!isNaN (n)) {
		v = n.valueOf();
	}
	return v;
};

BwMessage.prototype.count = function (path)
{
	var n = this.lookup (path);
	if (n == null) return -1;
	return BwXml.countHomonymNodes (n);
};

BwMessage.prototype.remove = function (path)
{
	var n = this.lookup (path);
	if (n == null) return null;
	return BwXml.removeNode (n);
};

BwMessage.prototype.create = function (path)
{
	try {
		return BwXml.navigate (path, this.root, true);
	} catch (e) {
		return null;
	}
};

BwMessage.prototype.lookup = function (path)
{
	try {
		return BwXml.navigate (path, this.root, false);
	} catch (e) {
		return null;
	}
};

