var txtCurrencyCode = "&pound;";
var mouseondiv = 0;
var xmlHttp;
var CurrentOpacity = 100;
var OpacityInterval = 15;
var IntervalTime = 75;
var WaitSeconds = 5000;
var direction=1;
var txtRedHot=0;
var CurrentTbl = 0;
var wait = 0;
var CurrentRow = 0;
var intProductPage = 0;
var CurRowSearch=-1;
var intTimes=0;
var blnSearchOver=false;
var intEmbroideryLine = 0;
var txtEmbroideryErrorMessage = "";

//Cart
function AddCart(intProductId) { //Add Product To Cart
	if (intProductId.length == 0) {
		return
	}
	try {
		txtSize = document.getElementById("txtSnickersSize").value;
	} catch (e) {
		txtSize = document.getElementById("os0").value;
	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
		return
	}
	try {
		intClearance = document.getElementById("intClearanceId").value;
	} catch(e) {
		intClearance = 0;
	}
	
	txtColour = document.getElementById("os1").value;
	intQuantityVal = document.getElementById("txtQuantity").value;
	
	if (!IsNumeric(intQuantityVal) && intQuantityVal != "0") {
		alert("Please Enter a valid Quantity");
		document.getElementById("txtQuantity").focus();
		return;
	}
	var url = "/includes/shop/addtocart.asp?intClearanceId=" + intClearance + "&intProductId=" + intProductId + "&txtColour=" + txtColour + "&txtSize=" + txtSize + "&intQuantity=" + intQuantityVal + "&s=" + Math.random();
	xmlHttp.onreadystatechange=UpdateCart;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function UpdateEmbroideryCartReq(intShoppingBasketId, intEmbroidery) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
		return
	}

	var url = "/includes/shop/updateembroiderycart.asp?intShoppingBasketId=" + intShoppingBasketId + "&intEmbroidery=" + intEmbroidery + "&s=" + Math.random();
	xmlHttp.onreadystatechange=function () {if (xmlHttp.readyState == 4) UpdateEmbroideryCartRes()};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function UpdateEmbroideryCartRes() {
	var oRootNode = GetRootNode(xmlHttp.responseText);
	var oResponse = oRootNode.getElementsByTagName("EmbroideryCartUpdateRes");	
	intTotalEmbroideries = oRootNode.getElementsByTagName("intTotalEmbroideries")[0].childNodes[0].nodeValue;
	txtEmbroideryPrice = oRootNode.getElementsByTagName("txtEmbroideryPrice")[0].childNodes[0].nodeValue;
	txtSubTotal = oRootNode.getElementsByTagName("txtSubTotal")[0].childNodes[0].nodeValue;
	txtShipping = oRootNode.getElementsByTagName("txtShipping")[0].childNodes[0].nodeValue;
	txtVAT = oRootNode.getElementsByTagName("txtVAT")[0].childNodes[0].nodeValue;
	txtTotal = oRootNode.getElementsByTagName("txtTotal")[0].childNodes[0].nodeValue;

	document.getElementById("spnTotalEmbroideries").innerHTML = intTotalEmbroideries;
	document.getElementById("spnEmbroideryPrice").innerHTML = txtCurrencyCode + txtEmbroideryPrice;
	document.getElementById("spnSubTotal").innerHTML = txtCurrencyCode + txtSubTotal;
	document.getElementById("spnShipping").innerHTML = txtCurrencyCode + txtShipping;
	document.getElementById("spnVAT").innerHTML = txtCurrencyCode + txtVAT;
	document.getElementById("spnTotal").innerHTML = txtCurrencyCode + txtTotal;
}


function FlashRow(txtRowId, intTimes, txtStartColour, txtFlashColour) {
	oShopRow = document.getElementById(txtRowId);
	txtBackColour = oShopRow.style.backgroundColor;
	if (intTimes < 11) {
		if (txtBackColour.indexOf("rgb") != -1) {
			var RGB = oShopRow.style.backgroundColor.substring(4,oShopRow.style.backgroundColor.length-1);
			oRGB = RGB.split(",");
			txtBackColour = "#" + RGBtoHex(oRGB[0], oRGB[1], oRGB[2]);
		}
		txtBackColour = getHexColor(txtBackColour)
		if (txtBackColour == txtFlashColour.toUpperCase()) {
			oShopRow.style.backgroundColor=txtStartColour;
		} else {
			oShopRow.style.backgroundColor=txtFlashColour;
		}
		intTimes++;
		setTimeout("FlashRow('" + txtRowId + "', '" + intTimes + "', '" + txtStartColour + "', '" + txtFlashColour + "')", 100);
	}
}


