$(function() {

    var hasErrors = false;
    $("#validationmail").hide();
    $("#validationerr").hide();

    $("#btnEnviar").click(function() {

        // Valida nombres
        var name = $("#TextNombres").val();
        if (name == "") {
            hasErrors = true;
        }

        // valida Email.
        var email = $("#TextEmail").val();
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        if (email == "") {
            hasErrors = true;
        }
        else if (!emailReg.test(email)) {
            $("#validationmail").show("fast");
            hasErrors = true;
        }

        // Valida Telefono
        var phone = $("#TextTelefono").val();
        if (phone == "") {
            hasErrors = true;
        }

        // Valida Mensaje
        var mensa = $("#TextMensaje").val();
        if (mensa == "") {
            hasErrors = true;
        }

        // Valida Motivo
        var motiv = $("#TextMotivo option:selected").text();
        var hora = $("#TextHorario option:selected").text();
        var fecha = $("#TextFecha").val().replace('/', '_');
        if (motiv == "") {
            hasErrors = true;
        }
        else if (motiv == "Cita") {
            // Valida horario
            if (hora == "") {
                hasErrors = true;
            }

            // Valida horario
            if (fecha == "") {
                hasErrors = true;
            }
        }
        if (!hasErrors) {
            var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone + '&motivo=' + motiv + '&mensaje=' + mensa + '&horario=' + hora + '&fecha=' + fecha;

            $.ajax({
                type: "POST",
                url: "bin/process.php",
                data: dataString,
                success: function() {
                    $("#contactinfo").hide("fast");
                    $("#citaform").hide("fast");
                    $("#divmotivo").hide("slow");
                    $("#gracias").show("slow");
                },
                error: function() {
                    $("#error").show("fast");
                    $("#validationmail").hide();
                    $("#validationerr").hide();
                }
            });

        }
        else {
            $("#validationerr").show("fast");
            hasErrors = false;
        }

    });
});
