// ==============================================================================================================================
// Class XML1
// ==============================================================================================================================
function XML1 (sXML) {
	if ( arguments.length > 0 ) this.construct(sXML);
}

var XML_ELEMENT_NODE                   = 1;
var XML_ATTRIBUTE_NODE                 = 2;
var XML_TEXT_NODE                      = 3;
var XML_CDATA_SECTION_NODE             = 4;
var XML_ENTITY_REFERENCE_NODE          = 5;
var XML_ENTITY_NODE                    = 6;
var XML_PROCESSING_INSTRUCTION_NODE    = 7;
var XML_COMMENT_NODE                   = 8;
var XML_DOCUMENT_NODE                  = 9;
var XML_DOCUMENT_TYPE_NODE             = 10;
var XML_DOCUMENT_FRAGMENT_NODE         = 11;
var XML_NOTATION_NODE                  = 12;

//-----------------------------------------------------------------
// Method XML1.construct()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XML1.prototype.construct = function (sXML) {
	this.document = this._createObject(sXML);
	var aChildNodes = this.document.childNodes;
	this.root = false;
	for ( var i = 0; i < aChildNodes.length; i++ ) {
		if ( aChildNodes.item(i).nodeType == XML_ELEMENT_NODE ) {
			this.root = new XMLNode1(aChildNodes.item(i));
			break;
		}
	}
}

//-----------------------------------------------------------------
// Method XML1._createObject()
//-----------------------------------------------------------------
// compatibility: IE
//-----------------------------------------------------------------
XML1.prototype._createObject = function (sXML) {
	var oObj = new ActiveXObject("MSXML2.DOMDocument.3.0");
	// XML declaration gives errors...
	sXML = sXML.replace(/<\?xml.*\?>/, '');
	oObj.loadXML(sXML);
	this.parseError = oObj.parseError;
	return oObj;
}

//-----------------------------------------------------------------
// Method XML1.valid()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XML1.prototype.valid = function () {
	return ( this.root ? true : false );
}


// ==============================================================================================================================
// Class XMLNode1
// ==============================================================================================================================
function XMLNode1 (oNode) {
	if ( arguments.length > 0 ) this.construct(oNode);
}

//-----------------------------------------------------------------
// Method XMLNode1.construct()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.construct = function (oNode) {
	this.node = oNode;
}

//-----------------------------------------------------------------
// Method XMLNode1.serialize()
//-----------------------------------------------------------------
// compatibility: IE
//-----------------------------------------------------------------
XMLNode1.prototype.serialize = function () {
	// (Mozilla has XMLSerializer)
	return this.node.xml;
}

//-----------------------------------------------------------------
// Method XMLNode1.getNodeName()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.getNodeName = function () {
	return this.node.nodeName
}

//-----------------------------------------------------------------
// Method XMLNode1.getAttribute()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.getAttribute = function (sAttribute) {
	var oAttribute = this.node.attributes.getNamedItem(sAttribute);
	if ( oAttribute )
		return oAttribute.nodeValue;
	else
		return false;
}

//-----------------------------------------------------------------
// Method XMLNode1.hasAttribute()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.hasAttribute = function (sAttribute) {
	var oAttribute = this.node.attributes.getNamedItem(sAttribute);
	if ( oAttribute )
		return true;
	else
		return false;
}

//-----------------------------------------------------------------
// Method XMLNode1.getCdata()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.getCdata = function () {
	if ( this.node.childNodes.length > 0 )
		return this.node.childNodes.item(0).nodeValue;
	else
		return '';
}

//-----------------------------------------------------------------
// Method XMLNode1.childNode()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.childNode = function (iIndex) {
	return new XMLNode1(this.node.childNodes.item(iIndex));
}

//-----------------------------------------------------------------
// Method XMLNode1.namedChildNode()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.namedChildNode = function (sNodeName) {
	// returns first child node with node name sNodeName
	// if no such node exists, false is returned
	var aChildren = this.node.childNodes;
	for ( var i = 0; i < aChildren.length; i++ )
		if ( aChildren.item(i).nodeName == sNodeName )
			return new XMLNode1(aChildren.item(i));
	return false;
}

//-----------------------------------------------------------------
// Method XMLNode1.numChildNodes()
//-----------------------------------------------------------------
// compatibility: W3C
//-----------------------------------------------------------------
XMLNode1.prototype.numChildNodes = function () {
	// returns number of child nodes
	return this.node.childNodes.length;
}
