_event('custom', {'category' : 'forms', 'action' : 'Show', 'label' : '/contact'}); // for the rest
function submitForm(form) {
var form_id = $(form).attr('id');
var w_id = $(form).data('widget_id');
var _errors_in_form = false;
var callback = $(form).data('callback');
var callback_param = $(form).data('callback_param');
$('#' + form_id + ' .form-group').removeClass('has-error');
$('#form_alert_' + form_id).remove();
// check mandatory ones
$('#' + form_id + ' .mandatory .form-control, #' + form_id + ' .mandatory input, #' + form_id + ' .mandatory select').each(function(i, e) {
if ($(e).attr('multiple') && (! $(e).val() || $(e).val().length <= 0)) {
// select multiple
alertMandatory(e);
_errors_in_form = true;
} else if ($(e).attr('type') == 'checkbox' && ! $(e).is(':checked')) {
// checkbox not checked
alertMandatory(e);
_errors_in_form = true;
} else if ($(e).attr('type') == 'radio') {
if ($('input[name="' + $(e).attr('name') + '"]:checked').length <= 0) {
// none of this radio "group" is checked
alertMandatory(e);
_errors_in_form = true;
}
} else if ($(e).val() == '') {
// value empty
alertMandatory(e);
_errors_in_form = true;
}
});
if (_errors_in_form) {
$('#' + form_id).append('');
return false;
} else {
// ok, submit the form
$('#' + form_id + ' button[type="submit"]').addClass('disabled');
$('#' + form_id).fadeTo('fast', 0.3);
// do we have files?
var have_files = false;
$('#' + form_id + ' input').each(function(i, e) {
if ($(this).attr('type') == "file") {
if ($(this).get(0).files.length > 0) {
have_files = true;
return false;
}
}
});
if (have_files) {
return true;
} else {
if (typeof p_id == "undefined") {
p_id = 0;
}
var url = _api_url + '?api=form&id=' + _site.s_id + '&p_id=' + p_id + '&submitform=1&w_id=' + w_id;
var form_data = $(form).serialize();
/*
var indexed_array = {};
var unindexed_array = $(form).serializeArray();
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
*/
$.post(url, form_data, function (form_result) {
if (form_result.error) {
alert("Sorry, there was an error submitting your form");
console.log(form_result);
$('#' + form_id).fadeTo('fast', 1);
$('#' + form_id + ' button[type="submit"]').removeClass('disabled');
return false;
} else {
// ok
_event('lead'); // for Facebook
_event('custom', {'category' : 'forms', 'action' : 'Submit', 'label' : '/contact'}); // for the rest
if (callback) {
window[callback](callback_param);
} else if (form_result.redirectto && form_result.redirectto.length > 1) {
$('body').fadeTo('fast', 0.2, function () {
window.location.href = form_result.redirectto;
});
} else {
$('#' + form_id).parent().fadeTo('fast', 0.2, function () {
var replace_form = '' + "Thanks, your form has been submitted" + '
';
$(this).html(replace_form).fadeTo('fast', 1);
});
}
}
});
}
return false;
}
}
function alertMandatory(field) {
// field is already a j
$(field).attr('aria-invalid', 'true');
var id = $(field).attr('id');
if (typeof id !== "undefined" && id) {
var formGroupID = id.replace('formField', 'formGroup');
$('#' + formGroupID).addClass('has-error');
}
}