
    Event.observe(window, 'load', function()
	{
        oMunicipality = $("municipality");
        oDivMunicipality = $("divMunicipality");
        
        if(oMunicipality)
        {
            oMunicipality.observe("keydown", function(p_oEvent)
            {
                keyDown = p_oEvent.keyCode;
                if(keyDown == 9 || keyDown == 27) // 9 : TAB, 27 : ESC
                {
                    oDivMunicipality.style.display = 'none';
                    oDivMunicipality.innerHTML = '';    
                }
            });
            oMunicipality.observe("keyup", function(p_oEvent)
            {
                if(keyDown > 46)
                {
                    oDivMunicipality.style.display = 'none';
                    oDivMunicipality.innerHTML = '';
                    if(oMunicipality.value.length >= 3)
                    {
                        retrieveMunicipalities();    
                    }
                }
            });
            oMunicipality.observe("blur", function(p_oEvent)
            {
                /*if(!oDivMunicipality.hasFocus())
                {
                    oDivMunicipality.style.display = 'none';
                    oDivMunicipality.innerHTML = '';
                }*/
            });    
        }
        
        oAutoReturn = $("autoReturn");
        if(oAutoReturn)
        {
            window.setTimeout("window.location = '"+oAutoReturn.value+"'",5000);
        }
        
        oDivAvatar = $("div_avatar");
        if(oDivAvatar)
        {
            displayAvatar(oDivAvatar);
        }
    });
    
    
    function retrieveMunicipalities()
    {
        var sURL = "/getMunicipalities.php?search=" + oMunicipality.value;
        var oRequest = new Ajax.Request(sURL, {onComplete : function(p_oRequest)
        {
            if(p_oRequest.responseText)
            {
                eval("var oResponse = " + p_oRequest.responseText);
                var oElement;
                var iIndex;
                oDivMunicipality.innerHTML = '';
                for(iIndex = 0; iIndex < oResponse.length; ++iIndex)
                {
                    oElement = oResponse[iIndex];
                    
                    var oBox = document.createElement('div');
                    oBox.className = 'container';
                    oBox.id = "row_"+iIndex;
                    oBox.onclick = function()
                    {
                        selectMunicipality(this);
                    };
                    
                    var oTop = document.createElement('div');
                    var oBottom = document.createElement('div');                
                    
                    if(oElement.label.id != undefined)
                    {
                    	Localization.setTranslation(oElement.label.id, oElement.label.translations['fr_FR']);
                    	var sMunicipality = Localization.getTranslation(oElement.label.id);
                    }
                 	else
                  	{
                  		var sMunicipality = oElement.label;
                  	}
                  	
                  	if(oElement.area.id != undefined)
                    {
                    	Localization.setTranslation(oElement.area.id, oElement.area.translations['fr_FR']);
                    	var sArea = Localization.getTranslation(oElement.area.id);
                    }
                 	else
                  	{
                  		var sArea = oElement.area;
                  	}
                  	
                  	if(oElement.countryDivision.id != undefined)
                    {
                    	Localization.setTranslation(oElement.countryDivision.id, oElement.countryDivision.translations['fr_FR']);
                    	var sCountryDivision = Localization.getTranslation(oElement.countryDivision.id);
                    }
                 	else
                  	{
                  		var sCountryDivision = oElement.countryDivision;
                  	}
                  	
                  	if(oElement.country.id != undefined)
                    {
                    	Localization.setTranslation(oElement.country.id, oElement.country.translations['fr_FR']);
                    	var sCountry = Localization.getTranslation(oElement.country.id);
                    }
                 	else
                  	{
                  		var sCountry = oElement.country;
                  	}
                  	
                  	var oSpan_1 = document.createElement('span');
                  	oSpan_1.appendChild(document.createTextNode(sMunicipality));
                  	oSpan_1.className = 'municipality';
                  	oSpan_1.id = 'municipality_'+oElement.id;
                  	oTop.appendChild(oSpan_1);
                   	
                  	if(sArea != '')
                  	{
                  	    var oSpan_2 = document.createElement('span');
                  	    oSpan_2.appendChild(document.createTextNode(', '));
                   	    oTop.appendChild(oSpan_2);
                  	    
                  	    var oSpan_3 = document.createElement('span');
                  	    oSpan_3.appendChild(document.createTextNode(sArea));
                  	    oSpan_3.className = 'area';
                  	    oSpan_3.id = 'area_'+oElement.idArea;
                  	    oTop.appendChild(oSpan_3);
                  	}
                  	
                  	var oSpan_4 = document.createElement('span');
                  	oSpan_4.appendChild(document.createTextNode(sCountryDivision));
                  	oSpan_4.className = 'countryDivision';
                  	oSpan_4.id = 'countryDivision_'+oElement.idCountryDivision;
                  	oBottom.appendChild(oSpan_4);
                   	
              	    var oSpan_5 = document.createElement('span');
              	    oSpan_5.appendChild(document.createTextNode(', '));
               	    oBottom.appendChild(oSpan_5);
              	    
              	    var oSpan_6 = document.createElement('span');
              	    oSpan_6.appendChild(document.createTextNode(sCountry));
              	    oSpan_6.className = 'country';
              	    oSpan_6.id = 'country_'+oElement.idCountry;
              	    oBottom.appendChild(oSpan_6);
                  	
                    oBox.appendChild(oTop);
                    oBox.appendChild(oBottom);
                    
                    oDivMunicipality.appendChild(oBox);
                }
            }
        }, onException : function(p_oRequest, p_oException)
        {
            throw p_oException;
        },
        onLoaded : function()
        {
            oDivMunicipality.style.display = '';
        }});
    }
    
    function selectMunicipality(oElement)
    {
        if(oElement)
        {
            sMunicipality = '';
            sArea = '';
            sCountryDivision = '';
            sCountry = '';
            
            idMunicipality = 0;
            idArea = 0;
            idCountryDivision = 0;
            idCountry = 0;
            for(iChild = 0; iChild < oElement.childNodes.length; iChild ++)
            {
                oDiv = oElement.childNodes[iChild];
                for(iDiv = 0; iDiv < oDiv.childNodes.length; iDiv ++)
                {
                    oSpan = oDiv.childNodes[iDiv];
                    if(oSpan.className == 'municipality')
                    {
                        sMunicipality = oSpan.innerHTML;
                        arrMunicipality  = oSpan.id.split('_');
                        idMunicipality = arrMunicipality[1];
                    }
                    else if(oSpan.className == 'area')
                    {
                        sArea = oSpan.innerHTML;
                        arrArea  = oSpan.id.split('_');
                        idArea = arrArea[1];
                    }
                    else if(oSpan.className == 'countryDivision')
                    {
                        sCountryDivision = oSpan.innerHTML;
                        arrCountryDivision  = oSpan.id.split('_');
                        idCountryDivision = arrCountryDivision[1];
                    }
                    else if(oSpan.className == 'country')
                    {
                        sCountry = oSpan.innerHTML;
                        arrCountry  = oSpan.id.split('_');
                        idCountry = arrCountry[1];
                    }
                }                    
            }
            
            document.forms["surveyForm"].municipality.value = sMunicipality;
            //document.forms["surveyForm"].area.value = sArea;
            document.forms["surveyForm"].countryDivision.value = sCountryDivision;
            document.forms["surveyForm"].country.value = sCountry;
            
            document.forms["surveyForm"].idMunicipality.value = idMunicipality;
            //document.forms["surveyForm"].idArea.value = idArea;
            document.forms["surveyForm"].idCountryDivision.value = idCountryDivision;
            document.forms["surveyForm"].idCountry.value = idCountry;
            
           oDivMunicipality.innerHTML = '';
           oDivMunicipality.style.display = 'none'; 
        }        
    }