// JavaScript Document
/************************* GESTION DES REGIONS/LIEUX ****************************/
var recherche_lieu = [];
	
$(document).ready(function(){
	recherche_lieu_init();
	$(".recherche-bloc-region select[name=region]").bind('change',function(){
		recherche_lieu_refresh();
	});
});

function recherche_lieu_init()
{
	var el = $(".recherche-bloc-lieu select[name=lieu] option");
	for(var i=0; i<el.length; i++)
	{
		 recherche_lieu.push($(el[i]).clone());
	}
	recherche_lieu_refresh(true);
}
function recherche_lieu_refresh(preserve)
{
	var region = $(".recherche-bloc-region select[name=region]");
	var regionID = region.selectValue();
	
	var lieu = $(".recherche-bloc-lieu select[name=lieu]");
	var lieuID = lieu.selectValue();

	$("option",lieu).remove();	
	
	for(var i=0; i<recherche_lieu.length; i++)
	{
		// on insere si :
		// pas de restriction (region=="")
		// c'est le titre "choisissez..." ({rel}==undefined)
		// le code correspond à la region de restriction ({rel}==regionID)
		if(regionID=="" || $(recherche_lieu[i]).attr('rel')==undefined || $(recherche_lieu[i]).attr('rel')==regionID)
			lieu.append(recherche_lieu[i]);
	}
	
	if(preserve==true)
		lieu.selectValue(lieuID);
	else
		lieu.selectValue("");

	if($("option",lieu).length>1)
		$('.recherche-bloc-lieu').show();
	else
		$('.recherche-bloc-lieu').hide();
}
