function lookForAttrValue(nodeList, attrName, value) {
	var nodes = new Array(0); 
	
	for (var i = 0; i<nodeList.length; i++) {
		var v = nodeList.item(i).getAttribute(attrName); 
		
		if (v == value) {
			nodes.push(nodeList.item(i));
		}
	}
	return nodes;
}

// Looks for nodes with tagName in the set of children for root returns an array of nodes
function lookForNodes(root, tagName) {
	var cs = root.childNodes;
	var nodes = new Array(0);
	
	for (var i = 0; i<cs.length; i++) {
		if (cs.item(i).nodeName == tagName) {
			nodes.push(cs.item(i));
		}
	}
	return nodes;
}
 
// Returns the value of element tagName in the set of children of root
function getNodeValue(root, tagName) {
	var nodes = lookForNodes(root, tagName);
	
	if (nodes.length > 0) {
		var textNode = nodes[0].firstChild;
		
		if (textNode != null) {
			return textNode.nodeValue;
		} 
		else {
			return "";
		}
	} 
	else {
		return "";
	}
}

// Replace the value of the element which has the attribute attrName equal to attrValue
function replaceTagTextByAttribute(nodes, attrName, attrValue, value) {
	var node = lookForAttrValue(nodes, attrName, attrValue)[0]; 
	node.firstChild.data = value;
}

//Fills src attribute of the tag img with id imgId taking url from the xml fragment with id xmlId
function fillWithFirstImage(xmlId, imgId) {
        var xmls = document.getElementsByTagName("XML");
	var xml = lookForAttrValue(xmls, "id", xmlId)[0];
        var fragments = lookForNodes(xml,"ATTACHMENTS");
        var attachments = fragments[0]; // supposing only one attachments section exists
        var imagesTag = lookForNodes(attachments, "IMAGES")[0]; // supposing only one image group exists
        var images = lookForNodes(imagesTag, "IMAGE-DATA");

	if (images.length > 0) {
		var image = images[0];
		var imageSrc = getNodeValue(image,"URL");
		var spans = document.getElementsByTagName("IMG");
		var imageNode = lookForAttrValue(spans, "id", imgId)[0];
		imageNode.setAttribute("src",imageSrc);
	}
}

// Fill with all the images available
// function fillAllImages(xmlId, templateId, cellStyle, firstIndex) 
function fillAllImages(xmlId, templateId, firstIndex) {
	var removeSection = true;
	var xml_list = document.getElementsByTagName("XML");
	
	if ( xml_list.length > 0 ) {
		var xml_id_list = lookForAttrValue(xml_list, "id", xmlId);
		
		if (xml_id_list.length > 0 ) {
			var xml = xml_id_list[0];
			var attachments_list = lookForNodes(xml,"ATTACHMENTS");
			
			if (attachments_list.length > 0) {
				 // supposing only one attachments section exists
				var attachments = attachments_list[0];
				
				// supposing only one image group exists
				var imagesTag = lookForNodes(attachments, "IMAGES")[0]; 
				
				var images = lookForNodes(imagesTag, "IMAGE-DATA");
				
				if (images.length > 0) {
					// Look for HTML tags whose text is going to be replaced
					//var spans = document.getElementsByTagName("TD");
					var divs = document.getElementsByTagName("DIV");
					var templateNode = lookForAttrValue(divs,"id", templateId)[0];
					
					if (templateNode != null) {
						//var nextNode = templateNode.nextSibling;
						//alert("Valore 2..."+nextNode);
						//var lastNode = nextNode.nextSibling;
						//alert("Valore 3..."+lastNode);
						
						var parent = templateNode.parentNode;

						//parent.removeChild(nextNode);
						//parent.removeChild(nextNode);
				
						for (var i=firstIndex; i<images.length; i++) {
							var image = images[i];
							
							var url = getNodeValue(image,"URL");
							var orientation = getNodeValue(image,"ORIENTATION");
							
							var spanNode = document.createElement("SPAN");
							
							var aNode = document.createElement("a");

							aNode.setAttribute("href", url);
							aNode.setAttribute("target", "_blank");
							
							var imgNode = document.createElement("img");

							imgNode.setAttribute("border","0");
							imgNode.setAttribute("src", url);

							if (orientation != null && orientation == 'portrait') {
								imgNode.setAttribute("width","102");
								imgNode.setAttribute("height","124");
							} 
							else {
								imgNode.setAttribute("width","124");
								imgNode.setAttribute("height","102");
							}
							spanNode.appendChild(aNode);

							aNode.appendChild(imgNode);

							parent.insertBefore(spanNode, templateNode);
						}
						parent.removeChild(templateNode);
					}
					removeSection = false;
				}
			}
		}
	}
	if (removeSection) {
		// Remove the div section for gita
		var divs = document.getElementsByTagName("DIV");
		var div = lookForAttrValue(divs,"id", "area_foto")[0];
		
		if (div != null) {
			var parentNode = div.parentNode;
			parentNode.removeChild(div);
		}
	}
}

