function paint( object, color ) {
	object.bgColor = color;
}
function checkAll (checked) {
 var form = document.forms[0];
 var i = 0;
 if (form.elements['checkbox[]'].length) { // Es un arreglo
  do {
   form.elements['checkbox[]'][i++].checked = checked;
  } while (form.elements['checkbox[]'][i]);
 } else form.elements['checkbox[]'].checked = checked;
}

function changeImg( cell, img ) {
	cell.style.backgroundImage="url('"+img+"')";
}

function changebgColor(cell, bgcolor) {
	if (bgcolor) {
		cell.style.backgroundImage = 'url()';
		cell.style.backgroundColor = bgcolor;
	} else 
		cell.style.backgroundImage = "url('../lib/img/bar_gray.gif')";
}

function validateChecked () {
 var form = document.forms[0];
 //if (form.op.selectedIndex == 0) return true;
 var anychecked = false;
 var i = 0;
 if (form.elements['checkbox[]'].length) { // Es un arreglo
  do {
   anychecked = (form.elements['checkbox[]'][i++].checked == true);
   if (anychecked) break;
  } while (form.elements['checkbox[]'][i]);
 } else anychecked = (form.elements['checkbox[]'].checked == true);
 if (!anychecked) {
	alert('Seleccione al menos un elemento');
 } else {
	anychecked = confirm('Esta seguro de eliminar los elementos seleccionados?');
 }
 if (anychecked) form.submit(); else return true; //form.op.selectedIndex = 0;
}

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 isFull( fieldvar, fieldname ) {
	var form = document.forms[0];
	eval('fieldvalue = form.' + fieldvar + '.value');
	if (fieldvalue == '') {
		alert('Campo \'' + fieldname + '\' requerido');
		eval('form.' + fieldvar + '.focus();');
		return false;
	} else return true;
}

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.year.options.length; i++) 
		if (form.year.options[i].value == now.getFullYear()) {
			form.year.selectedIndex = i;
			break;
		}
	// Mes
	for (i = 0; i < form.month.options.length; i++) 
		if (form.month.options[i].value == now.getMonth()+1) {
			form.month.selectedIndex = i;
			break;
		}
	// Día
	for (i = 0; i < form.day.options.length; i++) 
		if (form.day.options[i].value == now.getDate()) {
			form.day.selectedIndex = i;
			break;
		}
	// Hora
	if (form.hour != null)
	for (i = 0; i < form.hour.options.length; i++) 
		if (form.hour.options[i].value == now.getHours()) {
			form.hour.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.poblaciones_list.options.length >= 1) form.poblaciones_list.options[(form.poblaciones_list.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.poblaciones_list.options[j] = " + "new Option('" + arrPoblacion[2] + "')");
			eval("form.poblaciones_list.options[j].value = " + arrPoblacion[1]);
			if (arrPoblacion[1] == id_poblacion)
				eval("form.poblaciones_list.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);
}