// JavaScript Document
jQuery.noConflict();
jQuery(function (){

    jQuery(".toggleOutros").change(function() {
        vValor = jQuery(this).val();
        vCampo = jQuery(this).attr("id");
        vCampoOutro = "#" + vCampo + "_outros";
        jQuery(vCampoOutro).removeAttr("readonly");

        if(vValor != "Outros") {
            jQuery(vCampoOutro).attr("readonly", "readonly");
            jQuery(vCampoOutro).val("");
        } else {
            jQuery(vCampoOutro).focus();
        }

    });

    jQuery(".cep_mask").mask("99999-999");
    
    /* envio da enquete */
    jQuery("form[title='frm_enquete']").submit(function() {
        jQuery("#loading_enquete").show();

        var options = {
            success: function(msg) {
                // sucesso no envio
                if(jQuery.trim(msg) == "") {
                    alert('Voto registrado com sucesso. Obrigado!');
                    jQuery(this).resetForm();
                    jQuery.get('/ajax_processa.php', {
                        op: "enqueteResultado",
                        enquete: jQuery('#q_id').val()
                    }, function(data) {
                        jQuery('#textoenquetes').html(data);
                    });

                } else {
                    alert(jQuery.trim(msg));
                }

                jQuery("#loading_enquete").hide("slow");
            }
        };

        jQuery(this).ajaxSubmit(options);

        return false; // faz o submit normal
    });

    /* watermark */
    jQuery("#txtMatricula").watermark("Matrícula");
    jQuery("#txtSenha").watermark("Senha");
    jQuery("#senhaConfirmar").watermark("Confirme a nova senha");
    
    jQuery("#flash ul").cycle({
        fx: 'curtainX',
        speed: 2000,
        timeout: 5000,
        prev : '#prev',
        next : '#next',
        pager : '#pager'
    })

    /* envio do form */
    jQuery(".frmAjax").submit(function() {
        var enviar_ok = true;
        var form_name = jQuery(this).attr('id');
        var vCamposErro = "Preencha os seguintes campos: \n";

        jQuery("#loading").show();

        /* checar campos */
        jQuery('#'+form_name+' :input[title=requerido] ').each(function(){
            if(jQuery.trim(jQuery("#"+this.id).val()) == ''){
                jQuery("#"+this.id).css({
                    background: "#FF9F9F"
                });
                enviar_ok = false;
                vCamposErro = vCamposErro + " - " + jQuery(this).attr('name') + "\n";
            } else {
                jQuery("#"+this.id).css({
                    background: "#B8F5B1"
                });
            }
        });

        if(enviar_ok) {
            var options = {
                success: function(msg) {
                    jQuery("#loading").hide("slow");
                    // sucesso no envio
                    if(jQuery.trim(msg) == "") {
                        if(jQuery('#frmMsg').length == 1) {
                            alert(jQuery('#frmMsg').val());
                        } else {
                            alert('E-mail enviado com sucesso. Em breve retornaremos. Obrigado!');
                        }
                        jQuery('#'+form_name).resetForm();
                        if(jQuery('#frmRedirect').length == 1) {
                            window.location.replace(jQuery('#frmRedirect').val());
                        }
                    } else {
                        alert(jQuery.trim(msg));
                    }
                }
            };

            jQuery(this).ajaxSubmit(options);

            return false; // faz o submit normal
        } else {
            jQuery("#loading").hide("slow");
            alert(vCamposErro);
            return false; //cancela submit normal
        }
    });

    /* envio do form */
    jQuery(".frmNormal").submit(function() {
        var enviar_ok = true;
        var form_name = jQuery(this).attr('id');
        var vCamposErro = "Preencha os seguintes campos: \n";

        jQuery("#loading").show();

        /* checar campos */
        jQuery('#'+form_name+' :input[title=requerido] ').each(function(){
            if(jQuery.trim(jQuery("#"+this.id).val()) == ''){
                jQuery("#"+this.id).css({
                    background: "#FF9F9F"
                });
                enviar_ok = false;
                vCamposErro = vCamposErro + " - " + jQuery(this).attr('name') + "\n";
            } else {
                jQuery("#"+this.id).css({
                    background: "#B8F5B1"
                });
            }
        });

        if(enviar_ok) {
            return true; // faz o submit normal
        } else {
            jQuery("#loading").hide("slow");
            alert(vCamposErro);
            return false; //cancela submit normal
        }
    });
}) 
