

		var focus = false;
		var current_id_focus = 0;
		var current_search = '';
		var current_nbr = 0;
		var search_id = new Array('-1');
		var border_no_foc = '';
		var background_color_no_foc = '';
		var background_color_foc = '';
		var border_foc = '';
		var file_ref;
		//var HostUrl = "http://localhost/RssAnnonces/";
		
		/**
		  *		Permet d'initialiser les variables de config javascript
		  *		Cf. config.php  pour les significations des variables
		  *
		  */
		function autoCompletion_init(border_no_foc,border_foc,background_color_no_foc,background_color_foc,file_ref)
		{
			this.border_no_foc = border_no_foc;
			this.border_foc = border_foc;
			this.background_color_no_foc = background_color_no_foc;
			this.background_color_foc = background_color_foc;
			this.file_ref = file_ref;
		}
		
		
		/**
		  *		Fonction controlant les actions a faire en fonction de la valeur du code de l'event "KeyUp"
		  *		Permet la navigation clavier dans la liste dynamique
		  *		
		  *		@ param : value => valeur du champ de texte principal
		  *			       event => l'evenement associe au KeyUp
		  */
		function event_capture(value,event)
		{
		    this.current_search = document.getElementById('autoCompletion_input').value;
			/*
				Normalement le " event.keyCode " n'est utilise que par IE et pas par Mozilla et Firefox qui eux utilisent "which"
				Mais a ma grande stupefaction apres un test sous firefox .... cela marche ossi... alors pourquoi aller mettre du code en plus...
			*/
			switch (event.keyCode)
			{
				// TOUCHE " FLECHE BAS "
				case 40 :
					if ( current_id_focus > 0 && current_id_focus < current_nbr) loose_focus_style(current_id_focus);
					if ( current_id_focus < current_nbr ) {

					    give_focus_style(current_id_focus +1);
					    current_id_focus++;
						
					}
					break;
						
				// TOUCHE " FLECHE HAUT "
				case 38:
				    var old_id = 0;
				    if (current_id_focus >= 1) old_id = current_id_focus;
				    if (old_id > 0) loose_focus_style(old_id);

				    if (current_id_focus <=1) current_id_focus = current_nbr + 1;
				    if (current_id_focus >= 1) give_focus_style(current_id_focus -1);
				    current_id_focus--;

				    break;
								
				// TOUCHES " FLECHE GAUCHE " et " FLECHE DROITE " desactivees
				case 37 : 		break;
				case 39 : 		break;
				
				// TOUCHE " ENTRER "
				case 13:
				    if (value != '') {
				        var commune = SearchFromLastResultByValue();
				        if (commune != null) onSelectItem(commune);
				        break;
				    }
				
				// TOUTES LES AUTRES TOUCHES
				default :
								AJAX(value);
								break;
			}
		}
		
		/**
		  *		Permet la redirection apres un click ou l'appui sur "entrer"
		  *		 - si on click sur un element de la liste ou que l'on appui sur "entrer" sur un des elements => mode fiche => id en parametre ( id=X )
		  *		 - si on appui sur "entrer" dans le champ texte principal => mode recherche => valeur de la recherche en parametre ( search=xxx )
		  */
		function SearchFromLastResultByValue() {
		    if (lastResultSearchCommune != null && lastResultSearchCommune.d.length > 0) {
		        var ind = current_id_focus - 1;
		        if (ind > 0 && ind < lastResultSearchCommune.d.length) {
		            commune = lastResultSearchCommune.d[ind];
		            if (commune != null) return commune;
		            else return null;
		        }else {

		            var commune = lastResultSearchCommune.d[0];
		            if (commune != null) return commune;
		            else return null;
		        }
		    }
		    else return null;
	    }

	    function SearchFromLastResultById(id) {
	        if (lastResultSearchCommune != null) {
	            for (i = 0; i < lastResultSearchCommune.d.length; i++) { //
	                var commune = lastResultSearchCommune.d[i];
	                if (commune.idCommune == id) {
	                    return commune;
	                    break;
	                }
	            }
	        }
	    }
		
		
		var lastSearchValue = "";
		function AJAX(value) {
		    if (value.length > 1) {
		        lastSearchValue = value;
		        var idRegion = (currentLocalisation.idRegion != null ? currentLocalisation.idRegion : 0);
		        var idDepartement = (currentLocalisation.idDepartement != null ? currentLocalisation.idDepartement : 0);

		        $.ajax({
		            type: "POST",
		            url: HostUrlJs + "GlobalWS.asmx/SearchCommune",
		            data: "{value:'" + value + "', idRegion:'" + idRegion + "', idDepartement:'" + idDepartement + "'}",
		            contentType: "application/json; charset=utf-8",
		            dataType: "json",
		            success: function(msg) { SearchCommuneResponse(msg) },
		            error: function(xhr) {
		                alert(xhr.responseText);
		            }
		        });

		    }
		    else {
		        currentLocalisation.idCommune = 0;
		        LocalisationChange();
		        hide_list();
		    }
		}

		function SearchCommuneResponse(msg) {
            write_div(msg);
		}
		
		
		var lastResultSearchCommune;
		function write_div(msg) {
		    lastResultSearchCommune = msg;
			var div = document.getElementById('autoCompletion_answer'); // Block d'affichage
			// On cree le code html que l'on va inserer dans la div
			var output = '';
			for (i = 0; i < msg.d.length; i++) { //
			    var commune = msg.d[i];
			    var id = i + 1;

			    var tab = commune.libCommune.toLowerCase().split(lastSearchValue.toLowerCase());
			    var libele =  tab[0] + "<b>" + lastSearchValue + "</b>" + tab[1];


			    var style = "autoCompletion_item";  /*'background-color:' + background_color_no_foc + '; ' +
				            'text-align:left; ' +
				            'border:' + border_no_foc + ';';*/

				var actions = 'onBlur="focus_off();" ' +
				              'onClick="onItemClic(' + commune.idCommune + ');" ' +
				              'onMouseOver="give_focus_style(' + id + ',1);" ';

				output += '<div id="communeItem_' + id + '" class="' + style + '" ' + actions + '>' + libele + '</div>';
            }

            // Si la reponse ajax n'est pas vide on affiche, sinon on efface
            current_nbr = msg.d.length;
            if (current_nbr > 0)
			{
				div.innerHTML = output;
				div.style.display = 'block';
			}
			else hide_list();
		}
	
		
		function onItemClic(id) {

		    var commune = SearchFromLastResultById(id);
		    if (commune != null) onSelectItem(commune);
		}

		//var currentCommune;
		function onSelectItem(item) {
		    currentLocalisation.idCommune = item.idCommune;
		    currentLocalisation.libCommune = item.libCommune;
		    document.getElementById('autoCompletion_input').value = item.libCommune;
		    hide_list();
		    LocalisationChange();
		}
		 
		function focus_on()
		{
			focus = true;
		}
		function focus_off()
		{
			focus = false;
		}
		document.onclick = function ()
        {
		    if ( focus == false ) hide_list();
		};

		function hide_list() {
		    document.getElementById('autoCompletion_answer').style.display = 'none';
		    current_nbr = 0;
		    current_id_focus = 0;
		}
		
		
		
		/**
		  *		Permet de changer le style des inputs
		  *		
		  *		Changement de style par javascript car en css :hover sur <input> ou <div> ne fonctionne pas sous IE
		  *		Puis pour les changements de focus par entree clavier necessite du javascript
		  *
		  *		@ param : id => id de l'input text a changer le style
		  *			       control => permet suivant les cas l'activation de certaines operations
		  *
		  */
		function give_focus_style(id,control)
		{
			if ( id != 0 && id != current_id_focus)
			{
			    var input_hidden = document.getElementById('communeItem_' + id);
				input_hidden.style.backgroundColor = background_color_foc;
				input_hidden.style.border = border_foc;
				input_hidden.style.cursor = 'pointer'; // Pour IE qui ne prend pas en compte les :hover sur les div en CSS
			}
			if ( control == 1 && id != current_id_focus)
			{
				loose_focus_style(current_id_focus);
				current_id_focus = id;
			}
		}
		
		
		/**
		  *		Permet d'enlever le style focus et de remettre le style normal
		  *		Fonction appelee depuis " give_focus_style " avant de mettre le style focus au nouveau input courant
		  *
		  *		@ param : id => id de l'input text a desactiver le style focus
		  */
		function loose_focus_style(id)
		{
			if ( current_id_focus != 0 )
			{
			    var input_hidden = document.getElementById('communeItem_' + id);
				input_hidden.style.backgroundColor = background_color_no_foc;
				input_hidden.style.border = border_no_foc;
			}
        }

        var currentLocalisation = new Object();
        function LocalisationChange() {

            var cmbxRegion = document.getElementById('autoCompletion_Reg');
            var cmbxDepartement = document.getElementById('autoCompletion_Dep');
            

            //Localisation Name
            currentLocalisation.Name = "";
            if (currentLocalisation.idCommune > 0) currentLocalisation.Name = "Commune: " + currentLocalisation.libCommune;
            else if (currentLocalisation.idDepartement > 0) currentLocalisation.Name = "Departement: " + currentLocalisation.libDepartement;
            else if (currentLocalisation.idRegion > 0) currentLocalisation.Name = "Region: " + currentLocalisation.libRegion;
            else currentLocalisation.Name = "";

            AnalyseSearchParams();
        }

        /*function AddLocalisation() {
            document.getElementById('debug').innerHTML = currentLocalisation.Name;
        }*/

        function GetLocalisation() {
			// transféré vers asp-js-config
            /*var idCommune = getUrlVar("idCommune");
            var idDep = getUrlVar("idDepartement");
            var idRegion = getUrlVar("idRegion");*/
			
            if (idCommune != null || idDep != null || idRegion != null) {
                var query = "";
                query += "{idCommune:" + (idCommune != null ? idCommune : "0") + ",";
                query += "idDepartement:" + (idDep != null ? idDep : "0") + ",";
                query += "idRegion:" + (idRegion != null ? idRegion : "0") + "}";
                //alert(query);
                $.ajax({
                    type: "POST",
                    url: HostUrlJs + "GlobalWS.asmx/GetLocalisation",
                    data: query,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) { GetLocalisationResponse(msg) },
                    error: function(xhr) {
                        alert(xhr.responseText);
                    }
                });
            }
            else {
                GetRegion();
            }
        }

        function GetLocalisationResponse(msg) {

            currentLocalisation.idCommune = msg.d.idCommune;
            currentLocalisation.libCommune = msg.d.libCommune;
            currentLocalisation.idDepartement = msg.d.idDepartement;
            currentLocalisation.libDepartement = msg.d.libDepartement;
            currentLocalisation.idRegion = msg.d.idRegion;
            currentLocalisation.libRegion = msg.d.libRegion;

            GetRegion();
        }

        function GetRegion() {
            if (true) {
                $.ajax({
                    type: "POST",
                    url: HostUrlJs + "GlobalWS.asmx/GetRegion",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) { GetRegionResponse(msg) },
                    error: function(xhr) {
                        alert(xhr.responseText);
                    }
                });
            }
        }


        function GetRegionResponse(msg) {
            var cmbxRegion = document.getElementById('autoCompletion_Reg');
            cmbxRegion.length = 0;
            var options = cmbxRegion.options;
            var defaultSelected = currentLocalisation == null || currentLocalisation.idRegion==0;
            var defaultItem = new Option("Toutes les regions", "0", defaultSelected, defaultSelected);
            options[options.length] = defaultItem;

            var bFind = false;
            for (i = 0; i < msg.d.length; i++) { //
                var region = msg.d[i];
                var bSelected = false;
                if (currentLocalisation != null && currentLocalisation.idRegion == region.id) {
                    bSelected = true;
                    bFind = true;
                }
                var newItem = new Option(region.label, region.id, bSelected, bSelected);
                options[options.length] = newItem;
            }
            if (bFind) RegionChanged();
            else {
                currentLocalisation.idRegion = 0;
                currentLocalisation.idDepartement = 0;
                currentLocalisation.idCommune = 0;
                LocalisationChange();
            }
        }

        function RegionChanged() {
            var cmbxRegion = document.getElementById('autoCompletion_Reg');
            var cmbxDepartement = document.getElementById('autoCompletion_Dep');
            var txtInputSearch = document.getElementById('autoCompletion_input');

            var selectOption = cmbxRegion.options[cmbxRegion.selectedIndex];
            currentLocalisation.idRegion = selectOption.value;
            currentLocalisation.libRegion = selectOption.innerHTML;

            
            LocalisationChange();

            if (selectOption.value != "0") {
                cmbxDepartement.style.display = 'block';
                $(document).ready(function() {
                    $.ajax({
                        type: "POST",
                        url: HostUrlJs + "GlobalWS.asmx/SearchDepartement",
                        data: "{idRegion:" + selectOption.value + "}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg) { GetDepartementResponse(msg) },
                        error: function(xhr) {
                            alert(xhr.responseText);
                        }
                    });
                });
            } else {
                cmbxDepartement.style.display = 'none';
            }
            
        }

        function GetDepartementResponse(msg) {
            var cmbxDepartement = document.getElementById('autoCompletion_Dep');
            var txtInputSearch = document.getElementById('autoCompletion_input');
            cmbxDepartement.length = 0;
            
            var options = cmbxDepartement.options;
            var defaultSelected = currentLocalisation == null || currentLocalisation.idDepartement == 0;
            var defaultItem = new Option("Toutes les departement", "0", defaultSelected, defaultSelected);
            options[options.length] = defaultItem;

            var bFind = false;
            for (i = 0; i < msg.d.length; i++) { 
                var region = msg.d[i];
                var bSelected = false;
               
                if (currentLocalisation != null && currentLocalisation.idDepartement == region.id) {
                    bSelected = true;
                    bFind = true;
                }
                var newItem = new Option(region.label, region.id, bSelected, bSelected);
                options[options.length] = newItem;
            }

            if (!bFind) {
                currentLocalisation.idDepartement = 0;
                currentLocalisation.idCommune = 0;
                txtInputSearch.value = "";
            }   
            else {
                // set commune
                if (currentLocalisation.idCommune > 0) {
                    txtInputSearch.value = currentLocalisation.libCommune;
                }
            }
        }

        function DepartementChanged() {

            var cmbxRegion = document.getElementById('autoCompletion_Reg');
            var cmbxDepartement = document.getElementById('autoCompletion_Dep');
            var txtInputSearch = document.getElementById('autoCompletion_input');

            var selectOption = cmbxDepartement.options[cmbxDepartement.selectedIndex];
            currentLocalisation.idDepartement = selectOption.value;
            currentLocalisation.libDepartement = selectOption.innerHTML;
            
            currentLocalisation.idCommune = 0;    
            txtInputSearch.value = "";
            
            LocalisationChange();
        }

        GetLocalisation();
