
///<summary>Faz a concatenação entre XML e XLS</summary>
function XSLBinder (xml, xsl) {
	this.xml = xml;
	this.xsl = xsl;
	this.xmlDoc = null;
	this.xslDoc = null;
	this.configuracao = {
		onComplete: null
	};

	this.Parametros = {
		parametros: new Array(),		
		Adicionar: function(nome, valor)
		{
			this.parametros[this.parametros.length] = {"nome": nome, "valor": valor};
		},
		RemoverTodos: function()
		{
			this.parametros = null;
			this.parametros = new Array();
		}
	};
	
	this.Init = function()
	{
		this.CarregarXML(this.xml);
		this.CarregarXSL(this.xsl);
	};
	
	this.CarregarXML = function(xml)
	{
		this.xml = xml;
		
		// code for IE
		if (navigator.appVersion.indexOf("MSIE") > -1)
		{
			this.xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
			this.xmlDoc.async = false;
			this.xmlDoc.resolveExternals = false;
			this.xmlDoc.load(this.xml);
			this.xmlDoc.setProperty("SelectionLanguage", "XPath");
		}
		// code for Chrome, Safari	
		else if(navigator.appVersion.indexOf("Chrome") > -1 || navigator.appVersion.indexOf("Safari") > -1)
		{
			var xmlhttp = new window.XMLHttpRequest();
			xmlhttp.open("GET", this.xml, false);
			xmlhttp.send(null);
			this.xmlDoc = xmlhttp.responseXML;
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{
			this.xmlDoc=document.implementation.createDocument("","",null);
			this.xmlDoc.async=false;
			this.xmlDoc.load(this.xml);	
		}
	};
	
	this.CarregarXSL = function(xsl)
	{
		this.xsl = xsl;
		this.Parametros.RemoverTodos();
		
		// code for IE
		if (navigator.appVersion.indexOf("MSIE") > -1)
		{
			this.xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
			this.xslDoc.async = false;
			this.xslDoc.resolveExternals = false;
			this.xslDoc.load(this.xsl);
		}
		// code for Chrome, Safari	
		else if(navigator.appVersion.indexOf("Chrome") > -1 || navigator.appVersion.indexOf("Safari") > -1)
		{		
			var xmlhttp2 = new window.XMLHttpRequest();
			xmlhttp2.open("GET", this.xsl, false);
			xmlhttp2.send(null);
			this.xslDoc = xmlhttp2.responseXML.documentElement;
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{	
			this.xslDoc=document.implementation.createDocument("","",null);
			this.xslDoc.async=false;
			this.xslDoc.load(this.xsl);		
		}
	};

	this.Renderizar = function(controle)
	{
		//alert("Renderizar");
		
		// code for IE
		if (navigator.appVersion.indexOf("MSIE") > -1)
		{
			var xslt = new ActiveXObject("Msxml2.XSLTemplate");
			xslt.stylesheet = this.xslDoc;
			var xslProc = xslt.createProcessor();
			xslProc.input = this.xmlDoc;
			for(var i = 0; i < this.Parametros.parametros.length; i++)
			{
				xslProc.addParameter(this.Parametros.parametros[i].nome, this.Parametros.parametros[i].valor);
			}
			xslProc.transform();
			document.getElementById(controle).innerHTML= xslProc.output;
		}
		// code for Chrome, Safari	
		else if(navigator.appVersion.indexOf("Chrome") > -1 || navigator.appVersion.indexOf("Safari") > -1)
		{
			var xsltProcessor = new XSLTProcessor();
			for(var i = 0; i < this.Parametros.parametros.length; i++)
			{
				xsltProcessor.setParameter(null, this.Parametros.parametros[i].nome, this.Parametros.parametros[i].valor);
			}
			xsltProcessor.importStylesheet(this.xslDoc);
			var resultDocument = xsltProcessor.transformToFragment(this.xmlDoc,document);
			document.getElementById(controle).innerHTML = "";
			document.getElementById(controle).appendChild(resultDocument);
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{	
			var xsltProcessor=new XSLTProcessor();
			xsltProcessor.importStylesheet(this.xslDoc);
			for(var i = 0; i < this.Parametros.parametros.length; i++)
			{
				xsltProcessor.setParameter(null, this.Parametros.parametros[i].nome, this.Parametros.parametros[i].valor);
			}
			var resultDocument = xsltProcessor.transformToFragment(this.xmlDoc,document);
			document.getElementById(controle).innerHTML = "";
//			document.getElementById(controle).appendChild(resultDocument);
			//document.getElementById(controle).innerHTML = resultDocument.textContent;

			var serializer = new XMLSerializer(); 
			var output = serializer.serializeToString(resultDocument);
			output = output.replace(/&gt;/gi, '>').replace(/&lt;/gi, '<').replace(/&quot;/gi, '"').replace(/&amp;/gi, '&');
			document.getElementById(controle).innerHTML = output;
		}	
		
		if(this.configuracao.onComplete != null)
		{
			this.configuracao.onComplete()
		}
	};
	
	this.Init();
};

function xPath(oNodes, sXPath) {
    /*
    xPath function by Charles Toepfer: toepfer_c@hotmail.com 
    use: 'resulting xml dom' = xPath('xml dom object', 'xpath string');
    */
    if(oNodes) {
		if (oNodes.documentElement) {//Somente para IE e FF
			oNodes = oNodes.documentElement;
		}

		if (navigator.appVersion.indexOf("MSIE") > -1) { //IE
			try {
				//oNodes.setProperty("SelectionLanguage", "XPath");
				//                var oSelectedNode = oNodes.documentElement.selectNodes(sXPath);
				var oSelectedNode = oNodes.selectNodes(sXPath);
				return oSelectedNode;
			} 
			catch (e) {
				alert(e.description);
			}
		} else { //Mozilla, Firefox, Opera, etc.
			try {
				var oXpe = new XPathEvaluator();
				var oNsResolver = oXpe.createNSResolver(oNodes.ownerDocument == null ? oNodes.documentElement : oNodes.ownerDocument.documentElement);
				var oResult = oXpe.evaluate(sXPath, oNodes, oNsResolver, XPathResult.ANY_TYPE, null);
				var aFound = [];
				var oRes;
				while (oRes = oResult.iterateNext()) {
					aFound.push(oRes);
				}
				return aFound;
			} 
			catch (e) {
				alert(e.description);
			}
		}
    }
}

function criarArrayItens(oNodes, oConfiguracao) {
	//retorno: item[i][0] = indice, item[i][1] = valor
	/*	
	var a = criarListaItens(oNodes,{
		indice : 'firstChild.nodeValue',
		valor : 'firstChild.nodeValue',
		primeiroIndice : '0',
		primeiroValor : 'Escolha um dos itens',
		valoresUnicos : true,
		formatarIndice : function(indice, item){tem que retornar o valor novo},
		formatarValor : function(indice, item){tem que retornar o valor novo}
	} );
	*/
	
	var aLista = new Array();
	
	//Caso seja definido o primeiro valor insere ele
	if (oConfiguracao.primeiroValor) {
		if (oConfiguracao.primeiroIndice == null)
			oConfiguracao.primeiroIndice = oConfiguracao.primeiroValor;
		aLista.push(new Array(oConfiguracao.primeiroIndice, oConfiguracao.primeiroValor));
	}
	
	//Adicionar os itens
	for (i = 0; i < oNodes.length; i++) {
		var strIndice = eval('oNodes[' + i + '].' + oConfiguracao.indice);
		var strValor = eval('oNodes[' + i + '].' + oConfiguracao.valor);
	
		if (strIndice == null)
			strIndice = strValor;

		//Caso as formatações forem especificadas, utilizá-las para formatar os dados
		if (oConfiguracao.formatarIndice) {
			strIndice = oConfiguracao.formatarIndice(i, strIndice);
		}
		if (oConfiguracao.formatarValor) {
			strValor = oConfiguracao.formatarValor(i, strValor);
		}
		
		if (oConfiguracao.valoresUnicos == true) {
			if (!existeItem(aLista, strIndice, strValor))
				aLista.push(new Array(strIndice, strValor));
		} else {
			aLista.push(new Array(strIndice, strValor));
		}
	}
	return aLista;
	
	function existeItem(aLista, strIndice, strValor) {
		var i;
		var bolExiste = false;
		for(i = 0; i < aLista.length; i++) {
			if (aLista[i][0] == strIndice && aLista[i][1] == strValor) {
				bolExiste = true;
				break;
			}
		}
		return bolExiste;
	}
}

function carregarSelectPorArray(oSelect, oDados, strValorDefault)
{
	oSelect.disabled = true;

	//Excluir quase todos os itens, somente deixa o primeiro
	while (oSelect.length > 0) {
		oSelect.remove(0);
	}

	if (oDados == null) { // quando for nulo não encontrou os dados
		//oNodes.disabled = false; deixa desabilitado
		return false;
	}
	
	//Adicionar os itens
	for (i = 0; i < oDados.length; i++)
		adicionarItem(oSelect, oDados[i][0] , oDados[i][1], strValorDefault);
	
	oSelect.disabled = false;
	return true;
	
	
	function adicionarItem(oSelect, strIndice, strValor, strValorDefault){
		try {
			var opt = new Option(strValor, strIndice);
			if ((strValorDefault != undefined) && (strValorDefault != null) && (strIndice == strValorDefault)) {
				opt.selected = true;
			}
			oSelect.add(opt); // somente IE
		} catch(ex) {
			var opt = new Option(strValor, strIndice);
			if ((strValorDefault != undefined) && (strValorDefault != null) && (strIndice == strValorDefault)) {
				opt.selected = true;
			}
			oSelect.add(opt, null); // não funciona no IE
		}
	}
	
}
