// JScript source code
//alert("MozXML");
// add the browser detection script in the page.
//27-09-206 RT special properties in parser 6.0 required this settings to true

var browser;
var version;

if(navigator.userAgent.indexOf("MSIE") >= 0){ // browser is IE OR Opera !
	browser = "IE";
	// Now get the version of IE.
	version = navigator.userAgent.substr(navigator.userAgent.indexOf("MSIE") + 5, 3);
}
else if(navigator.appName == "Netscape"){ // browser is a Mozilla Browser.
	browser = "Mozilla";
	if(navigator.userAgent.indexOf("rv") >= 0){
		version = navigator.userAgent.substr(navigator.userAgent.indexOf("rv") +3, 3);
//		alert(version);
//		version = navigator.userAgent.substr(navigator.userAgent.indexOf("rv") +3, navigator.userAgent.indexOf(")", navigator.userAgent.indexOf("rv")) -navigator.userAgent.indexOf("rv") -3);
	}
}
else{
	alert("your browser is not IE nor Mozilla based, compatibility with this application can not be guaranty.");
	retun;
}


// for both IE and Mozilla, add a document.createXMLDocument() method.
//Document.prototype.createXMLDocument = function(){
function createXMLDocument(){
//alert("createXMLDocument");
	var xmldoc;
	// if browser is IE.
	if(browser == "IE"){
		// try to create an instance of the different versions of the MSXML PARSER.
		// 0309 RT 6.0 added 
		var ARR_ACTIVEX = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "Microsoft.XMLDOM"];
		// if an instance can be created, then it is returned, so that the higher version available will be used.
		for (var i=0; i < ARR_ACTIVEX.length ; i++) {
			try{
				xmldoc = new ActiveXObject(ARR_ACTIVEX[i]);
				//alert(ARR_ACTIVEX[i] + " will be used");
				xmldoc.async = false;
				//1909 RT added for parser 6.0
				xmldoc.resolveExternals = true;
    			//2709 RT special properties in parser 6.0 required this settings
				try {				    			    
				    xmldoc.setProperty("AllowXsltScript", true);
				    xmldoc.setProperty("AllowDocumentFunction", true);				    
				}catch(err){}
				return xmldoc;
			}
			catch(e){
			}
		}
		alert("your browser does not support XML,\nplease update or install MSXML PARSER.");
	}
	// if browser is Mozilla.
	else if(browser == "Mozilla"){
		// if version is at least 1.3
		if(Number(version) > 1.3){
			xmldoc = document.implementation.createDocument("", "", null);
			// set synchronous mode.
			xmldoc.async = false;
		}
		else{
			alert("we can not guaranty XML compatibility with your browser.\nPlease update it at http://mozilla.org or http://netscape.com");
			return -1;
		}
	}

	return xmldoc;
}
//alert("if browser is Mozilla, then add the document.loadXML() method.");
//if browser is Mozilla, then add the document.loadXML() method.
if(browser == "Mozilla"){
	XMLDocument.prototype.loadXML = function(strXML) {
	//	alert("loadXML function");
		//create a DOMParser
		var objDOMParser = new DOMParser();
		//create new document from string.
		var newdoc = objDOMParser.parseFromString(strXML, "text/xml");
		for (var i=0; i < newdoc.childNodes.length; i++) {
			//import and append node.
			var importedNode = this.importNode(newdoc.childNodes[i], true);
			this.appendChild(importedNode);
		}
	}
//alert("add the .xml property to Mozilla nodes.");
//add the .xml property to Mozilla nodes.
Node.prototype.__defineGetter__("xml", Node_getXML);
Node.prototype.__defineGetter__("text", Node_getText);
XMLDocument.prototype.__defineGetter__("parseError", XML_parseError);

//alert("for Mozilla return a document as the result of the transformation.");
	// for Mozilla return a document as the result of the transformation.
	Node.prototype.transformToDocument = function(objXSL){
		// if Mozilla 1.3+ .
		if(browser == "Mozilla" && version > "1.3"){
			// create a XSLTProcessor object.
			var	processor = new XSLTProcessor();
			// import the styleSheet.
			processor.importStylesheet(objXSL);
			//transfrom the XML document through the stylesheet.
			return processor.transformToDocument(this);
			
	/*		//empty destination document.
			for(var i=0; i<destDoc.childNodes.length; i++){
				destDoc.removeChild(destDoc.childNodes[i]);
			}
	*/		
		}
	}
	
//alert(" add the transformNodeToObject() support for Mozilla.");
	// add the transformNodeToObject() support for Mozilla.
	Node.prototype.transformNodeToObject = function(objXSL){
		try{
			// if Mozilla 1.3+ .
			if(browser == "Mozilla" && version > "1.3"){
				// create a XSLTProcessor object.
				var	processor = new XSLTProcessor();
				// import the styleSheet.
				processor.importStylesheet(objXSL);
				//transfrom the XML document through the stylesheet.
				return processor.transformToFragment(this);
				
		/*		//empty destination document.
				for(var i=0; i<destDoc.childNodes.length; i++){
					destDoc.removeChild(destDoc.childNodes[i]);
				}
		*/
			}	
		}catch(e){
			alert(e.message);
		}
	}
	
//	alert(" add the transformNode() support for Mozilla.");
	// add the transformNode() support for Mozilla.
	Node.prototype.transformNode = function(objXSL){
//	alert("transformNode");
		try{
			// if Mozilla 1.3+ .
			if(browser == "Mozilla" && version > "1.3"){
				// create a XSLTProcessor object.
				var	processor = new XSLTProcessor();
				// import the styleSheet.
				processor.importStylesheet(objXSL);
				//transfrom the XML document through the stylesheet.
				return processor.transformToFragment(this, document).xml;
				
		/*		//empty destination document.
				for(var i=0; i<destDoc.childNodes.length; i++){
					destDoc.removeChild(destDoc.childNodes[i]);
				}
		*/
			}
		}catch(e){
			alert(e.message);
		}
	}

// FROM SARISSA 0.94

// evaluate method :

/*
 > Is there a way to evaluate an XPath expression against an XMLDocument 
from
 > javascript?
 >
Yes, the W3C has an XPath API as a part of the W3C DOM Level 3 and 
Mozilla implements that

The main tool is a method
   evaluate
of an XML document, this takes an XPath expression, a context node, a 
namespace resolver, a result type and yields an XPath result that has 
different properties that are set depending of the result type of the 
XPath type has.

For instance with the following XML

<?xml version="1.0" encoding="UTF-8"?>
<gods>
   <god name="Kibo" />
</gods>

the expression
   //god[@name = "Kibo"]
selects an element node in the document by its attribute value, 
something you can't simply do with the DOM level 2 apis.

With Mozilla or Netscape 7 you can load the XML document and evaluate 
the XPath expression as follows:

var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", "test20030115.xml", false);
httpRequest.send(null)
var xmlDocument = httpRequest.responseXML;
var GOD = xmlDocument.evaluate('//god[@name="Kibo"]', xmlDocument, 
null, 9, null).singleNodeValue;
alert(GOD.getAttribute("name"))

Check the W3C site http://www.w3.org/ for details about the XPath 
module of DOM level 3.
*/

	XMLDocument.prototype.selectNodes = function(sExpr, contextNode)
	{
//	alert(this.xml);
	try{
		var oResult = this.evaluate(sExpr, (contextNode?contextNode:this), 
							this.createNSResolver(this.documentElement),
							XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	}
	catch(e){
		alert("error in selectNodes : " + e.message);
	}
		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;
		if(doc.selectNodes)
			return doc.selectNodes(sExpr, this);
		else
			throw "SarissaXPathOperationException: Method selectNodes is only supported by XML Nodes";
	};
	
	// Emulate IE's selectSingleNode
	XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode)
	{
		var ctx = contextNode?contextNode:null;
		sExpr += "[1]";
		var nodeList = this.selectNodes(sExpr, ctx);
		if(nodeList.length > 0)
			return nodeList[0];
		else 
			return null;
	};
	Element.prototype.selectSingleNode = function(sExpr)
	{
		var doc = this.ownerDocument;
		if(doc.selectSingleNode)
			return doc.selectSingleNode(sExpr, this);
		else
			throw "SarissaXPathOperationException: Method selectSingleNode is only supported by XML Nodes. (original exception: "+e+")";
	};

// END OF FROM SARISSA 0.94

// as selectNodes return an Array, we need to add the item() function to the arary class to be able to use it as a nodelist.
Array.prototype.item = function(index){
	return this[index];
}



} // END IF