function UpdateCart() { //Refresh Shopping Cart 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		try {
			document.getElementById("emptybasket").style.display='none';
		} catch(e) {}
		try {
			var oRootNode = GetRootNode(xmlHttp.responseText);
			var oProduct = oRootNode.getElementsByTagName("Product");
			oShoppingBasket = document.getElementById("ShoppingBasket");
			var tbody = document.getElementById("ShoppingBasket").getElementsByTagName("TBODY")[0];
			if (oShoppingBasket.rows.length > 0) {
				for (intCurrentRow=0; intCurrentRow<=oShoppingBasket.rows.length;intCurrentRow++) {
					document.getElementById("ShoppingBasket").removeChild(tbody);
				}
			}
			var tbody = document.createElement("TBODY");
			document.getElementById("ShoppingBasket").appendChild(tbody);
			for (intCurrentProduct=0; intCurrentProduct<=oProduct.length-1; intCurrentProduct++) {
				intShoppingBasketId = oRootNode.getElementsByTagName("intShoppingBasketId")[intCurrentProduct].childNodes[0].nodeValue;
				txtSupplierCode = oRootNode.getElementsByTagName("txtSupplierCode")[intCurrentProduct].childNodes[0].nodeValue;
				txtProductName = oRootNode.getElementsByTagName("txtProductName")[intCurrentProduct].childNodes[0].nodeValue;
				txtProductUrl = oRootNode.getElementsByTagName("txtProductUrl")[intCurrentProduct].childNodes[0].nodeValue;
				txtColour = oRootNode.getElementsByTagName("txtColour")[intCurrentProduct].childNodes[0].nodeValue;
				txtSize = oRootNode.getElementsByTagName("txtSize")[intCurrentProduct].childNodes[0].nodeValue;
				intQuantity = oRootNode.getElementsByTagName("intQuantity")[intCurrentProduct].childNodes[0].nodeValue;
				intClearanceId = oRootNode.getElementsByTagName("intClearanceId")[intCurrentProduct].childNodes[0].nodeValue;
				intVAT = oRootNode.getElementsByTagName("intVAT")[intCurrentProduct].childNodes[0].nodeValue;
				txtRate = oRootNode.getElementsByTagName("txtRate")[intCurrentProduct].childNodes[0].nodeValue;
				txtPrice = oRootNode.getElementsByTagName("txtPrice")[intCurrentProduct].childNodes[0].nodeValue;
				intEmbroidery = oRootNode.getElementsByTagName("intEmbroidery")[intCurrentProduct].childNodes[0].nodeValue;
				var tbody = document.createElement("TBODY");
				var row = document.createElement("TR");
				var td = document.createElement("TD");
				var table = document.createElement("TABLE");
				var tbody1 = document.createElement("TBODY");
				var row1 = document.createElement("TR");
				var row2 = document.createElement("TR");
				var td1 = document.createElement("TD");
				var td2 = document.createElement("TD");
				var td3 = document.createElement("TD");
				
				td1.appendChild(document.createTextNode(intQuantity + " X  <a href='" + txtProductUrl + "'>a</a>"));
				td1.className='color18';
				td2.appendChild(document.createTextNode("&#xBB; " + txtColour + ", " + txtSize));
				td3.appendChild(document.createTextNode("£" + txtPrice));
				td2.colspan="3";
				td2.style.padding="0px";
				td2.style.paddingLeft="30px";
				td3.align="right";
				
				row1.appendChild(td1);
				row1.appendChild(td3);
				row1.style.bacgroundColor="#000000";
				row2.appendChild(td2);
				tbody1.appendChild(row1);
				tbody1.appendChild(row2);
				table.appendChild(tbody1);
				table.width="100%";
				table.style.backgroundColor="#000000";
				table.id="shop" + intShoppingBasketId;
				td.appendChild(table);
				row.appendChild(td);
				document.getElementById("ShoppingBasket").getElementsByTagName("TBODY")[0].appendChild(row);
				oTable = document.getElementById("shop" + intShoppingBasketId);
				oTable.rows[0].cells[0].innerHTML = intQuantity  + " X <a href='" + txtProductUrl + "'>" + txtProductName + "</a>";
				oTable.rows[1].cells[0].innerHTML = "&#xBB; " + txtColour + ", " + txtSize;
			}
			intProductCount = oRootNode.getElementsByTagName("intProductCount")[0].childNodes[0].nodeValue;
			intEmbroidery = oRootNode.getElementsByTagName("intEmbroidery")[0].childNodes[0].nodeValue;
			txtSubTotal = oRootNode.getElementsByTagName("txtSubTotal")[0].childNodes[0].nodeValue;
			txtPostage = oRootNode.getElementsByTagName("txtPostage")[0].childNodes[0].nodeValue;
			txtVAT = oRootNode.getElementsByTagName("txtVAT")[0].childNodes[0].nodeValue;
			txtTotal = oRootNode.getElementsByTagName("txtTotal")[0].childNodes[0].nodeValue;
			intLastProductAdded = oRootNode.getElementsByTagName("intLastProductAdded")[0].childNodes[0].nodeValue;
			document.getElementById("TotalPrice").innerHTML = "£" + txtSubTotal;
			window.scrollTo(0,0);
			FlashRow("shop" + intLastProductAdded, 1, "#000000", "#FFCC66");
		} catch(e) {
//			setTimeout(function () {alert("The Shopping Cart has encounted an error.\n\nPlease Ring 0800 376 0273");}, 1000);
			window.location.href=window.location.href;
			return
		}
	}
}

