function verifyForm() {
let errors = 0;
let valid = true;
$('.form-control').map(function () {
let value = $(this).val();
let name = $(this).prop('name');
let required = $(this).prop('required');
if (required && value === '') {
errors++;
console.log('Field is not passed: ' + name);
}
});
let targetsMode = $('#targets_mode').val();
if (targetsMode == 2) {
let groupName = $('#targetRecipients').val();
if (groupName == 0) {
toastr.error('Please select group of targets');
errors++;
}
}
if (targetsMode == 3) {
var targets = [];
$('input.targets:checkbox:checked').each(function () {
targets.push($(this).val());
});
if (targets.length == 0) {
toastr.error('Please select at least one target');
errors++;
}
}
console.log('Total form errors: ' + errors);
if (errors > 0) {
valid = false;
toastr.error('Please provide all mandatory fields');
}
return valid;
}
$(document).ready(function () {
console.log('Ready!');
$('body').click(function (event) {
if (event.target.id == 'submitCampaignFormBtn') {
let email, emailStatus, status;
status = verifyForm();
if (status) {
email = $('#Email').val();
emailStatus = $('#Email-verifalia-classification').val();
console.log('Email status: ' + emailStatus);
if ($('#Email-verifalia-classification').length > 0) {
if (emailStatus == 'Deliverable' || emailStatus == 'Risky') {
$('#sendLoader').show();
let content = $('#BodyDiv').html();
$('#Body').val(content);
$('#submitCampaignFormBtn').prop('disabled', true);
document.getElementById('regForm').submit();
} else {
toastr.error('Provided email ' + email + ' is not deliverable');
}
} else {
$('#sendLoader').show();
let updatedContent=$('#BodyDiv').html();
$('#Body').val(updatedContent);
$('#submitCampaignFormBtn').prop('disabled', true);
document.getElementById('regForm').submit();
}
}
}
if (event.target.id == 'addCampaignTargetsBtn') {
let id;
let targets = [];
let messageid = $('#campaign_email_id').val();
let mode = $('#targets_mode').val();
$('.targets').map(function () {
if (mode == 2) {
id = $(this).val();
targets.push(id);
} else {
id = $(this).data('id');
if ($(this).prop('checked')) {
targets.push(id);
}
}
});
if (targets.length == 0) {
toastr.error('Please select at least one target');
} else {
if (confirm('Add selected target(s) to the campaign?')) {
let item = {
targets: targets,
messageid: messageid
}
let url = 'https://app.campaign.engineering/add-campaign-targets';
$.post(url, {item: JSON.stringify(item)}).done(function () {
toastr.success('Selected target(s) were added to the current campaign');
});
}
}
}
if (event.target.id == 'clearRecipients') {
let targets = [];
$('.targets').map(function () {
targets.push($(this).data('id'));
$(this).prop('checked', false);
});
if (targets.length > 0) {
if (confirm('Remove all targets from messaging?')) {
let item = {
targets: targets
}
let url = 'https://app.campaign.engineering/remove-campaign-targets';
$.post(url, {item: JSON.stringify(item)}).done(function () {
toastr.success('All targets were removed from messaging');
});
}
}
}
});
$('body').change(function (event) {
console.log('Element changed: ' + event.target.id);
console.log('ElementClass changed: ' + event.target.className);
if (event.target.className == 'targets') {
if (typeof dashboard === 'undefined') {
dashboard = 0;
}
console.log('JS deploymnet dashboard presence: '+ dashboard);
if (dashboard != 1) {
let id = $(event.target).data('id');
let status = $(event.target).prop('checked');
let item = {
id: id,
status: status
};
let url = 'https://app.campaign.engineering/set-individual-target-selection';
$.post(url, {item: JSON.stringify(item)}).done(function () {
//toastr.success('Selected target item was updated');
});
}
}
if (event.target.id == 'targetsGroup') {
let id = $('#campaign_email_id').val();
let group = $('#targetsGroup').val();
if (group != 0) {
let item = {
id: id,
group: group
};
let url = 'https://app.campaign.engineering/get-recipients-by-group2';
$.post(url, {item: JSON.stringify(item)}).done(function (groups) {
$('#groupTargetsSection').html(groups);
});
}
}
if (event.target.id == 'targetRecipients') {
let id = $('#campaign_email_id').val();
let targetid = $('#targetRecipients').val();
let targetsMode = $('#targets_mode').val();
if (typeof dashboard === 'undefined') {
dashboard = 0;
}
console.log('JS deploymnet dashboard presence: '+ dashboard);
if (targetid != 0 && dashboard !== 1) {
if (targetsMode == 2) {
let item = {
messageid: id,
targetid: targetid
};
let url = 'https://app.campaign.engineering/add-submission-recipients';
$.post(url, {item: JSON.stringify(item)}).done(function (targets) {
//toastr.success('Target(s) of selected group were added to the current campaign');
});
}
if (targetsMode == 3) {
let group = $('#targetRecipients').val()
$('#groupTargetsSection').html('');
let item = {
id: id,
group: group
};
let url = 'https://app.campaign.engineering/get-recipients-by-group2';
$.post(url, {item: JSON.stringify(item)}).done(function (groups) {
$('#groupTargetsSection').html(groups);
});
}
}
}
});
});