
let stResult = {};
// Set Recaptcha in case customer uses this
var captchaProtect = {
'init' : false,
'allow' : false
}
function recaptcha_result(result) {
captchaProtect.allow = true;
}
/** **
fb 03.02.21
This customCallback function can be overwritten in the "standard.js" file from customer.
Now we have the possibility to log the form
ATTENTION there is ATM no async binding. The result of the customCallback must be "TRUE" without waiting on ajax calls
Maybe this can be improved later...
*/
function customCallback() {
return true
}
$(document).ready(function() {
var allInputs = $("input");
var emailIDs = [];
allInputs.each(function(field) {
if( $(allInputs[field]).attr('type') == 'text.email' ) {
emailIDs.push($(allInputs[field]).attr('id'))
}
})
$(".mpg-form button[type=submit]").on('click', function(e) {
e.preventDefault();
/**
*	this parses the form and transforms it into a structure
*/
var $thisForm = $(this).closest('form.mpg-form');
var aForm = $thisForm.serializeArray();
var stForm = objectifyForm(aForm);
/**
*	Start validate Form
*/
mpFormValidate.init(
{
validationType : 'direct',
form : $thisForm,
notValidParent : '.row',
messageParent : '.mpInputCol',
fieldsEmail	: emailIDs,
errorMsg : {
required : "Veuillez remplir tous les champs obligatoires",
emailNotValid : "Cette adresse e-mail n'est pas correcte",
compareFaild : "Les champs ne correspondent pas",
},
onSuccess : function() {
var fActivateFormsRecaptcha = stForm.fActivateFormsRecaptcha == '1';
if(fActivateFormsRecaptcha == 1){
var recaptchaSiteKey = stForm.reCaptchaSiteKey;
if(typeof grecaptcha !== "undefined"){
try{
grecaptcha.execute(recaptchaSiteKey, {action: "submit"})
.then(function (token) {
$thisForm.find('input[name="ReCaptchaToken"]').val(token);
if( $thisForm.find('#dynamicRequest').length ) {
$thisForm.submit();
sessionStorage.removeItem(`frm_${window.location.hostname}_${window.location.pathname}`);
}else{
stForm['ReCaptchaToken'] = token;
sendFormAjx(stForm, $thisForm);
}
})
.catch(function (error) {
console.error("ReCaptcha error:", error);
$('form.mpg-form').html("ReCaptcha error");
});
}catch(error){
console.error("ReCaptcha error:", error);
$('form.mpg-form').html("ReCaptcha error");
}
}
}
else{
if( $thisForm.find('#dynamicRequest').length ) {
$thisForm.submit();
sessionStorage.removeItem(`frm_${window.location.hostname}_${window.location.pathname}`);
}else{
sendFormAjx(stForm, $thisForm);
}
}
}
}
);
})
});
function sendFormAjx(stForm, thisForm){
var data = $.param({
formString: JSON.stringify(stForm),
corp : 10097?version=211439336
});
$.ajax(
{
url: "/cfc/mp/proxy.cfc?method=sendForm&returnFormat=json",
type: "POST",
dataType: "json",
data : data,
success: function (resultData) {
message(resultData, thisForm);
sessionStorage.removeItem(`frm_${window.location.hostname}_${window.location.pathname}`);
},
error: function (error) {
console.log(error);
}
}
);
}
function objectifyForm(formArray) {
var returnArray = {};
for (var i = 0; i < formArray.length; i++){
if( formArray[i]['name'].indexOf('checkbox-group') != -1 ) {
var groupName = formArray[i]['name'].replace('[]', '');
returnArray[groupName] = typeof(returnArray[groupName]) != 'undefined' ? returnArray[groupName] + ',' + formArray[i]['value'] : formArray[i]['value'];
}else{
returnArray[formArray[i]['name']] = formArray[i]['value'];
}
}
return returnArray;
}
var resultData = {
'data' : {
'mailSent' : true,
'answerUrl' : '',
'answer' : ``,
}
}
function message(resultData, thisForm) {
if(resultData.data.answerUrl != '') {
document.location = '/' + resultData.data.answerUrl;
}else{
thisForm.parent().append(resultData.data.answer);
thisForm.hide();
}
//alert('Es ist ein Fehler aufgetreten, das Formular wurde nicht gesendet.')
}
