/*
    #############################################################################
    # Nome: contato.js                                                          #
    # Sistema: Sinodo Paranapanema                                              #
    # Criado por: Breno Henrique Vivarelli                                      #
    # Empresa: Netnigro                                                         #
    # E-mail: breno@netnigro.com.br                                             #
    # Descrição: Validações do formulário de contato                            #
    # Histórico:                                                                #
    #    . 01/06/2008 - Breno Henrique Vivarelli                                #
    #            Criação da Página                                              #
    #############################################################################
*/

    function UploadSubsidio()
    {
        $("#frmUploadSubsidio").ajaxForm(
            {
                success: function(msg)
                {                      
                    alert(msg);
                    //$("#frmUploadSubsidio").resetForm(); 
                }
            }
        );
        //var dados = $("#frmUploadSubsidio").formSerialize();
        /*$.ajax(
        {
            type: "POST",
            url: "upload_subsidio.php",
            data: dados,   
            success: function(msg)
            {                      
                alert(msg);
                //$("#frmUploadSubsidio").resetForm(); 
            }
        }); */
    }
    
    function ValidaFormulario()
    {
        //Nome
        if($("#txtNome").val() == "")
        {
            alert("Favor preencher o campo Nome.");
            $("#txtNome").focus();
            return false;
        }
        
        //E-mail
        if($("#txtEmail").val() == "")
        {
            alert("Favor preencher o campo Email para que seja possível o retorno do contato.");
            $("#txtEmail").focus();
            return false;
        }
        
        //Titulo
        if($("#txtTitulo").val() == "")
        {
            alert("Favor preencher o campo Título do Subsídio.");
            $("#txtTitulo").focus();
            return false;
        }
        
        //Arquivo
        if($("#txtArquivo").val() == "")
        {
            alert("Favor selecionar o arquivo do subsídio.");
            $("#txtArquivo").focus();
            return false;
        }
        
        return true;
    }  
    
    $(function()
    {         
         //Email
         $("#txtEmail").blur(
            function()
            {    
                if(this.value != "")
                {        
                    var retorno = ValidaEmail(this);   
                    if(!retorno)
                    {
                        this.focus();
                        alert("E-mail inválido!");
                    }
                }
            }
         ); 
         
         //SWFUpload
         var swfu;
         
         swfu = new SWFUpload(
            {
                // Backend settings
                upload_url: "../upload_subsidio.php",    // Relative to the SWF file, you can use an absolute URL as well.
                file_post_name: "resume_file",

                // Flash file settings
                file_size_limit : "1024",    // 1 MB
                file_types : "*.doc;*.pdf;*.ppt",    // or you could use something like: "*.doc;*.wpd;*.pdf",
                file_types_description : "Documentos;Arquivos PDF;Arquivos do Powerpoint",
                file_upload_limit : "0", // Even though I only want one file I want the user to be able to try again if an upload fails
                file_queue_limit : "1", // this isn't needed because the upload_limit will automatically place a queue limit

                
                
                // Event handler settings
                swfupload_loaded_handler : swfUploadLoaded,
                
                //file_dialog_start_handler : fileDialogStart,        // I don't need to override this handler
                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,

                
                // Flash Settings
                flash_url : "../lib/swfupload/swfupload_f9.swf",    // Relative to this file

                // UI settings
                swfupload_element_id : "flashUI",        // setting for the graceful degradation plugin
                degraded_element_id : "degradedUI",

                custom_settings : {
                    progress_target : "fsUploadProgress",
                    upload_successful : false
                },
                
                // Debug settings
                debug: false
            }
         );    
         
         $("#btnSelecione").click(
            function()
            {                           
                fileBrowse.apply(swfu);
            }
         );

        
        $("#frmUploadSubsidio").bind("submit", 
            function() 
            { 
                if(ValidaFormulario())
                {
                    UploadSubsidio();
                }
                return false;           
            } 
        );
        
        $("#txtNome").focus();
         
    });