$(function(){ var userCreateForm; if (!userCreateForm) { userCreateForm = { errorFields: {}, goodFields: {}, errorTexts: [], errorFocused: false, errorFieldClassName: 'error_field', goodFieldClassName: 'good_field', ajaxURL: '/ajax.php', email_exists: 0, name_exists: 0, confirm: function() { /* if (typeof(_gaq) != 'undefined') { _gaq.push(['_trackEvent', 'Test User Registration', 'Start JS Form Check']); } */ this.disableSubmitButton(); this.cleanErrors(); this.checkUserData(document.create_user.user_email.value, document.create_user.name.value, this.checkLocalUserData.bind(this)); }, processCheckedUserData: function(){ var form_data = userCreateForm.getFormData(); if (this.haveErrors()) { this.showErrors(); logEvents.log('create_user_form_error', {data: form_data, errors: this.errorTexts}); this.enableSubmitButton(); } else { if (typeof(_gaq) != 'undefined') { _gaq.push(['_trackEvent', 'Test User Registration', 'Success']); } logEvents.log('create_user_form_submit_success', {data: form_data}); var salt = BCCrypt.gensalt(10); var pass = document.create_user.user_password.value; BCCrypt.hashpw( pass, salt, userCreateForm.handlerHashDone ); } }, checkLocalUserData: function() { if (document.create_user.is_expert != undefined) { if (document.create_user.i_agree_expert.checked == false) { this.addError('i_agree_expert', "Du må bekrefte at du har faglig kompetanse innen kosthold", false); } else { this.addGood('i_agree_expert'); } } if (document.create_user.is_expert != undefined) { if (document.create_user.i_agree_privacy.checked == false) { this.addError('i_agree_privacy', "Du må godta Vilkår for bruk og Vilkår for bruk for eksperter", false); } else { this.addGood('i_agree_privacy'); } } if (document.create_user.name.value == '') { this.addError('name', "Kallenavn må fylles ut", true); } else if (this.name_exists == 1) { this.addError('name', "Dette finnes allerede en bruker med dette kallenavnet", true); } else { this.addGood('name'); } // Проверяем email if (document.create_user.user_email.value == '') { this.addError('user_email', "E-post må fylles ut", true); } else if (this.email_exists == 1) { this.addError('user_email', "Du er registrert med denne e-post fra før. Prøv å logge inn eller få tilsendt passord", true); } else { this.addGood('user_email'); } if (document.create_user.user_password.value == '') { this.addError('user_password', "Passord må fylles ut", true); this.addError('user_password2', "", true); } else if (document.create_user.user_password2.value == '') { this.addError('user_password2', "Du må gjenta passordet", true); this.addError('user_password', "", true); } else if (document.create_user.user_password.value != document.create_user.user_password2.value) { this.addError('user_password', "Passordene er forskjellige", true); this.addError('user_password2', "", true); } else if (document.create_user.user_password.value.length < 6) { this.addError('user_password', "Passord må ha minst seks tegn", true); this.addError('user_password2', "", true); } else { this.addGood('user_password'); this.addGood('user_password2'); } // Дата рождения if ($('#birth_day option:selected').val() > 0 && $('#birth_day option:selected').val() <= 31 && $('#birth_mon option:selected').val() > 0 && $('#birth_mon option:selected').val() <= 12 && $('#birth_year option:selected').val() > 1900 && $('#birth_year option:selected').val() <= 3000 ) { this.addGood('birth_day'); this.addGood('birth_mon'); this.addGood('birth_year'); this.addGood('birth'); } else { this.addError('birth_day', "", false); this.addError('birth_mon', "", false); this.addError('birth_year', "", false); this.addError('birth', "Bad Birthday", false); } // Страна if ($('#country_id').attr('type') != 'hidden') { if ($('#country_id option:selected').val() > 0) { this.addGood('country_id'); this.addGood('country'); } else { this.addError('country_id', "", false); this.addError('country', "Bad Country", false); } } // Система измерений if ($('#measure_id').attr('type') != 'hidden') { if ($('#measure_id option:selected').val() > 0) { this.addGood('measure_id'); this.addGood('measure'); } else { this.addError('measure_id', "", false); this.addError('measure', "Bad Measure", false); } } // Активность if ($('#activity_level_id option:selected').val() > 0) { this.addGood('activity_level_id'); this.addGood('activity_level'); } else { this.addError('activity_level_id', "", false); this.addError('activity_level', "Bad Activity Level", false); } // Пол var sexValue = $('#sex option:selected').val(); if (sexValue == 'f' || sexValue == 'm' || sexValue == 'p' || sexValue == 'l') { this.addGood('sex'); } else { this.addError('sex', "Bad Sex", false); } // Таблица питания if ($('#food_table_id').attr('type') != 'hidden') { if ($('#food_table_id option:selected').val() > 0) { this.addGood('food_table_id'); this.addGood('food_table'); } else { this.addError('food_table_id', "", false); this.addError('food_table', "Bad Food Table", false); } } // Проверяем рост if (document.create_user.height.value == '') { this.addError('height', "Høyde må fylles ut", true); } else if (!document.create_user.height.value.match(/^[0-9,.]+$/)) { this.addError('height', "Skriv inn din høyde i centimeter, f.eks.: 173", true); } else if ( ($('#measure_id option:selected').val() == 1 && (document.create_user.height.value < 140 || document.create_user.height.value > 250)) || ($('#measure_id option:selected').val() == 2 && (document.create_user.height.value < 55 || document.create_user.height.value > 98)) || (document.create_user.height.value < 55 || document.create_user.height.value > 250) ) { this.addError('height', "Vennligst angi korrekt høyde", true); } else { this.addGood('height'); } // Проверяем вес if (document.create_user.weight.value == '') { this.addError('weight', "Vekt må fylles ut", true); } else if (!document.create_user.weight.value.match(/^[0-9,.]+$/)) { this.addError('weight', "Skriv inn din vekt i kilogram, f.eks.: 70", true); } else if ( ($('#measure_id option:selected').val() == 1 && (document.create_user.weight.value < 40 || document.create_user.weight.value > 600)) || ($('#measure_id option:selected').val() == 2 && (document.create_user.weight.value < 88 || document.create_user.weight.value > 1320)) || (document.create_user.weight.value < 40 || document.create_user.weight.value > 1320) ) { this.addError('weight', "Vennligst angi korrekt vekt", true); } else { this.addGood('weight'); } if (document.create_user.image_code.value == '') { this.addError('image_code', "Bildekode må fylles ut", true); } else { //this.addGood('image_code'); } this.processCheckedUserData(); }, handlerHashDone: function(hash){ $('#user_password_hash').val(hash); $('#user_password').val(''); $('#user_password2').val(''); document.create_user.submit(); }, getFormData: function() { return { email: document.create_user.user_email.value, name: document.create_user.name.value, birth_day: $('#birth_day option:selected').val(), birth_mon: $('#birth_mon option:selected').val(), birth_year: $('#birth_year option:selected').val(), country: $('#country_id option:selected').text(), country_id: $('#country_id option:selected').val(), activity: $('#activity_level_id option:selected').text(), activity_id: $('#activity_level_id option:selected').val(), sex: $('#sex option:selected').val(), height: document.create_user.height.value, weight: document.create_user.weight.value, image_code: document.create_user.image_code.value }; }, haveErrors: function() { if (this.errorTexts.length > 0) return true; return false; }, cleanErrors: function() { $('#errors').hide(); for (var fieldID in this.errorFields) { $('#'+fieldID).removeClass(this.errorFieldClassName); $('#field_status_'+fieldID).html(' '); } for (var fieldID in this.goodFields) { $('#'+fieldID).removeClass(this.goodFieldClassName); $('#field_status_'+fieldID).html(' '); } $('#errors').html(''); this.errorFields = {}; this.goodFields = {}; this.errorTexts = []; this.errorFocused = false; }, addError: function(fieldID, text, focus) { this.errorFields[fieldID] = true; this.errorTexts[this.errorTexts.length] = text; // Фокусируемся на первом найденном ошибочном поле if (focus && !this.errorFocused) { $('#'+fieldID).focus(); this.errorFocused = true; } }, addGood: function(fieldID) { this.goodFields[fieldID] = true; }, showErrors: function() { if (this.haveErrors()) { for (var fieldID in this.errorFields) { $('#'+fieldID).addClass(this.errorFieldClassName); $('#field_status_'+fieldID).html(''); } for (var fieldID in this.goodFields) { $('#'+fieldID).addClass(this.goodFieldClassName); $('#field_status_'+fieldID).html(''); } var errorsHTML = ''; for (var i in this.errorTexts) { if (this.errorTexts[i] != '') { errorsHTML += '
'+this.errorTexts[i]+'
'; } } $('#errors').html(errorsHTML); $('#errors').show(); } }, redrawImage: function(){ $('#img_code').attr('src', '/image_code.php?rnd='+Math.random()); }, checkUserData: function(email, name, callback){ this.email_exists = 0; this.name_exists = 0; var email_exists = 0; var name_exists = 0; var that = this; $.ajax({ url: this.ajaxURL, dataType: 'json', data: { module: 'user_data', action: 'check_user', email: email, name: name }, success: function(data){ that.email_exists = data.email_exists; that.name_exists = data.name_exists; callback(); } }); }, enableSubmitButton: function(){ var button = $('#button_register'); button.addClass('button_green'); button.removeClass('button_green_disabled'); button.bind('click', function(){ userCreateForm.confirm(); }); $('#button_loading').css('display', 'none'); }, disableSubmitButton: function(){ var button = $('#button_register'); button.addClass('button_green_disabled'); button.removeClass('button_green'); button.unbind('click'); $('#button_loading').css('display', 'inline-block'); }, changeMeasureSystem: function(){ var list = document.create_user.measure_id; var system = list.options[list.selectedIndex].value; $.post( '/ajax.php', {module: 'system', action: 'measure_system_change', measure_system_id: system}, function(data){ if (data == 'ok') { document.create_user.create.value = ''; document.create_user.submit(); } } ); }, changeCountry: function(){ var list = document.create_user.country_id; var country = list.options[list.selectedIndex].value; $.post( '/ajax.php', {module: 'system', action: 'country_change', country_id: country}, function(data){ if (data == 'ok') { document.create_user.create.value = ''; document.create_user.submit(); } } ); }, showHelp: function(layer){ var form_data = userCreateForm.getFormData(); logEvents.log('create_user_form_field_focus', {data: form_data}); $('#'+layer).show(); }, hideHelp: function(layer){ $('#'+layer).hide(); }, populateHelpHandlers: function(){ var that = this; var el2field = { birthday: [ 'birth_day', 'birth_mon', 'birth_year' ] }; var elements = $('#user_create_form span.user_create_help_box'); elements.each(function(i, el){ var help_layer = el.id; var field_id = el.id.replace(/^help_/, ''); if (el2field[field_id] == undefined) { var fieldsList = [field_id]; } else { var fieldsList = el2field[field_id]; } for (var j = 0; j < fieldsList.length; j++) { var field = $('#' + fieldsList[j]); (function(help_layer){ field.bind('focus', function(){ that.showHelp(help_layer); }); field.bind('blur', function(){ that.hideHelp(help_layer); }); })(help_layer); } }); } }; }; window.userCreateForm = userCreateForm; userCreateForm.enableSubmitButton(); $('#img_code').click(function(){ userCreateForm.redrawImage(); }); $('#help_image_redraw').click(function(){ userCreateForm.redrawImage(); }); userCreateForm.populateHelpHandlers(); if (document.create_user) document.create_user.name.focus(); /* User prifile page */ var userProfileForm; if (!userProfileForm) { userProfileForm = { ValidDate: function(y,m,d) { with (new Date(y, m, d)) return getMonth() == m && getDate() == d; }, ReadISO8601date: function(Q) { var T; if ((T = /^(\d+)([-\/])(\d+)(\2)(\d+)$/.exec(Q)) == null) { return - 2; } for (var j = 1; j <= 5; j += 2) { T[j] = + T[j]; } if (!this.ValidDate(T[1], T[3] - 1, T[5])) { return - 1; } return [T[1], T[3], T[5]]; }, DiffY: function(S1, S2) { var D1 = this.ReadISO8601date(S1); if (D1 < 0) { return 0; } var D2 = this.ReadISO8601date(S2); if (D2 < 0) { return 0; } return D1[0] - D2[0] - (D1[1] * 100 + D1[2] < D2[1] * 100 + D2[2]); }, setPhoto: function() { var file = document.user_profile.photo.value; if (file != '') { document.user_profile.action.value = 'set_photo'; document.user_profile.enctype = 'multipart/form-data'; document.user_profile.submit(); } }, // User profile form checking checkUserProfileForm: function() { if ($('#old_user_name').val() != $('#user_name').val()) { userCreateForm.checkUserData('', $('#user_name').val(), this.checkUserProfileFormContinue.bind(this)); } else { this.checkUserProfileFormContinue(); } }, // Continue user profile form checking checkUserProfileFormContinue: function() { var cur_date = new Date(); var age = this.DiffY(cur_date.getFullYear() + '-' + (cur_date.getMonth()+1) + '-' + cur_date.getDate(), document.user_profile.birthday.value); if (age < 18) { alert('You Must 18 or Greather Age'); } else if ($('#old_user_name').val() != $('#user_name').val() && userCreateForm.name_exists == '1') { alert('Det finnes allerede en bruker med dette aliaset.'); } else { document.user_profile.submit(); } } }; } window.userProfileForm = userProfileForm; });