$(function() {
    $('.error').hide();
    $('input.text-input').css({
        backgroundColor:"#FFFFFF"
    });
    $('input.text-input').focus(function(){
        $(this).css({
            backgroundColor:"#FFDDAA"
        });
    });
    $('input.text-input').blur(function(){
        $(this).css({
            backgroundColor:"#FFFFFF"
        });
    });




    $(".bt-enviar").click(function() {
        // validate and process form
        // first hide any error messages
        $('.error').hide();

        var nome = $("input#nome").val();
        if (nome == "nome") {
            $("label#nome_error").show();
            $("input#nome").focus();
            return false;
        }
        if (nome == "") {
            $("label#nome_error").show();
            $("input#nome").focus();
            return false;
        }

        var title = $("input#title").val();
        //<!--    -->
        var email = $("input#email").val();
        if (email == "E-mail") {
            $("label#email_error").show();
            $("input#email").focus();
            return false;
        }
        if (email == "") {
            $("label#email_error").show();
            $("input#email").focus();
            return false;
        }
        if(email =="" || email.indexOf('@')==-1 || email.indexOf('.')==-1 ) {
            $("label#email_invalido_error").show();
            $("input#email").focus();
            return false;
        }
        //<!--    -->
        var sex = new Array();
        if (!$('input[type=radio][name=sex]:checked').val()) {
            $("label#sex_error").show();
            return false;
        } else {
            $("input[name='sex']:checked").each(function(){
                sex.push($(this).val());
            });
        }

        //<!--    -->
        var tel1 = $("input#tel1").val();
        if (tel1 == "") {
            $("label#tel1_error").show();
            $("input#tel1").focus();
            return false;
        }
        var tel1 = $("input#tel1").val();
        if (tel1 == "(__)____-____") {
            $("label#tel1_error").show();
            $("input#tel1").focus();
            return false;
        }
        //<!--    -->
        var tel2 = $("input#tel2").val();
        if (tel2 == "") {
            $("label#tel2_error").show();
            $("input#tel2").focus();
            return false;
        }
        if (tel2 == "(__)____-____") {
            $("label#tel2_error").show();
            $("input#tel2").focus();
            return false;
        }
        //<!--    -->
        var rg = $("input#rg").val();
        if (rg == "") {
            $("label#rg_error").show();
            $("input#rg").focus();
            return false;
        }
        //<!--    -->
        var cpf = $("input#cpf").val();
        if (cpf == "") {
            $("label#cpf_error").show();
            $("input#cpf").focus();
            return false;
        }
        if (cpf == "___.___.___-__") {
            $("label#cpf_error").show();
            $("input#cpf").focus();
            return false;
        }
        //<!--    -->
        var d_nasc = $("input#d_nasc").val();
        if (d_nasc == "") {
            $("label#d_nasc_error").show();
            $("input#d_nasc").focus();
            return false;
        }
        if (d_nasc == "__/__/____") {
            $("label#d_nasc_error").show();
            $("input#d_nasc").focus();
            return false;
        }
        //<!--    -->

        var est_civil = new Array();
        if (!$('input[type=radio][name=est_civil]:checked').val()) {
            $("label#est_civil_error").show();
            return false;
        } else {
            $("input[name='est_civil']:checked").each(function(){
                est_civil.push($(this).val());
            });
        }


        //<!--    -->
        var end = $("input#end").val();
        if (end == "") {
            $("label#end_error").show();
            $("input#end").focus();
            return false;
        }
        //<!--    -->
        var cep = $("input#cep").val();
        if (cep == "") {
            $("label#cep_error").show();
            $("input#cep").focus();
            return false;
        }
        //<!--    -->
        var estado = $("select#estado").val();
        if (estado == "") {
            $("label#estado_error").show();
            $("select#estado").focus();
            return false;
        }
        //<!--    -->
        var cidade = $("input#cidade").val();
        if (cidade == "") {
            $("label#cidade_error").show();
            $("input#cidade").focus();
            return false;
        }
        //<!--    -->

        var alergia = new Array();
        if (!$('input[type=radio][name=alergia]:checked').val()) {
            $("label#alergia_error").show();
            return false;
        } else {
            $("input[name='alergia']:checked").each(function(){
                alergia.push($(this).val());
            });
        }

        //<!--    -->
        var quais = $("input#quais").val();
//        if (quais == "") {
//            $("label#quais_error").show();
//            $("input#quais").focus();
//            return false;
//        }
        //<!--    -->
        var t_sanguinio = $("select#t_sanguinio").val();
        if (t_sanguinio == "") {
            $("label#t_sanguinio_error").show();
            $("select#t_sanguinio").focus();
            return false;
        }

        // Contato em caso de emergência

        var nome2 = $("input#nome2").val();
        if (nome2 == "") {
            $("label#nome2_error").show();
            $("input#nome2").focus();
            return false;
        }
        //<!--    -->
        var tel3 = $("input#tel3").val();
        if (tel3 == "") {
            $("label#tel3_error").show();
            $("input#tel3").focus();
            return false;
        }
        if (tel3 == "(__)____-____") {
            $("label#tel3_error").show();
            $("input#tel3").focus();
            return false;
        }
        //<!--    -->
        var g_parente = $("input#g_parente").val();
        if (g_parente == "") {
            $("label#g_parente_error").show();
            $("input#g_parente").focus();
            return false;
        }
        //<!--    -->
        // Etapas

        var categoria = new Array();
$("input[name='categoria[]']:checked").each(function(){
    categoria.push($(this).val());});


            //<!--    -->

            var anuidade = new Array();
            if (!$('input[type=radio][name=anuidade]:checked').val()) {
                $("label#anuidade_error").show();
                return false;
            } else {
                $("input[name='anuidade']:checked").each(function(){
                    anuidade.push($(this).val());
                });
            }

            //<!--    -->
            var termo = $("input:checkbox#termo").val();
            if ($('input:[type=checkbox][name=termo]:not(":checked)').val() ){
                $("label#termo_error").show();
                $("input:checkbox#termo").focus();
                return false;
            }
            //<!--    -->




            var dataString = 'nome='+ nome +
                '&title=' + title +
                '&email=' + email +
                '&sex=' + sex +
                '&tel1=' + tel1 +
                '&tel2=' + tel2 +
                '&rg=' + rg +
                '&cpf=' + cpf +
                '&d_nasc=' + d_nasc +
                '&est_civil=' + est_civil +
                '&end=' + end +
                '&cep=' + cep +
                '&estado=' + estado +
                '&cidade=' + cidade +
                '&alergia=' + alergia +
                '&quais=' + quais +
                '&t_sanguinio=' + t_sanguinio +
                '&nome2=' + nome2 +
                '&tel3=' + tel3 +
                '&g_parente=' + g_parente +
                '&categoria=' + categoria +
                '&anuidade=' + anuidade +
                '&termo=' + termo;
            //alert (dataString);return false;
		
            $.ajax({
                type: "POST",
                url: "/wp-content/themes/kitetour/library/jquery-ajax/bin/process.php",
                data: dataString,
                success: function() {
                    $('#contact_form').html("<div id='message'></div>");
                    $('#message').html("<h2>Seu cadastro foi enviado com sucesso!</h2>")
                    .append("<p>Aguarde a validação dos seus dados no nosso sistema! </p>")
                    .hide()
                    .fadeIn(1500, function() {
                        $('#message').append(" ");
                    });
                }
            });
            return false;
        });
    });
    runOnLoad(function(){
        $("input#nome").select().focus();
    });

