/**********************************************************************************
****** Funciones JavaScript utilizadas en las páginas de la sección NOTICIAS
***********************************************************************************/
function print(id)
{
	var URL = (isInternetExplorer()) ? "" : "noticias/";
	URL += "imprimirNoticia.asp?id=" + id;
	var windowName = "Imprimir";
	var windowOption = "toolbar=0,location=0,scrollbars=1,directories=0,status=0,menubar=0,resizable=1"; 
	var w = 790;
	var h = 550;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	windowOption += ",height=" + h + ",width=" + w + ",top=" + wint + ",left=" + winl + "resizable=0";
	window.open(URL,windowName,windowOption);
}
function printPage(page)
{
	var URL = (isInternetExplorer()) ? "" : "noticias/";
	URL += "imprimirPagina.asp?page=" + page;
	var windowName = "Imprimir";
	var windowOption = "toolbar=0,location=0,scrollbars=1,directories=0,status=0,menubar=0,resizable=1"; 
	var w = 790;
	var h = 550;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	windowOption += ",height=" + h + ",width=" + w + ",top=" + wint + ",left=" + winl + "resizable=0";
	window.open(URL,windowName,windowOption);
}
function adjustResolution(width)
{
	var pagination = document.getElementById("pages");
	var content    = document.getElementById("page_content");
	if (screen.width < 900 && screen.height < 700)
	{
		if (pagination != null)
			pagination.style.width = "" + width + "px";
		content.style.width = "" + width + "px";
	}
}
function clean()
{
	document.forms[0].reset();
	if (document.forms[0].elements["texto"] != null)
	{
		document.forms[0].elements["texto"].value = "";
	}
	if (document.forms[0].elements["rte"] != null)
	{
		document.forms[0].elements["rte"].value = "";
	}
	if (frames["rte"] != null)
	{
		frames["rte"].document.body.innerHTML = "";
	}
}

var divId;
var oldLayerId = null;
var rowLayer;
var cellLayer;
var newsId;
var isInProcess = false;
var objXML = false;

function getText(layerId, id)
{
	divId = layerId;
	rowLayer = "row" + layerId;
	cellLayer = "cell" + layerId;
	newsId = id;

	var param = 'id=' + newsId;
	var url = 'noticias/getNews07.asp?' + param;

//alert("Realizando petición al servidor...");
	if (!isInProcess)
	{
		try
		{
			objXML = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e2)
		{
			objXML = false;
		}

		if (window.XMLHttpRequest)
			objXML = new XMLHttpRequest();

		if (!objXML && typeof XMLHttpRequest != 'undefined')
			objXML = new XMLHttpRequest();

		if (objXML)
		{
			isInProcess = true;
			objXML.open('GET', url, true);
			objXML.onreadystatechange = handleResponse;
			objXML.send(null);
		}
	}
}

function handleResponse(xmlHttpRequest, responseHeader)
{
	isInProcess = false;
	if (objXML.readyState == 4)
	{
		var text = objXML.responseText;
//alert("Respuesta recibida del servidor\n" + text);
		text = decodeAnchor(text);
//alert("Texto descodificado");
		text = replaceAll(text, "&quot;", "\"");
		/*
		text += "&nbsp;&nbsp;<a href='#' onclick='print(" + newsId + ");return false;'>" +
		        "<img src='imagenes/print07.jpg' border='0' alt='Versión para imprimir' align='middle'></a>"
		*/

//alert("Escribiendo texto en la página");
text = text + "<div><iframe src=\"http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.escubiello.com%2Fnoticias%2FnewsFacebook.asp%3Fid%3D" + newsId + "\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; width:74px; height:25px;margin-top:10px;\"></iframe></div>";
		document.getElementById(cellLayer).innerHTML = text;
		if (text.toLowerCase().indexOf("<span") > -1)
			document.getElementById(cellLayer).className = "";
		showAndHide();
	}
}

function showAndHide()
{
	onOffDiv(rowLayer);

	if (oldLayerId != null && oldLayerId != divId)
	{
		onOffDiv("row" + oldLayerId);
	}
	oldLayerId = (oldLayerId == divId) ? null : divId;
}

