﻿
/***********************\
* ITALCOM S.p.A.        *
* Library: Firefox.js   *
* Version: 1.0          *
* Require: Core.js      *
\***********************/


//============
// DOMDocument
//============

function CreateDOMDocument()
{
	var result = null;
	try
	{
		if (Browser.ie)
		{
			result = new ActiveXObject("Microsoft.XMLDOM");
			if (result != null)
				result.async = false;
		}
		else if (document.implementation && document.implementation.createDocument)
		{
			result = document.implementation.createDocument("", "", null);
			if (result.hasDOMExtensions != true)
				CreateDOMExtensions();
		}
	}
	catch(e){ return null }
	return result;
}

function CreateDOMExtensions()
{
	XMLDocument.prototype.hasDOMExtensions = true;	
	
	HTMLElement.prototype.__defineGetter__("innerText", function() { return this.textContent; });
	HTMLElement.prototype.__defineSetter__("innerText", function(txt) { this.textContent = txt; });	
	HTMLElement.prototype.__defineSetter__("outerHTML", 
	function (str) 
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var df = r.createContextualFragment(str);
		this.parentNode.replaceChild(df, this);
		return str;
	});
	
	Element.prototype.__defineGetter__("text", function() { return this.textContent });
	
	XMLDocument.prototype.__defineGetter__("xml", function() { return (new XMLSerializer()).serializeToString(this) });
	
	Attr.prototype.__defineGetter__("text",	function() { return this.textContent });
	
	XMLDocument.prototype.loadXML
	=//========================== 
	function (xmlString)
    {
        var childNodes = this.childNodes;
        for (var i = childNodes.length - 1; i >= 0; i--)
            this.removeChild(childNodes[i]);

        var dp = new DOMParser();
        var newDOM = dp.parseFromString(xmlString, "text/xml");
        var newElt = this.importNode(newDOM.documentElement, true);
        this.appendChild(newElt);
    };
    
	XMLDocument.prototype.selectNodes
	=//============================== 
	function (cXPathString, xNode)
    {
		if (!xNode) xNode = this;
		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
			aResult[i] =  aItems.snapshotItem(i);
		return aResult;
	};
	
	Element.prototype.selectNodes 
	=//========================== 
	function (cXPathString)
	{
		if (this.ownerDocument.selectNodes)
			return this.ownerDocument.selectNodes(cXPathString, this);
        else
        	throw "For XML Elements Only";
	};

	XMLDocument.prototype.selectSingleNode
	=//=================================== 
	function (cXPathString, xNode)
	{
		if (!xNode) xNode = this;
		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
			return xItems[0];
		else
			return null;
	};
       
    Element.prototype.selectSingleNode 
    =//=============================== 
    function (cXPathString)
	{    
		if (this.ownerDocument.selectSingleNode)
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		else
			throw "For XML Elements Only";
    };
}