
    function applyForm() {
        var oForm = document.forms["customSurveyForm"];
         if(isValidForm(oForm)) {
            oForm.action = "/saveSondage_cm.php";
            oForm.submit();
         }
         else {
             scroll(0,0);
         }
    }
    
    // Rafraichir la page (1 fois) si l'une des questions a «N/A» comme «type de question»
    function doNaCheck(p_questionType) {
        var oForm = document.forms["customSurveyForm"];
        
        var v_keybookAccount = oForm.idKeybookAccount.value;
        var v_user = oForm.idUser.value;
        var v_customSurvey = oForm.idCustomSurvey.value;
        var v_naCheckValue = oForm.naCheckValue.value;
        var v_httpReferer = oForm.httpReferer.value;
        
        var v_label = "";
        if(oLabel = document.getElementById('label_csQuestion_'+v_naCheckValue))
        {
            v_label = oLabel.innerHTML;   
        }        
        
        var sURL = "/ajax-naCheckEmail.php";
        var oRequest = new Ajax.Request(sURL, { asynchronous: false,
            parameters: {label: v_label, idKeybookAccount: v_keybookAccount, idUser: v_user, idCustomSurvey: v_customSurvey, selfUrl: window.location.href, questionType: p_questionType, httpReferer: v_httpReferer},
            onComplete : function(p_oRequest)
            {
                go(setURLParameter(window.location.href, "naCheck", 1));        
            }, onException : function(p_oRequest, p_oException)
            {
                throw p_oException;
            },
            onLoaded : function()
            {
            }
        });
    }
    
    function displayAvatar(oDivAvatar) {
        var oForm = document.forms['customSurveyForm'];
        
        if(document.all)
        {
            pWidth = document.documentElement.clientWidth;
        }
        else
        {
            pWidth = window.innerWidth;
        }
        
        var sSource = '/getImage.php?path='+oForm.avatar.value+'&canvasHeight=200&background='+oForm.pageColor.value+'&centerd=1';
        if(pWidth < 800)
        {
            sSource = sSource + '&canvasWidth='+(pWidth - 100);
        }
        
        var oImage = document.createElement('img');
        oImage.src = sSource;
        
        oDivAvatar.appendChild(oImage);
        
        oPage = $("laPage");
        if(oPage)
        {
            if(pWidth < 800)
            {
                oPage.style.width = '100%';
            }
        }
    }
    
    function onBirthDateChanged(p_cfName) {
        var oForm             = document.forms["customSurveyForm"];
        var oBirthYear        = oForm[p_cfName+"_year"];
        var oBirthMonth       = oForm[p_cfName+"_month"];
        var oBirthDay         = oForm[p_cfName+"_day"];
        
        var selectedYear  = oBirthYear.options[oBirthYear.selectedIndex].value;
        var selectedMonth = oBirthMonth.options[oBirthMonth.selectedIndex].value;
        var selectedDay   = oBirthDay.options[oBirthDay.selectedIndex].value;
        
        // prepare an array of days of each month
        var arrDay = Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
        
        // if the selected month is february...
        if(selectedMonth == 2) {
            // if the chosen birth year is not empty, and is bissectile...
            if(selectedYear > 0) {
                if(selectedYear % 4 == 0) {
                    // then february has 29 days that year
                    arrDay[2] = 29;
                }    
            }
        }
        
        // clear the options from the birthDay select element
        select_clearOptions(p_cfName+"_day");
        
        // add a default option for the birthDay select element
        oBirthDay.options[oBirthDay.options.length] = new Option(Localization.getTranslation("page.day"), 0);
        
        // build options for each available day of the selected month
        if(selectedMonth > 0) {
            var daysThatMonth = arrDay[selectedMonth];
            
            if(daysThatMonth > 0) {
                for(var iDay = 1; iDay <= daysThatMonth; iDay += 1) {
                    oBirthDay.options[oBirthDay.options.length] = new Option(iDay, iDay);
                }
            }
        }
    }
    
    function onQuestionGroupClicked(idQuestionGroup) {
        var oDivChildren = document.getElementById("customSurvey_questionGroup_" + idQuestionGroup + "_children");
        var oImg         = document.getElementById("img_customSurvey_questionGroup_" + idQuestionGroup + "_retract");
        if(oDivChildren && oImg) {
            if(oDivChildren.style.display != "none") {
                oDivChildren.style.display = "none";
                oImg.src = "/resources/images/menu/openMenu.gif";
            }
            else {
                oDivChildren.style.display = "";
                oImg.src = "/resources/images/menu/closeMenu.gif";
            }
        }
    }
    
    
    var oDivMunicipality = null;
    var oMunicipality = null;
    var municipalityKeyDown = 0;
    var municipalityIndex = -1;
    var municipalityLength = 0;
    
    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 = '';
                
                municipalityLength = oResponse.length;
                for(iIndex = 0; iIndex < oResponse.length; ++iIndex)
                {
                    oElement = oResponse[iIndex];
                    
                    var oBox = document.createElement('div');
                    oBox.className = 'container';
                    oBox.id = "row_municipality_"+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];
                    }
                }                    
            }
            
            oMunicipality.value = sMunicipality;
            document.forms.customSurveyForm["valueOf_"+oMunicipality.name].value = idMunicipality;
            
            setMunicipalityParent(sCountryDivision, sCountry);
            
           oDivMunicipality.innerHTML = '';
           oDivMunicipality.style.display = 'none'; 
        }        
    }
    
    function setMunicipalityParent(sCountryDivision, sCountry)
    {
        for(iElement = 0; iElement < document.forms.customSurveyForm.elements.length; iElement ++)
        {
            oElement = document.forms.customSurveyForm.elements[iElement];
            if(oElement.id)
            {
                if(oElement.value == 'countryName' && oElement.id.indexOf('fieldName') == 0)   
                {
                    document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].value = sCountry;
                }
                else if(oElement.value == 'countryDivisionName' && oElement.id.indexOf('fieldName') == 0)   
                {
                    document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].value = sCountryDivision;
                }
            }    
        }
    }
    
    function initMunicipalityControl()
    {
        if(document.forms.customSurveyForm)
        {
            for(iElement = 0; iElement < document.forms.customSurveyForm.elements.length; iElement ++)
            {
                oElement = document.forms.customSurveyForm.elements[iElement];
                if(oElement.id)
                {
                    if(oElement.value == 'municipalityName' && oElement.id.indexOf('fieldName') == 0)   
                    {
                        oMunicipality = $(oElement.id.replace('fieldName', 'csQuestion'));
                        oDivMunicipality = $('divOf_'+oMunicipality.id);
                    }
                }    
            } 
        }   
    }
