$(document).ready(function() {
	
	$("#id_type_diplome").live('change',function()
	{		
		// si non vide
		if ($(this).attr("value") != "Choisissez un type de formation...") {
			affFromType();
		}
		else {
			reset_recherche();
		}
	});
	
	$("#id_liste_fams_fos").live('change',function()
	{
		// si non vide
		if ($(this).attr("value") != "") {
			affFromFamFo();
		}
		else {
			reset_recherche_1();
		}
	});
	
	$("#id_liste_fos").live('change',function()
		{
			// si non vide
			if ($(this).attr("value") != "") {
				affDeptsCondition();
				affEtabsCondition();
				affBoutonRecherche();
			}
			else {
				reset_recherche_2();
			}
	});
	
	$(".bouton_navigation").live("click",function(){
		var bouton_url=$(this).attr("href");
		$.get(bouton_url, function(data){
			$('#bloc_resultat').html(data);
		});
		return false;
	});
	
	
});

// bouton recherche
function affBoutonRecherche()
{
	$('#bloc_bouton_recherche').html("<input type=\"submit\" value=\"Rechercher\" id=\"bouton_rechercher\" />");
}

// raz du formulaire
function reset_recherche()
{
	$('#bloc_fam_fos').html("");
	$('#bloc_fos').html("");
	$('#bloc_bouton_recherche').html("");
}

function reset_recherche_1()
{
	$('#bloc_fos').html("");
	$('#bloc_bouton_recherche').html("");
}

function reset_recherche_2()
{
	$('#bloc_bouton_recherche').html("");
}

//récupération du type de formations (bts, dut, bac pro...)
function affFromType()
{	
	//on récupère le type de diplome
	var type_formation=$('#id_type_diplome').attr('value');
	
	if(type_formation=='bac_pro') 
	{
		type_formation='bac pro';
	}
	if(type_formation!='')
	{
		affFamFosType(type_formation);
	}	
}
//affichage dans la page de la liste des familles formations associées au type choisi
function affFamFosType(type_formation)
{
 	$.get("ajax/ajax_search_multi.php?action=affFamFos&type="+type_formation+"",function(data){
		$('#bloc_fam_fos').html(data);
		affFromFamFo();
	});
}

//récupération de la famille formation choisi
function affFromFamFo()
{
	var type_formation=$('#id_type_diplome').attr('value');
	var id_fam_fo=$('#id_liste_fams_fos').attr('value');
	if(type_formation=='bac_pro') 
	{
		type_formation='bac pro';
	}
	if(type_formation!='' && id_fam_fo!='')
	{
		affFosFam(type_formation, id_fam_fo);
	}
}

//affichage dans la page de la liste des formations associées en fonction de la famille formation choisie
function affFosFam(type_formation, id_fam_fo)
{
 	$.get("ajax/ajax_search_multi.php?action=affFosFam&type="+type_formation+"&id="+id_fam_fo+"",function(data){
		$('#bloc_fos').html(data);
		affDeptsCondition();
		affEtabsCondition();
	});
}

//affiche la liste des départements
function affDeptsCondition()
{
	var type_formation=$('#id_type_diplome').attr('value');
	var id_fam_fo=$('#id_liste_fams_fos').attr('value');
	var id_fo=$('#id_liste_fos').attr('value');
	var code_dept=getDeptValue();
	if(type_formation!='' && id_fam_fo!='' && id_fo!='')
	{
		affDepts();	
	}
}

//affiche dans la page la liste des départements
function affDepts()
{
	$.get("ajax/ajax_search_multi.php?action=afficheDepts",function(data){
		$('#bloc_depts').html(data);
	});
}

function affEtabsCondition()
{	
	var type_formation=$('#id_type_diplome').attr('value');
	var id_fam_fo=$('#id_liste_fams_fos').attr('value');
	var id_fo=$('#id_liste_fos').attr('value');
	var code_dept=getDeptValue();
	if(type_formation!='' && id_fam_fo!='' && id_fo!='')
	{
		affEtabs(id_fo, code_dept);	
	}
}


//affiche les résultats d'établissements
function affEtabs(id_fo, code_dept)
{
	var page=1;
	$(".bouton_navigation").live("click",function(){
		var bouton_url=$(this).attr("href");
		$.get(bouton_url, function(data){
			$('#bloc_resultat').html(data);
		});
		return false;
	});
	$.get("ajax/ajax_search_multi.php?action=afficheRes&id_fo="+id_fo+"&cp="+code_dept+"&nupage="+page+"", function(data){
		$('#bloc_resultat').html(data);
	});
}

function getDeptValue()
{
	var dept_value=$('#id_liste_depts').attr('value');
	return dept_value;
}