Wrap all JS code in IIFE to avoid creating global variables

parent f103eaf1
var noticeTimer, noticeShow = false; (function() {
var noticeTimer, noticeShow = false;
function getEMail()
{
var emailInput = $('.svcCheckEmail');
var email = emailInput.val();
try {
email = email.trim();
} catch(e) {
//
}
return email;
}
function closeNotice() {
if(noticeShow === true) {
clearTimeout(noticeTimer);
emailNotice.animate({ marginTop: '-=51px' }, 300);
noticeShow = false;
}
}
function showNotice(type, message) {
if(!document.getElementById('svcCheckEmail_notice')) {
var notice = '<div id="svcCheckEmail_notice"><div class="notice_center_auto">';
notice += '<div id="notice_text"></div>';
notice += '<div id="notice_close_btn" onclick="closeNotice();">&nbsp;</div>';
notice += '</div></div>';
emailNotice = $(notice);
$('body').prepend(emailNotice);
}
$('#notice_text').html(message);
emailNotice.removeClass().addClass(type); function getEMail()
$('#notice_close_btn').removeClass().addClass(type); {
var emailInput = $('.svcCheckEmail');
if(noticeShow === false) { var email = emailInput.val();
emailNotice.animate({ marginTop: '+=51px' }, 300); try {
noticeShow = true; email = email.trim();
} } catch(e) {
/* Настраиваем скрытие через N секунд */ //
clearTimeout(noticeTimer); }
noticeTimer = setTimeout(function() { return email;
emailNotice.animate({ marginTop: '-=51px' }, 300);
noticeShow = false;
}, 7000);
}
function showErrorNotice(error)
{
var noticeText;
switch (error) {
case 'dns_records_not_found':
noticeText = 'Проверьте e-mail, такой домен не обнаружен.';
break;
case 'typed_domain':
noticeText = 'Обнаружена опечатка в домене. Проверьте внимательно указанный вами e-mail.';
break;
case 'wrong_email_format':
noticeText = 'Что-то не так в написании адреса. Возможно, указаны лишние точки или пробелы.<br>Адрес должен иметь вид: <b>имя_пользователя@имя_домена</b> (например <b>somebody@example.com</b>)';
break;
// FIXME
case 'check':
noticeText = 'Идёт проверка адреса e-mail, это может занять несколько секунд.';
default:
// FIXME
noticeText = 'Введённый вами адрес недоступен. Пожалуйста, укажите корректный e-mail.';
break;
} }
showNotice('red', noticeText);
}
// Добавляем CSS и расставляем вызов
$(function() {
$('<link />').attr({
rel: 'stylesheet',
type: 'text/css',
href: '//eterfund.ru/api/email/svcCheckEmail.css'
}).appendTo('head');
var prev_emailCheck, checkDoneStatus = 'empty';
var emailInput = $('.svcCheckEmail');
var emailNotice, emailForm = emailInput.closest('form');
emailForm.submit(function() { function closeNotice() {
if(getEMail() == '') { if(noticeShow === true) {
return false; clearTimeout(noticeTimer);
emailNotice.animate({ marginTop: '-=51px' }, 300);
noticeShow = false;
} }
// FIXME }
if(checkDoneStatus == 'empty') {
emailInput.blur(); function showNotice(type, message) {
if(!document.getElementById('svcCheckEmail_notice')) {
var notice = '<div id="svcCheckEmail_notice"><div class="notice_center_auto">';
notice += '<div id="notice_text"></div>';
notice += '<div id="notice_close_btn" onclick="closeNotice();">&nbsp;</div>';
notice += '</div></div>';
emailNotice = $(notice);
$('body').prepend(emailNotice);
} }
if(checkDoneStatus != 'done') { $('#notice_text').html(message);
showErrorNotice(checkDoneStatus);
}
if(checkDoneStatus == 'done') { emailNotice.removeClass().addClass(type);
closeNotice(); $('#notice_close_btn').removeClass().addClass(type);
return true;
if(noticeShow === false) {
emailNotice.animate({ marginTop: '+=51px' }, 300);
noticeShow = true;
} }
/* forbid send */ /* Настраиваем скрытие через N секунд */
return false; clearTimeout(noticeTimer);
}); noticeTimer = setTimeout(function() {
emailNotice.animate({ marginTop: '-=51px' }, 300);
noticeShow = false;
}, 7000);
}
emailInput.blur(function() { function showErrorNotice(error)
var email = getEMail(); {
var regex = /^([^@\s]+)@(([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2}|aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|post|pro|tel|travel|xxx))$/; var noticeText;
switch (error) {
emailInput.val(email); case 'dns_records_not_found':
noticeText = 'Проверьте e-mail, такой домен не обнаружен.';
if(prev_emailCheck == email) { break;
return false; case 'typed_domain':
noticeText = 'Обнаружена опечатка в домене. Проверьте внимательно указанный вами e-mail.';
break;
case 'wrong_email_format':
noticeText = 'Что-то не так в написании адреса. Возможно, указаны лишние точки или пробелы.<br>Адрес должен иметь вид: <b>имя_пользователя@имя_домена</b> (например <b>somebody@example.com</b>)';
break;
// FIXME
case 'check':
noticeText = 'Идёт проверка адреса e-mail, это может занять несколько секунд.';
default:
// FIXME
noticeText = 'Введённый вами адрес недоступен. Пожалуйста, укажите корректный e-mail.';
break;
} }
showNotice('red', noticeText);
}
// Добавляем CSS и расставляем вызов
$(function() {
$('<link />').attr({
rel: 'stylesheet',
type: 'text/css',
href: '//eterfund.ru/api/email/svcCheckEmail.css'
}).appendTo('head');
prev_emailCheck = email; var prev_emailCheck, checkDoneStatus = 'empty';
checkDoneStatus = 'fail'; var emailInput = $('.svcCheckEmail');
var emailNotice, emailForm = emailInput.closest('form');
if(email.length == 0) { emailForm.submit(function() {
if(noticeShow === true) { if(getEMail() == '') {
clearTimeout(noticeTimer); return false;
emailNotice.css({ marginTop: '-51px' }); }
noticeShow = false; // FIXME
if(checkDoneStatus == 'empty') {
emailInput.blur();
} }
emailInput.removeClass('bg-loader').removeClass('bg-true').removeClass('bg-false'); if(checkDoneStatus != 'done') {
showErrorNotice(checkDoneStatus);
}
if(checkDoneStatus == 'done') {
closeNotice();
return true;
}
/* forbid send */
return false; return false;
} });
inputHeight = parseInt(emailInput.height()) + parseInt(emailInput.css('margin-top')) + parseInt(emailInput.css('margin-bottom'));
showNotice('yellow', 'Идёт проверка адреса e-mail, это может занять несколько секунд.');
checkDoneStatus = 'check';
emailInput.removeClass('bg-true').removeClass('bg-false').addClass('bg-loader');
clearTimeout(noticeTimer);
// Проверяем без отправки на сервер
if(email.length < 5 || !regex.test(email)) {
checkDoneStatus = 'wrong_email_format';
showErrorNotice(checkDoneStatus);
emailInput.removeClass('bg-loader').addClass('bg-false');
return false;
}
// Проверяем на сервере emailInput.blur(function() {
$.ajax({ var email = getEMail();
url: '//eterfund.ru/api/email/svcCheckEmail.php', var regex = /^([^@\s]+)@(([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2}|aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|post|pro|tel|travel|xxx))$/;
type: 'GET',
data: { emailInput.val(email);
email: email
}, if(prev_emailCheck == email) {
error: function() { return false;
showNotice('red', 'Ошибка связи с сервером, проверка адреса не удалась.'); }
emailInput.removeClass('bg-loader').addClass('bg-true');
// FIXME prev_emailCheck = email;
checkDoneStatus = 'fail';
if(email.length == 0) {
if(noticeShow === true) {
clearTimeout(noticeTimer);
emailNotice.css({ marginTop: '-51px' });
noticeShow = false;
}
checkDoneStatus = 'true'; emailInput.removeClass('bg-loader').removeClass('bg-true').removeClass('bg-false');
},
success: function(response) {
try {
response = JSON.parse(response);
if(response.status === false) { return false;
showErrorNotice(response.error); }
emailInput.removeClass('bg-loader').addClass('bg-false');
inputHeight = parseInt(emailInput.height()) + parseInt(emailInput.css('margin-top')) + parseInt(emailInput.css('margin-bottom'));
checkDoneStatus = response.error;
} showNotice('yellow', 'Идёт проверка адреса e-mail, это может занять несколько секунд.');
else { checkDoneStatus = 'check';
showNotice('green', 'Email адрес указан верно.');
emailInput.removeClass('bg-true').removeClass('bg-false').addClass('bg-loader');
clearTimeout(noticeTimer);
// Проверяем без отправки на сервер
if(email.length < 5 || !regex.test(email)) {
checkDoneStatus = 'wrong_email_format';
showErrorNotice(checkDoneStatus);
emailInput.removeClass('bg-loader').addClass('bg-false');
return false;
}
// Проверяем на сервере
$.ajax({
url: '//eterfund.ru/api/email/svcCheckEmail.php',
type: 'GET',
data: {
email: email
},
error: function() {
showNotice('red', 'Ошибка связи с сервером, проверка адреса не удалась.');
emailInput.removeClass('bg-loader').addClass('bg-true'); emailInput.removeClass('bg-loader').addClass('bg-true');
document.CheckEmailHash = response.hash; // FIXME
checkDoneStatus = 'true';
/* emailhash hidden field support */ },
els = document.getElementsByName('emailhash'); success: function(response) {
if (els.length) try {
els[0].value = response.hash; response = JSON.parse(response);
checkDoneStatus = 'done'; if(response.status === false) {
showErrorNotice(response.error);
emailInput.removeClass('bg-loader').addClass('bg-false');
checkDoneStatus = response.error;
}
else {
showNotice('green', 'Email адрес указан верно.');
emailInput.removeClass('bg-loader').addClass('bg-true');
document.CheckEmailHash = response.hash;
/* emailhash hidden field support */
els = document.getElementsByName('emailhash');
if (els.length)
els[0].value = response.hash;
checkDoneStatus = 'done';
}
// FIXME
} catch (e) {
showNotice('yellow', 'Не удалось проверить адрес e-mail.');
emailInput.removeClass('bg-loader').addClass('bg-true');
checkDoneStatus = 'done';
}
} }
// FIXME });
} catch (e) { return true;
showNotice('yellow', 'Не удалось проверить адрес e-mail.');
emailInput.removeClass('bg-loader').addClass('bg-true');
checkDoneStatus = 'done';
}
}
}); });
});
return true; })();
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment