function showLayer(object) {
	if (document.layers && document.layers[object])
		document.layers[object].visibility = 'visible';
	else if (document.all) {
		document.all[object].style.visibility = 'visible';
		document.all[object].style.zIndex = 100;
	} else {
		var layer = document.getElementById(object);
		layer.style.visibility = 'visible';
	}
}

function hideLayer(object) {
	if (document.layers && document.layers[object])
		document.layers[object].visibility = 'hidden';
	else if (document.all)
		document.all[object].style.visibility = 'hidden';
	else {
		var layer = document.getElementById(object);
		layer.style.visibility = 'hidden';
	}
}

function checkAll (checked, form ) {
 if (form == null) {
	 var form = document.forms[0];
 }
 var i = 0;
 if (form.elements['id[]'])	// Hay alguno
	 if (form.elements['id[]'].length) { // Es un arreglo
	  do {
	   form.elements['id[]'][i++].checked = checked;
	  } while (form.elements['id[]'][i]);
	 } else form.elements['id[]'].checked = checked;
}

function validateChecked ( message, form ) {
 if (form == null) {
	 var form = document.forms[0];
 }
 var anychecked = false;
 var i = 0;
 if (message == null) message = 'eliminar'; else message = message.toLowerCase();
 if (form.elements['id[]'])	// Hay alguno
	 if (form.elements['id[]'].length) { // Es un arreglo
	  do {
	   anychecked = (form.elements['id[]'][i++].checked == true);
	   if (anychecked) break;
	  } while (form.elements['id[]'][i]);
	 } else anychecked = (form.elements['id[]'].checked == true);
 if (!anychecked) {
	alert('Seleccione al menos un elemento');
 } else {
	if (!message) anychecked = true; else anychecked = confirm('Desea '+ message +' los elementos seleccionados ?');
 }
 return (anychecked); 
}

function openWin( url, width, height) {
	var top;
	var left;
	if (width == null) width = 640;
	if (height == null) height = 500;
	top = (screen.height - height)/2 - 50;
	left = (screen.width - width)/2;
	window.open( url, 'popupWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left);
}

function jumpTo( target ){ 
	document.location = target;
}

function setNow() {
	var form = document.forms[0];
	var now = new Date();
	// Año
	for (var i = 0; i < form.ano.options.length; i++) 
		if (form.ano.options[i].value == now.getFullYear()) {
			form.ano.selectedIndex = i;
			break;
		}
	// Mes
	for (i = 0; i < form.mes.options.length; i++) 
		if (form.mes.options[i].value == now.getMonth()+1) {
			form.mes.selectedIndex = i;
			break;
		}
	// Día
	for (i = 0; i < form.dia.options.length; i++) 
		if (form.dia.options[i].value == now.getDate()) {
			form.dia.selectedIndex = i;
			break;
		}
	// Hora
	if (form.hora != null)
	for (i = 0; i < form.hora.options.length; i++) 
		if (form.hora.options[i].value == now.getHours()) {
			form.hora.selectedIndex = i;
			break;
		}
	// Minutos
	if (form.min != null)
	for (i = 0; i < form.min.options.length; i++) 
		if (form.min.options[i].value == now.getMinutes()) {
			form.min.selectedIndex = i;
			break;
		}
}

function toPesetas( euros ) {
	var pesetas = 166.386 * euros;
	if (pesetas.toFixed) pesetas = pesetas.toFixed(2);
	return "(" + pesetas + " Pta)";
}

function populatePob( id_provincia, id_poblacion ) {
	var form = document.forms[0];
	var arrPoblacion;
	var j = 0;

    while (form.id_poblacion.options.length >= 1) form.id_poblacion.options[(form.id_poblacion.options.length-1)] = null;
	for (var i=0; i < arrPoblaciones.length -1; i++) {
		arrPoblacion = arrPoblaciones[i].split('|');
		if (arrPoblacion[0] == id_provincia && !inList('poblaciones_int[]', arrPoblacion[1])) {
			eval("form.id_poblacion.options[j] = " + "new Option('" + arrPoblacion[2] + "')");
			eval("form.id_poblacion.options[j].value = " + arrPoblacion[1]);
			if (arrPoblacion[1] == id_poblacion)
				eval("form.id_poblacion.options[j].selected = true");
			j++;
		}
	}
}

function inList( ddl, value ) {
	var form = document.forms[0];
	var list = eval('form.elements[\'' + ddl + '\']');
	var found = false;
	if (!list) return false;
	for (var j = 0; j < list.options.length; j++) {
		if (list.options[j].value == value) {
			found = true;
			break;
		}
	}
	return found;
}

function selectAll( ddl ) {
	var form = document.forms[0];
	var list = eval('form.elements[\'' + ddl + '\']');
	for (var i = 0; i < list.options.length; i++ ) list.options[i].selected = true;
}

function getElement( ddl, index, deleteItem ) {
	var form = document.forms[0];
	var list = eval('form.elements[\'' + ddl + '\']');
	var value;
	
	value = list.options[index].value + '|' + list.options[index].label;
	if (deleteItem == true) list.options[index] = null;
	return value;
}

function sortSelect(obj) {
	var o = new Array();

	for (var i = 0; i < obj.options.length; i++ ) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
	}
	o = o.sort( 
		function(a, b) { 
			if (( a.text + "") < (b.text + "")) { return -1; }
			if (( a.text + "") > (b.text + "")) { return 1; }
			return 0;
		} 
	);
	for (var i = 0; i < o.length; i++ ) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}

function addItem( ddlFrom, ddlTo, prefix ) {
	var form = document.forms[0];
	var i;
	var picked = new Array();
	var fromlist = eval('form.elements[\'' + ddlFrom + '\']');
	var tolist = eval('form.elements[\'' + ddlTo + '\']');
	if (!prefix) prefix = "";

	for (var j = 0; j < fromlist.options.length; j++)
		if (fromlist.options[j].selected == true) {
			i = tolist.options.length;
			tolist.options[i] = new Option(prefix + fromlist.options[j].text, fromlist.options[j].value);
			picked[j] = true;
		}
	for (var j = fromlist.options.length-1;j >= 0; j--)
		if (picked[j] == true) fromlist.options[j] = null;
	
	sortSelect(tolist);
}

function delItem( ddlFrom, ddlTo, prefix ) {
	var form = document.forms[0];
	var i;
	var picked = new Array();
	var fromlist = eval('form.elements[\'' + ddlFrom + '\']');
	var tolist = eval('form.elements[\'' + ddlTo + '\']');
	if (!prefix) prefix = false;

	for (var j = 0; j < fromlist.options.length; j++)
		if (fromlist.options[j].selected == true) {
			var arrPob = fromlist.options[j].text.split(' - ');
			i = tolist.options.length;
			if (prefix != false) {
				if (arrPob[0] == prefix) tolist.options[i] = new Option(arrPob[1], fromlist.options[j].value);
			} else {
				tolist.options[i] = new Option(arrPob[0], fromlist.options[j].value);
			}
			picked[j] = true;
		}
	for (var j = fromlist.options.length-1;j >= 0; j--)
		if (picked[j] == true) fromlist.options[j] = null;
	sortSelect(tolist);
}