var XmlBuildertxtRedirect;

function XmlBuilder(txtRootTag) {
	this.xmlHttp = null;
	this.txtRedirect = "";
	this.StackIndex = 0;
	this.Xml = "";
	this.RootTag = txtRootTag;
}

XmlBuilder.prototype.Indent = function() {
	for (i=0; i<this.StackIndex; i++) { 
		this.Xml += "     ";
	}
}

XmlBuilder.prototype.Push = function(Elem, Attributes) {
	this.Indent();
	this.Xml += "<" + Elem;
	if (Attributes.length != 0) {
		this.Xml += " " + Attributes;
	}
	this.Xml += ">\n";
	this.StackIndex += 1;
};

XmlBuilder.prototype.AddElement = function(Elem, Content, Attributes, Break) {
	this.Indent();
	this.Xml += "<" + Elem;
	Content = "" + Content;
	if (Attributes.length != 0) {
		this.Xml += " " + Attributes;
	}
	Content = Content.replace(/\n/g, "[br]");
	this.Xml += ">" + Content.replace("&", "&amp;") + "</" + Elem + ">";
	if (Break == 1) {
		this.Xml += "\n";
	}
};

XmlBuilder.prototype.Pop = function(Elem) {
	this.StackIndex -= 1;
	this.Indent();
	this.Xml += "</" + Elem + ">\n";
};

XmlBuilder.prototype.GetXml = function() {
	return this.Xml;
};

XmlBuilder.prototype.SendXmlReq = function(txtUrl, txtRedirect) {
	XmlBuildertxtRedirect = txtRedirect;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
		return
	}
	xmlHttp.onreadystatechange=this.SendXmlRes;
	xmlHttp.open("POST", txtUrl, true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send(this.GetXml());
};

XmlBuilder.prototype.SendXmlRes = function() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		try {
			var oRootNode = GetRootNode(xmlHttp.responseText);
			blnRedirect = GetValue(oRootNode, "Redirect");
			txtResponseText = GetValue(oRootNode, "ResponseText");
			oContents = oRootNode.getElementsByTagName("Product");
			
			if (blnRedirect == "true" && XmlBuildertxtRedirect.length > 0) {
				document.getElementById("LoadingCurrentProcess").innerHTML = "";
				window.location=XmlBuildertxtRedirect;
			} else {
				txtButton = "<input type='button' value='Ok' onclick=\"HideOverlay('Loading');\" />";
				document.getElementById("LoadingCurrentProcess").innerHTML = "Submit Complete<br /><strong>Response</strong> " + txtResponseText + "<br />" + txtButton;
			}
		} catch(e) {
			txtButton = "<input type='button' value='Ok' onclick=\"HideOverlay('Loading');\" />";
			document.getElementById("LoadingCurrentProcess").innerHTML = xmlHttp.responseText + "<br />" + txtButton
		}
	}
};

XmlBuilder.prototype.AppendXml = function(Elem, Content, insertAfter) {
	txtBefore = this.Xml.substr(0, insertAfter);
	txtAfter = this.Xml.substr(insertAfter, this.Xml.length);
	this.Xml = txtBefore + "\n";
	this.AddElement(Elem, Content, "", 0)
	this.Xml += txtAfter;
};

XmlBuilder.prototype.RemoveTag = function(Elem, Attributes) {
	if (Attributes.length != 0) {
		Attributes = " " + Attributes;
	}
	intBeforePos = this.Xml.indexOf("<" + Elem + Attributes + ">");
	intAfterPos = parseInt(this.Xml.indexOf("</" + Elem + ">", intBeforePos))+3+Elem.length;
	this.Xml = this.Xml.substr(0, intBeforePos) + this.Xml.substr(intAfterPos, this.Xml.length);
}
