(function () {
var MAIL_FIELD_IDS = [
1499729, 1499899, 1499900, 1499901, 1499902, 1499903,
1499904, 1499905, 1499906, 1499907, 1499908, 1499909,
1499910, 1499911, 1499912, 1499913, 1499914, 1499915,
1499916, 1499917, 1499918, 1499919, 1499920, 1499921,
1499922, 1499923, 1499924, 1499925, 10074028, 1794780,
1794801, 1794849, 1852427, 10001933
];
function initForm($block) {
if ($block.data('delivery-init')) return;
$block.data('delivery-init', true);
var $form = $block.is('form') ? $block : $block.find('form').first();
if (!$form.length) return;
var $fileBlock = null;
for (var i = 0; i < MAIL_FIELD_IDS.length; i++) {
var $el = $block.find('#field-input-' + MAIL_FIELD_IDS[i]);
if ($el.length) {
$fileBlock = $el.closest('.builder-item');
break;
}
}
if (!$fileBlock || !$fileBlock.length) return;
var $btn = $form.find('button[type="submit"]').first();
if (!$btn.length) return;
var btnId = $btn.attr('id'); // например "button1761878"
var $choiceBlock = $block.find('.delivery-choice-block').first();
var $error;
if ($choiceBlock.length) {
$choiceBlock.show();
$error = $choiceBlock.find('.delivery-error');
} else {
$choiceBlock = $(
'
' +
'
Как получить удостоверение?
' +
'
' +
'
' +
'
' +
'
'
);
$fileBlock.before($choiceBlock);
$error = $choiceBlock.find('.delivery-error');
}
$fileBlock.hide();
disableFileField();
function disableFileField() {
$fileBlock.find('input[id^="field-input-"]').attr('disabled', 'disabled');
$fileBlock.find('.custom-field-value').attr('disabled', 'disabled');
$fileBlock.find('.required-sign').hide();
}
function enableFileField() {
$fileBlock.find('input[id^="field-input-"]').removeAttr('disabled');
$fileBlock.find('.custom-field-value').removeAttr('disabled');
$fileBlock.find('.required-sign').show();
}
function showError(msg) {
$error.text(msg).fadeIn(200);
$('html, body').animate({ scrollTop: $choiceBlock.offset().top - 100 }, 300);
}
function hideError() { $error.fadeOut(150); }
function unlockButton() {
if (btnId) window['prs' + btnId] = false;
$btn.prop('disabled', false).removeAttr('disabled').removeClass('disabled');
$form.removeClass('lt-form-loading lt-form-disabled lt-form-sending');
$btn.find('.fa-spinner, .glyphicon-refresh').remove();
}
$choiceBlock.on('change', 'input[name="delivery_choice"]', function () {
hideError();
if ($(this).val() === 'post') {
enableFileField();
$fileBlock.show();
} else {
disableFileField();
$fileBlock.hide();
}
});
function validate() {
var choice = $choiceBlock.find('input[name="delivery_choice"]:checked').val();
if (!choice) {
showError('Пожалуйста, выберите способ получения удостоверения.');
return false;
}
if (choice === 'post') {
var fileVal = $fileBlock.find('.custom-field-value').val();
var isEmpty = !fileVal || fileVal === '""' || fileVal === '' || fileVal === 'null';
if (isEmpty) {
$fileBlock.find('.custom-field-input').css('outline', '2px solid #e53935');
showError('Пожалуйста, загрузите заявление для отправки удостоверения почтой.');
$('html, body').animate({ scrollTop: $fileBlock.offset().top - 100 }, 300);
return false;
}
$fileBlock.find('.custom-field-input').css('outline', '');
}
hideError();
return true;
}
$fileBlock.on('change', 'input[id^="field-input-"]', function () {
$fileBlock.find('.custom-field-input').css('outline', '');
hideError();
});
$btn[0].addEventListener('mousedown', function (e) {
if (!validate()) {
e.preventDefault();
e.stopImmediatePropagation();
unlockButton();
}
}, true);
$btn[0].addEventListener('touchstart', function (e) {
if (!validate()) {
e.preventDefault();
e.stopImmediatePropagation();
unlockButton();
}
}, { capture: true, passive: false });
$form[0].addEventListener('submit', function (e) {
if (!validate()) {
e.preventDefault();
e.stopImmediatePropagation();
unlockButton();
return false;
}
}, true);
}
function initAll() {
// delivery-form может быть на внешнем div или на самой форме
$('.delivery-form').each(function () { initForm($(this)); });
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initAll);
} else {
initAll();
}
new MutationObserver(function (mutations) {
for (var i = 0; i < mutations.length; i++) {
var nodes = mutations[i].addedNodes;
for (var j = 0; j < nodes.length; j++) {
var node = nodes[j];
if (node.nodeType !== 1) continue;
var $node = $(node);
if ($node.hasClass('delivery-form')) initForm($node);
$node.find('.delivery-form').each(function () { initForm($(this)); });
}
}
}).observe(document.body, { childList: true, subtree: true });
})();