/*lang:*/
(function ($) {
"use strict";
if (window.hasOwnProperty('register')) return;
var Register = function () {
var o = this;
$(document).ready(function () {
o.initialize();
});
};
var p = Register.prototype;
var self = false;
var _initialized = false;
Register.base_url = 'https://signup.crma.pro';
Register.default_url = '/';
Register.IS_SANDBOX = false;
p._requests = null;
p.initialize = function() {
if (_initialized) return;
_initialized = true;
console.log('Initialize...');
self = this;
var o = this;
o._requests = {};
o._form = $('form#register-form[name="registerform"]');
self.isFree = o._form.hasClass('register-intrum-free');
o._form.off('submit').on('submit', function () {
o.submited();
return false;
});
o._form.find('input[type="text"],select')
.val('')
.off('change')
.change(o._onchange)
.off('keydown')
//.off('keypress')
.keydown(o._onchange);
var jFinalSubmitBtn = o._form.find('#register_final_btn div input');
jFinalSubmitBtn.data('label', jFinalSubmitBtn.val());
var ref = o._getParameterByName('ref');
if (ref !== null && ref !== '') {
o._setCookie('ref', ref);
}
var code = o._getParameterByName('code');
if (code !== null && code !== '') {
o._setCookie('code', code);
}
// Обработчик на галочку "У меня есть промокод"
o._form.find('input.use-promo-checkbox').off('change').on('change', function () {
if ($(this).is(':checked')) {
o._form.find('.promo-code-container').show();
o._form.find('input#promo-code').val('').prop('disabled', false);
o._form.find('input.use-promo-checkbox').data('consider', true);
} else {
o._form.find('.promo-code-container').hide();
o._form.find('.promo-code-under').hide();
}
});
o._form.find('input#promo-code').off('keyup').on('keyup', function () {
o._form.find('.promo-code-under').show();
o._form.find('.promo-code-under .code-apply-result').hide().html('');
o._form.find('.promo-code-under .apply-promo-code').show();
});
o._form.find('.promo-code-under .apply-promo-code').off('click').on('click', function () {
var isAutoapply = $(this).data('auto-apply');
var codeInput = o._form.find('input#promo-code');
var postData = {
'applyCode': codeInput.val().trim()
};
if (!postData.applyCode.length) {
o._form.find('.promo-code-parent-container').show();
return;
}
o._form.find('.promo-code-under').show().find('.code-apply-result').show().html('');
$(this).hide();
o.request('/ajax', postData, function (result) {
//console.log('applyCode', result);
if (isAutoapply) {
if (result && result.success && result.show_reward) {
o._form.find('.promo-code-parent-container').show();
} else {
o._form.find('.promo-code-parent-container').remove();
}
}
var resultText = '';
if (result && result.success && result.hasOwnProperty('reward')) {
if (result.reward > 0) {
resultText = "You will receive %% of bonuses".replace('%%', result.reward);
} else {
resultText = 'Code successfully applied';
}
o._form.find('input#promo-code').prop('disabled', true);
} else {
if (isAutoapply) {
resultText = 'Invalid promo code or partner code';
} else {
resultText = 'Failed to apply the code.
Check your entries and try again';
}
o._form.find('input#promo-code').prop('disabled', false);
}
o._form.find('.promo-code-under .code-apply-result').show().html(resultText);
}, function (error) {
console.error('applyCode', error);
o._form.find('.promo-code-under .code-apply-result').show().html('Failed to apply code');
});
});
var existsCode = o._getCookie('code') || o._getCookie('ref') || code || ref;
if (existsCode) {
o._form.find('input.use-promo-checkbox').prop('checked', true).trigger('change');
o._form.find('input#promo-code').val(existsCode).prop('disabled', true);
o._form.find('input.use-promo-checkbox').data('consider', true);
o._form.find('.promo-code-under .apply-promo-code').data('auto-apply', true).trigger('click').data('auto-apply', false);
} else {
o._form.find('.promo-code-parent-container').show();
}
/*
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
options.crossDomain ={
crossDomain: true
};
options.xhrFields = {
withCredentials: true
};
});
*/
}
p._onchange = function (e) {
var curr = $(this).val();
var prev = ($(this).data('prevValue') || '');
if (curr != prev) {
var errorField = $(this).next();
if (errorField.hasClass('error')) {
errorField.remove();
}
if ($(this).attr('name') === 'email') {
window.register.emailTest(this);
}
if ((e.type === 'keydown' || e.type === 'keypress') && ($(this).attr('name') === 'domain' || $(this).attr('name') === 'company')) {
self._form.find('#register_final_btn input.btn[type="submit"]').prop('disabled', true);
}
}
$(this).data('prevValue', curr);
}
p._getParameterByName = function (name) {
var url = window.location.href;
/*
var referrer = (document.referrer || '');
var deniedRefs = ['ya.ru', 'yandex.ru', 'yandex.com', 'google.ru', 'google.com'];
var deniedRefsFired = false;
for (var i = 0; i < deniedRefs.length; i++) {
deniedRefsFired = deniedRefsFired || (referrer.indexOf(deniedRefs[i]) != -1);
if (deniedRefsFired) {
return '';
}
}
*/
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)");
var results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
p._setCookie = function(key, value) {
var o = this;
o.log('set cookie: ' + key + ':' + value);
var expires = new Date();
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';path=/;expires=' + expires.toUTCString();
}
p._getCookie = function(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
p.log = function (msg, data) {
if (Register.IS_SANDBOX) {
if (typeof data === 'undefined') {
console.log(msg);
} else {
console.log(msg, data);
}
}
}
p.request = function(url, params, onSuccess, onError, options) {
var o = this;
var jBtn = o._form.find('#register_final_btn div input');
var request_type = 'post';
var _unique_id = (new Date()).getTime();
var _url = (typeof url === 'undefined' || url === false) ? o.default_url : url;
_url = Register.base_url + _url;
o.log('Requested _url[ ' + _unique_id + ' ] = ' + _url);
jBtn.prop('disabled', true);
if (o.__btnDisableTimeout) clearTimeout(o.__btnDisableTimeout);
o.__btnDisableTimeout = setTimeout(function(){
jBtn.prop('disabled', true).val(' ').next().show();
}, 700);
$.ajax({
url: _url,
type: request_type,
dataType: 'json',
data: params,
xhrFields: {
withCredentials: true
},
crossDomain: {
crossDomain: true
},
success: function(data) {
if (o.__btnDisableTimeout) clearTimeout(o.__btnDisableTimeout);
o.log('ajax OK');
if (typeof onSuccess === 'function') {
try { onSuccess(data); } catch (e) {}
}
//var jBtn = self._form.find('#register_final_btn input.btn[type="submit"]');
jBtn.prop('disabled', false).removeAttr('disabled').val(jBtn.data('label')).next().hide();
},
error: function(data) {
if (o.__btnDisableTimeout) clearTimeout(o.__btnDisableTimeout);
o.log('ajax FAIL', data);
if (typeof onError === 'function') {
try { onError(data); } catch (e) {}
}
//var jBtn = self._form.find('#register_final_btn input.btn[type="submit"]');
jBtn.prop('disabled', false).removeAttr('disabled').val(jBtn.data('label')).next().hide();
}
});
}
p.__notEmpty = function(str) {
return (typeof str !== 'undefined' && str != null && str != '');
}
p.genFieldError = function(fieldName, text) {
var o = this;
o.clearFieldError(fieldName);
var jLabel = $('');
if (fieldName == 'domain') {
jLabel.insertAfter(o._form.find('[name="' + fieldName + '"]').parent());
} else if (fieldName == 'i_accept_terms_and_privacy_policy') {
jLabel.insertAfter(o._form.find('[name="' + fieldName + '"]').next());
} else {
jLabel.insertAfter(o._form.find('[name="' + fieldName + '"]'));
}
}
p.clearFieldError = function(fieldName) {
var o = this;
var jLabel = o._form.find('label.error[for="' + fieldName + '"]');
jLabel.remove();
}
p.checkFields = function(step) {
var o = this;
var errorText = '';
var errors = [];
var post = {};
var _step = (typeof step === 'undefined') ? 1 : 2;
var regexCompany = new RegExp(/[^a-zа-яA-ZА-Я0-9ёЁ \\\"\\\-\&]+/i);
var regexName = new RegExp(/[^a-zа-я0-9ё ]+/i);
var regexEmail = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
var regexPhone = new RegExp(/[^0-9\)\(\- ]+/i);
var regexDomain = new RegExp(/[^a-z0-9]+/i);
o._form.find('label.error[for]').remove();
if (_step >= 1 || self.isFree) {
// Step 1
var elIntrumType = o._form.find('select[name="intrum_type"]');
if (elIntrumType.length == 0) {
elIntrumType = o._form.find('input[name="intrum_type"]');
}
post.intrum = elIntrumType.val();
if (post.intrum == '') {
errorText = 'Select industry version';
errors.push(errorText);
o.genFieldError('intrum_type', errorText);
}
post.name = o._form.find('input#name').val();
if (post.name == '') {
errorText = 'Contact person not specified';
errors.push(errorText);
o.genFieldError('name', errorText);
} else if (regexName.test(post.name)) {
errorText = 'Name can contain characters either from A to Z, or from А to Я, and spaces only';
errors.push(errorText);
o.genFieldError('name', errorText);
}
post.email = o._form.find('input#email').val();
if (post.email == '') {
errorText = 'Email address not specified';
errors.push(errorText);
o.genFieldError('email', errorText);
} else if (!regexEmail.test(post.email)) {
errorText = 'Email address invalid';
errors.push(errorText);
o.genFieldError('email', errorText);
}
post.phone = o._form.find('input#phone').val();
if (post.phone == '') {
errorText = 'Phone number not specified';
errors.push(errorText);
o.genFieldError('phone', errorText);
} else {
post.phone = post.phone.replace(/\D+/g, "");
if (post.phone.length == 0) {
errorText = 'Phone number invalid';
errors.push(errorText);
o.genFieldError('phone', errorText);
}
/*
if (post.phone.length == 11) {
if ( post.phone.substring(0, 1) == '8' ) {
post.phone = '7' + post.phone.substring( 1 );
}
if (post.phone.substring(0, 1) != '7') {
errorText = 'Phone number invalid';
errors.push(errorText);
o.genFieldError('phone', errorText);
}
} else {
errorText = 'Phone number invalid';
errors.push(errorText);
o.genFieldError('phone', errorText);
}
*/
}
if (!self.isFree) {
if (o._form.find('#terms_and_policy_accepted').length > 0) {
post.i_accept_terms_and_privacy_policy = 0;
if (!o._form.find('#terms_and_policy_accepted').is(':checked')) {
errorText = 'Kindly agree to the Terms of Service and Privacy Policy';
errors.push(errorText);
o.genFieldError('i_accept_terms_and_privacy_policy', errorText);
} else {
post.i_accept_terms_and_privacy_policy = 1;
o.clearFieldError('i_accept_terms_and_privacy_policy');
}
}
var usePromoCheckbox = o._form.find('input.use-promo-checkbox');
if (usePromoCheckbox.length && usePromoCheckbox.data('consider')) {
post.force_use_promo = usePromoCheckbox.is(':checked');
if (o._form.find('input#promo-code').length) {
post.force_promo_code = o._form.find('input#promo-code').val();
}
}
} else {
post.force_use_promo = true;
if (o._form.find('input#promo-code').length) {
post.force_promo_code = o._form.find('input#promo-code').val();
}
}
}
if (!self.isFree) {
if (_step >= 2) {
// Step 2
post.company = o._form.find('input#company').val();
if (post.company == '') {
errorText = 'Please specify the company name';
errors.push(errorText);
o.genFieldError('company', errorText);
}
post.domain = o._form.find('input#domain').val().toLowerCase();
if (post.domain == '') {
errorText = 'Domain not specified';
errors.push(errorText);
o.genFieldError('domain', errorText);
} else if (post.domain.length < 3) {
errorText = 'Domain name can not be shorter than 3 characters';
errors.push(errorText);
o.genFieldError('domain', errorText);
} else if (post.domain.length > 15) {
errorText = 'Domain name can`t be longer than 15 symbols';
errors.push(errorText);
o.genFieldError('domain', errorText);
} else if (regexDomain.test(post.domain)) {
errorText = 'Domain name can contain characters from A to Z and digits only';
errors.push(errorText);
o.genFieldError('domain', errorText);
}
}
}
post.ref = o._form.find('input[name="ref"]').val();
return {
'errors': errors,
'post': post
};
}
p.checkDomain = function() {
var o = this;
var domainInput = o._form.find('input#domain');
var postData = {
'domainCheck': domainInput.val().toLowerCase()
};
var timer = domainInput.data('timer');
if (timer) clearTimeout(timer);
domainInput.data('timer', setTimeout(function(){
o.request('/ajax', postData, function (result) {
o.log('domainCheck success', result);
if (result && result.result === true) {
//
o.clearFieldError('domain');
domainInput.css({'border': '1px solid #00ff00'});
} else {
if (result && result.text !== false) {
o.genFieldError('domain', result.text);
}
domainInput.css({'border': '1px solid #ff0000'});
}
}, function (data) {
o.log('domainCheck error', data);
o.genFieldError('domain', 'Domain name verification error');
});
}, 700));
}
p.strTranslit = function(el) {
var o = this;
var inp_el = document.getElementById('domain');
var A = new Array();
A["Ё"]="YO";A["Й"]="I";A["Ц"]="TS";A["У"]="U";A["К"]="K";A["Е"]="E";A["Н"]="N";A["Г"]="G";A["Ш"]="SH";A["Щ"]="SCH";A["З"]="Z";A["Х"]="H";A["Ъ"]="";
A["ё"]="yo";A["й"]="i";A["ц"]="ts";A["у"]="u";A["к"]="k";A["е"]="e";A["н"]="n";A["г"]="g";A["ш"]="sh";A["щ"]="sch";A["з"]="z";A["х"]="h";A["ъ"]="";
A["Ф"]="F";A["Ы"]="I";A["В"]="V";A["А"]="A";A["П"]="P";A["Р"]="R";A["О"]="O";A["Л"]="L";A["Д"]="D";A["Ж"]="ZH";A["Э"]="E";
A["ф"]="f";A["ы"]="i";A["в"]="v";A["а"]="a";A["п"]="p";A["р"]="r";A["о"]="o";A["л"]="l";A["д"]="d";A["ж"]="zh";A["э"]="e";
A["Я"]="YA";A["Ч"]="CH";A["С"]="S";A["М"]="M";A["И"]="I";A["Т"]="T";A["Ь"]="";A["Б"]="B";A["Ю"]="YU";
A["я"]="ya";A["ч"]="ch";A["с"]="s";A["м"]="m";A["и"]="i";A["т"]="t";A["ь"]="";A["б"]="b";A["ю"]="yu";
A[" "]="";A["\""]="";A["\'"]="";A["*"]="";A["("]="";A[")"]="";A["+"]="";A["/"]="";A["\\"]="";
A["|"]="";A[","]="";A["="]="";A["-"]="";A["#"]="";A["@"]="";A["*"]="";A["&"]="";A["!"]="";A["?"]="";
inp_el.value = el.value.toLowerCase().replace(/([\u0410-\u0451 "'*()+\/\\|,\?\*\&!=\-\#\@])/g,
function (str,p1,offset,s) {
if (A[str] != 'undefined'){return A[str];}
}
).toLowerCase();
o.checkDomain();
}
p.emailTest = function(el) {
var o = this;
var jEmail = $(el);
var email = jEmail.val().toLowerCase();
if (email == '') return;
var timer = jEmail.data('timer');
if (timer) clearTimeout(timer);
var postData = {
'isFree': (self.isFree ? 1 : 0),
'emailCheck': email
};
$('div#form_register_btn input').prop('disabled', true);
jEmail.data('timer', setTimeout( function(){
o.request('/ajax', postData, function (result) {
var jEmail = $(el);
if (result.result === true) {
//enableRows();
o.clearFieldError('email');
} else {
//disableRows();
o.genFieldError('email', result.text);
}
$('div#form_register_btn input').prop('disabled', false);
}, function () {
//
});
}, 700) );
jEmail.data('prevValue', jEmail.val());
}
p.commonError = function (text) {
var o = this;
o._form.find('.contact-form-msg').html(text).css('color', '#f00');
}
p.submited = function () {
var o = this;
var post = {};
o.log('Submited');
if (self.isFree) {
var isEmailOk = ($('label.error[for="email"]').length == 0);
post = o.checkFields();
if (post.errors.length == 0 && isEmailOk) {
$('.register-step[step="0"]').hide();
$('.register-step[step="1"]').hide();
$('.register-step[step="99"]').show();
var ref = o._getCookie('ref');
var code = o._getCookie('code');
post = post.post;
//post.trial = 1;
post.ajax = 1;
if (ref) post.cookie_ref = ref;
if (code) {
post.cookie_code = code;
post.partner = code;
}
o.request('/realtyfree', post, function (data) {
o.log('success', data);
if (data.hasOwnProperty('error')) {
o.commonError(data.error);
$('.register-step[step="0"]').show();
$('.register-step[step="1"]').show();
$('.register-step[step="99"]').hide();
} else {
/*
if (data.hasOwnProperty('ruid')) {
if (window.hasOwnProperty('sRdC')) {
$('body').append('');
try { window.sRdC(); } catch (e) { }
}
}
*/
if (data.hasOwnProperty('redirect_url')) {
setTimeout(function() {
window.location = data.redirect_url;
}, 3000);
}
}
}, function (data) {
o.log('error', data);
});
} else if (!isEmailOk) {
o.emailTest($('input.form-control#email[name="email"]'));
}
return false;
}
if (o._form.find('.register-step[step="0"]').is(':visible') === true) {
o.log('step 1');
var isEmailOk = ($('label.error[for="email"]').length == 0);
post = o.checkFields();
if (post.errors.length == 0 && isEmailOk) {
o._form.find('.register-step[step="0"]').hide();
o._form.find('.register-step[step="1"]').show();
} else if (!isEmailOk) {
o.emailTest($('input.form-control#email[name="email"]'));
}
} else {
o.log('step 2');
post = o.checkFields(2);
if (post.errors.length == 0) {
o.log('post', post);
var ref = o._getCookie('ref');
var code = o._getCookie('code');
post = post.post;
//post.trial = 1;
post.ajax = 1;
if (ref) post.cookie_ref = ref;
if (code) {
post.cookie_code = code;
post.partner = code;
}
$('.register-step[step="0"]').hide();
$('.register-step[step="1"]').hide();
$('.register-step[step="99"]').show();
o.request('/' + (post.intrum || 'demo'), post, function (data) {
o.log('success', data);
if (data.hasOwnProperty('error')) {
o.commonError(data.error);
$('.register-step[step="1"]').show();
$('.register-step[step="99"]').hide();
} else {
//array( 'id' => $last_id, 'title' => $company, 'http_host' => $domain, 'request_key' => $preview_key)
if (data.hasOwnProperty('ruid')) {
if (window.hasOwnProperty('sRdC')) {
$('body').append('');
try { window.sRdC(); } catch (e) { }
}
}
if (data.hasOwnProperty('redirect_url')) {
setTimeout(function() {
window.location = data.redirect_url;
}, 3000);
}
/*
var jSuccess = $('\