function ChangePaginationReq(intDisplayPerPage) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request");
		return
	}

	var url = "/includes/shop/changepagination.asp?intDisplayPerPage=" + intDisplayPerPage + "&s=" + Math.random();
	xmlHttp.onreadystatechange=function () {if (xmlHttp.readyState == 4) window.location.href=window.location.href;};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

// Embroidery
function UpdateEmbroideryPreview() {
	txtFontName = document.getElementById("selFont").options[document.getElementById("selFont").selectedIndex].getAttribute("FolderName");
	GetEmbroideryFontReq(Base64.encode(document.getElementById("txtLine1").value), document.getElementById("intFontId").value, "divLine1", document.getElementById("intLine1Percent").value);
	GetEmbroideryFontReq(Base64.encode(document.getElementById("txtLine2").value), document.getElementById("intFontId").value, "divLine2", document.getElementById("intLine2Percent").value);
	GetEmbroideryFontReq(Base64.encode(document.getElementById("txtLine3").value), document.getElementById("intFontId").value, "divLine3", document.getElementById("intLine3Percent").value);
}

function GetEmbroideryFontReq(txtConvertText, intFontId, txtDivName, intPercentId) {
	var oXmlDom
	oXmlDom=GetXmlHttpObject()
	if (oXmlDom == null) {
		alert ("Browser does not support HTTP Request");
		return
	}
	
	var url = "/includes/branding/generateembroidery.asp?txtConvertText=" + txtConvertText + "&intFontId=" + intFontId + "&intPercentId=" + intPercentId + "&s=" + Math.random();
	oXmlDom.onreadystatechange=function () {
		if (oXmlDom.readyState == 4) GetEmbroideryFontRes(txtDivName, oXmlDom.responseText)
	};
	oXmlDom.open("GET",url,true);
	oXmlDom.send(null);	
}

function GetEmbroideryFontRes(txtDivName, responseText) {
	if (txtDivName == "divLine1") {
		intEmbroideryLine = 0;
		txtEmbroideryErrorMessage = "";
	}
	var oRootNode = GetRootNode(responseText);
	var oLetters = oRootNode.getElementsByTagName("Letter");
	txtDivContents = "";
	//if (oLetters.length > 0) {
		for (intLoop=0; intLoop<oLetters.length;intLoop++) {
			txtDivContents = txtDivContents + "<img src='" + oLetters[intLoop].getElementsByTagName('txtSrc')[0].childNodes[0].nodeValue + "' width='" + oLetters[intLoop].getElementsByTagName('txtWidth')[0].childNodes[0].nodeValue + "' height='" + oLetters[intLoop].getElementsByTagName('txtHeight')[0].childNodes[0].nodeValue + "' />";
		}
		try {
			txtRemoved = oRootNode.getElementsByTagName("Removed")[0].childNodes[0].nodeValue;
		} catch(e) {
			txtRemoved = ""
		}
	//}
	
	intEmbroideryLine++;
	if (txtRemoved.length > 0) {
		txtEmbroideryErrorMessage = txtEmbroideryErrorMessage + "Line " + intEmbroideryLine + " was too big so I have removed the following characters {" + txtRemoved + "}\n";
	}
	document.getElementById(txtDivName).innerHTML = txtDivContents;
	if (txtDivName == "divLine3" && txtEmbroideryErrorMessage.length > 0) {
		alert("I encountered these problems whilst processing your request.\n\n" + txtEmbroideryErrorMessage);
		
	}
}

