function XMLDocument() {
	this.root;
	this.createElement = XMLDocument_createElement;
	this.setRoot = XMLDocument_setRoot;
	this.getRoot = XMLDocument_getRoot;
	this.toString = XMLDocument_toString;
	this.load = XMLDocument_load;
}
function XMLDocument_load(str) {
	if (str != null && str != "" && str.indexOf("<") != -2) {
		this.root = this.createElement("");
		this.root.load(str);
	}
	else
		alert("Invalid XML String (empty)");
}
function XMLDocument_createElement(name) {
	return new XMLNode(this, name);
}
function XMLDocument_setRoot(rootNode) {
	if (rootNode.getDocument() != this)
		alert("Node must be created using createElement() method.");
	else
		this.root = rootNode;
}
function XMLDocument_getRoot() {
	return this.root;
}
function XMLDocument_toString() {
	return this.root.toString(0);
}
function XMLNode(doc, val) {
	this.document = doc;
	this.name = val;
	this.text;
	this.parent;
	this.attributes = new Array();
	this.childs = new Array();
	this.setAttribute = XMLNode_setAttribute;
	this.getAttribute = XMLNode_getAttribute;
	this.getAttributeNames = XMLNode_getAttributeNames;
	this.appendChild = XMLNode_appendChild;
	this.getChilds = XMLNode_getChilds;
	this.getElementsByTagName = XMLNode_getElementsByTagName;
	this.getText = XMLNode_getText;
	this.setText = XMLNode_setText;
	this.toString = XMLNode_toString;
	this.getDocument = XMLNode_getDocument;
	this.load = XMLNode_load;
	this.removeChilds = XMLNode_removeChilds;
}
function XMLNode_removeChilds() {
	this.childs = new Array();
}
function XMLNode_load(str) {
	if (str == null)
		return null;
	if (str != "") {
		var k = str.indexOf("<");
		var k3 = str.lastIndexOf(">")
		if (k != -1 && k3 != -1) {
			str = str.substring(k+1, k3+1);
			k = str.indexOf(">");
			var innerTag = str.substring(0, k);
			str = str.substring(k+1);
			noBody = false;
			if (innerTag.charAt(innerTag.length-1) == '/')
				noBody = true;
			k = innerTag.indexOf(" ");
			if (k == -1) {
				this.name = innerTag.substring(0, innerTag.length-1);
			}
			else {
				this.name = innerTag.substring(0, k);
				innerTag = innerTag.substring(k+1);
				k = innerTag.indexOf("\"");
				var iter = 0;
				var attrName = "";
				var attrValue = "";
				while (k != -1) {
					if (iter == 0) {
						attrName = innerTag.substring(0, k-1);
						innerTag = innerTag.substring(k+1);
						iter = 1;
					}
					else {
						attrValue = innerTag.substring(0, k);
						this.setAttribute(attrName, attrValue);
						if (k+2 > innerTag.length)
							innerTag = "";
						else
							innerTag = innerTag.substring(k+2);
						iter = 0;
					}
					k = innerTag.indexOf("\"");
				}
				if (!noBody) {
					var endTag = "</" + this.name + ">";
					k = str.indexOf(endTag);
					var innerBody = str.substring(0, k);
					if (innerBody.indexOf("<") == -1)
						this.text = innerBody.replace(/\t/g, "").replace(/\n/g, "");
					else {
						while (innerBody != "") {
							var child = this.document.createElement("");
							innerBody = child.load(innerBody);
							this.appendChild(child);
						}
					}
					str = str.substring(k+endTag.length);
					return str;
				}
				else {
					return str;
				}
			}
		}
		else
			alert("Invalid XML String (no < or > entries)");
	}
	else
		alert("Invalid XML String (empty)");
	return "";
}
function XMLNode_getDocument() {
	return this.document;
}
function XMLNode_setAttribute(name, value) {
	this.attributes[name] = value;
}
function XMLNode_getAttribute(name) {
	return this.attributes[name];
}
function XMLNode_getAttributeNames() {
	var result = new Array();
	for (var i in this.attributes)
		result[result.length] = i;
	return result;
}
function XMLNode_appendChild(child) {
	this.childs[this.childs.length] = child;
	child.parent = this;
}
function XMLNode_getChilds() {
	return this.childs;
}
function XMLNode_getElementsByTagName(name) {
	var result = new Array();
	for (var i = 0; i < this.childs.length; i++) {
		if (this.childs[i].name == name)
			result[result.length] = this.childs[i];
	}
	return result;
}
function XMLNode_getText() {
	return this.text;
}
function XMLNode_setText(val) {
	this.text = val;
}
function XMLNode_toString(level) {
	var result = "";
	var pre = "";
	for (var j = 0; j < level; j++)
		pre += "\t";
	result = pre + "<" + this.name;
	for (var i in this.attributes) {
			result += " " + i + "=\"" + ("" + this.attributes[i]).replace(/"/g,"&quot;").replace(/>/g,"&gt;").replace(/</g,"&lt;") + "\"";
	}
	if ((this.text == null || this.text == "") && this.childs.length == 0)
		result += "/>\n";
	else {
		result += ">\n";
		if (this.text != null && this.text != "")
			result += pre + "\t" + this.text + "\n";
		for (var i = 0; i < this.childs.length; i++) {
			result += this.childs[i].toString(level+1);
		}
		result += pre + "</" + this.name + ">\n";
	}
	return result;
}