{% load i18n %}
//repo-create-form
$('#repo-create')
.click(function() {
    $('#repo-create-form').modal({appendTo: '#main', autoResize: true});
})
.hover(
    function() {
        $(this).css({'background-color': '#fff', 'cursor': 'pointer'});
    },
    function() {
        $(this).css('background-color', '#f5f5f5');
    }
);
$('#encrypt-switch').click(function () {
    var form = $('#repo-create-form'),
        pwd_input = $('input[type="password"]', form);
    
    if ($(this).attr('checked')) {
        pwd_input.attr('disabled', false).removeClass('input-disabled');
    } else {
        pwd_input.attr('disabled', true).addClass('input-disabled');
    }
});
$('#repo-create-form').submit(function() {
    var form = $(this), 
        form_id = form.attr('id'),
        name = $('[name="repo_name"]', form).val(),
        desc = $('[name="repo_desc"]', form).val(),
        encrypted = $('#encrypt-switch').attr('checked'),
        passwd, passwd_again;

    if (!$.trim(name)) {
        apply_form_error(form_id, "{% trans "Name is required" %}");
        return false;
    }
    if (!$.trim(desc)) {
        apply_form_error(form_id, "{% trans "Description is required" %}");
        return false;
    }
    if (encrypted) {
        passwd = $('input[name="passwd"]', form).val();
        passwd_again = $('input[name="passwd_again"]', form).val();

        if (!$.trim(passwd)) {
            apply_form_error(form_id, "{% trans "Please enter password" %}");
            return false;
        }
        if ($.trim(passwd).length < {{repo_password_min_length}}) {
        apply_form_error(form_id, "{% trans "Password is too short" %}");
            return false;
        }
        if ($.trim(passwd).length > 35) {
            apply_form_error(form_id, "{% trans "Password is too long (maximum is 35 characters)" %}");
            return false;
        }
        if (!$.trim(passwd_again)) {
            apply_form_error(form_id, "{% trans "Please enter the password again" %}");
            return false;
        }
        if ($.trim(passwd) != $.trim(passwd_again)) {
            apply_form_error(form_id, "{% trans "Passwords don't match" %}");
            return false;
        }

        // gen uu_id as new repo_id
        var uu_id = uuid.v4();

        var magic_str = gen_magic_str(uu_id, passwd);

        var fileKey = new FileKey(passwd);
        var encrypted_file_key = fileKey.gen_enc();
    }

    var submit_btn = $('input[type="submit"]', form);
    disable(submit_btn);

    var post_data = {
        'repo_name': name,
        'repo_desc': desc,
        'encryption': encrypted ? 1 : 0
    };
    {% if create_shared_repo %}
    $.extend(post_data, {
        'permission': $('select[name="permission"]', form).val()
    });
    {% endif %}

    if (encrypted) {
        $.extend(post_data, {
            'uuid': uu_id,
            'magic_str': magic_str,
            'encrypted_file_key': encrypted_file_key
        });
    }

    $.ajax({
        url: '{{ post_url }}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: post_data,
        success: function(data) {
            repoCreateSuccessCallback(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            var error = $.parseJSON(xhr.responseText).error;
            apply_form_error(form_id, error);
            enable(submit_btn);
        }
    });

    return false;
});