function ChangePosition(intPosition) {
	oPosition = document.getElementById("selPosition");
	for (intCurrentPosition=0; intCurrentPosition<oPosition.length; intCurrentPosition++) {
		if (oPosition.options[intCurrentPosition].value == intPosition) {
			oPosition.selectedIndex = intCurrentPosition;
			
		}
	}
	document.getElementById("intPositionId").value=intPosition;
	FlashRow("tdPositionSelectHolder", 5, "#FFFFFF", "#FFCC66");
}

//Functions
var Base64 = {
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },
	
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function getHexColor(txtColour) {
	if (txtColour.indexOf("rgb") != -1) {
		var RGB = txtColour.substring(4,txtColour.length-1);
		oRGB = RGB.split(",");
		txtColour = "#" + RGBtoHex(oRGB[0], oRGB[1], oRGB[2]);
	}
	return txtColour.toUpperCase();
}

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}

function toHex(N) {
	if (N==null) return "00";
	N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}

function GetXmlHttpObject() {
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function GetRootNode(XmlData) {
	var xmlDoc;
	try {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(XmlData);
	} catch(e) {
		try {
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(XmlData,"text/xml");
		} catch(e) {
			alert("The Shopping Cart has encounted an error.\n\nPlease Ring 0800 376 0273");
			return
		}
	}
	return xmlDoc;
}

function GetValue(oRootNode, txtElementName) {
	return oRootNode.getElementsByTagName(txtElementName)[0].childNodes[0].nodeValue;
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

//Popup
function HideOverlay(txtDivName) {
	oSelect = document.getElementsByTagName("select");
	for (i=0; i<oSelect.length; i++) {
		oSelect[i].style.display='';
	}
	document.getElementById("divOverlay").style.display='none';
	document.getElementById("div" + txtDivName).style.display='none';
}

function DisplayOverlay(txtDivName) {
	oSelect = document.getElementsByTagName("select");
	for (i=0; i<oSelect.length; i++) {
		if (oSelect[i].show!=1) {
			oSelect[i].style.display='none';
		}
	}
	updateOverlay(txtDivName);
	document.getElementById("divOverlay").style.display='';
	document.getElementById("div" + txtDivName).style.display='';
}

function updateOverlay(txtDivName) {
	if (self.pageYOffset) {
		ScrollTop = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop > -1) {
		ScrollTop = document.documentElement.scrollTop;
	} else if (document.body) {
		ScrollTop = document.body.clientHeight;
	}
	intWidth = window.document.body.clientWidth - parseInt(document.getElementById("tbl" + txtDivName).style.width);
	intHeight = parseInt(document.documentElement.clientHeight) - parseInt(document.getElementById("tbl" + txtDivName).style.height);
	document.getElementById("divOverlay").style.width = document.body.scrollWidth + "px";
	document.getElementById("divOverlay").style.height = document.body.scrollHeight + "px";
	try {
		document.getElementById("div" + txtDivName).style.width = document.body.scrollWidth + "px";
		document.getElementById("div" + txtDivName).style.height = document.body.scrollHeight + "px";
		document.getElementById("tbl" + txtDivName).style.top = parseInt(ScrollTop) + (intHeight / 2) + "px";
		document.getElementById("tbl" + txtDivName).style.left = (intWidth / 2) + "px";
	} catch (e) {}
}

function EmbroideryPreview(txtImageName) {
	document.getElementById('imgEmbroideryPreview').src = "/images/embexamples/large/" + txtImageName + ".jpg";
	DisplayOverlay('EmbroideryPreview');
}

function HideEmbroideryPreview() {
	document.getElementById('imgEmbroideryPreview').src = "/images/embexamples/large/blank.jpg";
	HideOverlay('EmbroideryPreview');
}

function HideElement(txtElementId) {
	document.getElementById(txtElementId).style.visibility='hidden';
	document.getElementById(txtElementId).style.display='none';
}

function ShowElement(txtElementId) {
	document.getElementById(txtElementId).style.visibility='visible';
	document.getElementById(txtElementId).style.display='';
}

//Search 

function CloseSearch() {
	document.getElementById("divSearchResults").style.display='none';
	document.getElementById("divOverlay").style.display='none';
}

function getSearch() {
	txtSearchTermVal = document.getElementById("txtSearchTerm").value;
	document.getElementById("iFrameSearchResults").src='/includes/search/search.asp?txtSearchTerm=' + txtSearchTermVal + "&s=" + Math.random();
	intX = findPosX(document.getElementById("txtSearchTerm"));
	intY = findPosY(document.getElementById("txtSearchTerm"));
	intHeight = document.getElementById("divSearchResults").style.height;
	intWidth = document.getElementById("divSearchResults").style.width;
	document.getElementById("divSearchResults").style.left = (intX-475) + "px";
	document.getElementById("divSearchResults").style.top = (intY+20) + "px";
	document.getElementById("divOverlay").style.width = document.body.scrollWidth + "px";
	document.getElementById("divOverlay").style.height = document.body.scrollHeight + "px";
	document.getElementById("divSearchResults").style.display='';
	document.getElementById("divOverlay").style.display='';
}

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);
}

