$(document).ready(function() {
    
    $("#id_username").keyup(function() {
              
       $(this).doTimeout('typing', 400, function() {
           
           $.post('/api/users/check_username/', { 'username' : $(this).val() }, function(json) {
                
                var input = $('#id_username');
                
               if(json.available) {
                      input.addClass('available').removeClass('taken');                      
                  } else {
                      input.addClass('taken')
                  }
           }, 'json');
           
         

       });
    }).keypress(function(e) {
      if (e.which == 13) {
        return false;
      }
    });

    $("#email_registration_form #id_email").keyup(function() {
        $(this).doTimeout('typing', 400, function() {
            $.ajax({
                url: '/api/users/check_email/',
                dataType: 'json',
                type: 'post',
                data: {
                    'email' : $(this).val()
                },
                success: function(json) {
                    if(json.success) {
                        
                        var input = $("#email_registration_form #id_email");
                        console.log(input);
                        
                        if(json.available){
                            input.addClass('available').removeClass('taken');
                            console.log('available');
                        } else {
                            input.addClass('taken').removeClass('available');
                            console.log('taken');
                        }
                    }
                }
            });
        });
    }).keypress(function(e){ if (e.which == 13) { return false; } });
    
    $("#email_registration_form").ajaxForm({
        dataType : 'json',
        success  : function(json) {
            if(json.success) {
                $("#email_registration_method").html('<h1 id="no_robots">Please check your email. We want to make sure you\'re not a robot.</h1>');
            }
        }
    });
   
});