<% @pagetitle = "Add File" %>
<% package_bread_crumb(@pagetitle) -%>

<%= render :partial => 'tabs' %>

<h3>Add File to <%= @package.name %> (Project <%= @project.name %>)</h3>

<p>The filename is taken from the uploaded file if none is provided. Alternatively, if only a filename is provided, a new empty file is created (i.e. touched).</p>
<p>You may also specify remote URLs to src.rpm files which will get extracted. URLs to git repositories willbe stored in a tar ball.</p>

<%= form_tag({:action => :save_file, :project => @project, :package => @package}, {:multipart => true}) do %>
  <p>
    <label for="filename">
      <b>Filename (taken from uploaded file if empty):</b><br/>
    </label>
    <%= text_field_tag('filename', '', :size => 80) %><br/>
    <label for="file_type">
      <b>Upload from</b> <%= select_tag('file_type', options_for_select([['local file', 'local'], ['remote URL', 'remote']])) %> :<br/>
    </label>
    <%= file_field_tag('file') %>
    <%= text_field_tag('file_url', '', :size => 80, :class => "hidden") %>
  </p>
  <p><%= submit_tag 'Save changes ', id: 'submit_button', disabled: true %></p>
<% end %>

<%= javascript_tag do %>
  $('#file_type').change(function() {
    if ($('#file_type option:selected').attr('value') == 'local') {
      $('#file').show();
      $('#file_url').hide();
    } else {
      $('#file').hide();
      $('#file_url').show();
    }
  });

  $('#filename, #file_url').keyup(function(){
    changeButton();
  });

  $('#file').change(function(){
    changeButton();
  });

  function changeButton(){
    var filename = $('#filename').val().length;
    var file = $('#file').val().length;
    var file_url = $('#file_url').val().length;

    if(filename != 0 || file != 0 || file_url != 0)
      $('#submit_button').attr('disabled', false);
    else
      $('#submit_button').attr('disabled',true);
  }
<% end %>
