/**
 * 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;
}

/*
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) {
		var r = obj.attachEvent('on' + evType, fn);
		return r;
	} else {
		return false;
	}
}

/*
Comprueba si un campo es vacio o no
*/
function isVacio(valor) {
	return valor == null || valor == '' || valor == 'null';
}

/*
Abre una ventana emergente
param url: ruta que se abrirá en la ventana
param ancho: ancho de la ventana (opcional)
param alto: alto de la ventana (opcional)
*/
function popup(url, ancho, alto) {
	if (ancho == null)
		ancho = screen.width * 0.75;
	if (alto == null)
		alto = screen.height * 0.75;
	var top = (screen.height - alto) / 2;
	var left = (screen.width - ancho) / 2;
	window.open(url, 'popup', 'width='+ancho+',height='+alto+',scrollbars=yes,left='+left+',top='+top);
}

/*
Redimensiona una ventana en función del tamaño de una imagen
param img: identificador de la imagen en la página
param ancho: tamaño a incrementar la ventana sobre el ancho de la imagen (opcional)
param alto: tamaño a incrementar la ventana sobre el alto de la imagen  (opcional)
*/
function redimensiona(img, ancho, alto) {
	var i = $(img);
	
	i.onload = function() {
		if (ancho == null)
			ancho = 50;
		if (alto == null)
			alto = 80;
			
		ancho += i.width;
		alto += i.height;
		
		if (screen.width < ancho) ancho = screen.width;
		if (screen.height < alto) alto = screen.height;
		
		window.resizeTo(ancho, alto);
		
		var left = (screen.width - ancho) / 2;
		var top = (screen.height - alto) / 2;
		
		window.moveTo(left, top);
	}
}