// Contact Form Scripts

$(function() { 

    $("#RegistroForm input").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var rsocial = $("input#razon_social").val();
            var email = $("input#register_email").val();
            var cuit = $("input#cuit").val();
            var telefono = $("input#telefono").val();
            var provincia = $("input#provincia").val();
            var localidad = $("input#localidad").val();
            var domicilio_fiscal  =  $("input#domicilio_fiscal").val();
            var firstName= rsocial;  
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            
            $.ajax({
                url: "././mail/contact_registro.php",
                type: "POST",
                data: {
                    rsocial: rsocial,
                    cuit: cuit,
                    email: email,
                    telefono: telefono,
                    provincia: provincia,
                    localidad: localidad,
                    domicilio_fiscal:domicilio_fiscal
                    
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success_register').html("<div class='alert alert-success'>");
                    $('#success_register> .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success_register> .alert-success')
                        .append("<strong>Su registro ha sido exitoso. </strong>");
                    $('#success_register> .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#RegistroForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success_register').html("<div class='alert alert-danger'>");
                    $('#success_register> .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success_register> .alert-danger').append($("<strong>").text("Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!"));
                    $('#success_register> .alert-danger').append('</div>');
                    //clear all fields
                    $('#RegistroForm').trigger("reset");
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#razon_social').focus(function() {
    $('#success_register').html('');
});
