/******************************
con TILDES POR EL NETSCAPE/MOZILLA
*******************************/
var tlcXml = Class.create();
tlcXml.prototype = {
	xmlDoc: null,
	initialize: function(){},
	load: function(xmlFile){
		this.xmlDoc = this.loadXmlDoc(xmlFile);
	},
	loadXsl: function(xslFile){
		this.xslManager = this.loadXslDoc(xslFile);
	},
	/*Crea un objeto con un documento y un procesador para usarlo depende en que navegador*/
	loadXslDoc: function(xslFile){
		xslDoc = this.loadXmlDoc(xslFile);
		var processor;
		if (typeof XSLTProcessor != "undefined") {
			processor = new XSLTProcessor();
			processor.importStylesheet(xslDoc);
		}
		return {xslDoc:xslDoc, processor:processor, xslFile:xslFile};
	},
	loadXmlDoc: function(xmlFile){
		var xdoc;
		if( window.ActiveXObject && /Win/.test(navigator.userAgent) ){
			xdoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
		}else if( document.implementation && document.implementation.createDocument )
		{
			xdoc = document.implementation.createDocument("", "", null);
		}
		else
		{
			return false;
		}
		xdoc.async = false;
		xdoc.load(xmlFile);
		return xdoc;
	},
	getXml:function(doc){
		if(doc == null)
			doc = this.xmlDoc;
		if(window.ActiveXObject){
			return doc.xml
		}else if(document.implementation && document.implementation.createDocument){
			var s = new XMLSerializer();
			var str = s.serializeToString(doc);
			return(str);
		}
	},
	getInnerXml:function(doc){
		if(doc == null)
			doc = this.xmlDoc;
		if(window.ActiveXObject){
			return doc.text;
		}else if(document.implementation && document.implementation.createDocument){
			return doc.textContent;
		}
	},
	transformStr:function(node, xslManager){
		if(node == null)
			node = this.xmlDoc;
		if(xslManager == null)
			xslManager = this.xslManager;
		if (xslManager.processor != null) {
			var auxDoc = xslManager.processor.transformToDocument(node);
			return this.getXml(auxDoc);
		}else if ("transformNode" in node) {
			return node.transformNode(xslManager.xslDoc);
		}
	},
	/*Hace una busqueda "no modificable" de nodos*/
	selectXPath:function(expr, node) {
		if(node == null)
			node = this.xmlDoc;
		if(window.ActiveXObject){
			return {
				list: node.selectNodes(expr),
				i : 0,
				next: function() {
					if (this.i > this.list.length)
					return null;
					return this.list[this.i++];
				}
			}
		}else if(document.implementation && document.implementation.createDocument){
			return {
			list : node.evaluate(expr,node,null,XPathResult.ANY_TYPE,null),
			next : function() { 
				return this.list.iterateNext()
			}
			}
		}
	},
	/*Hace una busqueda "modificable" de nodos*/
	selectXPathST:function(expr, node) {
		if(node == null)
			node = this.xmlDoc;
		if(window.ActiveXObject){
			return {
				list: node.selectNodes(expr),
				getItem:function(i){
					return this.list[i];
				},
				length:function(){
					 return this.list.length
				}
			}
		}else if(document.implementation && document.implementation.createDocument){
			return {
				list : node.evaluate(expr,node,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null),
				getItem:function(i){
					return this.list.snapshotItem(i);
				},
				length: function(){
					return this.list.snapshotLength;
				}
			}
		}
	},
	selectSingleNode:function(expr, node){
		if(node == null)
			node = this.xmlDoc;
		return this.selectXPath(expr, node).next();
	},
	loadFromString:function(string) {
		this.actXAllowed = true;
		if(window.ActiveXObject){
			//--- Creo un nuevo objeto de la libreria Msxml2.DOMDocument.4.0 que es la que se encarga
			//--- en Internet Explorer de 'parsear' un archivo XML.
			try{
				this.xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
				//--- Defino la asincronizacion a false
				this.xmlDoc.async = false;
				//Cargo la cadena xml en el documento
				this.xmlDoc.loadXML(string);
			}catch(e){
				this.actXAllowed = false;
			}
		}else if(document.implementation && document.implementation.createDocument){
			var xmlParser, xmlDocument;
			xmlParser = new DOMParser();
			this.xmlDoc = xmlParser.parseFromString(string, 'text/xml');		
		}else{
			alert ('Script not supported, upgrade your browser');
		}
	},
	isDefaultViewActive:function(){
		vNode = this.selectSingleNode("//view[@activa=1]");
		return vNode.getAttribute("pordefecto") == "1";
	},
	isOriginal:function(){
		return (this.xmlDoc.documentElement.getAttribute("original") == "1")
	}
}


//Clase que extiende el gestor de xml con caracteristicas 
//para gestionar el envio de una accion al servidor
var tlcXmlRActionSend = Class.create();
Object.extend(Object.extend(tlcXmlRActionSend.prototype, tlcXml.prototype),{
	_parentXML : tlcXml.prototype,
	initialize: function(options){
		this._parentXML.initialize.call(this,options);
		
		//Creamos el documento y marcamos los atributos que vienen en las opciones
		this.loadFromString("<xmlAccion></xmlAccion>");
		for(j=0; j<options.attributes.length; j++){
			attr = options.attributes[j];
			this.xmlDoc.documentElement.setAttribute(attr.nombre,attr.valor);	
		}
	},
	//Agrega un nodo con la definicion de los filtros de un informe
	//El objeto que recibe como parametro es un tlcXmlReport
	agregarFiltros:function(tlcXmlR){
		//Le aņadimos el nodo de filtros al documento de comunicacion
		nfs = tlcXmlR.selectSingleNode("//filtros/defs");
		nf = nfs.cloneNode(true);
		this.xmlDoc.documentElement.appendChild(nf);
	}
});

//Clase que extiende el gestor de xml con caracteristicas 
//para gestionar la respuesta del servidor para los informes
var tlcXmlRActionResp = Class.create();
Object.extend(Object.extend(tlcXmlRActionResp.prototype, tlcXml.prototype),{
	_parentXML : tlcXml.prototype,
	initialize: function(){
		this._parentXML.initialize.call(this);
	},
	hasError: function(){
		if(this.xmlDoc.documentElement.getAttribute("error") == "1"){
			window.location.replace(wsRoot + "/index.aspx");
			return true;
		}else{
			return false;
		}
	},
	hasChange: function(){
		return this.xmlDoc.documentElement.getAttribute("cambio") == "1";
	}	
});