$(function(){ 
      $('#read').click(function() {
            $('#rules').toggle();
      });
})

$(function(){ 
      $('#add_user').click(function() {
      $('.error-message').html('');
      var is_agree = 0;
      var is_abonent = 0;
      if ($('#is_read_rules').is(":checked")){
            is_agree = 1;      
      }
      if ($('#is_abonent').is(":checked")){
            is_abonent = 1;
      }
            $.post('/registration/add_user', {
                  'mylanck_login':$('#mylanck_login').val(), 
                  'mylanck_pass':$('#mylanck_pass').val(),
                  'mylanck_re_pass':$('#mylanck_re_pass').val(),
                  'mylanck_captcha_id':$('#mylanck_captcha_id').val(),
                  'mylanck_captcha_text':$('#mylanck_captcha_text').val(),
                  'is_abonent':is_abonent,
                  'is_read_rules':is_agree,
                  'mylanck_email':$('#mylanck_email').val()
            }, function(data){
                  if (data.status == 'error'){
                        if (data.message == ''){
                              $.each(data.errors, function(i, item){
                                    //obj = item.field;
                                    //alert($(".error-message[id='" + item.field + "_response']"));
                                    $(".error-message[id='" + item.field + "_response']").html(item.message);                   
                              });
                              var noCache = Date();
                              $.getJSON('/util/get_captcha', {"noCache": noCache}, function(data){ //get new captcha instance
                                    $('#captcha').attr('src',data.url); // reload captcha
                                    $('#mylanck_captcha_id').attr('value',data.id); // renew captcha id after reload
                                    $('#mylanck_captcha_text').attr('value','');    // clear captcha textfield after post
                              });
                        } else { 
                              $('#registration').html('<p>'+data.message+'</p>');
                        }
                  } else {
                        $('#registration').html('<p>'+data.message+'</p>');
                  }
            }, "json");
      });
})