function bookmarksite(title, url)
{
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
}

function autofocus(id)
{
	var element = document.getElementById(id);
	
	if (typeof(element) == 'undefined')
		return false;
	
	element.focus();
	return true;
}

function build_search_url(search_url, search_field_id)
{
	var search_field = document.getElementById(search_field_id);
	
	return (typeof(search_field) == 'undefined') ? search_url : search_url+':'+encodeURI(search_field.value.replace(' ', '+'));
}

function select_search_url(flag_field_id, search_url1, search_url2)
{
	var flag_field = document.getElementById(flag_field_id);
	
	return (flag_field.checked) ? search_url1 : search_url2;
}


// countries selection
function move_selected_options(origin_id, destiny_id)
{
	if (!document.getElementById)
		return false;
	
	var box_origin = document.getElementById(origin_id);
	var box_destiny = document.getElementById(destiny_id);
	
	if (!box_origin || !box_destiny)
		return false;
	
	for (var i = box_origin.length - 1; i >= 0; i--)
	{
		if (box_origin.options[i].selected == 1)
		{
			box_origin.options[i].selected = 0;
			add_option(box_origin.options[i], box_destiny);
			remove_option(i, box_origin);
		}
	}
	
	order_selection_by_value(box_destiny);
}

function move_all_options(origin_id, destiny_id)
{
	if (!document.getElementById)
		return false;
	
	var box_origin = document.getElementById(origin_id);
	var box_destiny = document.getElementById(destiny_id);
	
	if (!box_origin || !box_destiny)
		return false;
	
	for (var i = box_origin.length - 1; i >= 0; i--)
	{
		box_origin.options[i].selected = 0;
		add_option(box_origin.options[i], box_destiny);
		remove_option(i, box_origin);
	}
	
	order_selection_by_value(box_destiny);
}

function add_option(option, selection)
{
	selection.length ++;
	selection.options[selection.options.length - 1] = new Option(option.text, option.value);
}

function remove_option(pos, selection)
{
	if (pos < 0 || pos > selection.length)
	{
		alert('remove_option : pos fuera de rango');
		return false;
	}

	var max = selection.length - 1;

	for (var i = pos; i < max; i++)
	{
		selection.options[i].value = selection.options[i+1].value;
		selection.options[i].text = selection.options[i+1].text;
		selection.options[i].selected = selection.options[i+1].selected;
	}
	
	selection.length --;
}

function order_selection_by_value(selection)
{
	var max = selection.length - 1;
	var unordered = true;
	
	while (unordered)
	{
		unordered = false;
		for (var i = 0; i < max; i++)
		{
			if (selection.options[i].value < selection.options[i+1].value)
				continue;
			
			interchange_options(selection.options[i], selection.options[i+1]);
			unordered = true;
		}
	}
}

function interchange_options(option1, option2)
{
	var value_aux = option1.value;
	var text_aux = option1.text;
	
	option1.value = option2.value;
	option1.text = option2.text;
	option2.value = value_aux;
	option2.text = text_aux;
}

function update_selection_value(hidden_id, box_id)
{
	if(!document.getElementById)
		return false;

	var box_destiny = document.getElementById(box_id);
	var hidden_field = document.getElementById(hidden_id);

	if(!box_destiny || !hidden_field)
		return false;
	
	var new_value = '';
	
	for (var i = 0; i < box_destiny.length; i++)
	{
		if (i > 0)
			new_value += ','
		
		new_value += box_destiny.options[i].value;
	}
	
	hidden_field.value=new_value;
}