function XmlSubmit(txtSubmitHref, txtRedirectURL) {
	DisplayOverlay('Loading');
	oLoadingCurrentProcess = document.getElementById("LoadingCurrentProcess");
	oLoadingCurrentProcess.innerHTML = "Parsing HTML for XmlTags";
	var Stack=new Array();
	var StackIndex = -1;
	var intTagOpen;
	var appendXml;
	var insertAfter = -1;
	oForms = document.getElementsByTagName("form");
	intMainForm = -1;
	if (oForms.length == 0) {
		alert("There are no forms to parse.");
		return
	}
	for(intCurrentForm=0; intCurrentForm<oForms.length; intCurrentForm++) {
		if (oForms[intCurrentForm].getAttribute("MainForm") == 1) {
			intMainForm = intCurrentForm;
			break;
		}
	}
	if (intMainForm == -1) {
		alert("I am unable to parse any forms on this page.");
		return
	}
	var oXml = new XmlBuilder();
	oXml.Push(oForms[intCurrentForm].getAttribute("XmlTag"), "");
	oSearchTags = "input,textarea".split(",");
	for (intCurrentSearchTag=0; intCurrentSearchTag<oSearchTags.length; intCurrentSearchTag++) {
		oInputTags = oForms[intMainForm].getElementsByTagName(oSearchTags[intCurrentSearchTag]);
		for (intCurrentInput=0; intCurrentInput<oInputTags.length; intCurrentInput++) {
			if (oInputTags[intCurrentInput].getAttribute("XmlTag") != null) {
				oXmlTags = (oInputTags[intCurrentInput].getAttribute("XmlTag")+"-").split("-");
				for (intCurrentTag=0; intCurrentTag<oXmlTags.length-1; intCurrentTag++) {
					if (intCurrentTag == oXmlTags.length-2) {
						if (oInputTags[intCurrentInput].type == "checkbox") {
							if (oInputTags[intCurrentInput].checked) {
								txtValue = "true";
							} else {
								txtValue = "false";
							}
						} else {
							txtValue = oInputTags[intCurrentInput].value;
						}
						
						if (insertAfter > 44) {
							oXml.AppendXml(oXmlTags[intCurrentTag], txtValue, insertAfter);
							insertAfter = 0;
						} else {
							oXml.AddElement(oXmlTags[intCurrentTag], txtValue, "", 1);
							if (intTagOpen == true) {
								oXml.Pop(Stack[StackIndex], "");
								StackIndex -= 1;
								intTagOpen = false;
							}
						}
					} else {
						txtLink = oInputTags[intCurrentInput].getAttribute("XmlLink");
						if (txtLink != "undefined") {
							Attributes = "XmlLink='" + txtLink + "'";
						} else {
							Attributes = "";
						}
						
						insertAfter = oXml.GetXml().indexOf("<" + oXmlTags[intCurrentTag] + " " + Attributes + ">") + 42;
						if (insertAfter == 41) {
							oXml.Push(oXmlTags[intCurrentTag], Attributes);
							Stack[StackIndex]=oXmlTags[intCurrentTag];
							intTagOpen = true;
						}
					}
				}
			}
		}
	}
	oXml.Xml = XmlSubmitExtra(oXml.Xml);
	oXml.Pop(oForms[intCurrentForm].getAttribute("XmlTag"));
	oLoadingCurrentProcess.innerHTML = "Sending Request";
	oXml.SendXmlReq(txtSubmitHref, txtRedirectURL);
}