/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {

    $("#pregunta1").show();
    $("#ddlPreguntas").change(function () {

        $(".textofaq").hide("fade");
        $("#" + $("#ddlPreguntas").val()).show("fade");
    }
    )
});

function enviarContacto() {

    $(".mensajeEnviado2").hide("fade");
    $(".mensajeError2").hide("fade");

    if (validarContacto()) {
        $("#mensajeEnviado2").hide("fade");
        $(".mensajeEnviando2").show("fade");
        if (!document.sending) {
            document.sending = true;
            $.ajax({
                data: {
                    nombre : $("#txtNombre").val(),
                    apellido : $("#txtApellido").val(),
                    consulta : $("#txtConsulta").val(),
                    telefono : $("#txtTelefono").val(),
                    email : $("#txtEmail").val()
                },
                type: 'POST',
                url: WEB_PATH + 'app/ajax/send_contacto.php',
                success: function(response) {
                    document.sending = false;
                    if (response == "-1") {
                        $(".mensajeError2").html('Error inesperado al enviar el mensaje');
                        $(".mensajeError2").show("fade");
                    }else {
                        $(".mensajeEnviado2").show("fade");
                        $(".mensajeEnviando2").hide("fade");
                    }
    
                }
            });
        }
    }

}

function limpiarFormContacto() {

    $("#txtNombre").val("");
    $("#txtApellido").val("");
    $("#txtTelefono").val("");
    $("#txtEmail").val("");
    $("#txtConsulta").val("");
    $("#txtNombre").val("");

    $(".mensajeError2").hide();
    $(".mensajeEnviando2").hide();
    $(".mensajeEnviado2").hide();
    
}

function validarContacto(){

    $(".mensajeError2").hide("fade");

    if ($("#txtNombre").val().trim() == "") {
        $(".mensajeError2").html('Debe ingresar su nombre');
        $(".mensajeError2").show("fade");
        return false;
    }
    if ($("#txtConsulta").val().trim() == "") {
        $(".mensajeError2").html('Debe ingresar una consulta');
        $(".mensajeError2").show("fade");
        return false;
    }
    if ($("#txtTelefono").val().trim() == "") {
        $(".mensajeError2").html('Debe ingresar su teléfono');
        $(".mensajeError2").show("fade");
        return false;
    }
    if ($("#txtEmail").val().trim() == "" || !is_email($("#txtEmail").val().trim())) {
        $(".mensajeError2").html('Debe ingresar un email válido');
        $(".mensajeError2").show("fade");
        return false;
    }

    return true;
}
