$(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 += '