    var swfupload_questions = new Array();
    var swfupload_entities = new Array();
    
    function applyForm(p_ToDisplay, p_mode) {
        var oForm = document.forms["customSurveyForm"];
        
        if(!p_ToDisplay) {
            p_ToDisplay = '';
        }
        
        if(!p_mode) {
            p_mode = '';
        }
        
        if(p_mode == 'group' || p_mode == '') {
            if(oGroupToDisplay = oForm['groupToDisplay']) {
                oGroupToDisplay.value = p_ToDisplay;
            }
        }
        
        if(p_mode == 'question' || p_mode == '') {
            if(oQuestionToDisplay = oForm['questionToDisplay']) {
                oQuestionToDisplay.value = p_ToDisplay;
            }
        }
        
        if(isValidForm(oForm)) {
            if(swfupload_entities.length > 0) {
                doSubmit();
            }
            else {
                submitForm();    
            }
        }
    }
    
    function validateForm(oForm, bValid) {
        if(swfupload_questions.length > 0) {
            for(swfupload_loop = 0; swfupload_loop < swfupload_questions.length; swfupload_loop ++) {
                var swfupload_question = swfupload_questions[swfupload_loop];
                
                if(oRequired = document.getElementById('required_csQuestion_'+swfupload_question)) {
                    var oHidden = oForm['csQuestion_'+swfupload_question];
                    var oInput = oForm['txtFileName_'+swfupload_question];
                    if(isEmpty(oInput.value)) {
                        writeMessage(oHidden.name, "form.requiredField.message");
                        bValid = false;
                        swicthClassName(oHidden,true);    
                    }
                    else {
                        swicthClassName(oHidden,false);    
                    }
                }
            }
        }
        if(oElements = document.getElementsByName('article_csQuestion[]')) {
            if(oElements.length > 0) {
                var bArticle = 0;
                for(iElement = 0; iElement < oElements.length; iElement ++) {
                    var oElement = oElements[iElement];
                    var idElement = oElement.value;
                    var oField = oForm["csQuestion_"+idElement];
                    if(oField.value > 0) {
                        bArticle ++;    
                    }
                }
                if(bArticle == 0) {
                    writeMessage("order", "page.order.invalid");
                    bValid = false;
                }
            }
        }
        
        return bValid;
    }
    
    function submitForm() {
        var oForm = document.forms["customSurveyForm"];
        oForm.action = "/saveSondage_cm.php";
        oForm.submit();    
    }
    
    function resetForm() {
        if(confirm(Localization.getTranslation("form.command.reset.confirm"))) {
            if(oDiv = document.getElementById('divReset')) {
                var oHidden = createNamedElement('input', 'resetCustomSurvey');
                oHidden.id = 'resetCustomSurvey';
                oHidden.type = 'hidden';
                oHidden.value = 1;
                oDiv.appendChild(oHidden);
                
                var oForm = document.forms["customSurveyForm"];
                oForm.action = "/saveSondage_cm.php";
                oForm.submit();
            }
        }
    }
    
    function clickSelectAll(oLabel) {
        var oMultiple = oLabel.parentNode.parentNode;
        var nbInput = 0;
        var nbChecked = 0;
        
        for(iDiv = 0; iDiv < oMultiple.childNodes.length; iDiv ++) {
            var oDiv = oMultiple.childNodes[iDiv];
            if(oDiv.style.display != 'none') {
                var oInput = oDiv.firstChild;
                if(oInput.checked) {
                    nbChecked ++;
                }
                nbInput ++;
            }
        }
        
        for(iDiv = 0; iDiv < oMultiple.childNodes.length; iDiv ++) {
            var oDiv = oMultiple.childNodes[iDiv];
            if(oDiv.style.display != 'none') {
                var oInput = oDiv.firstChild;
                if(oInput.checked && nbInput == nbChecked) {
                    oInput.checked = false;
                    if(sAttribute = oInput.getAttribute('onclick')) {
                        if(sAttribute.indexOf('displayMultipleDependencies') != -1) {
                            displayMultipleDependencies(oInput.name);    
                        }
                    }
                }
                else {
                    oInput.checked = true;
                    if(sAttribute = oInput.getAttribute('onclick')) {
                        if(sAttribute.indexOf('displayMultipleDependencies') != -1) {
                            displayMultipleDependencies(oInput.name);    
                        }
                    }
                }
            }
        }
    }
    
    function selectInOptions(oSelect, v_question) {
        var oForm = document.forms["customSurveyForm"];
        var o_search = oForm["search_csQuestion_"+v_question];
        o_search.value = oSelect.options[oSelect.selectedIndex].text;   
    }
    
    function searchInOptions(v_question) {
        var oForm = document.forms["customSurveyForm"];
        var v_keybookAccount = oForm["idKeybookAccount"].value;
        var v_search = oForm["search_csQuestion_"+v_question].value;
        var oSelect = oForm["csQuestion_"+v_question];
        var oSpan = document.getElementById("loading_csQuestion_"+v_question);
        oSpan.innerHTML = '';
        
        var oImage = document.createElement('img');
        oImage.src = '/resources/images/ajax-loader.gif';
        oSpan.appendChild(oImage);
        
        var sURL = "/getQuestionMultiples.php";
        var oRequest = new Ajax.Request(sURL, { asynchronous: true,
            parameters: {search: v_search, idKeybookAccount: v_keybookAccount, idQuestion: v_question},
            onComplete : function(p_oRequest)
            {
                if(p_oRequest.responseText)
                {
                    select_clearOptions(oSelect);
                    
                    eval("var oResponse = " + p_oRequest.responseText);
                    var oElement;
                    var iIndex;
                    var iOption = 0;
                    
                    oSelect.options[iOption] = new Option(Localization.getTranslation('page.select.choose'), 0);
                    iOption ++;
                    for(iIndex = 0; iIndex < oResponse.length; ++iIndex)
                    {
                        oElement = oResponse[iIndex];
                        
                        oSelect.options[iOption] = new Option(oElement.label, oElement.id);
                        iOption ++;
                    }
                }
                
                oSpan.innerHTML = '';
            }, onException : function(p_oRequest, p_oException)
            {
                throw p_oException;
            },
            onLoaded : function()
            {
                
            }
        });
    }
    
    // Dependencies
    {
        function displayDependencies(p_name, p_value)
        {
            if(document.forms.customSurveyForm)
            {
                if(oDependencies = document.forms.customSurveyForm['dependency_'+p_name])
                {
                    var strDependencies = oDependencies.value;
                    var arrDependencies = strDependencies.split(';');
                    
                    var arrVerified = new Array();
                    var arrCount = new Array();
                    
                    for(iDependency = 0; iDependency < arrDependencies.length; iDependency ++)
                    {
                        var strDependency = arrDependencies[iDependency];
                        var arrDependency = strDependency.split(':');
                        var dependencyValue = arrDependency[0];
                        var dependencyFields = arrDependency[1];
                        var arrFields = dependencyFields.split(',');
                        
                        for(iField = 0; iField < arrFields.length; iField ++)
                        {
                            var fieldName = arrFields[iField];
                            if(!isEmpty(fieldName))
                            {
                                if(!arrVerified.in_array(fieldName))
                                {
                                    arrVerified[arrVerified.length] = fieldName;
                                    arrCount[fieldName] = 0;
                                }
                                
                                if(p_value == dependencyValue)
                                {
                                    if(oRow = document.getElementById('container_'+fieldName))
                                    {
                                        arrCount[fieldName] ++;            
                                    }
                                }
                            }
                        }
                        
                        for(iVerified = 0; iVerified < arrVerified.length; iVerified ++)
                        {
                            var fieldName = arrVerified[iVerified];  
                            var countValid = arrCount[fieldName];
                            
                            if(countValid > 0)
                            {
                                if(oRow = document.getElementById('container_'+fieldName))
                                {
                                    oRow.style.display = '';    
                                }
                            }
                            else
                            {
                                if(oRow = document.getElementById('container_'+fieldName))
                                {
                                    oRow.style.display = 'none';    
                                }
                            }
                        }
                    }
                }
                
                if(oDependencies = document.forms.customSurveyForm['dependencyOption_'+p_name])
                {
                    var strDependencies = oDependencies.value;
                    var arrDependencies = strDependencies.split(';');
                    
                    var arrVerified = new Array();
                    var arrCount = new Array();
                    
                    for(iDependency = 0; iDependency < arrDependencies.length; iDependency ++)
                    {
                        var strDependency = arrDependencies[iDependency];
                        var arrDependency = strDependency.split(':');
                        var dependencyValue = arrDependency[0];
                        var dependencyFields = arrDependency[1];        
                        
                        var arrOptions = dependencyFields.split(',');
                        for(iOption = 0; iOption < arrOptions.length; iOption ++)
                        {
                            var strOption = arrOptions[iOption];
                            var arrSplited = strOption.split('~');
                            if(arrSplited.length > 0)
                            {
                                var fieldName = strOption;
                                if(!isEmpty(fieldName))
                                {
                                    if(!arrVerified.in_array(fieldName))
                                    {
                                        arrVerified[arrVerified.length] = fieldName;
                                        arrCount[fieldName] = 0;
                                    }
                                    
                                    if(p_value == dependencyValue)
                                    {
                                        arrCount[fieldName] ++;
                                    }
                                }   
                            }
                        }
                        
                        for(iVerified = 0; iVerified < arrVerified.length; iVerified ++)
                        {
                            var strOptionName = arrVerified[iVerified]; 
                            var arrOptionName = strOptionName.split('~');
                            var fieldName = arrOptionName[0];
                            var fieldValue = arrOptionName[1];
                             
                            var countValid = arrCount[strOptionName];
                            if(fieldName.charAt(fieldName.length-1) == ']')
                            {
                                var arrMultiple = document.getElementsByName(fieldName);
                                for(iMultiple = 0; iMultiple < arrMultiple.length; iMultiple ++)
                                {
                                    var oMultiple = arrMultiple[iMultiple];
                                    if(oMultiple.value == fieldValue)
                                    {
                                        if(countValid > 0)
                                        {
                                            if(oDiv = oMultiple.parentNode)
                                            {
                                                oDiv.style.display = '';    
                                            }
                                        }
                                        else
                                        {
                                            if(oDiv = oMultiple.parentNode)
                                            {
                                                oDiv.style.display = 'none';    
                                            }
                                        }    
                                    }    
                                }
                            }
                        }
                    }
                }
            }
        }
        
        function displayMultipleDependencies(p_name)
        {
            if(document.forms.customSurveyForm)
            {
                var strDependencies = document.forms.customSurveyForm['dependency_'+p_name].value;
                var arrDependencies = strDependencies.split(';');
                
                var arrVerified = new Array();
                var arrCount = new Array();
                
                for(iDependency = 0; iDependency < arrDependencies.length; iDependency ++)
                {
                    var strDependency = arrDependencies[iDependency];
                    var arrDependency = strDependency.split(':');
                    var dependencyValue = arrDependency[0];
                    var dependencyFields = arrDependency[1];
                    var arrFields = dependencyFields.split(',');
                    
                    for(iField = 0; iField < arrFields.length; iField ++)
                    {
                        var fieldName = arrFields[iField];
                        if(!isEmpty(fieldName))
                        {
                            if(!arrVerified.in_array(fieldName))
                            {
                                arrVerified[arrVerified.length] = fieldName;
                                arrCount[fieldName] = 0;
                            }
                            
                            if(arrCheckBox = document.getElementsByName(p_name))
                            {
                                for(iCheckBox = 0; iCheckBox < arrCheckBox.length; iCheckBox ++)
                                {
                                    if(arrCheckBox[iCheckBox].checked)
                                    {
                                        if(arrCheckBox[iCheckBox].value == dependencyValue)
                                        {
                                            arrCount[fieldName] ++;        
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                            
                for(iVerified = 0; iVerified < arrVerified.length; iVerified ++)
                {
                    var fieldName = arrVerified[iVerified];  
                    var countValid = arrCount[fieldName];
                    
                    if(countValid > 0)
                    {
                        if(oRow = document.getElementById('container_'+fieldName))
                        {
                            oRow.style.display = '';    
                        }
                    }
                    else
                    {
                        if(oRow = document.getElementById('container_'+fieldName))
                        {
                            oRow.style.display = 'none';    
                        }
                    }
                }
            }    
        }
    }
    
    // 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(!isEmpty(oForm.avatar.value)) 
        {
            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 onQuestionDateChanged(p_cfName) {
        var oForm = document.forms["customSurveyForm"];
        var oYear = oForm[p_cfName+"_year"];
        var oMonth = oForm[p_cfName+"_month"];
        var oDay = oForm[p_cfName+"_day"];
        var oField = oForm[p_cfName];
        
        oField.value = (oYear ? str_pad(oYear.value, 4, '0', 'STR_PAD_LEFT') : '0000')+'-'+(oMonth ? str_pad(oMonth.value, 2, '0', 'STR_PAD_LEFT') : '00')+'-'+(oDay ? str_pad(oDay.value, 2, '0', 'STR_PAD_LEFT') : '00');
    }
    
    function onBirthDateChanged(p_cfName, p_day) {
        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  = 0;
        if(oBirthYear) {
            if(oBirthYear.selectedIndex != -1) {
                selectedYear = oBirthYear.options[oBirthYear.selectedIndex].value;
            }
        }
        var selectedMonth = 0;
        if(oBirthMonth.selectedIndex != -1) {
            selectedMonth = oBirthMonth.options[oBirthMonth.selectedIndex].value;
        }
        var selectedDay   = oBirthDay.selectedIndex;
        
        // 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);
                    
                    if(p_day == iDay) {
                        selectedDay = iDay;
                    }
                }
                oBirthDay.selectedIndex = selectedDay;
            }
        }
        
        onQuestionDateChanged(p_cfName);
    }
    
    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(idCountryDivision, sCountryDivision, idCountry, sCountry);
            
           oDivMunicipality.innerHTML = '';
           oDivMunicipality.style.display = 'none'; 
        }        
    }
    
    function setMunicipalityParent(idCountryDivision, sCountryDivision, idCountry, 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)   
                {
                    if(document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].nodeName.toLowerCase() == 'input')
                    {
                        document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].value = sCountry;
                    }
                    else
                    {
                        sIndex = findIndexOfOption(document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')], idCountry);
                        document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].selectedIndex = sIndex;
                        document.forms.customSurveyForm[oElement.id.replace('fieldName', 'valueOf_csQuestion')].value = sCountry;
                    }
                }
                else if(oElement.value == 'countryDivisionName' && oElement.id.indexOf('fieldName') == 0)   
                {
                    if(document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].nodeName.toLowerCase() == 'input')
                    {
                        document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].value = sCountryDivision;
                    }
                    else
                    {
                        sIndex = findIndexOfOption(document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')], idCountryDivision);
                        document.forms.customSurveyForm[oElement.id.replace('fieldName', 'csQuestion')].selectedIndex = sIndex;
                        document.forms.customSurveyForm[oElement.id.replace('fieldName', 'valueOf_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);
                    }
                }    
            } 
        }   
    }
    
    function changeCountry(oSelect) {
        document.forms.customSurveyForm['valueOf_'+oSelect.id].value = oSelect.options[oSelect.selectedIndex].text;
    }
    
    function changeCountryDivision(oSelect) {
        document.forms.customSurveyForm['valueOf_'+oSelect.id].value = oSelect.options[oSelect.selectedIndex].text;
    }
    
    function calculateSummary() {
        var oForm = document.forms.customSurveyForm;
        var nSubtotal = 0;
        
        for(iElement = 0; iElement < oForm.elements.length; iElement ++) {
            var oElement = oForm.elements[iElement];
            if(oElement.id) {
                if(oElement.id.indexOf('csQuestion_') == 0) {
                    if(oPrice = oForm['priceOf_'+oElement.id]) {
                        if((isInteger(oPrice.value) || isFloat(oPrice.value)) && (isInteger(oElement.value) || isFloat(oElement.value))) {
                            nSubtotal += Number(oPrice.value) * Number(oElement.value);
                        }
                    }
                }
            }    
        }
        
        var nTax1 = 0;  //(nSubtotal * KeybookConfiguration.getTps());
        var nTax2 = 0;  //((parseFloat(nSubtotal) + parseFloat(nTax1)) * KeybookConfiguration.getTvq());
        var nTotal = (parseFloat(nSubtotal) + parseFloat(nTax1) + parseFloat(nTax2));
        
        oForm.csSubtotal.value = parseFloat(nSubtotal).toFixed(2)+'$';
        oForm.csTax1.value = parseFloat(nTax1).toFixed(2)+'$';
        oForm.csTax2.value = parseFloat(nTax2).toFixed(2)+'$';
        oForm.csTotal.value = parseFloat(nTotal).toFixed(2)+'$';
    }
    
    // Draw
    {
        function drawQuestion(oInput)
        {
            var oForm = document.forms.customSurveyForm;
            if(isValidForm(oForm)) 
            {
                lockForm(oForm);
                var nId = oInput.id.replace('btnDraw_','');
                if(arr3[nId] == arr1[nId]) {
                    alert(Localization.getTranslation('message.draw.maxAttemptReach'));
                    applyForm();
                }
                else {
                    if(arr3[nId] == 0) {
                        var n2 = Math.floor(Math.random() * 101);
                        if(n2 <= Number(arr2[nId])) {
                            alert(Localization.getTranslation('message.draw.winner'));
                            arr4[nId] = 1;
                            oForm["valueOf_csQuestion_"+nId].value = 1;
                        }
                        else {
                            alert(Localization.getTranslation('message.draw.loser'));
                            if(arr4[nId] != 1) {
                                oForm["valueOf_csQuestion_"+nId].value = 0;
                            }
                        }
                    }
                    else {
                        alert(Localization.getTranslation('message.draw.additionalChance'));
                    }
                    arr3[nId] ++;
                    oForm["try_csQuestion_"+nId].value = arr3[nId];
                    oForm["csQuestion_"+nId].value = arr5[nId];
                    if(arr3[nId] == arr1[nId]) {
                        applyForm();
                    }
                }
            }
        }
        
        function lockForm(oForm)
        {
            for(iInput = 0; iInput < oForm.elements.length; iInput ++)
            {
                if(oForm.elements[iInput].type != "hidden" && oForm.elements[iInput].type != "button" && oForm.elements[iInput].type != "submit" && oForm.elements[iInput].nodeName.toLowerCase() != "fieldset" && oForm.elements[iInput].disabled == false)
                {
                    oInput = oForm.elements[iInput];
                    if(oInput.id)
                    {
                        if(oInput.id.indexOf('csQuestion_') != -1)
                        {
                            var sTableName = oInput.id.replace('csQuestion', 'tableName');
                            var oTableName = oForm[sTableName];
                            if(oTableName)
                            {
                                if(!isEmpty(oTableName.value))
                                {
                                    if(oInput.nodeName.toLowerCase() == "textarea")
                                    {
                                        oInput.setAttribute('readonly',true);    
                                    }
                                    
                                    if(oInput.type == "text")
                                    {
                                        oInput.setAttribute('readonly',true);    
                                    }
                                }
                            }
                        }
                    }
                }
            }        
        }           
    }
    
    function addMultiplePromoCode(v_question) {
        var oForm = document.forms["customSurveyForm"];
        var oCount = oForm["promoCodeCount_"+v_question];
        
        if(oInput = oForm["csQuestion_"+v_question]) {
            var oDiv = oInput.parentNode;
            
            var nCount = oCount.value;
            nCount = Number(nCount) + 1;
            
            var nInput = oInput.cloneNode(false);
            nInput.id = nInput.id + '_' + nCount;
            nInput.name = nInput.name + '_' + nCount;
            nInput.value = '';
            
            var nDiv = document.createElement('div');
            nDiv.className = 'customSurvey_question_multiplePromoCode';
            nDiv.style.marginTop = '5px';
            nDiv.appendChild(nInput);
            
            oDiv.parentNode.insertBefore(nDiv, oDiv.nextSibling);
            
            oCount.value = nCount;
        }
    }