// defines the .xml property.
function Node_getXML() {
    //create XMLSerializer.
    var objXMLSerializer = new XMLSerializer;
    //return XML string.
    return objXMLSerializer.serializeToString(this);
}

// define the .text property getter.
function Node_getText(){
//alert("Node_getText");
	var text = ""; 
	// if node is a text node.
	if(this.nodeType == 3){
		return this.nodeValue;
	}
	// if node is an element node.
	if(this.nodeType == 1 | this.nodeType == 9){
		//get text for every childNode.
		for(var i=0; i<this.childNodes.length; i++){
			text += this.childNodes[i].text;
		} 
	}
	return text;
}

function XML_parseError() {
	//' returns the parseError property for Mozilla (like IE)
	if (this.documentElement && this.documentElement.tagName=="parsererror") {
		return 1;
	} else {
		return 0;
	}
}
//set xsl param

setXslParameter = function(oXslDoc, sParamQName, sParamValue)
{
	try
	{
		var params = oXslDoc.getElementsByTagName(_SARISSA_IEPREFIX4XSLPARAM+"param");
		var iLength = params.length;
		var bFound = false;
		var param;
		
		if(sParamValue)
		{
			for(i=0; i < iLength && !bFound;i++)
			{
				// match a param name attribute with the name given as argument
				if(params[i].getAttribute("name") == sParamQName)
				{
					param = params[i];
					// clean up the parameter
					while(param.firstChild)
						param.removeChild(param.firstChild);
					if(!sParamValue || sParamValue == null)
					{
						// do nothing; we've cleaned up the parameter anyway
					}
					// if String
					else if(typeof sParamValue == "string")
					{ 
						param.setAttribute("select", sParamValue);
						bFound = true;
					}
					// if node
					else if(sParamValue.nodeName)
					{
						param.removeAttribute("select");
						param.appendChild(sParamValue.cloneNode(true));
						bFound = true;
					}
					// if NodeList
					else if (sParamValue.item(0)
						&& sParamValue.item(0).nodeType)
					{
						for(j=0;j < sParamValue.length;j++)
						if(sParamValue.item(j).nodeType) // check if this is a Node
							param.appendChild(sParamValue.item(j).cloneNode(true));
						bFound = true;
					}
					// if Array or IE's IXMLDOMNodeList
					else
						throw "SarissaTypeMissMatchException in method: Sarissa.setXslParameter. (original exception: "+e+")";
				}
			}
		}
		return bFound;
	}
	catch(e)
	{
		throw e;
		return false;
	}
}