/*
	Funciones que se utilizan en el formulario de alta de noticias
*/
function valida()
{
	updateRTE('rte');
	document.forms[0].elements["texto"].value = document.forms[0].elements["rte"].value;

	var titulo = document.forms[0].elements["titulo"].value;
	var texto  = document.forms[0].elements["texto"].value;

	titulo = LTrim(titulo);
	texto = LTrim(texto);

	if (document.forms[0].elements["quien"] != null)
	{
		var quien  = document.forms[0].elements["quien"].value;
		quien = LTrim(quien);

		if (quien == "")
		{
			alert("El nombre y los apellidos son obligatorios");
			document.forms[0].elements["quien"].focus();
			return false;
		}
	}
	if (titulo == "")
	{
		alert("El título es obligatorio");
		document.forms[0].elements["titulo"].focus();
		return false;
	}
	if (texto == "")
	{
		alert("El texto es obligatorio");
		//document.forms[0].elements["texto"].focus();
		return false;
	}

	return true;
}

/**  Funciones comunes Bug Internet Explorer **/
/*
	Código especial para el menú de navegación
*/
var firstLeftTD = 0;

function isInternetExplorer()
{
	var output = true;

	try
	{
		var navegador = navigator.appName;
		output = (navegador == "Microsoft Internet Explorer");
	}
	catch(err)
	{
		txt="Se ha producido un error en isInternetExplorer.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return output;
}
function getDisplayProperty()
{
	var output = "block";

	try
	{
		output = (isInternetExplorer()) ? "block" : "table-row";
	}
	catch(err)
	{
		txt="Se ha producido un error en getDisplayProperty.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return output;
}
function isHome()
{
	var output = false;

	try
	{
		output = (document.location.href.indexOf("portada07.asp") > -1);
	}
	catch(err)
	{
		txt="Se ha producido un error en isHome.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}
	
	return output;
}
/*

	Funciones para manejo de capas

*/
function onOffCursor(layer)
{
	try
	{
		var handValue = (document.all) ? "hand" : "pointer";
		if (document.getElementById(layer).style.cursor == handValue)
			document.getElementById(layer).style.cursor = "default";
		else
			document.getElementById(layer).style.cursor = handValue;
	}
	catch(err)
	{
		txt="Se ha producido un error en onOffCursor(" + layer + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}
}
function onOffDiv(layer)
{
	try
	{
		if (!isDeployed(layer))
			document.getElementById(layer).style.display = getDisplayProperty();
		else
			document.getElementById(layer).style.display = "none";

		/*
		if (layer == 'asociacion' || layer.indexOf("row") > -1)
		{
			document.getElementById('leftlayermenu').style.height = firstLeftTD + "px";
			//document.getElementById("transparentImage").style.height = firstLeftTD + "px";
			//alterMenuHeight();
		}
		*/
	}
	catch(err)
	{
		txt="Se ha producido un error en onOffDiv(" + layer + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}
}

function alterDiv(layer,title,link)
{
	try
	{
		var HTML = "<img src=\"imagenes/transparent.gif\" width=\"20\" height=\"12\" align=\"absmiddle\">";
		HTML += "<a class=\"PortadaEnlace\" href=\"" + link + "\">" + title + "</a>";

		document.getElementById(layer).innerHTML = HTML;
	}
	catch(err)
	{
		txt="Se ha producido un error en alterDiv(" + layer + "," + title + "," + link + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}
}

function isDeployed(layer)
{
	var deployed = false

	try
	{
		var displayStatus = document.getElementById(layer).style.display;
		deployed = (displayStatus != "none" && displayStatus != "");
	}
	catch(err)
	{
		txt="Se ha producido un error en isDeployed(" + layer + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return deployed;	
	
}


function alterMenuHeight()
{
	//alert("En alterMenuHeight");
	var h12 = 0;
}

function alterContentHeight()
{
	try
	{
		// TABLE 	-> full_page (todo html)
		// TD 	-> lower_frame (menu de la derecha)
		// SCROLL 	-> page_content (capa con scroll)

		var availH = 0;
		if (isInternetExplorer())
			availH = document.documentElement.clientHeight;
		else
			availH = window.innerHeight;


		var fullPageHeight   = document.getElementById('full_page').offsetHeight;
		var layerMenuHeight = document.getElementById('layermenu').offsetHeight;
		var contentHeight = document.getElementById('page_content').style.height;
		if (isNaN(contentHeight))
		{
			var position = contentHeight.lastIndexOf("px");
			contentHeight = parseInt(contentHeight.substring(0, position), 10);
		}
		var extraOffset = (layerMenuHeight - contentHeight > 130) ? layerMenuHeight - contentHeight - 130  : 0; 

		var newHeight = contentHeight + (availH - fullPageHeight) + extraOffset - 20;

//alert("contentHeight " + contentHeight + "\navailH " + availH + "\nfullPageHeight " + fullPageHeight  + "\nnewHeight " + newHeight);
		document.getElementById('page_content').style.height = newHeight + "px";

	}
	catch(err)
	{
		txt="Se ha producido un error en alterContentHeight.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}
}

function alterMenuHeightOnLoad()
{
	try
	{
		var parentTD = document.getElementById('layermenu').offsetHeight;
		var childTD  = document.getElementById('childlayermenu').offsetHeight;
		var leftTD   = document.getElementById('leftlayermenu').offsetHeight;
		var newHeight = leftTD + (parentTD - childTD);

		if (firstLeftTD == 0)
			firstLeftTD = leftTD;

		document.getElementById("bannerAdjust").height = document.getElementById("bannerAdjust").height + (parentTD - childTD);
	}
	catch(err)
	{
		txt="Se ha producido un error en alterMenuHeightOnLoad.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}
}


/*

	Funciones para manipulación de cadenas

*/
function LTrim(s)
{
	// Devuelve una cadena sin los espacios del principio
	var cadena = s;

	try
	{
		while (cadena.substring(0,1) == ' ')
		{
			cadena = cadena.substring(1,cadena.length);
		}
	}
	catch(err)
	{
		txt="Se ha producido un error en LTrim(" + s + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return cadena;
}

function ltrim(s)
{
	var cadena = s;

	try
	{
		cadena = s.replace(/^\s+/, "");
	}
	catch(err)
	{
		txt="Se ha producido un error en ltrim(" + s + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return cadena;
}

function rtrim(s)
{
	var cadena = s;

	try
	{
		cadena = s.replace(/\s+$/, "");
	}
	catch(err)
	{
		txt="Se ha producido un error en rtrim(" + s + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return cadena;
}

function trim(s)
{
	var cadena = s;

	try
	{
		cadena = rtrim(ltrim(s));
	}
	catch(err)
	{
		txt="Se ha producido un error en trim(" + s + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return cadena;
}

function repeatString(string, times)
{
	var output = "";

	try
	{
		for (var i = 0; i < times; i++)
			output += string;
	}
	catch(err)
	{
		txt="Se ha producido un error en repeatString(" + string + "," + times + ").\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return output;
}

function replaceAll(oldStr,findStr,repStr)
{
	var newStr = oldStr;

	try
	{
		var srchNdx = 0;  // srchNdx will keep track of where in the whole line
						  // of oldStr are we searching.
		var newStr = "";  // newStr will hold the altered version of oldStr.
		while (oldStr.indexOf(findStr,srchNdx) != -1)  
						// As long as there are strings to replace, this loop
						// will run. 
		{
			newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
							// Put it all the unaltered text from one findStr to
							// the next findStr into newStr.
			newStr += repStr;
							// Instead of putting the old string, put in the
							// new string instead. 
			srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
							// Now jump to the next chunk of text till the next findStr.           
		}
		newStr += oldStr.substring(srchNdx,oldStr.length);
						// Put whatever's left into newStr.             
	}
	catch(err)
	{
		//txt="Se ha producido un error en replaceAll(" + oldStr + "," + findStr + "," + repStr + ").\n\n";
		txt="Se ha producido un error en replaceAll.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return newStr;
}

function decodeAnchor(anchor)
{
	//var strHTML = anchor.toLowerCase();
	var strHTML = anchor;

	try
	{
		strHTML = replaceAll(strHTML, "href=&quot;", "href=\"");
		if (strHTML.indexOf("++target=+_blank&quot;>") == -1)
			strHTML = replaceAll(strHTML, "++target=+_blank\">", "\" target=\"_blank\">");
		else
			strHTML = replaceAll(strHTML, "++target=+_blank&quot;>", "\" target=\"_blank\">");
	}
	catch(err)
	{
		//txt="Se ha producido un error en decodeAnchor(" + anchor + ").\n\n";
		txt="Se ha producido un error en decodeAnchor.\n\n";
		txt+="Descripción: " + err.description + "\n\n";
		txt+="Nacho, toma una captura de esta pantalla.\n\n";
		alert(txt);
	}

	return strHTML;
}

