//_________________________________________________________ getParentByTagName
function getParentByTagName(node, tag)
{
	var T = tag.toUpperCase();
	var N = node;
	while(N)
	{
		var NT = N.tagName.toUpperCase();
		if("BODY" == NT || T == NT) break;
		N = N.parentNode;
	}
	return (N && T == N.tagName.toUpperCase()) ? N : null;
}

//_________________________________________________________ condChanged
function condChanged(node)
{
	var tr = getParentByTagName(node, "tr");

	if(tr && tr.nextSibling && tr.nextSibling.className != "final")		// there are more conditions
	{
		if(0 != node.selectedIndex) return true;
		tr.parentNode.removeChild(tr);
	}
	else
	{
		if(0 == node.selectedIndex) return true;
		var tr2 = tr.parentNode.insertBefore(tr.cloneNode(true), tr.nextSibling);

		// borrar todos los valores de la nueva fila
		var tags = ["SELECT", "A", "INPUT"];
		for(var t=0; t < tags.length; t++)
		{
			var list = tr2.getElementsByTagName(tags[t]);
			for(var i=0; i < list.length; i++)
			{
				var item = list.item(i);
				switch(item.tagName.toUpperCase())
				{
					case "SELECT" : item.selectedIndex = 0; break;
					case "INPUT" :
						var type = item.type.toUpperCase();
						switch(type)
						{
							case "TEXT" :
								item.value = '';
								if("words" == item.className) item.onchange = function() { qryChanged(item); }
								break;

							case "CHECKBOX" :
							case "RADIO" : item.checked = false; break;
						}
						break;
					case "A" : if("sepwords" == item.className) item.style.visibility = "hidden"; break;
				}
			}
		}
		tipChanged(tr2.getElementsByTagName("td")[1].firstChild);
	}
}

//_________________________________________________________ tipChanged
function tipChanged(node)
{
	var tr = getParentByTagName(node, "tr");
	var tds = tr.getElementsByTagName("td");
	var td = tds[2];

	// tipo de documento
	if(4 == node.selectedIndex && "SELECT" != td.firstChild.tagName.toUpperCase())
	{
		td.removeChild(td.firstChild);

		var N = td.appendChild(document.createElement("select"));
		N.style.width = "100%";
		N.name = "q[]";

		var sel = document.getElementById("d");
		for(var i=0; i < sel.length; i++)
		{
			var opt = document.createElement("option");
			if(i > 0) opt.appendChild(document.createTextNode(sel[i].text));
			N.appendChild(opt);
		}

		var ope = tr.getElementsByTagName("td")[1].firstChild;
		ope.selectedIndex = 3;
		ope.style.visibility = "visible";
	}
	// año de publicación
	if(10 == node.selectedIndex && "SPAN" != td.firstChild.tagName.toUpperCase())
	{
		td.removeChild(td.firstChild);

		var span = document.createElement("span");
		var t;

		span.appendChild(document.createElement("span").appendChild(document.createTextNode("entre")).parentNode);

		t = document.createElement("input");
		t.setAttribute("type", "text");
		t.style.width = "6em";
		t.name = "a[0][]";
		span.appendChild(t);

		span.appendChild(document.createElement("span").appendChild(document.createTextNode("y")).parentNode);

		t = document.createElement("input");
		t.setAttribute("type", "text");
		t.style.width = "6em";
		t.name = "a[1][]";
		span.appendChild(t);

		t = document.createElement("input");
		t.setAttribute("type", "hidden");
		t.name = "q[]";
		t.value = "--";
		span.appendChild(t);

		td.appendChild(span);

		var ope = tds[1].firstChild;
		ope.selectedIndex = 3;
		ope.style.visibility = "hidden";
	}
	// otros campos
	if(4 != node.selectedIndex && 10 != node.selectedIndex && "INPUT" != td.firstChild.tagName.toUpperCase())
	{
		td.removeChild(td.firstChild);
		var txt = document.createElement("input");
		txt.setAttribute("type", "text");
		txt.name = "q[]";
		txt.style.width = "98%";
		txt.onchange = function() { qryChanged(txt); }
		td.appendChild(txt);

		var ope = tds[1].firstChild;
		ope.selectedIndex = 0;
		ope.style.visibility = "visible";
	}

	qryChanged(tds[2].firstChild);
}

