
// Popup
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function ch_newsletter (el)
{
	if (el.value == '')
	{
		el.className = 'texto_suave';
		el.value = 'Introduce tu email';
	}
}

function fo_newsletter (el)
{
	if (el.value == 'Introduce tu email')
	{
		el.value = '';
		el.className = '';
	}
	return;
}

// EmailValido
// Verifica si el valor es un email
function email_valido (txt)
{	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(txt)) return true;
	else return false;
}


function send_newsletter (formu)
{
	var el = document.getElementById ('SubscriptionEmail').value;
	var email = email_valido (el);
	if (!email)
	{
		alert ('El email que has introducido no es válido. Por favor, introduce un email válido.');
		return false;
	}
	formu.submit ();
}



// ----------------------------------------------------------------
// clase Pestanas
// __________________________
// argumentos:		ids  -  array asociativo multiple con el id de la pestana y el id del div a ocultar o mostrar
// 							class_active  -  nombre de la clase de la pestana cuando esta activa
//							class_inactive  -  nombre de la clase de la pestana cuando esta inactiva
//							nombre_objeto  -   nombre de la variable que contiene el objeto que se va a crear
// ----------------------------------------------------------------

var Pestanas = Class.create ();

Pestanas.prototype = {
	
	initialize:	function (ids, class_active, class_inactive, nombre_objeto) {
						this.ids = ids;
						this.class_active = class_active;
						this.class_inactive = class_inactive;
						for (a = 0 ; a < ids.length ; a++)
						{
							eval ('this.' + ids [a][0] + '="' + ids[a][1] + '"'); // cada id del menu tienen una propiedad que es el id del contenido.
							$(ids[a][0]).objeto = nombre_objeto;
						}
						this.eventos();
					},
					
	eventos:	function () {
						for (var a = 0 ; a < this.ids.length ; a++)
						{
							Event.observe (this.ids [a][0], 'click', this.actua, true);
						}
					},
					
	actua:		function (e) {
						// cambiamos las pestanas
						var el = Event.element (e);
						var ids = eval (el.objeto + '.ids'); // obtenemos el valor de los ids del objeto al que corresponde 'el'
						var id_div = eval (el.objeto +'.' + el.id); // obtenemos el id del div que le corresponde
						el.className = eval (el.objeto + '.class_active'); // cambiamos la clase
						Element.show (id_div); // mostramos el elemento;
						
						// ponemos todas las pestanas en off
						// y escondemos todos los divs
						for (var a = 0 ; a < ids.length ; a++)
						{
							if (ids[a][0] != el.id)
								$(ids[a][0]).className =  eval (el.objeto + '.class_inactive');
							
							if (ids[a][1] != id_div)
								Element.hide (ids[a][1]);
						}
					}
};




// ----------------------------------------------------------------
// clase ToggleImg
// __________________________
// argumentos:		ids  -  array asociativo con el id de la imagen que actua como boton y el id del div a ocultar o mostrar
// 							img_on  -  direccion de la imagen que se muestra cuando esta el div abierto
//							img_off  -  direccion de la imagen que se muestra cuando esta el div cerrado
// ----------------------------------------------------------------


var ToggleImg = Class.create();

ToggleImg.prototype = {
	
	initialize:		function (ids, img_on, img_off) {
							this.ids = ids;
							var el_img = $(ids[0]);
							var el_div = $(ids[1]);
							el_img.div = ids[1];
							el_img.img_on = img_on;
							el_img.img_off = img_off;
							Event.observe (el_img, 'click', this.actua, true);
						},
						
	
	actua:			function (e) {
							var el_img = Event.element (e);
							var el_div = $(el_img.div);
							el_div.style.display = el_div.style.display == 'none' ? 'block' : 'none';
						
							el_img.src = el_div.style.display == 'block' ? el_img.img_on : el_img.img_off;
						}
};



// ----------------------------------------------------------------
// clase Borrar
// __________________________
// argumentos:		ids  -  array asociativo con el id de la etiqueta o div que actua como boton y el id del div a ocultar o mostrar
// 							id_marcador  -  id del elemento input oculto donde se anotan los elementos que estan siendo borrados
//							valor  -  valor que se anota en el input oculto
// ----------------------------------------------------------------


var Borrar = Class.create();