function fillTripData(xmlId) {
        var valutazioneItinerarioMap = ["() 0/5", "(*) 1/5", "(**) 2/5","(***) 3/5", "(****) 4/5", "(*****) 5/5"];
        var valutazioneGitaMap = ["Sconsigliata", "Consigliata"];
        var presenzaSegnaleMap = ["No", "Si"];
        var xml_list = document.getElementsByTagName("XML");
	var removeSection = true;

	if (xml_list.length>0) {
		var xml_id_list = lookForAttrValue(xml_list, "id", xmlId);
		
		if (xml_id_list.length>0) {
			var xml = xml_id_list[0];
			var attachments_list = lookForNodes(xml,"ATTACHMENTS"); // supporing only one ATTACHMENTS exists

			if (attachments_list.length>0) {
				var attachments = attachments_list[0];
				
				var gita = lookForNodes(attachments, "GITA")[0]; // supposing only one gita exists

				if (gita != null) {
					var dataEscursione = getNodeValue(gita, "DATA-ESCURSIONE");
					var oidItinerario = getNodeValue(gita, "OID-ITINERARIO");
					var neveSalita = getNodeValue(gita, "NEVE-SALITA");
					var neveDiscesa = getNodeValue(gita, "NEVE-DISCESA");
					var valutazioneItinerario = getNodeValue(gita, "VALUTAZIONE-ITINERARIO");
					var valutazioneGita = getNodeValue(gita, "VALUTAZIONE-GITA");
					var condizioniMeteo = getNodeValue(gita, "CONDIZIONI-METEO");
					var valanghe = getNodeValue(gita, "VALANGHE-OSSERVATE");
					var operatore = getNodeValue(gita, "OPERATORE");
					var segnaleBase = getNodeValue(gita, "SEGNALE-BASE");
					var segnaleVetta = getNodeValue(gita, "SEGNALE-VETTA");

					// Look for HTML tags whose text is going to be replaced
					var span = document.getElementsByTagName("SPAN");

					replaceTagTextByAttribute(span, "id", "data_escursione", dataEscursione);
					replaceTagTextByAttribute(span, "id", "oid_itinerario", oidItinerario);
					replaceTagTextByAttribute(span, "id", "neve_salita", neveSalita);
					replaceTagTextByAttribute(span, "id", "neve_discesa", neveDiscesa);
					replaceTagTextByAttribute(span, "id", "valutazione_itinerario", valutazioneItinerarioMap[valutazioneItinerario]);
					replaceTagTextByAttribute(span, "id", "valutazione_gita", valutazioneGitaMap[valutazioneGita]);
					replaceTagTextByAttribute(span, "id", "condizioni_meteo", condizioniMeteo);
					replaceTagTextByAttribute(span, "id", "valanghe", valanghe);
					replaceTagTextByAttribute(span, "id", "operatore", operatore);

					if (operatore=="-") {	
						replaceTagTextByAttribute(span, "id", "segnale_base", "-");
						replaceTagTextByAttribute(span, "id", "segnale_vetta", "-");
					} 
					else { 
						if (segnaleBase == "0" || segnaleBase == "1") {
							replaceTagTextByAttribute(span, "id", "segnale_base", presenzaSegnaleMap[segnaleBase]);
						} 
						else {
							replaceTagTextByAttribute(span, "id", "segnale_base", "-");
						}

						if (segnaleVetta == "0" || segnaleVetta == "1") {
							replaceTagTextByAttribute(span, "id", "segnale_vetta", presenzaSegnaleMap[segnaleVetta]);
						} 
						else {
							replaceTagTextByAttribute(span, "id", "segnale_vetta", "-");
						}
					}
					removeSection = false;
				} 
			}
		}
	}
	if (removeSection) {
		// Remove the div section for gita
		var divs = document.getElementsByTagName("DIV");
		var div = lookForAttrValue(divs, "id", "area_gita")[0];
		
		if (div != null) {
			var parentNode = div.parentNode;
			parentNode.removeChild(div);
		}
	}
}

function fillLink(xmlId) {
	var removeSection = true;
	var xml_list = document.getElementsByTagName("XML");
	var url = "/mski/catalog/user/detailsRouteUser.do?oid=";
	
	if (xml_list.length > 0 ) {
		var xml_id_list = lookForAttrValue(xml_list, "id", xmlId);
		
		if (xml_id_list.length > 0) {
			var xml = xml_id_list[0];
			var attachments_list = lookForNodes(xml,"ATTACHMENTS"); // supporing only one ATTACHMENTS exists

			if (attachments_list.length > 0) {
				var attachments = attachments_list[0];
				var gita = lookForNodes(attachments, "GITA")[0]; // supposing only one gita exists

				if (gita != null) {
					var oidItinerario = getNodeValue(gita, "OID-ITINERARIO");
					var titoloGita = getNodeValue(gita, "TITOLO-GITA");
					
					url = url+oidItinerario;
					
					var spans = document.getElementsByTagName("SPAN");
					var templateNode = lookForAttrValue(spans, "id", "titolo_gita")[0];
					var nextNode = templateNode.nextSibling;
					var parent = templateNode.parentNode;
				
					parent.removeChild(templateNode);
					
					var aNode = document.createElement("a");
					
					aNode.setAttribute("href", url);
					
					parent.insertBefore(aNode, nextNode);
					aNode.innerHTML = titoloGita;
					
					removeSection = false;
				} 
			}
		}
	}
	if (removeSection) {
		// Remove the div section for titolo gita
		var divs = document.getElementsByTagName("SPAN");
		var div = lookForAttrValue(divs, "id", "titolo_gita")[0];
		
		if (div != null) {
			var parentNode = div.parentNode;
			parentNode.removeChild(div);
		}
	}
}
