$(document).ready(function() {
    $("#cadastro").hide();
    $("#fcad").click(function() {
        $("#cadastro").toggle("none");
        return false;
    });

    $("#esqueciSenha").click(function() {
        window.open($(this).attr("href"), "", "width=400,height=200", true);
        return false;
    })
    $("#form1").validate({
        errorLabelContainer: $('#error'),
        wrapper: 'p',

        rules: {
            email: {
                required: true,
                email: true
            },
            senha: 'required'
        },

        messages: {
            email: {
                required: '\n Preencha o campo e-mail.',
                email: '\n O endere&ccedil;o de e-mail n&atilde;o &eacute; v&aacute;lido.'
            },
            senha: '\n Preencha o campo senha.'
        }

    });

    if (location.href.indexOf("Logoff=true") != -1) {
        window.setTimeout("$('#error').html('')", 2000);
    }

    $("#form2").validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "cadastro.asp",
                data: InvariantEncoding("form2"),
                beforeSend: function() { $("#error2").html("<img src='../imagens/ajax-loader.gif' alt='' /> Aguarde...").fadeIn("slow") },
                success: response
            });
            return false;
        },

        rules: {
            txtNome: 'required',
            txtEmail: {
                required: true,
                email: true
            },
            txtSenha: 'required',
            txtSenhaConfirma: {
                required: true,
                equalTo: '#txtSenha'
            }
        }
    });

    function response(data) {
        $("#form2").resetForm();
        $("#form2").hide();
        $('#error2').html(data).fadeIn('slow').animate({ opacity: '+=0' }, 20000);
        window.setTimeout(function() {
        $("#form2").show();
        $('#error2').html('');
        $("#cadastro").hide();
        }, 20000);
    }

    function InvariantEncoding(idForm) {
        var elements = decodeURIComponent($("#" + idForm).serialize());
        var valores = elements.split("&");
        var newArray = [];
        for (i = 0; i < valores.length; i++) {
            conteudo = valores[i];
            nome = conteudo.split("=")[0]
            valor = conteudo.split("=")[1]
            newArray.push(nome + "=" + escape(valor))
        }
        return newArray.join('&').toString();
    }



});