Borrar.prototype = {
	
		initialize:		function (ids, id_marcador, valor) {
								var el_a = $(ids[0]);
								var el_div = $(ids[1]);
								el_a.div = ids[1];
								el_a.marcador = id_marcador;
								el_a.valor = valor;
								Event.observe (el_a, 'click', this.actua, true);
							},
							
		actua:			function (e) {
								var a = confirm ('Do you want to delete this block?');
								var el_a = Event.element (e);
								if (a) 
								{
									if (el_a.marcador != false)
										$(el_a.marcador).value += el_a.valor + ',';	
									Element.remove (el_a.div);
								}
							}
};


// ----------------------------------------------------------------
// clase EditaGuarda
// __________________________
// argumentos:		click_edita  -  el id del boton que edita el contenido
// 							click_guarda  -  el id del boton que guarda los cambios
//							contenedor  -  el editor que se encuentra oculto
//							ids_comunicados  -  array asociativo multiple con el siguiente par (id_campo_input, id_campo_div), donde div es el campo donde se guardan los cambios
// ----------------------------------------------------------------

var EditaGuarda = Class.create();

EditaGuarda.prototype = {
	
	initialize:		function (click_edita, click_guarda, contenedor, ids_comunicados) {
							var el_click_edita = $(click_edita);
							var el_click_guarda = $(click_guarda);
							
							el_click_edita.contenedor = contenedor;
							el_click_guarda.click_edita = click_edita;
							el_click_guarda.contenedor = contenedor;
							el_click_guarda.ids_comunicados = ids_comunicados;
							
							
							Event.observe (el_click_edita, 'click', this.abre, true);
							Event.observe (el_click_guarda, 'click', this.guarda, true);
								
							for (var a = 0 ; a < ids_comunicados.length ; a++)
							{
								var el_input = $(ids_comunicados[a][0]);
								el_input.click_edita = click_edita;
								el_input.contenedor = contenedor;
								el_input.ids_comunicados = ids_comunicados;
								Event.observe (el_input, 'keydown', this.guarda, true);
							}
								 
						},
	
	abre:				function (e) {
							var el_click_edita = Event.element (e);
							var el  = $(el_click_edita.contenedor);
							Element.show (el_click_edita.contenedor);
							Element.hide (el_click_edita);
						},
						

	guarda:			function (e) {
							if (e.type == 'keydown' && e.keyCode != 13) return;
							var el_click_guarda = Event.element (e);
							for (var a = 0 ; a < el_click_guarda.ids_comunicados.length ; a++)
								if (el_click_guarda.ids_comunicados[1] != false)
									$(el_click_guarda.ids_comunicados[a][1]).innerHTML = $F(el_click_guarda.ids_comunicados[a][0]);
							
								
							Element.hide (el_click_guarda.contenedor);
							Element.show (el_click_guarda.click_edita);
						}
};

// ----------------------------------------------------------------
// clase ToggleName
// __________________________
// argumentos:		click_edita  -  el id del boton que edita el contenido
// 							click_guarda  -  el id del boton que guarda los cambios
//							contenedor  -  el editor que se encuentra oculto
// ----------------------------------------------------------------

var ToggleName = Class.create();

ToggleName.prototype = {
	
	initialize:		function (click_edita, click_guarda, contenedor) {
							var el_click_edita = $(click_edita);
							var el_click_guarda = $(click_guarda);
							
							el_click_edita.contenedor = contenedor;
							el_click_guarda.click_edita = click_edita;
							el_click_guarda.contenedor = contenedor;
							el_click_edita.click_guarda = click_guarda;
							
							Event.observe (el_click_edita, 'click', this.abre, true);
							Event.observe (el_click_guarda, 'click', this.guarda, true);
						},
	
	abre:				function (e) {
							var el_click_edita = Event.element (e);
							var el  = $(el_click_edita.contenedor);
							Element.show (el_click_edita.contenedor);
							Element.show (el_click_edita.click_guarda);
							Element.hide (el_click_edita);
						},
						

	guarda:			function (e) {
							if (e.type == 'keydown' && e.keyCode != 13) return;
							var el_click_guarda = Event.element (e);
							Element.hide (el_click_guarda.contenedor);
							Element.hide (el_click_guarda);
							Element.show (el_click_guarda.click_edita);
						}
};


// ----------------------------------------------------------------
// clase ToggleRadio
// __________________________
// argumentos:		ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var ToggleRadio = Class.create();

