function getItemWithId( arr, id) {
	for( i = 0; i < arr.length; i++) {
		var item = arr[i];	
		if( item.id == id ) {
			return item;
		}
	}
	return null;
}
	
function clearMinusFirst( idSelect ) {
	if ( document.getElementById( idSelect ) == null ) {
		return;
	}
	if ( document.getElementById( idSelect ).options.length == 0 ) {
		return;
	}
	document.getElementById( idSelect ).options.length = 1;
}	
	
function clearDestino() {
	clearMinusFirst( "destino" );
}
	
function addOptionToSelect( idSelect, id, value ) {
	if( document.getElementById( idSelect ) == null ) {
		return;
	}
	document.getElementById( idSelect ).options[ document.getElementById( idSelect ).options.length ] = new Option(value, id);
}
	
function selectPais( idPais ) {
	clearDestino();

	var pais = getItemWithId( paisesDestinos, idPais);
	if( pais == null ) {
		return;
	}
	for( i = 0; i < pais.destinos.length; i++) {
		var destino = pais.destinos[i];
		addOptionToSelect( "destino", destino.id, destino.name);
	}
	
	var dst = document.getElementById("destino");
	if( dst != null && dst.options.length == 2 ) {
		dst.selectedIndex = 1;
	}
}
	
function todosDestinos() {
	clearDestino();
	for( i = 0; i < paisesDestinos.length; i++) {
		var pais = paisesDestinos[i];
		for( j = 0; j < pais.destinos.length; j++) {
			var destino = pais.destinos[j];
			addOptionToSelect( "destino", destino.id, destino.name);
		}
	}
}

function setFiltroPaisDestino(pais, destino) {
	document.getElementById("pais").value = pais;
	selectPais( pais );
	document.getElementById("destino").value = destino;
	$('#llistaPaisos').hide('fast');
}
