var captchaPass=false;
function HumanCheckComplete(isHuman)
{

     if (isHuman)
     {
        captchaPass=true;

         //form.submit();
         var form=$('#registration_form');
         form.submit();
     }
     else
     {
        $('#asirra_InstructionsTextId').html('Are you a human? If so, please select <b>ALL</b> of the <b> cat</b> photos <div class="invalid">Please choose cats only</div>');

     }

}
$(document).ready(

    function()
    {
        $('#asirra_InstructionsTextId').html('Are you a human? If so, please select <b>ALL</b> of the <b> cat</b> photos');

           $('#registration_form').validate(
            {
                errorClass:'invalid',
                rules:{

                        'data[user][username]':{
                            required:true,
                            minlength:4,
                            maxlength:20
                        },
                        'data[user][password]':{
                            required:true,
                            minlength:4,
                            maxlength:20

                        },
                        'data[user][confirm_password]':{
                            required:true,
                            minlength:4,
                            maxlength:20,
                            equalTo:'#userPassword'

                        },
                        'data[user][email]':{
                            required:true,
                            email:true
                        },
                        'data[user][agree]':{
                            required:'#userAgree:checked'
                        },
                        'data[user][year]':{
                            required:true,
                            digits:true,
                            minlength:4

                        }


                }
            }
        );

        $('#registration_form').submit(
            function()
            {
                $('label[generated=true]').remove();

                if (captchaPass)
                {
                    $('#Asirra_Ticket').attr('name','data[user][captcha]');

                    return true;
                }
                if (!$(this).valid())
                {
                    return false;
                }
                Asirra_CheckIfHuman(HumanCheckComplete);

                return false;
            }
        );

        $('#username_check').click(
            function()
            {

                var val=$('#userUsername').val();
                var updateme=$('#username_status');

                if (val=='')
                {

                    updateme.html('<span class="invalid">enter your username first</span>');
                    return;
                }


                $.ajax(

                    {
                        type:'post',
                        cache:false,
                        url:'/users/check',
                        data:'username='+val,
                        success:function(result){updateme.html(result);}
                    }
                );

            }
        );


    }
);
