HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/investeren-ouddorp.komma.pro/wwwroot/js/contact.js
/**
 * Created by Pascal on 15/06/16.
 */

$(function () {

    //Set to true if we want to send the form via AJAX
    var contactFormViaAjax = true;
    $('#contactForm').append('<input type="hidden" name="secretCode" value="isHuman" />');

    $('#contactForm .submit').click(function () {

        //Set the form fields to an object
        var formFields = setFormFields();
        //Remove the alert classes
        $('.check-alert').removeClass('show');
        $('.input-rows .alert').removeClass('alert');
        $('.check-alert.temp').remove();

        //Validate the fields, and set the send variable
        var send = validateFields(formFields);

        //If Send is true, Send the contactForm with Ajax
        if (send) sendContactForm(formFields);

        // var send = true;
        // $('#contactForm').submit();

        //If .submit isn't a input field but an div, p or other html element
        //if (send && contactFormViaAjax == false){
        //$('#contactForm').submit();
        //}

        //Stop the form from submitting
        return false;
    });

    $('#subscribeMail .submit').click(function () {

        if ( ! $('#subscribeMail').hasClass('sended')) {
            var email = $('#subscribeMail #email').val();
            if (email == '') $('#subscribeMail #email').addClass('false');
            else {
                sendSubscribeForm(email);
            }
        }

    });

});


/**
 * A quick ajax validation of the fields
 * We only check if certain fields are not empty
 *
 * @param values
 * @returns {boolean}
 */
function validateFields(values) {
    //By default the send value is true
    var send = true;
    //Remove the alert class from the input fields before we validate
    $('#contactForm input, #contactForm textarea, #contactForm .checkbox').removeClass('alert');

    //The fields we are going to validate
    // var fields = ['first_name', 'last_name', 'email', 'formMessage', 'checkLegal'];
    var fields = ['name', 'email', 'formMessage', 'checkLegal'];

    //Loop trough the fields
    $.each(fields, function (index, key) {

        //Check the element based on the key is not empty, if true, try next element
        if (values[key] != '' && values[key] != false) return;

        //If empty
        //Set send to false
        send = false;
        //Add an alert class to the form element of the key
        if (key == 'checkLegal') $('#contactForm #checkLegal').parent().parent().addClass('alert');
        else $('#contactForm #' + key).addClass('alert');
        //console.log(key);


    });
    //Return the send variable
    return send
}

/**
 * Sent the given formFields with Ajax
 *
 * @param formFields
 */
function sendContactForm(formFields) {
    $.ajax({
        //Set method to post
        method: "POST",
        //Add the formFields
        data: formFields,
        //Get the url from the action url
        url: $('#contactForm').attr('action')
    }).done(function (msg) {
        //What to do when the form is submitted
        //If the return message is not the string "true"
        if (msg != 'true') {
            // Loop trough the message
            for (var key in msg) {
                //Set the first message for every key
                $('#contactForm .form-element.' + key + ' .error').html(msg[key][0]).addClass('active');
            }
            console.log(msg);
            //Exit the function
            return;
        }

        //All was good
        //Remove the contactForm
        $('.contact-form .thanks').height($('#contactForm').height());
        $('#contactForm').remove();
        //Show the thanks block
        $('.contact-form .thanks').removeClass('inactive');
    });
}

function sendSubscribeForm(data) {
    $.ajax({
        //Set method to post
        method: "POST",
        //Add the formFields
        data: {
            email_subscribe: data,
            secretCode: 'isHuman'
        },
        //Get the url from the action url
        url: $('#subscribeMail').attr('action')
    }).done(function (msg) {
        //What to do when the form is submitted
        //If the return message is not the string "true"

        if (msg != 'true') {
            // Loop trough the message
            for (var key in msg) {
                //Set the first message for every key
                $('#subscribeMail .form-element.email .error-message').html(msg[key][0]).addClass('active');
            }
            console.log(msg);
            //Exit the function
            return
        }

        //All was good
        //Remove the contactForm

        $('#subscribeMail input').remove();
        $('#subscribeMail').attr('action', '').addClass('sended');
        $('#subscribeMail .submit, #subscribeMail .form-element').addClass('success');
        $('#subscribeMail .submit p').html('Ingeschreven');
        $('.subscribe-form .thanks').removeClass('inactive');
    });
}

/**
 * Return an object of form fields
 * Add the secretCode.
 *
 * @returns object
 */
function setFormFields() {

    var checkLegal = false;
    if ($('#contactForm #checkLegal').prop('checked')) checkLegal = true;

    return {
        name: $('#contactForm #name').val(),
        // first_name: $('#contactForm #first_name').val(),
        // last_name: $('#contactForm #last_name').val(),
        phone: $('#contactForm #phone').val(),
        email: $('#contactForm #email').val(),
        checkLegal: checkLegal,
        formMessage: $('#contactForm #formMessage').val(),
        secretCode: 'isHuman'
    };
}