ToggleRadio.prototype = {
	
	initialize:		function (ids) {
							for (var a = 0 ; a < ids.length ; a++)
							{
								var el_input = $(ids[a][0]);
								this.ids = ids;
								el_input.div = ids[a][1];
								el_input.ids = ids;
								if (ids[a][1] != null)
									$(ids[a][1]).style.display = $(ids[a][0]).checked == true ? 'block' : 'none';
								Event.observe (el_input, 'change', this.actua.bindAsEventListener (this), true);
								Event.observe (el_input, 'click', this.actua.bindAsEventListener (this), true);
							}
						},
						
	actua:			function (e) {
							var el_input = Event.element (e);
							
							var el_div = el_input.div != null ? $(el_input.div) : null;

							for (var a = 0 ; a < this.ids.length ; a++)
							{
								if (this.ids[a][1] != null)
								$(this.ids[a][1]).style.display = 'none';
							}
							if (el_div != null)
								el_div.style.display = 'block';
						}
};	


// ----------------------------------------------------------------
// clase ToggleCheckBox
//			en un grupo de botones radio se adjudica un div para cada boton.
//			si esta activado se muestra el div, sino pues no.
// __________________________
// argumentos:		ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var ToggleCheckBox = Class.create();

ToggleCheckBox.prototype = {
	
	initialize:		function (ids) {
							var el_input = $(ids[0]);
							el_input.div = ids[1];
							Event.observe (el_input, 'click', this.actua, true);
						},
						
	actua:			function (e) {
							var el_input = Event.element (e);
							var el_div = $(el_input.div);
							if (el_input.checked == true)
								el_div.style.display = 'block';
							else
								el_div.style.display = 'none';
						}
};	


// ----------------------------------------------------------------
// clase DirectoDiv
// __________________________
// argumentos:		ids  -  array asociativo multiple con el siguiente par (id_input, id_div)
// ----------------------------------------------------------------

var DirectoDiv = Class.create();

DirectoDiv.prototype = {
	
	initialize:		function (input, div) {
							$(input).div = div;
							Event.observe ($(input), 'keyup', this.actua, true);
						},
						
	actua:			function (e) {
							var input = Event.element (e);
							var div = $(input.div);
							div.innerHTML = input.value;
						}
};	



// ----------------------------------------------------------------
// clase ToggleSelect
// __________________________
// argumentos:		id_select  -  id del elemento select
//							opciones  -  array asociativo con el siguiente par [varlor_option, id_div]
// ----------------------------------------------------------------

var ToggleSelect = Class.create();

ToggleSelect.prototype = {
	
	initialize:		function (id_select, opciones) {
							$(id_select).opciones = opciones;
							Event.observe ($(id_select), 'change', this.actua, true);
						},
						
	actua:			function (e) {
							var el_select = Event.element (e);
							var opciones = el_select.opciones;
							var seleccionada = $F(el_select);
							for (var a = 0 ; a < opciones.length ; a++)
							{
								if ($(opciones[a][1]) != false)
								{
									if (opciones[a][0] == seleccionada)
										$(opciones[a][1]).style.display = 'block';
									else $(opciones[a][1]).style.display = 'none';
								}
							}
						}
};	



// ----------------------------------------------------------------
// clase EditarDiv
// __________________________
// argumentos:		id_select  -  id del elemento select
//							opciones  -  array asociativo con el siguiente par [id_option, id_div]
// ----------------------------------------------------------------


var EditarDiv = Class.create();

EditarDiv.prototype = {
	
		initialize:		function (id_div, id_input, clase_over, texto_defecto) {
								var div = $(id_div);
								var input = $(id_input);
								div.id_input = id_input;
								div.clase_over = clase_over;
								input.id_div = id_div;
								input.texto_defecto = texto_defecto;
								Event.observe (div, 'click', this.muestra_input, true);
								Event.observe (div, 'mouseover', this.anade_clase, true);
								Event.observe (div, 'mouseout', this.quita_clase, true);
								Event.observe (input, 'blur', this.muestra_div, true);
							},
						
	muestra_input:	function (e) {
							var div = Event.element (e);
							$ (div.id_input).style.display = 'block';
							div.style.display = 'none';
						},
						
	muestra_div:		function (e) {
							var input = Event.element (e);
							$ (input.id_div).style.display = 'block';
							input.style.display = 'none';
							if ($F (input) != '')
								$ (input.id_div).innerHTML = $F (input);
							else
								$ (input.id_div).innerHTML = input.texto_defecto;
						},
						
	anade_clase:		function (e) {
							var div = Event.element (e);
							Element.addClassName (div, div.clase_over);
						},
						
	quita_clase:		function (e) {
							var div = Event.element (e);
							Element.removeClassName (div, div.clase_over);
						}
						
};	


