$(document).ready(function() { var pays = null; var region = null; var departement = null; var ville = null; /***************/ // Init /***************/ load_event_pays(); // RAZ $('#delete_fil_ariane_localisation').click(function() { $("#pays").show(); $("#region").hide(); $("#departement").hide(); $("#ville").hide(); $('#fil_ariane_localisation').text(""); $('#delete_fil_ariane_localisation').hide(); $('.pays').val(""); }); /***************/ // EVENT Pays /***************/ function load_event_pays() { $('.select_pays').val(""); $('.select_pays').change(function() { var paysName = $(this).children("[value="+$(this).val()+"]").text(); if(thisPays = $(this).val()) { thisPays = thisPays.split('_'); $("#pays").hide(); $('#fil_ariane_localisation').text(paysName); $('#delete_fil_ariane_localisation').show(); // Ajax ajax_region(thisPays[0]); } }); } /***************/ // EVENT Region /***************/ function load_event_region() { $('.select_region').change(function() { var regionName = $(this).children("[value="+$(this).val()+"]").text(); if(thisRegion = $(this).val()) { thisRegion = thisRegion.split('_'); $("#region").hide(); $('#fil_ariane_localisation').text($('#fil_ariane_localisation').text() + ' / ' + regionName); // Ajax ajax_departement(thisRegion[0], thisRegion[1]); } }); } /***************/ // EVENT Departement /***************/ function load_event_departement() { $('.select_departement').change(function() { var departementName = $(this).children("[value="+$(this).val()+"]").text(); if(thisDepartement = $(this).val()) { thisDepartement = thisDepartement.split('_'); $("#departement").hide(); $('#fil_ariane_localisation').text($('#fil_ariane_localisation').text() + ' / ' + departementName); // Ajax ajax_ville(thisDepartement[0], thisDepartement[1], thisDepartement[2]); } }); } /***************/ // EVENT Ville /***************/ function load_event_ville() { $('.select_ville').change(function() { var villeName = $(this).children("[value="+$(this).val()+"]").text(); if(thisVille = $(this).val()) { thisVille = thisVille.split('_'); $("#ville").hide(); $('#fil_ariane_localisation').text($('#fil_ariane_localisation').text() + ' / ' + villeName); } }); } /***************/ //AJAX Region /***************/ function ajax_region(pays) { $.ajax( { type: "GET", url: "ajax/region.php", data: ({refPays : pays}), success: function(region) { if(region != "null") { $("#region").children('.localisation_select').html(region); $('#region').show(); // On regarde les évenements sur region load_event_region(); } // Il n'y a pas de regions, on regarde les départements else { ajax_departement(pays, ''); } } }); } /***************/ // AJAX Departement /***************/ function ajax_departement(pays, region) { $.ajax( { type: "GET", url: "ajax/departement.php", data: ({ refPays : pays, refRegion : region}), success: function(departement) { // Si il existe des départements if(departement != "null") { $("#departement").children('.localisation_select').html(departement); $('#departement').show(); load_event_departement(); } // Il n'y a pas de département, on regarde les villes else { ajax_ville(pays, region, ''); } } }); } /***************/ // AJAX Ville /***************/ function ajax_ville(pays, region, departement) { $.ajax( { type: "GET", url: "ajax/ville.php", data: ({ refPays : pays, refRegion : region, refDepartement : departement}), success: function(ville) { // Si il existe des villes if(ville != "null") { $("#ville").children('.localisation_select').html(ville); $('#ville').show(); load_event_ville(); } } }); } });