//<script language='javascript'>
// C599AE19-5521-4F0F-8B4D-C3002866C9F9
// x-platform.js  -  a cross plaform (MSIE/Firefox) implementation for DOM documents
// This file requires ele_edit.js and browser.js to be accessible

// The file comprises a number of different 'classes' to support navigating, reading
// and writing DOM documents

// -------------------------------------------------------------------------------------

// Add functionality to Firefox to mimic IE's functionality 
if (GetAgentType() == mlFIREFOX) 
{	
	// Parse error value thrown on load errors
	var mlFirefoxParseError = -1000000;

	// Function to change the ready state
	XMLDocument.prototype.changeReadyState = function(iReadyState) 
	{
    // Change the readyState
    this.sReadyState = iReadyState;
    // If there is an onreadystatechange event handler, run it
    if (this.onreadystatechange != null && typeof this.onreadystatechange == "function")
			this.onreadystatechange();
	}
	
	// Add support for the loadXML function in the DOM for firefox	
	XMLDocument.prototype.loadXML = function(strXML)
	{
		// Set the ready state
		this.changeReadyState(1);
		// Create a DOMParser
		var oDOMParser = new DOMParser();
		// Create new document from string
		var oDoc = oDOMParser.parseFromString(strXML, "text/xml");
		// Copy the document in
		this.copyDocument(oDoc);
		// Set the ready state
		handleOnLoad(this);
	}
	
	// Add functionality to clear the document
	XMLDocument.prototype.emptyDocument = function()
	{
		// Empty the document 
		while (this.hasChildNodes()) this.removeChild(this.lastChild);
	}

	// Add support to copy one document into another
	XMLDocument.prototype.copyDocument = function(oDoc)
	{
		// Empty the document first
		this.emptyDocument();
		// Now fill it with the nodes from the new document
		for (var i = 0; i < oDoc.childNodes.length; i++) 
		{
			// Import the node
			var oImportedNode = this.importNode(oDoc.childNodes[i], true);            
			// Append the child to the current document
			this.appendChild(oImportedNode);
		}
	}

	// Add support for ready state
	XMLDocument.prototype.sReadyState = "0";

	// Add load function
	XMLDocument.prototype.__load__ = XMLDocument.prototype.load;
	
	XMLDocument.prototype.load = function(sURL)
	{
		// Clear any parse errors
		this.parseError.errorCode = 0;
		this.parseError.url = "";
		this.parseError.reason = "";
    // Change the readyState
    this.changeReadyState(1);
    try
    {
			this.__load__(sURL);
		}
		catch(e)
		{
			// Set a parse error
			this.parseError.errorCode = mlFirefoxParseError;
			this.parseError.url = sURL;
			this.parseError.reason = e.text;
			// Change the ready state
			changeReadyState(this, 4);
		}
	}	

	// Event listener for load event of createDocument calls this 
	XMLDocument.prototype.handleOnLoad = function()
	{
		// Check for a parsing error
		if (!this.documentElement || this.documentElement.tagName == "parsererror")
		{
			this.parseError.errorCode = mlFirefoxParseError;
			this.parseError.url = "";
			if (this.documentElement.tagName == "parsererror") this.parseError.reason = "Error parsing file";
			else this.parseError.reason = "Document not loaded";
		}
		// Change the readyState
		this.changeReadyState(4);
	}
	
	// Add support for onreadystatechange event
	XMLDocument.prototype.onreadystatechange = null;
	
	// Add error handling
	XMLDocument.prototype.parseError = new function()
	{
		this.errorCode = 0;
		this.url = "";
		this.reason = "";
	}
	
	// Add selectNodes functionality for nodes (which includes documents)
	Node.prototype.selectNodes = function(cXPathString)
	{
		// Get the document to which the node applies
		var doc = this;
		// Make sure we've an xml document to work from
		if (doc.createNSResolver == null) doc = this.ownerDocument;
		// Evaluate the query
		return doc.evaluate(cXPathString, this, doc.createNSResolver(doc.documentElement), 
			XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	}
	
	// Add selectSingleNode functionality for nodes
	Node.prototype.selectSingleNode = function(cXPathString)
	{
		// Get the array of all nodes for the search
		var oItems = this.selectNodes(cXPathString);
		// If we've got any, return the first one
		if (oItems.snapshotLength) return oItems.snapshotItem(0);
		// Otherwise return null
		return null;
	}
	
	// Allow the length of an XPath result to be returned by a length attribute
	XPathResult.prototype.__defineGetter__("length", function() { return this.snapshotLength; });

	// Allow the indexed snapshot item to be retrieved
	XPathResult.prototype.Index = function(index)
	{
		return this.snapshotItem(index);
	}
	
	// Add support for the xml attribute
	Node.prototype.__defineGetter__("xml", function _Node_getXML() 
	{
    // Create a new XMLSerializer
    var oXMLSerializer = new XMLSerializer;
    // Get the XML string
    return oXMLSerializer.serializeToString(this);
	}	);
	
	// Add getter/setters for text over textContent
	Node.prototype.__defineGetter__("text", function() { return this.textContent; });
	Node.prototype.__defineSetter__("text", function(txt) { this.textContent = txt; } );
	
	// Allow innerText to be used
	HTMLElement.prototype.__defineGetter__("innerText", function() { return this.innerHTML; });
	HTMLElement.prototype.__defineSetter__("innerText", function(txt) {  this.innerHTML = txt; });

	// Element support
	Element.prototype.__defineGetter__("parentElement", function() { return this.parentNode; } );
	
	// Event support
	Event.prototype.__defineGetter__("srcElement", function() { return this.target; } );
	
	// Add a function to return a snapshot item
	function GetNodeItem(nodes, index)
	{
		return nodes.snapshotItem(index);
	}
}
else if (GetAgentType() == mlMSIE) 
{
	// Add a function to return a node item
	function GetNodeItem(nodes, index)
	{
		return nodes[index];
	}
}

// -------------------------------------------------------------------------------------

// Factory class to create a DOM
function createDOMDocument(sNamespaceURI, sRootTagName) 
{
	// If this is IE, determine which string to use
	if (GetAgentType() == mlMSIE)
	{
		// Different available activeX objects
		var aActiveX = ["MSXML4.DOMDocument", 
			              "MSXML3.DOMDocument",
				            "MSXML2.DOMDocument", 
					          "MSXML.DOMDocument",
						        "Microsoft.XmlDom"];
		// Name of the activeX object we are supporting
		var sActiveX = "";
		// Whether the particular activeX object we are attempting to create can be created
		var bFound = false;
		// Iterate through strings to determine which one to use
		for (var i = 0; i < aActiveX.length && !bFound; i++) 
		{
			// Catch any failure to create the activeX objectx
			try 
			{
				// Now try and create it
				var objXML = new ActiveXObject(aActiveX[i]);
									
				// We've created it successfully so we'll use this component	
				sActiveX = aActiveX[i];
				bFound = true;
			}
			catch (objException) { }	
    }
		// If we couldn't create any ActiveX document throw an error
		if (!bFound) throw "MSXML not found on your computer."
		// Create the DOM document
		var oDOM = new ActiveXObject(sActiveX);
		// If there is a root tag name
		if (sRootTagName) 
		{	
			// If we've also a namespace
			if (sNamespaceURI)
				oDOM.loadXML("<a0:" + strRootTagName + "xmlns:a0=" + strNamespaceURI + " />");
			// Just a root tag name
			else 
				oDOM.loadXML("<" + strRootTagName + "/>");
		}
		return oDOM;
	}

	// Determine if this is a standards-compliant browser like Mozilla
	if (document.implementation && document.implementation.createDocument) 
	{
		// Create the DOM Document the standards way
		var oDOM = document.implementation.createDocument(sNamespaceURI, sRootTagName, null);
    // Add the event listener for the load event
		oDOM.addEventListener("load", oDOM.handleOnLoad, false);
		// Return the document
		return oDOM;
	} 
		
	// Couldn't create one
	return null;
}

// -------------------------------------------------------------------------------------

// Function to send a document to the server; returns the response XML
function SendDocument(doc, location)
{
	// Holds our XML HTTP object
	var oXMLHTTP;
	if (GetAgentType() == mlMSIE)
	{
		// Create a new HTTP post request for the document
		oXMLHTTP = new ActiveXObject('msxml2.XMLHTTP');
		// Post the document to the server
		oXMLHTTP.open('POST', location, false);
		// Submit the document
		oXMLHTTP.send(doc);
		// Return the response object
		var tempxml = createDOMDocument();
		// Load the response
		tempxml.load(oXMLHTTP.responseXML);
	}
	else if (GetAgentType() == mlFIREFOX)
	{
		// Create a new HTTP request
		oXMLHTTP = new XMLHttpRequest();
		// Post the DOM back
		oXMLHTTP.open('POST', location, false);
		// Submit the DOM
		oXMLHTTP.send(doc);
		// Return the response object
		var tempxml = createDOMDocument();
		// Load the response
		tempxml.copyDocument(oXMLHTTP.responseXML); 
	}
	// Not IE or Firefox
	else return null;
	// return the document
	return tempxml;
}

// -------------------------------------------------------------------------------------

// Function to get a document from a server; returns the document
function GetDocument(location)
{
	// Create a new document
	var oDoc = createDOMDocument();
	// Wait for the document to load
	oDoc.async = false;
	// Go get it
	oDoc.load(location);
	// Now validate the document
	ValidateDocument(oDoc);
	// Return the document 
	return oDoc;
}

/*
function GetHTTPDocument(location)
{
	if (GetAgentType() == mlMSIE)
	{
		return GetDocument(location);
	}
	if (GetAgentType() == mlFIREFOX)
	{
		var oXMLHTTP = new XMLHttpRequest();
		oXMLHTTP.open("GET", location, false);
		oXMLHTTP.send(null);
		// *** get the XML document
		ValidateDocument(oXMLHTTP.responseXML);
		return oXMLHTTP.responseXML;
	}
	return null;
}*/

// -------------------------------------------------------------------------------------

// Parses the document for an error; throws an exception on error
function ValidateDocument(doc)
{
	// Get the parse error from the object
	var oParseError = doc.parseError;
	// If there is an error code			
	if (oParseError.errorCode != 0)
	{
		// Create the exception object to throw
		var e =	oParseError.reason;
		// If there's a URL, add that to the exception
		if (oParseError.url.length > 0)
		{
			e+= oParseError.url;
			e+= "\n";
		}
		// Throw our parser exception
		throw e;
	}
	// No problems, the DOM is loaded
}

// -------------------------------------------------------------------------------------

function CheckXMLResponseForError(doc)
{
	// Return the error number from the error node
	var node = doc.selectSingleNode("/document/error/ErrorNumber");
	if (node == null) return 0;
	return parseInt(node.text);
}

// -------------------------------------------------------------------------------------

function TransformXML(oXML, oXSL)
{
	// Holds the XML/XSL documents
	var dXML = oXML, dXSL = oXSL;
	// If necessary go and get the XML/XSL documents
	if (typeof oXML == "string") dXML = GetDocument(oXML);
	if (typeof oXSL == "string") dXSL = GetDocument(oXSL);
	// Now transform them
	if (GetAgentType() == mlFIREFOX)
	{
		var oXSLT = new XSLTProcessor();
		oXSLT.importStylesheet(dXSL);
		var docfrag = oXSLT.transformToFragment(dXML, document);
		return docfrag.xml;
	}
	if (GetAgentType() == mlMSIE)
	{
		return dXML.transformNode(dXSL);
	}
}

// -------------------------------------------------------------------------------------
//</SCRIPT>
