if (typeof aay.ecc == "undefined") {
	aay.ecc = {};
};

if (typeof aay.ecc.xml == "undefined") {
	aay.ecc.xml = {};
};



aay.ecc.xml.XmlHttpRequestObject = function() {
	if (document.all) return new ActiveXObject("MSXML2.XMLHTTP");
	else return new XMLHttpRequest();
};


aay.ecc.xml.XmlObject = function(xml) {
	var tmp;
	if (document.all) tmp = aay.ecc.xml.IeXmlObject();
	else tmp = document.implementation.createDocument("", "xml", null);
	tmp.async = false;
	if (xml != undefined) tmp.load(xml);
	aay.ecc.xml.cleanWhitespace(tmp);
	return tmp;
};


aay.ecc.xml.XslObject = function(xsl) {
	var tmp;
	if (document.all) tmp = aay.ecc.xml.IeXmlObject();
	else tmp = document.implementation.createDocument("", "xslt", null);
	tmp.async = false;
	if (xsl != undefined) 
	try {
		tmp.load(xsl);
	} catch(ex)	{}
	return tmp;
};


aay.ecc.xml.IeXmlObject = function() {
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	for (i = 0; i < prefixes.length; i++) {
		try {
			tmpAXO = new ActiveXObject(prefixes[i] + ".DOMDocument");
			return (new ActiveXObject(prefixes[i] + ".DOMDocument"));
		} catch (ex) {
			error(0,ex);
			return;
		}
	}
};


aay.ecc.xml.transform = function(xml, xsl) {
	try {
		if (document.all) {
			return  xml.transformNode(xsl);
		} else {
			xslp = new XSLTProcessor();
			xslp.importStylesheet(xsl);
			tmpNd = document.createElement("tmp");
			tmpNd.appendChild(xslp.transformToFragment(xml, document));
			return tmpNd.innerHTML;
		}
	} catch (ex) {
		return "";
	}
};

//////////////////////////////////////////////////////////////////////
// Creates a Node
//
// xmldoc			The XML document object
// name				Name of the node
// value			The value of the node [optional]
//
aay.ecc.xml.createNode = function(xmldoc, name, value) {
	if (value == undefined) value = "";
	var node = xmldoc.createElement(name);
	var textNode = xmldoc.createTextNode(value);
	node.appendChild(textNode);
	return node;
};

//////////////////////////////////////////////////////////////////////
// Returns the value of a node
//
// node				The node
// childTagName		Name of the child node who's value you want [optional]
// childPos			Position of the childTagName if multiples [optional]
//
aay.ecc.xml.getNodeValue = function(node, childTagName, childPos) {
	var value;
	if (childPos == undefined) childPos = 0;
	try {
		value = (childTagName == undefined) ? node.childNodes[0].nodeValue : node.getElementsByTagName(childTagName)[childPos].childNodes[0].nodeValue;
	} catch(ex) {
		value = "";
	}
	if (value == null) value = "";

	value = value.replace(/\</g,"&lt;");
	value = value.replace(/\>/g,"&gt;");
	return unescape(value);
};

//////////////////////////////////////////////////////////////////////
// Sets the value of a node (the first text node it comes across)
//
// node				The node
// value			The value to set
//
aay.ecc.xml.setNodeValue = function(node, value) {
	if (node == null) {
		return;
	}
	var firstTextNode = -1;
	var children = node.childNodes;
	if (!children) return;
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeType == 3) {
			firstTextNode = i;
			i = children.length;
		}
	}
	if (firstTextNode > -1) { 
		node.childNodes[firstTextNode].nodeValue = value;
	} else {
		var tNode = node.ownerDocument.createTextNode(value);
		node.appendChild(tNode);
	}
};

//////////////////////////////////////////////////////////////////////
// Cleans any whitespace and formatting within a node
//		For use with Mozilla
//
// node				Node to clean
//
aay.ecc.xml.cleanWhitespace = function(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x];
		if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
			node.removeChild(node.childNodes[x--]);
		}
		if (childNode.nodeType == 1) cleanWhitespace(childNode);
	}
};

//////////////////////////////////////////////////////////////////////
// Creates some XML helper functions on various XML objects that
// aren't standard in Mozilla
//
if (!document.all) aay.ecc.xml.mozillaXMLaddon();
aay.ecc.xml.mozillaXMLaddon = function() {
	// to simulate IE's [XMLObject].xml 
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});

	Element.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});

	// to simulate IE's [XMLObject].loadXML
	Document.prototype.loadXML = function(strXML) {
		while (this.hasChildNodes()) this.removeChild(this.lastChild);
		var objDOMParser = new DOMParser();
		var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
		this.document = objDoc;
		for (var i = 0; i < objDoc.childNodes.length; i++) {
			var objImportedNode = this.importNode(objDoc.childNodes[i], true);
			this.appendChild(objImportedNode);
		};
	};

	// to simulate IE's .selectNodes
	Document.prototype.selectNodes = function(sExpr, contextNode) {
		var oResult = this.evaluate(sExpr, (contextNode? contextNode : this), this.createNSResolver(this.documentElement), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		var nodeList = new Array(oResult.snapshotLength);
		nodeList.expr = sExpr;
		for(i=0;i<nodeList.length;i++) nodeList[i] = oResult.snapshotItem(i);
		return nodeList;
	};

	Element.prototype.selectNodes = function(sExpr) {
		var doc = this.ownerDocument;
		return ((doc.selectNodes) ? doc.selectNodes(sExpr, this) : null);
	};

	// to simulate IE's selectSingleNode
	XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode) {
		var ctx = contextNode ? contextNode : null;
		sExpr += "[1]";
		var nodeList = this.selectNodes(sExpr, ctx);
		return ((nodeList.length > 0) ? nodeList[0] : null);
	};

	Element.prototype.selectSingleNode = function(sExpr) {
		var doc = this.ownerDocument;
		return ((doc.selectSingleNode) ? doc.selectSingleNode(sExpr, this) : null);
	};
};
