/**
 * @author albert.sole
 */

function loadXMLDoc(fname)
{
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject){
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument){
	  xmlDoc=document.implementation.createDocument("","",null);
	  document.acceptCharset = "utf-8"; 
	}
	else{
	  alert('Your browser cannot handle this script');
	}
	xmlDoc.async=false;
	xmlDoc.load(fname);
	return(xmlDoc);
}

function stringToXML(string){
	var doc;
	if (window.ActiveXObject) {
	    var doc = new ActiveXObject("Microsoft.XMLDOM");
	    doc.async = "false";
	    doc.loadXML(string);
	} else {
	    var doc = new DOMParser().parseFromString(string,"text/xml");
	}	
	return doc;
}

function XMLToString(xmlObject) {
	if (Ext.isIE) {
		return xmlObject.xml;
	}else{
		return (new XMLSerializer()).serializeToString(xmlObject);
	}
}

function displayResult(xml_source, xsl_source, whereToDisplay){
	
	//var xml=loadXMLDoc(xml_source);
	var xsl=loadXMLDoc(xsl_source);
	var xml=xml_source;
	//var xsl=xsl_source;	
	
	var result = null;
	// code for IE
	if (window.ActiveXObject){
	  ex=xml.transformNode(xsl);
	  result = ex;
	  
	  var intIndexOfMatch = result.indexOf('&lt;');
	  // Loop over the string value replacing out each matching
	  // substring.
	  while (intIndexOfMatch != -1) {
	  	result = result.replace('&lt;','<');
		intIndexOfMatch = result.indexOf('&lt;');
	  }

	  var intIndexOfMatch = result.indexOf('&gt;');
	  // Loop over the string value replacing out each matching
	  // substring.
	  while (intIndexOfMatch != -1) {
	  	result = result.replace('&gt;','>');
		intIndexOfMatch = result.indexOf('&gt;');
	  }
	  	  
	  document.getElementById(whereToDisplay).innerHTML=result;
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument){
	  xsltProcessor=new XSLTProcessor();
	  xsltProcessor.importStylesheet(xsl);
	  resultDocument = xsltProcessor.transformToFragment(xml,document);
	  result = resultDocument;
	  //document.getElementById(whereToDisplay).appendChild(resultDocument);
	  var string = (new XMLSerializer()).serializeToString(resultDocument);
	  
	  var intIndexOfMatch = string.indexOf('&lt;');
	  // Loop over the string value replacing out each matching
	  // substring.
	  while (intIndexOfMatch != -1) {
	  	string = string.replace('&lt;','<');
		intIndexOfMatch = string.indexOf('&lt;');
	  }

	  var intIndexOfMatch = string.indexOf('&gt;');
	  // Loop over the string value replacing out each matching
	  // substring.
	  while (intIndexOfMatch != -1) {
	  	string = string.replace('&gt;','>');
		intIndexOfMatch = string.indexOf('&gt;');
	  }	  	  
	  
	  Ext.getDom(whereToDisplay).innerHTML = string;
	}
	
	return result;
}

/**
 * DOM HELPER FUNCTIONS
 */

/**
 * find an attributes node with name attribut name in the node element and returns its value
 * if no attribute return null.
 * @param {Object} element: element where to find the attribute
 * @param {Object} attributName: name of the attribute
 */
function findAttributValueInNode(element, attributeName){
	var attributes = element.attributes;
	var i = 0;
	for( i = 0 ; i < attributes.length; i++){
		//Only process only attributes
		var el = attributes[i];
		if (el.nodeType == 2) {
			//check if is the attribut we are looking for
			if (attributeName == el.name) {
				return el.value;
			}
		}		
	}
	return null;
}

/**
 * Returns the first son of the element
 * @param {Object} element
 */
function findFirstElementSon(element){
	if(element != null){
		var nodes = element.childNodes;
		var i = 0;
		for( i = 0 ; i < nodes.length; i++){
			//Only process only nodes
			var el = nodes[i];
			if (el.nodeType == 1) {
				//return the son
				return el;
			}		
		}
	}
	return null;
}

/**
 * Return the text value of a text node inside the element if
 * exists
 * @param {Object} element
 */
function findDirectTextNodeValue(element){
	if(element != null){
		var nodes = element.childNodes;
		var i = 0;
		for( i = 0 ; i < nodes.length; i++){
			//Only process only nodes
			var el = nodes[i];
			if (el.nodeType == 3) {
				//return the value of the node
				return el.data;
			}		
		}
	}
	
	return null;
}

/**
 * Return the text value of a text node inside the element if
 * exists
 * @param {Object} element
 */
function findFirstChildXMLDoc(element){
	if(element != null){
		var nodes = element.childNodes;
		var i = 0;
		for( i = 0 ; i < nodes.length; i++){
			//Only process only nodes
			var el = nodes[i];
			if (el.nodeType == 1) {
				//return the value of the node
				return el;
			}		
		}
	}
	return null;
}

/**
 * Returns the node text of the nodeName tag, nodeName tag is located inside
 * the element. 
 * @param {Object} element
 * @param {String} nodeName
 */
function findDirectTextNodeInsideTagValue(element, nodeName){
	if(element != null){
		var nodes = element.childNodes;
		var i = 0;
		for( i = 0 ; i < nodes.length; i++){
			//Only process only nodes
			var el = nodes[i];
			if (el.nodeType == 1 && el.tagName == nodeName) {
				//return the value of the node
				var result = el.firstChild.data;
				return result;
			}		
		}
	}
	return null;
}

/**
 * Remove the node nodeName from inside the element 
 * @param {Object} element
 * @param {String} nodeName
 */
function removeDirectNode(element,nodeName){
	if(element != null){
		var nodes = element.childNodes;
		var i = 0;
		for( i = 0 ; i < nodes.length; i++){
			//Only process only nodes
			var el = nodes[i];
			if (el.nodeType == 1 && el.tagName == nodeName) {
				//return the value of the node
				element.removeChild(el);
			}		
		}
	}
}
