/**
 * Find the element in the current HTML document with the given id, or if more
 * than one parameter is passed, return an array containing the found elements.
 * Any non-string arguments are left as is in the reply.
 * This function is inspired by the prototype library however it probably works
 * on more browsers than the original.
 * @see http://www.getahead.ltd.uk/dwr/util-general.html
 */
function $$()
{
    var elements = new Array();

    for (var i = 0; i < arguments.length; i++)
    {
        var element = arguments[i];
        if (typeof element == 'string')
        {
            if (document.getElementById) {
                element = document.getElementById(element);
            }
            else if (document.all) {
                element = document.all[element];
            }
        }
        if (arguments.length == 1) {
            return element;
        }
        elements.push(element);
    }
    return elements;
}

/**
 * Recupera la url actual, sin parametros
 */
function getUrl() {
	return location.href.indexOf('?') > 0 ? location.href.substring(0, location.href.indexOf('?')) : location.href ;
}

/**
 * funcion para añadir eventos a objetos
 * param obj: objeto al que añadir el evento
 * param evType: tipo de evento
 * param fn: funcion a asignar al evento
 * ej: addEvent(window, 'load', foo);
 */
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return true;
	} else
	if (obj.attachEvent)
		return obj.attachEvent('on' + evType, fn);
	else
		return false;
}

/**
 * Comprueba si un campo es vacio o no
 */
function isVacio(valor) {
	return valor == null || valor == '' || valor == 'null';
}

/**
 * Elimina todos los hijos de un nodo
 * Ej: removeAllNodes('misFavoritos');
 */
function removeAllNodes(nodo) {
	ele = $(nodo);
	var child = ele.firstChild;
	var next;
	while (child != null) {
		next = child.nextSibling;
		ele.removeChild(child);
		child = next;
	}
}


/**
 * Rellena el formulario indicado con los valores indicados.
 * Forma de uso:
 * var errores = {
 *   campo1 : 'valor1',
 *   campo2 : 'valor2';
 * }
 * rellenaFormulario('myFormId', errores);
 * param idForm: identificador del formulario
 * param newValues: nombre del objeto que contiene los nuevos valores
 */
function rellenaFormulario(idForm, newValues)
{
	var campos = $(idForm).elements;

	for (i=0; i<campos.length; i++)
	{
		if (campos[i].tagName == 'INPUT' && (campos[i].type == 'radio' || campos[i].type == 'checkbox')) {
			campos[i].checked = false;
			if (eval(newValues + '.' + campos[i].name)) 
			{
				var values = eval(newValues + '.' + campos[i].name).split('|');
				for (j=0; j<values.length; j++)
					if (campos[i].value == values[j]) campos[i].checked = true;
			}
		}
		else if (eval(newValues + '.' + campos[i].name)) 
			campos[i].value = eval(newValues + '.' + campos[i].name);
	}
}

/**
 * Comprueba si estamos ante un navegador IE6
 */
function isIE6()
{
	return typeof document.body.style.maxHeight == 'undefined';
}
function isIE()
{
	return navigator.appName.indexOf('Microsoft') >= 0;
}

/**
 * Devuelve el tamaño en pixels de 1em
 */
function tamEM()
{
	var a = document.createElement('div');
	a.style.position = 'absolute';
	a.style.visibility = 'hidden';
	a.style.paddingTop = '1em';
	document.body.appendChild(a);
	var tam = a.offsetHeight;
	document.body.removeChild(a);
	return tam;
}

/**
 * Establece una altura mínima al elemento indicado
 */
function setHeight(id, tam)
{
	if (isIE6())
		tam != null ? $$(id).style.height = tam + 'px' : $$(id).style.height = '';
	else
		tam != null ? $$(id).style.minHeight = tam + 'px' : $$(id).style.minHeight = '';
}

/**
 * Incrementa el tamaño mínimo del elemento indicado con el tamaño pasado
 */
function incHeight(id, tam)
{
	if (isIE6())
		$$(id).style.height = parseInt( $$(id).style.height.replace("px","") ) + (tam) + 'px';
	else
		$$(id).style.minHeight = parseInt( $$(id).style.minHeight.replace("px","") ) + (tam) + 'px';
}

/**
 * Devuelve el padding vertical del elemento especificado.
 * El padding vertical se refiere a la suma de los paddings superior e inferior
 */
function getPaddingHeight(id)
{
	var alto = $$(id).offsetHeight;
	setHeight(id, alto);
	var padding = $$(id).offsetHeight - alto;
	setHeight(id, alto - padding);
	return padding;
}