var BorraCampos = Class.create ();

BorraCampos.prototype = {
	
	initialize:	function (ids, boton) {
						
						$(boton).ids = ids;
						Event.observe (boton, 'click', this.actua, true);
					},
					
	actua:		function (e) {
						var a = confirm ('Do you want to delete this block?');
						if (a)
						{
							var boton = Event.element(e);
							var ids = boton.ids;
							for (a = 0 ; a < ids.length ; a++)
							{
								var nodo = $(ids[a])
								if (nodo.nodeName == 'IMG')
									nodo.src = 'images/nofoto.gif';
									
								if (nodo.nodeName == 'TEXTAREA' || nodo.nodeName == 'INPUT')
									nodo.value = '';
							}
						}
					}
					
};



// ----------------------------------------------------------------
// clase ListUtil
// ----------------------------------------------------------------


var ListUtil = new Object();



ListUtil.getSelectedIndexes = function (oListbox) {

    var arrIndexes = new Array;

    for (var i=0; i < oListbox.options.length; i++) {
        if (oListbox.options[i].selected) {
            arrIndexes.push(i);
        }
    }
    return arrIndexes;
};



ListUtil.add = function (oListbox, sName, sValue) {

    var oOption = document.createElement("option");
    oOption.appendChild(document.createTextNode(sName));

    if (arguments.length == 3) {
        oOption.setAttribute("value", sValue);
    }
    oListbox.appendChild(oOption);
}


ListUtil.remove = function (oListbox, iIndex) {
    oListbox.remove(iIndex);
};


ListUtil.clear = function (oListbox) {
    for (var i=oListbox.options.length-1; i >= 0; i--) {
        ListUtil.remove(oListbox, i);
    }

};


ListUtil.move = function (oListboxFrom, oListboxTo, iIndex) {

    var oOption = oListboxFrom.options[iIndex];

    if (oOption != null) {
        oListboxTo.appendChild(oOption);
    }
};



ListUtil.shiftUp = function (oListbox, iIndex) {

    if (iIndex > 0) {    

        var oOption = oListbox.options[iIndex];
        var oPrevOption = oListbox.options[iIndex-1];
        oListbox.insertBefore(oOption, oPrevOption);

    }    

};



ListUtil.shiftDown = function (oListbox, iIndex) {

    if (iIndex < oListbox.options.length - 1) {
        var oOption = oListbox.options[iIndex];
        var oNextOption = oListbox.options[iIndex+1];
        oListbox.insertBefore(oNextOption, oOption);

    }

};


function delete_data (mensaje, url)
{
	var a = confirm (mensaje);
	if (a) window.location.href = url;
}



// validacion --------------

// EspacioBlanco
// Se encarga de verificar si solo hay espacios en blanco
function EspaciosBlanco (Valor)
{
	var EspacioBlanco = ' \n\r\t'; // los espacios en blanco
	for (var i = 0; i < Valor.length; i++)
	{
		Caracter = Valor.charAt (i);
		if (EspacioBlanco.indexOf (Caracter) == -1)
		{
			return false;
		}
	}
	return true; 
}

// CampoVacio
// Se encarga de comprobar si el campo esta vacio
function CampoVacio (Valor)
{
	if (Valor == "" || Valor == null)
	{
		return true
	}
	return false
}

// CaracterNumero
// Se encarga de verificar si el caracter es un numero
function CaracterNumero (Caracter)
{
	var Numeros = '0123456789 ';
	if (Numeros.indexOf(Caracter) == -1)
	{
		return false;
	}
	return true;
}

// SoloNumeros
// se encarga de verficar si el campo solo tiene numeros
function SoloNumeros (Valor)
{
	for (i = 0; i < Valor.length; i++)
	{
		Caracter = Valor.charAt(i);
		if (!CaracterNumero (Caracter))
		{
			return false;
		}
	}
	return true;
}


// EmailValido
// Verifica si el valor es un email
function EmailValido (Valor)
{
	if (Valor.length < 5)
		return false;
	var Arroba = Valor.indexOf('@');
	var Punto = Valor.indexOf ('.');
	
	if (Arroba == -1 || Punto == -1)
		return false;
		
	
	return true;
}
