File: D:/HostingSpaces/EUmans/umansradepo.be/wwwroot/js/offer.js
/**
* Created by Pascal on 15/06/16.
*/
$(function () {
//Set to true if we want to send the form via AJAX
$('#offer-form').append('<input type="hidden" name="secretCode" value="isHuman" />');
$('#offer-form .submit-button').click(function () {
//Set the form fields to an object
var offerFields = setOfferFormFields();
//console.log(formFields);
//Remove the alert classes
$('.check-alert').removeClass('show');
$('.input-rows .alert').removeClass('alert');
$('.check-alert.temp').remove();
console.log(offerFields);
//Validate the fields, and set the send variable
var send = validateOfferFields(offerFields);
//If .submit isn't a input field but an div, p or other html element
if (send){
$('#offer-form').submit();
}
//Stop the form from submitting
});
});
/**
* A quick ajax validation of the fields
* We only check if certain fields are not empty
*
* @param values
* @returns {boolean}
*/
function validateOfferFields(values) {
//By default the send value is true
var send = true;
//Remove the alert class from the input fields before we validate
$('#offer-form input, #offer-form textarea').removeClass('alert');
//The fields we are going to validate
var fields = ['first_name','last_name', 'email', 'formMessage'];
//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] != '') return;
//If empty
//Set send to false
send = false;
//Add an alert class to the form element of the key
$('#offer-form #' + key).addClass('alert');
console.log(key);
});
//Return the send variable
return send
}
/**
* Return an object of form fields
* Add the secretCode.
*
* @returns object
*/
function setOfferFormFields() {
return {
first_name: $('#offer-form #first_name').val(),
last_name: $('#offer-form #last_name').val(),
phone: $('#offer-form #phone').val(),
email: $('#offer-form #email').val(),
// address: $('#offer-form #address').val(),
// city: $('#offer-form #city').val(),
// zip_code: $('#offer-form #zip_code').val(),
formMessage: $('#offer-form #formMessage').val(),
secretCode: 'isHuman'
};
}