
    Event.observe(window, 'load', function()
	{

        oAutoReturn = $("autoReturn");
        if(oAutoReturn)
        {
            window.setTimeout("window.location = '"+oAutoReturn.value+"'",5000);
        }
        
        oDivAvatar = $("div_avatar");
        if(oDivAvatar)
        {
            displayAvatar(oDivAvatar);
        }
        
        initMunicipalityControl();
        
        if(oMunicipality)
        {
            municipalityKeyDown = 0;
            
            oMunicipality.observe("keydown", function(p_oEvent)
            {
                municipalityKeyDown = p_oEvent.keyCode;
                oKeyEvent = new KeyEvent(KeyEvent.Type.PRESSED, p_oEvent);
                if(oKeyEvent.getCode() == KeyEvent.UP) {
                    if(oRow = document.getElementById('row_municipality_'+municipalityIndex)) {
                        oRow.className = 'container';
                    }
                    municipalityIndex --;
                    if(municipalityIndex < 0) {
                        municipalityIndex = (municipalityLength-1);
                    }
                    if(oRow = document.getElementById('row_municipality_'+municipalityIndex)) {
                        oRow.className = 'container isHover';
                    }
                }
                if(oKeyEvent.getCode() == KeyEvent.DOWN) {
                    if(oRow = document.getElementById('row_municipality_'+municipalityIndex)) {
                        oRow.className = 'container';
                    }
                    municipalityIndex ++;    
                    if(municipalityIndex > (municipalityLength-1)) {
                        municipalityIndex = 0;
                    }
                    if(oRow = document.getElementById('row_municipality_'+municipalityIndex)) {
                        oRow.className = 'container isHover';                        
                    }
                }
                if(oKeyEvent.getCode() == KeyEvent.ENTER) {
                    if(municipalityIndex > -1) {
                        if(oRow = document.getElementById('row_municipality_'+municipalityIndex)) {
                            selectMunicipality(oRow);
                            municipalityIndex = -1;
                        }                       
                    }
                }
                if(oKeyEvent.getCode() == KeyEvent.TAB || oKeyEvent.getCode() == KeyEvent.ESCAPE) {
                    oDivMunicipality.style.display = 'none';
                    oDivMunicipality.innerHTML = '';
                }
            });
            oMunicipality.observe("keyup", function(p_oEvent)
            {
                if(municipalityKeyDown > 46)
                {
                    oDivMunicipality.style.display = 'none';
                    oDivMunicipality.innerHTML = '';
                    if(oMunicipality.value.length >= 3)
                    {
                        retrieveMunicipalities();    
                    }
                }
            });
            oMunicipality.observe("change", function(p_oEvent)
            {
                document.forms.customSurveyForm["valueOf_"+oMunicipality.name].value = '';
            });
        }
        
        for(swfupload_loop = 0; swfupload_loop < swfupload_questions.length; swfupload_loop ++)
        {
            var swfQuestion = swfupload_questions[swfupload_loop];
            
            swfupload_entities[swfupload_loop] = new SWFUpload({
				// Backend settings
				upload_url: "/uploadSondage_cm.php",
				file_post_name: "Filedata",
				post_params: {"idKeybookAccount" :document.forms.customSurveyForm.idKeybookAccount.value, "idUser" :document.forms.customSurveyForm.idUser.value, "idCustomSurvey" :document.forms.customSurveyForm.idCustomSurvey.value},

				// Flash file settings
				file_size_limit : "5 MB",
				file_types : "*.*",			// or you could use something like: "*.doc;*.wpd;*.pdf",
				file_types_description : "All Files",
				file_upload_limit : "0",
				file_queue_limit : "1",

				// Event handler settings
				//swfupload_loaded_handler : swfUploadLoaded,
				
				file_dialog_start_handler: fileDialogStart,
				file_queued_handler : fileQueued,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				
				//upload_start_handler : uploadStart,	// I could do some client/JavaScript validation here, but I don't need to.
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete,

				// Button Settings
				button_image_url : "/resources/images/swfupload/XPButtonUploadText_61x22.png",
				button_placeholder_id : "spanButtonPlaceholder_"+swfQuestion,
				button_width: 61,
				button_height: 22,
				
				// Flash Settings
				flash_url : "/resources/applications/swfupload/swfupload.swf",

				custom_settings : {
					idQuestion : swfQuestion,
					progress_target : "fsUploadProgress_"+swfQuestion,
					upload_successful : false
				},
				
				// Debug settings
				debug: false
			});
        }       
    });