var Staff = {
	
	visible : false,
	
	show : function()
	{
		if (!this.visible)
		{
			if (!$$('fondoStaff'))
			{
				var div = document.createElement('DIV');
				div.setAttribute('id', 'fondoStaff');
				div.style.height = document.body.clientHeight+'px';
				div.onclick = function() { Staff.close(); }
				document.body.appendChild(div);
			}
			else {
				$$('fondoStaff').style.display = 'block';
			}
			
			if (!$$('frameStaff'))
			{
				var frame = document.createElement('IFRAME');
				frame.setAttribute('id', 'frameStaff');
				frame.src = '/easturias/contenidos/system/modules/com.fti.easturias/templates/staff.html';
				frame.width = '600px';
				frame.height = '295px';
				frame.frameBorder = '0';
				frame.style.left = ((document.body.clientWidth - 500) / 2) + 'px';
				document.body.appendChild(frame);
				setTimeout('Staff.resize()', 200)
			}
			else {
				$$('frameStaff').style.display = 'block';
			}
			
			this.visible = true;
		}
	},
	
	close : function()
	{
		if (this.visible)
		{
			$$('fondoStaff').style.display = 'none';
			$$('frameStaff').style.display = 'none';
			this.visible = false;
		}
	},
	
	resize : function()
	{
		$$('frameStaff').height = ($$('frameStaff').contentDocument.body.scrollHeight + 5) + 'px';
	}
}


var developed = {

	f : false,
	t : false,
	i : false,
	
	press : function(e)
	{
		evt = e ? e : event;
		tcl = (window.Event) ? evt.which : evt.keyCode;
		switch (tcl) {
			case 70: this.f = true; break;
			case 84: this.t = true; break;
			case 73: this.i = true; break;
		}
		
		if (this.f && this.t && this.i) {
			Staff.show();
			this.f = false;
			this.t = false;
			this.i = false;
		}
	},
	
	release : function(e)
	{
		evt = e ? e : event;
		tcl = (window.Event) ? evt.which : evt.keyCode;
		switch (tcl) {
			case 70: this.f = false; break;
			case 84: this.t = false; break;
			case 73: this.i = false; break;
		}
	}
}
addEvent(document, 'keydown', developed.press);
addEvent(document, 'keyup', developed.release);



/**
 * Crea una listado de secciones en pestañas
 */
var Pestanas = {
	
	listado : 'pestanias',
	contenido : 'contenido_pestania',
	
	init : function (idListado, idContenido)
	{
		Pestanas.listado = $$(idListado);
		Pestanas.contenido = $$(idContenido);
		
		Pestanas.listado.className += ' pestanias';
		var secciones = Pestanas.listado.childNodes;
		for (i=0; i<secciones.length; i++) {
			var trozos = secciones[i].childNodes;
			var siguiente = false;
			for (j=0; j<trozos.length; j++)
				if (trozos[j].tagName)
				{
					if (siguiente) {
						trozos[j].style.display = 'none';
						Pestanas.contenido.appendChild( trozos[j] );
						break;
					}
					siguiente = true;
				}
		}
		
		var h3s = Pestanas.listado.getElementsByTagName('H3');
		for (i=0; i<h3s.length; i++) {
			h3s[i].style.cursor = 'pointer';
		}
		Pestanas.muestra(0);
	},
	
	muestra : function (seccion)
	{
		var lis = Pestanas.listado.getElementsByTagName('LI')
		for (i=0; i<lis.length; i++) {
			if (i == seccion) lis[i].className = 'selected';
			else lis[i].className = '';
		}
		
		var c = -1;
		var secciones = Pestanas.contenido.childNodes;
		for (i=0; i<secciones.length; i++)
			if (secciones[i].tagName)
			{
				c++;
				if (c == seccion)
					secciones[i].style.display = 'block';
				else
					secciones[i].style.display = 'none';
			}
	},
	
	size : function()
	{
		return Pestanas.listado.getElementsByTagName('H3').length;
	}
}

/*
To get all a elements in the document with a “info-links” class.
getElementsByClassName(document, "a", "info-links");
To get all div elements within the element named “container”, with a “col” class.
getElementsByClassName(document.getElementById("container"), "div", "col");
To get all elements within in the document with a “click-me” class.
getElementsByClassName(document, "*", "click-me");
*/
function getElementsByClassName(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function comprobarRespuestas(classname)
{
	var lista = getElementsByClassName(document, "*", classname);
	var contador = 0;
	for (var i = 0; i < lista.length; i++){
	
		if( lista[i].checked )
		contador++;
	}
	
	if( contador > 0 )
		document.getElementById("votar").disabled = '';
	else
		document.getElementById("votar").disabled = 'true';
	

}