//_________________________________________________________ separateWords
function separateWords(anch)
{
	if(anch.disabled) return false;
	var tr = anch.parentNode.parentNode;
	var tds = tr.getElementsByTagName("td");

//	var words = tds[2].firstChild.value.toString().split(/\W+/);
	var words = tds[2].firstChild.value.toString().split(/[^\wà-ù]/);
	if(words.length < 2) return false;

	var trCampo = tds[0].firstChild.selectedIndex;
	var trOpe = tds[1].firstChild.selectedIndex;
	var trCond = tds[3].firstChild.selectedIndex;

	for(var i=0; i < words.length; i++, tr = tr.nextSibling)
	{
		var tdList = tr.getElementsByTagName("td");
		var tr2;

		if(i < words.length-1)
		{
			tr2 = tr.parentNode.insertBefore(tr.cloneNode(true), tr.nextSibling);
		}

		tdList[0].firstChild.selectedIndex = trCampo;
		tdList[1].firstChild.selectedIndex = trOpe;
		tdList[2].firstChild.value = words[i];
		tdList[2].firstChild.onchange = function() { qryChanged(tdList[2].firstChild); }
		tdList[3].firstChild.selectedIndex = ("final" == tr.nextSibling.className) ? 0 : (i < words.length-1 ? 1 : trCond);
		qryChanged(tdList[2].firstChild);
	}

	return false;
}

//_________________________________________________________ qryUpdate
var updateTimeout = null;
var qryUpdate = (function qryUpdate()
{
	if(updateTimeout) clearTimeout(updateTimeout);
	updateTimeout = null;

	var busq = document.getElementById("buscadorAvanzado");
	if(busq)
	{
		var inpList = busq.getElementsByTagName("input");
		for(var i=0; i < inpList.length; i++)
		{
			var inp = inpList[i];
			if(!inp.onchange) continue;
			inp.onchange();
		}
	}

	setTimeout("qryUpdate()", 333);

	return arguments.callee;
})();

//_________________________________________________________ qryChanged
function qryChanged(qry)
{
	if("INPUT" != qry.tagName.toUpperCase()) return;

//	var words = qry.value.toString().split(/\W+/);
	var words = qry.value.toString().split(/[^\wà-ù]/);
	var anchList = qry.parentNode.parentNode.getElementsByTagName("a");
	for(var i=0; i < anchList.length; i++)
	{
		if("sepwords" == anchList[i].className)
		{
			anchList[i].style.visibility = (words.length > 1) ? "visible" : "hidden";
			return;
		}
	}
}

//_________________________________________________________ toggleHelp
function toggleHelp(anch)
{
	var ayuda = document.getElementById("ayuda");
	ayuda.style.display = "block" != ayuda.style.display ? "block" : "none";
	anch.innerHTML = "none" == ayuda.style.display ? "Mostrar ayuda" : "Ocultar ayuda";
	return false;
}

//_________________________________________________________ setupMore
function setupMore()
{
	var list = document.getElementsByTagName("div");
	for(var i=0; i < list.length; i++)
	{
		if("leer_mas" != list[i].className) continue;
		var d = list[i];
		var diff = d.scrollHeight - d.offsetHeight;
		if(diff <= 5)
		{
			d.style.overflow = "visible";
			d.nextSibling.style.display = "none";
		}
		else
		{
			d.nextSibling.expanded = false;
		}
	}
}

//_________________________________________________________ toggleMore
function toggleMore(elem)
{
	elem.expanded = !elem.expanded;
	var d = elem.previousSibling;
	d.style.overflow = elem.expanded ? "visible" : "hidden";
//	d.style.height = elem.expanded ? d.scrollHeight : "7em";
	d.style.maxHeight = elem.expanded ? "32000em" : "7em";

	var what = d.getElementsByTagName("strong");
	what = (what && what.length) ? " " + what[0].innerHTML : "";
	what = what.toLowerCase().substr(0, what.length - 1);

	var htmlFrom = !elem.expanded ? "cerrar"+what : "leer más...";
	var htmlTo = !elem.expanded ? "leer más..." : "cerrar"+what;
	elem.innerHTML = elem.innerHTML.split(htmlFrom).join(htmlTo);
}

//_________________________________________________________ issuuVisualizar
function issuuVisualizar(a)
{
	var w = 1024, h = 768;
	var screenFactor = .75;
	var sw = screen && screen.width > 0 ? screen.width : 1024;
	var sh = screen && screen.height > 0 ? screen.height : 768;
	var factor = Math.min(sw / w, sh / h) * screenFactor;
	var ww = Math.min(w, Math.round(w * factor));
	var hh = Math.min(h, Math.round(h * factor));
	var margin = 0;

	var S = "";
	S += "<html>";
	S += "<head><title>"+a.innerHTML+"</title></head>";
	S += "<body style='margin:0; padding:0'>";
	S += "<iframe src='"+a.getAttribute('href')+"?mode=embed&documentId="+a.getAttribute('rel')+"&layout=grey' style='width:100%; height:100%; border:0; padding:0; margin:0;'></iframe>";
	S += "</body></html>";

	var win = window.open("about:blank", "_blank", [
		"width="+(ww + margin*2),
		"height="+(hh + margin*2),
		"top="+(.75 * (sh - (hh+margin*2)) / 2),
		"left="+((sw - (ww+margin*2)) / 2),
		"resizable=yes",
		"directories=no",
		"location=no",
		"menubar=no",
		"status=no",
		"titlebar=yes",
		"toolbar=no"
	].join(","));
	win.document.open("text/html");
	win.document.write(S);
	win.document.close();
	return false;
}

