function isInternetExplorer()
{
	var navegador = navigator.appName;
	return (navegador == "Microsoft Internet Explorer");
}
function getDisplayProperty()
{
	return (isInternetExplorer()) ? "block" : "table-row";
}
function isHome()
{
	return (document.location.href.indexOf("portada07.asp") > -1);
}
function abreHistoria()
{
	var destination = "flash/historiaLasSalas07.asp";
	if (isInternetExplorer() && !isHome())
		destination = "../flash/historiaLasSalas07.asp";

	window.open(destination,windowName(),'toolbar=1,location=0,scrollbars=1,directories=0,status=0,menubar=0,resizable=1,left=0,top=0,width=700,height=550');    
}
function abrePPS()
{
	var destination = "../documentos/pseudo07.html";
	if (isInternetExplorer() && !isHome())
		destination = "../../documentos/pseudo07.html";

	window.open(destination,'closable','toolbar=1,location=0,scrollbars=1,directories=0,status=0,menubar=0,resizable=1,left=0,top=0,width=700,height=550');
}
function viewMap()
{
	var ancho = 1000;
	var alto = 550;
	var wleft = (screen.width - ancho) / 2;
	var wtop = (screen.height - alto) / 2;
	var destination = (isInternetExplorer()) ? "mapa.asp" : "lassalas/mapa.asp";
	var map = window.open(destination,'mapa','toolbar=0,location=0,scrollbars=0,directories=0,status=0,menubar=0,resizable=0,left=' + wleft + ',top=' + wtop + ',width=' + ancho + ',height=' + alto);
}
function abreCuentos(url)
{
	var myWidth = 700;
	var myHeight = 550;
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	var features = 'toolbar=1,location=0,scrollbars=1,directories=0,status=0,menubar=0,resizable=1,';
	features += 'left=' + myLeft + ',top=' + myTop + ',width=' + myWidth + ',height=' + myHeight
	var preffix = (isInternetExplorer()) ? "" : "lassalas/";
	window.open(preffix + url,'ventana_foto',features);    
}
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";
	}
}
/*

	Funciones para manejo de capas

*/
function onOffCursor(layer)
{
	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;
}
function onOffDiv(layer)
{
	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();
	}
	*/
}

function alterDiv(layer,title,link)
{
	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;
}

function isDeployed(layer)
{
	var displayStatus = document.getElementById(layer).style.display;
	return (displayStatus != "none" && displayStatus != "");
}

/*
	Código especial para el menú de navegación
*/
var firstLeftTD = 0;


function alterMenuHeight()
{
//alert("En alterMenuHeight");
/*
	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('leftlayermenu').style.height = newHeight + "px";
	document.getElementById("transparentImage").style.height = newHeight + "px";
*/
}

function alterContentHeight()
{
	// 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";

}

function alterCustomContentHeight(customHeight)
{
	// 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 - customHeight  : 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";
}

function alterMenuHeightOnLoad()
{
	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);
}

/*
	Funciones para validaciones comunes
*/
function ValidaFecha(dia,mes,ano)
{
	if ((dia > 31) || (dia < 1))  {return false; }    
	if ((mes > 12) || (mes < 1))  { return false; }    
	if ((ano < 1900) || (ano > 2100))  { return false; }
	
	switch(parseInt(mes,10))    
		{  case 4:  if (dia > 30) {return false;} break;    
		   case 6:  if (dia > 30) {return false;} break;    
		   case 9:  if (dia > 30) {return false;} break;    
		   case 11: if (dia > 30) {return false;} break;    
		   case 2:	if ((ano%400 == 0) || ((ano%4 == 0) && (ano%100 != 0)))    
					{  if (dia > 29) { return false; }
					}    
					else     
					{  if (dia > 28) {return false;}
					}    
					break;    				          
		}
	return true;    
}

function validatePassword()
{
	dni = document.forms[0].elements["dni"].value;
	tienePunto = (dni.indexOf(".") != -1) ? true : false ;
	tieneBlancos = (dni.indexOf(" ") != -1) ? true : false ;
	if ( (dni=="") || tienePunto || tieneBlancos || isNaN(dni) )
	{
		alert("Por favor, tu contraseña");
		document.forms[0].elements["dni"].select();
		document.forms[0].elements["dni"].focus();
		return false;
	}
	var index = document.forms[0].elements["codigo"].selectedIndex;
	if (index == 0)
	{
		alert("Por favor, elige tu nombre en la lista");
		document.forms[0].elements["codigo"].focus();
		return false;
	}
	return true;
}

