/**
* Created by iry on 16.10.2015.
*/
$(document).ready(function() {
/**
* This function make ajax request to server by method POST.
* Used jQuery $.post method. Simple to add new form for ajax request.
* @var event - default.
* @var objectFrom - form object selected by name parameter.
* @var linkToScript - link to PHP script.
* */
function formAjaxPost(event, objectForm, linkToScript, buttonID) {
event.preventDefault(); // block default moves by browser
var serializeData = objectForm.serialize(); //select data from form fields
var button = $('#' + buttonID);
var oldText = button.html();
$('#msg-block').remove(); //delete block, if he has already been created
var messageBlock = ''; //message block in HTML page
document.cookie = "phone=" + $(objectForm).find("input[name='phone']").val() + "; path=/;";
document.cookie = "email=" + $(objectForm).find("input[name='email']").val() + "; path=/;";
document.cookie = "time=" + $(objectForm).find("select[name='call-time']").val() + "; path=/;";
$.ajax({
type: "POST",
url: linkToScript,
cache: false,
data: serializeData,
beforeSend: function() {
button.prop('disabled', true);
button.html('
');
},
success: function(JSONResponse) {
var responce = JSON.parse(JSONResponse);
if (responce && responce.result == 'ok') {
window.location.replace('/thank-you');
}
},
complete: function(JSONResponse) {
button.prop('disabled', false);
button.html(oldText);
},
error: function(JSONResponse) {
messageBlock += '
';
messageBlock += '
Произшла ошибка, попробуйте повторить запрос позже.
';
$(messageBlock).insertBefore(objectForm); //input block above form
}
});
}
//handler
$(function() {
//smooth scrolling after button click to section form
$('.btn-scroll').on('click', function(e) {
$('html,body').stop().animate({ scrollTop: $('#section-fill-form').offset().top }, 1000);
e.preventDefault();
});
});
//Fill form section submit data
$("#fill-form").submit(function(event) {
var linkToScript = '../functions/section_fill_form.php';
formAjaxPost(event, $(this), linkToScript, 'btn-fill-form');
});
$("#fill-form-2").submit(function(event) {
var linkToScript = '../functions/section_fill_form.php';
formAjaxPost(event, $(this), linkToScript, 'btn-fill-form-2');
});
//Modal get call submit data
$('#get-call, #get-call-land').submit(function(event) {
var linkToScript = '../functions/modal-get-call.php';
formAjaxPost(event, $(this), linkToScript, 'btn-get-call');
});
//Modal write us submit data
$('#write-us').submit(function(event) {
var linkToScript = '../functions/modal-write-us.php';
formAjaxPost(event, $(this), linkToScript, 'btn-write-us');
});
//Modal write us submit data
$('#get-program').submit(function(event) {
var linkToScript = '../functions/modal-get-program.php';
formAjaxPost(event, $(this), linkToScript, 'btn-get-program');
});
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
var a = getUrlParameter('adch');
if (a !== '') {
deleteCookie('adch');
var date = new Date;
date.setDate(date.getDate() + 30);
document.cookie = "adch=" + a + "; expires=" + date;
}
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
function setCookie(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires == "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires * 1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for (var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
}
function deleteCookie(name) {
setCookie(name, "", {
expires: -1
})
}
var referrer = getCookie('referrer');
if (typeof referrer === 'undefined') {
setCookie('referrer', document.referrer);
}
var queryString = getCookie('query_string');
if (typeof queryString === 'undefined') {
setCookie('query_string', location.search);
}
});
var short_height = 130;
$('#section-text-reviews .review-text').on('click', function() {
if ($(this).find('div').outerHeight() == short_height) {
$(this).find('div').height('100%');
var full_height = $(this).find('div').outerHeight();
$(this).find('div').height(short_height);
$(this).find('div').animate({ height: full_height }, 300);
$('.carousel').carousel('pause');
} else {
$(this).find('div').animate({ height: short_height }, 300);
$('.carousel').carousel();
}
})
$(document).mouseup(function(e) {
var div = $('#section-text-reviews .review-text');
if (!div.is(e.target) && div.has(e.target).length === 0) {
div.find('div').animate({ height: short_height }, 300);
$('.carousel').carousel();
}
});
$('#next').click(function() {
$('.carousel').carousel('next');
})
$('#prev').click(function() {
$('.carousel').carousel('prev');
})
function calcCarouselPage() {
var currentPage = $('.carousel div.active').index() + 1;
$('#currentPage').text(currentPage);
}
$(function() {
var totalPages = $('.carousel .item').length;
$('#totalPages').text(totalPages);
calcCarouselPage();
})
$('.carousel').on('slid.bs.carousel', function() {
calcCarouselPage();
});
$('.modal').on('show.bs.modal', function(e) {
$('#get-call-fixed').css('right', '47px');
})
$('.modal').on('hidden.bs.modal', function(e) {
$('#get-call-fixed').css('right', '30px');
})
$('#education').on('change', function() {
if ($(this).val() === 'Среднее') {
$('#exam_list').slideDown();
$('#exam_mark').slideDown();
$('#exam_list input').removeAttr('disabled');
$('#exam_mark input').removeAttr('disabled');
} else {
$('#exam_list').slideUp();
$('#exam_mark').slideUp();
$('#exam_list input').attr('disabled', 'disabled');
$('#exam_mark input').attr('disabled', 'disabled');
}
})
$('#it-modal-2').on('click', function(e) {
e.preventDefault();
$('#it-modal-label').text('Информатика и вычислительная техника с профилем подготовки «Системы автоматизированного проектирования»');
$('#it-modal-text').show();
$('#it-modal-list').hide();
});
$('#it-modal-3').on('click', function(e) {
e.preventDefault();
$('#it-modal-label').text('Прикладная информатика c профилем подготовки «Прикладная информатика в экономике»');
$('#it-modal-text').show();
$('#it-modal-list').hide();
});
$('#it-modal-4').on('click', function(e) {
e.preventDefault();
$('#it-modal-label').text('Программная инженерия');
$('#it-modal-text').show();
$('#it-modal-list').hide();
});
$('#it-modal').on('hide.bs.modal', function() {
$('#it-modal-label').text('Информатика и вычислительная техника');
$('#it-modal-text').hide();
$('#it-modal-list').show();
});
$('#it-modal-back').on('click', function(e) {
e.preventDefault();
$('#it-modal-label').text('Информатика и вычислительная техника');
$('#it-modal-text').hide();
$('#it-modal-list').show();
});
var currentStep = 0;
var steps = ['speciality', 'education', 'citizenship', 'city', 'health', 'ege', 'send', 'sended'];
var stepsDesc = ['Специальность', 'Образование', 'Гражданство РФ', '', 'Ограничения по здоровью', 'Наличие ЕГЭ', 'Предложение', ''];
var hasError = false;
var orderName = '';
var orderEmail = '';
var orderPhone = '';
var orderLastedu = '';
var orderCitizenship = '';
var orderCity = '';
var orderCityCode = '';
var orderHealth = '';
var orderEge = '';
var orderComment = '';
var form = '';
var speciality;
var showEge = true;
function openGetDirectionDialog() {
openDialog('distdirection');
}
function openGetOneDirectionDialog() {
$("#step1-modal").modal("hide");
speciality = 'Помочь с выбором';
openDialog('distdirection', 1);
}
function openGetPriceDialog(specialitiesFor) {
openDialog('distprice', null, null, specialitiesFor);
}
function openIVTGetPriceDialog() {
$('#it-modal').modal('hide');
$('#it-modal').on('hidden.bs.modal', function() {
speciality = 'Информатика и ВТ';
openDialog('distprice', 1, 'Специальность: ' + speciality + "\r\n");
});
}
function openDialog(_form, step, comment, specialitiesFor) {
currentStep = step ? step : 0;
orderName = '';
orderEmail = '';
orderPhone = '';
orderLastedu = '';
orderCitizenship = '';
orderCity = '';
orderCityCode = '';
orderHealth = '';
orderEge = '';
orderComment = comment ? comment : '';
form = _form;
showEge = true;
if (specialitiesFor) {
$('.speciality-btn').hide(0, function() {
$('.speciality-' + specialitiesFor).show();
});
}
$('#get-cost-modal').modal();
showCurrentStep();
}
$(function() {
$('#get-cost-modal .modal-body button').on('click', function() {
var step = steps[currentStep];
var value = $(this).text().replace(/\r?\n/g, "").replace(/\s{2,}/g, " ");
hasError = false;
switch (step) {
case 'speciality':
speciality = value;
orderComment += stepsDesc[currentStep] + ': ' + value + "\r\n";
break;
case 'education':
value = '';
$('#get-cost-modal input[name=education]:checked').each(function() {
value += (value ? ', ' : '');
value += $('label[for=' + $(this).attr('id') + ']').text();
});
orderLastedu = value;
orderComment += stepsDesc[currentStep] + ': ' + value + "\r\n";
if (value.indexOf('Учусь в школе') !== -1) {
window.location.replace('/thank-you-fail');
}
if (value.indexOf('Среднее (полное) общее образование') === -1) {
showEge = false;
}
if (value === '') {
hasError = true;
}
break;
case 'citizenship':
orderCitizenship = value;
orderComment += stepsDesc[currentStep] + ': ' + value + "\r\n";
if (orderCitizenship === 'Нет') {
showEge = false;
}
break;
case 'city':
orderCity = getValue('city', true);
orderCityCode = getValue('city_code');
value = orderCity;
break;
case 'health':
orderHealth = value;
orderComment += stepsDesc[currentStep] + ': ' + value + "\r\n";
if (orderHealth === 'Да') {
showEge = false;
}
break;
case 'ege':
orderEge = value;
if (orderEge === 'Нет') {
window.location.replace('/thank-you-fail');
return;
}
orderComment += stepsDesc[currentStep] + ': ' + value + "\r\n";
break;
case 'send':
orderName = getValue('name', true);
orderEmail = getValue('email');
orderPhone = getValue('phone');
if ((orderEmail === '' && orderPhone === '') || getParams['utm_source'] === 'postupi_online') {
getValue('email', true);
getValue('phone', true);
}
value = orderName + ' ' + orderEmail + ' ' + orderPhone;
if (!hasError && orderPhone !== '') {
var request = '';
$('#get-cost-modal input[name=request]:checked').each(function() {
request += (request ? ', ' : '');
request += $(this).val();
});
}
break;
}
if (hasError) {
return;
}
$.ajax({
type: 'POST',
url: '/functions/modal-get-cost-step.php',
data: { step: step, value: value },
});
if (step === 'send' && orderName && orderCity) {
document.cookie = "speciality=" + speciality + "; path=/;";
document.cookie = "phone=" + orderPhone + "; path=/;";
document.cookie = "email=" + orderEmail + "; path=/;";
$.ajax({
type: 'POST',
url: '/functions/modal-get-cost.php',
data: {
orderName: orderName,
orderEmail: orderEmail,
orderPhone: orderPhone,
orderSpeciality: speciality,
orderLastedu: orderLastedu,
orderCitizenship: orderCitizenship,
orderCity: orderCity,
orderCityCode: orderCityCode,
orderHealth: orderHealth,
orderEge: orderEge,
orderComment: orderComment,
form: form,
referer: $('input[name=referer]').val(),
utm: $('input[name=utm]').val()
},
}).done(function(response) {
orderName = '';
orderEmail = '';
orderPhone = '';
orderLastedu = '';
orderCitizenship = '';
orderCity = '';
orderCityCode = '';
orderHealth = '';
orderEge = '';
orderComment = '';
});
ym(33476283,'reachGoal','price_form');
window.location.replace('/thank-you');
} else {
if (step === 'health' && !showEge) {
currentStep++;
}
currentStep++;
showCurrentStep();
}
})
});
function showCurrentStep() {
$('#get-cost-modal .get-cost-step').hide();
$('#get-cost-modal .' + steps[currentStep]).fadeIn();
}
function showOneCurrentStep() {
$('#get-cost-modal .get-cost-step').hide();
$('#get-cost-modal .' + steps[1]).fadeIn();
}
function getValue(fieldName, required) {
var input = $('#get-cost-modal input[name=' + fieldName + ']');
var value = input.val();
if (value === '' && required) {
input.parent().addClass('has-error');
hasError = true;
} else {
input.parent().removeClass('has-error');
}
return value;
}
var getParams = window
.location
.search
.replace('?', '')
.split('&')
.reduce(
function(p, e) {
var a = e.split('=');
p[decodeURIComponent(a[0])] = decodeURIComponent(a[1]);
return p;
}, {}
);
function customMask(object) {
let codes2 = ["992", "993", "998"];
let codes3 = ["994", "995", "996"];
let masks = ["+0 (000) 000-00-0000", "+00 (000) 000-00-000", "+000 (000) 000-00-00", "+000 (00) 000-0000", "+000 (000) 000-000"];
let getMask = function(val) {
let clearVal = val.replace(/\D+/g, "");
let code = clearVal.substring(0, 3);
if (codes2.includes(code)) {
return masks[3];
}
if (codes3.includes(code)) {
return masks[4];
}
return clearVal.length < 12 ? masks[0] : (clearVal.length === 12 ? masks[1] : masks[2]);
};
if (typeof object.val() !== "undefined") {
object.mask(getMask(object.val()), {
onKeyPress: function(val, e, field, options) {
if (e.keyCode !== 8 && e.keyCode !== 46) { //backspace && delete
object.mask(getMask(val), options);
}
}
});
}
}