$(document).ready(function () {
  var ouremail = true;
  var mysecret = false;
  var defselect = $('#secret_td').html();
  
  $('#emaildiv').html('<select id="emaildomain" name="emaildomain" class="formselect" style="width: 260px"><option value="data.bg">'+ $('#databg_user').val() +'@data.bg</option></select>');
  
  var wrongemail = $('#wrongemail');
  $("#regform").validate({
    errorPlacement: function(error, element) {
      error.appendTo( element.parent("td").next("td") );
     },
    rules: {
      email: {
        required: false,
        email: true
      }
    },
    messages: {
      email: {
        email: '<img src="/imgs/icon_input_notokkk.gif"/>'
      }
    }
  });
  
  $("#secretq").change( function() {
    if (this.value == 'own') {
      mysecret = true;
      $('#secret_td').html(defselect + '<br/><br/><input type="text" id="mysecretq" name="mysecretq"/>');
      $('#secretq').selectOptions('own', true);
      $('#secretq').blur();
      $('#mysecretq').focus();
    } else {
      if (mysecret == true) {
        $('#secret_td').html(defselect);
        mysecret = false;
      }
    }
  });
  
  var validateUsernameImg = $('#showtakenimg');
  var validateUsernameTxt = $('#showtakentxt');
  var username;
  $('#databg_user').keyup(function () {
    var t = this; 
    username = this.value;
    if (ouremail == true) {
      emaildiv.html('<select id="emaildomain" name="emaildomain" class="formselect" style="width: 260px"><option value="data.bg">'+ username +'@data.bg</option></select>');
    }
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      
      if (t.value.length < 3) {
        validateUsernameImg.html('<img src="/imgs/icon_input_notokkk.gif"/>')
        return false;
      }
      
      validateUsernameImg.html('<img src="/imgs/icon_input_loading.gif" />');
      this.timer = setTimeout(function () {
        $.ajax({
          url: '/register/check/check/'+ t.value,
          data: '',
          dataType: 'json',
          type: 'get',
          success: function (j) {
            
            if (j.ok) {
              validateUsernameImg.html('<img src="/imgs/icon_input_ok.gif"/>')
              validateUsernameTxt.css('visible:none');
              //validateUsernameTxt.html('<b style="color: green;">'+ j.msg +'</b>')
            } else {
              validateUsernameImg.html('<img src="/imgs/icon_input_notokkk.gif"/>')
              validateUsernameTxt.html('<b style="color: red;">'+ j.msg +'</b>')
              validateUsernameTxt.css('visible:visible');
            }
          }
        });
      }, 500);
      
      this.lastValue = this.value;
    }
  });
  
  var emaildiv = $('#email');
  $('#wantemail').click(function () {
    if (!username) {
      username = '';
    }
    if (ouremail == true) {
      emaildiv.html('<input type="text" id="email" name="email"/>');
      emaildiv.focus();
      $('#wantemail').blur();
      ouremail = false;
    } else {
      emaildiv.html('<select id="emaildomain" name="emaildomain" class="formselect" style="width: 260px"><option value="data.bg">'+ username +'@data.bg</option></select>');
      ouremail = true;
    }
  });
  
  $('#country').change(function() {
    if ($(this).val() != 'bg') {
      $('#citytr').css('display', 'none');
    } else {
      $('#citytr').css('display', '');
    }
  });
});