function sendPassword(adminMail)
{
	var index = document.forms[0].elements["codigo"].selectedIndex;
	if (index == 0)
	{
		alert("Por favor, elige tu nombre en la lista");
		document.forms[0].elements["codigo"].focus();
	}
	else
	{
		var nombre = document.forms[0].elements["codigo"][index].text;
		var mensaje = "Se va a enviar un email a la dirección de correo de " + nombre + ".\n" +
		              "Si no es correcta o tienes algún problema, pulsa Cancelar, y manda un e-mail " + 
					  "al administrador (" + adminMail + ") ¿Estás seguro de que deseas continuar?";

		mensaje = "El Administrador va a enviarte tu contraseña a la dirección de correo que figura\n" +
		          "en tus datos personales. Comprueba antes que es correcta. Si no es correcta\n" + 
				  "o tienes algún problema, pulsa Cancelar y manda un e-mail al Administrador\n" +
				  "(" + adminMail + "). ¿Estás seguro de que deseas continuar?"

		if (confirm(mensaje))
		{
			var identificador = document.forms[0].elements["codigo"][index].value;
			var parametros = "scrollbars=no,status=no,width=300,height=240";
			var posH = (screen.width-300)/2;
			var posV = (screen.height-240-100)/2;
			if (posH < 0) posH = 0;
			if (posV <0 ) posV = 0;
			parametros += ",left=" + posH;
			parametros += ",top=" + posV;
			var ventana = window.open('envioEmail07.asp?id=' + identificador,'envioEmail',parametros);
		}
	}
}

/*

	Funciones para ventanas emergentes

*/
function lanzaVentana(documento)
{
	window.open(documento,windowName(),'toolbar=0,location=0,scrollbars=1,directories=0,status=0,menubar=0,resizable=1,left=0,top=0,width=795,height=590')
}
function windowName()
{
	var thisdate = new Date();
	return "" + thisdate.getTime();
}

/*

	Funciones para manipulación de cadenas

*/
function LTrim(s)
{
	// Devuelve una cadena sin los espacios del principio
	var cadena = new String(s);
	while (cadena.substring(0,1) == ' ')
	{
		cadena = cadena.substring(1,cadena.length);
	}
	return cadena;
}

function dateDiff(date,unit)
{
	var re = /(\d{1,2})\/(\d{1,2})\/(\d{4})\s(\d{1,2})\:(\d{1,2})/
	var m = re.exec(date);
	var date1 = new Array();
	if (m != null)
	{
		date1[0] = parseInt(m[1],10);
		date1[1] = parseInt(m[2],10) - 1;
		date1[2] = parseInt(m[3],10);
		date1[3] = parseInt(m[4],10);
		date1[4] = parseInt(m[5],10);
		date1[5] = 0;
	}
	else
	{
		re = /(\d{1,2})\/(\d{1,2})\/(\d{4})\s(\d{1,2})\:(\d{1,2}):(\d{1,2})/
		m = re.exec(date);
		if (m != null)
		{
			date1[0] = parseInt(m[1],10);
			date1[1] = parseInt(m[2],10) - 1;
			date1[2] = parseInt(m[3],10);
			date1[3] = parseInt(m[4],10);
			date1[4] = parseInt(m[5],10);
			date1[5] = parseInt(m[6],10);
		}
		else
		{
			date = date.split("/");
			date1[0] = parseInt(date1[0],10);
			date1[1] = parseInt(date1[1],10) - 1;
			date1[2] = parseInt(date1[2],10);
			date1[3] = 0;
			date1[4] = 0;
			date1[5] = 0;
		}
	}

	var myDate1 = new Date();
	myDate1.setFullYear(date1[2],date1[1],date1[0],date1[3],date1[4],date1[5]);

	var myDate2 = new Date();

	var seconds = 1000;
	var minutes = seconds * 60;
	var hours = minutes * 60;
	var days = hours * 24;
	var months = days * 30;
	var years = days * 365;

	var ratio = 1;
	switch (unit)
	{
		case 'Y':
			ratio = years;
			break;
		case 'M':
			ratio = months;
			break;
		case 'D':
			ratio = days;
			break;
		case 'H':
			ratio = hours;
			break;
		case 'm':
			ratio = minutes;
			break;
		case 'S':
			ratio = seconds;
			break;
		default:
			ratio = days;
			break;
	}

	var numberOfMiliseconds = myDate1.getTime() - myDate2.getTime();
	numberOfMiliseconds = (numberOfMiliseconds < 0) ? -numberOfMiliseconds : numberOfMiliseconds;
	return Math.round(numberOfMiliseconds / ratio);
}

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 = "";
	}
}

function ltrim(s)
{
   return s.replace(/^\s+/, "");
}

function rtrim(s)
{
   return s.replace(/\s+$/, "");
}

function trim(s)
{
   return rtrim(ltrim(s));
}

function repeatString(string, times)
{
	var output = "";
	for (var i = 0; i < times; i++)
		output += string;
	return output;
}

function replaceAll(oldStr,findStr,repStr)
{
//alert("En replaceAll");
	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.             
//alert("Output replaceAll\n" + newStr);
	return newStr;
}

function decodeAnchor(anchor)
{
	//var strHTML = anchor.toLowerCase();
//alert("En decodeAnchor");
	var strHTML = anchor;

	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\">");

	return strHTML;
}

String.prototype.capitalize = function(){ //v1.0
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};