Index: common/db/task.php
===================================================================
--- common/db/task.php	(revision 1033)
+++ common/db/task.php	(working copy)
@@ -159,4 +159,17 @@
     return $idlist;
 }
 
+// Returns list of running round ids that include this task to which
+// $user_id can submit
+function task_get_submit_rounds($task_id, $user_id) {
+    $rounds = task_get_parent_rounds($task_id);
+    foreach($rounds as $id => $round) {
+        $round = round_get($rounds[$id]);
+        if (!security_query($user_id, "round-submit", $round)) {
+            unset($rounds[$id]);
+        }
+    }
+    return array_values($rounds);
+}
+
 ?>
Index: common/job.php
===================================================================
--- common/job.php	(revision 1033)
+++ common/job.php	(working copy)
@@ -28,13 +28,18 @@
 
     // Validate round id.
     $round = null;
-    if (array_key_exists('round_id', $args)) {
-        if (!is_round_id($args['round_id'])) {
-            $errors['round_id'] = "Id de runda invalid";
-        } else if (is_null($round = round_get($args['round_id']))) {
-            $errors['round_id'] = "Runda '{$args['round_id']}' nu exista.";
-        }
+    if (!array_key_exists('round_id', $args)) {
+        $errors['round_id'] = "Nu ai specificat un concurs.";
+    } else if (!is_round_id($args['round_id'])) {
+        $errors['round_id'] = "Nu ai specificat un concurs corect.";
+    } else if (is_null($round = round_get($args['round_id']))) {
+        $errors['round_id'] = "Runda '{$args['round_id']}' nu exista.";
     }
+    // Check if task is new and hasn't been added to any rounds
+    if (getattr($args, "round_id") == "" &&
+        security_query($user, 'task-submit', $task)) {
+        unset($errors["round_id"]);
+    }
 
     // Validate compiler id
     $valid_compilers = array('c', 'cpp', 'fpc', 'py');
@@ -64,39 +69,15 @@
     if ($task && !security_query($user, 'task-submit', $task)) {
         $errors[] = "Nu ai voie sa trimiti la acest task.";
     }
-    if ($round && !security_query($round, 'task-submit', $round)) {
+    if ($round && !security_query($user, 'round-submit', $round)) {
         $errors[] = "Nu poti sa trimiti la aceasta runda.";
     }
 
-    // FIXME: check round-submit or something?
-
     // Only now create the job.
     if (count($errors) === 0) {
-        // round_id can be missing.
-        // If it's missing then the job is multiplied and sent to all parent rounds.
-        // This is compatible with 2.1.3
-        if (array_key_exists('round_id', $args)) {
-            job_create($args['task_id'], $args['round_id'], $user['id'],
-                    $args['compiler_id'], $args['solution'],
-                    getattr($args, 'remote_ip_info'));
-        } else {
-            $parent_rounds = task_get_parent_rounds($args['task_id']);
-            if (count($parent_rounds) === 0) {
-                // some jobs just don't have a round
-                job_create($args['task_id'], '', $user['id'],
-                        $args['compiler_id'], $args['solution'],
-                        getattr($args, 'remote_ip_info'));
-            }
-            else {
-                foreach ($parent_rounds as $round_id) {
-                    if (security_query($user, 'round-submit', round_get($round_id))) {
-                        job_create($args['task_id'], $round_id, $user['id'],
-                                $args['compiler_id'], $args['solution'],
-                                getattr($args, 'remote_ip_info'));
-                    }
-                }
-            }
-        }
+        job_create($args['task_id'], $args['round_id'], $user['id'],
+                $args['compiler_id'], $args['solution'],
+                getattr($args, 'remote_ip_info'));
     }
 
     return $errors;
Index: common/security.php
===================================================================
--- common/security.php	(revision 1033)
+++ common/security.php	(working copy)
@@ -508,17 +508,7 @@
             return $is_waiting || $is_admin;
 
         case 'round-submit':
-            // FIXME: This sucks.
-            // FIXME: job_get_submit_targets
-            $rparams = round_get_parameters($round['id']);
-            $time = time();
-            $rstart = db_date_parse($round['start_time']);
-            $rduration = getattr($rparams, 'duration', 10000000) * 60 * 60;
-            if ($time >= $rstart && $time <= $rstart + $rduration) {
-                return true;
-            } else {
-                return false;
-            }
+            return $round["state"] == "running";
 
         case 'sensitive-info':
             return ($usersec == 'admin' || $usersec == 'helper');
Index: common/task.php
===================================================================
--- common/task.php	(revision 1033)
+++ common/task.php	(working copy)
@@ -276,4 +276,40 @@
     return $task_page["forum_topic"];
 }
 
+// Returns list of rounds to which a task can be submitted
+// along with a default round
+function task_get_submit_options($task_id) {
+    // Fetch task parent rounds.
+    $rounds = array();
+    $round_ids = task_get_submit_rounds($task_id,
+                                        identity_get_user());
+
+    // Check if task is new and hasn't been added to any round yet
+    if (count($round_ids) == 0) {
+        $task = task_get($task_id);
+        if (identity_can("task-submit", $task)) {
+            $rounds[] = array("id" => "", "title" => "Inexistent");
+        }
+        $default = "";
+    } else {
+        // Try and determine what round a user wants to submit to.
+        $default = getattr($_SESSION, '_ia_last_submit_round', NULL);
+        if (!is_round_id($default) || !in_array($default, $round_ids)) {
+            if (count($round_ids) == 1) {
+                $default = $round_ids[0];
+            } else {
+                $rounds[] = array("id" => "", "title" => "[ Alegeti runda ]");
+                $default = "";
+            }
+        }
+        foreach($round_ids as $round_id) {
+            $round = round_get($round_id);
+            $rounds[] = array("id" => $round_id,
+                              "title" => $round["title"]);
+        }
+    }
+
+    return array("rounds" => $rounds, "default" => $default);
+}
+
 ?>
Index: www/controllers/json.php
===================================================================
--- www/controllers/json.php	(revision 1033)
+++ www/controllers/json.php	(working copy)
@@ -1,6 +1,7 @@
 <?php
 
 require_once(IA_ROOT_DIR.'common/db/textblock.php');
+require_once(IA_ROOT_DIR.'common/db/task.php');
 require_once(IA_ROOT_DIR.'www/wiki/wiki.php');
 
 // This controller serves as a data server for AJAX requests.
@@ -10,6 +11,8 @@
 // FIXME: separate functions and url magic?
 // it works this way.
 function controller_json($suburl) {
+    // FIXME: We need to refactor this,
+    // don't put all json requests in controllers/json.php
     switch ($suburl) {
         case 'wiki-preview':
             // Parse wiki markup and return JSON with HTML output.
@@ -24,8 +27,7 @@
             // check permissions & generate mark-up
             if (!identity_can('textblock-view', $textblock)) {
                 $output = 'Not enough privileges to preview this page';
-            }
-            else {
+            } else {
                 $output = wiki_process_text($page_content);
             }
 
@@ -39,11 +41,25 @@
             // output JSON
             execute_view_die('views/json.php', $view);
 
+        case 'task-get-rounds':
+            // Return list of parent rounds for a task
+            $task_id = request('task_id');
+            if (!is_task_id($task_id)) {
+                // Die with error code 400 Bad Request
+                die_http_error(400, 'Task invalid.');
+            }
+
+            $json = task_get_submit_options($task_id);
+            $view = array(
+                'json' => $json,
+                'debug' => request('debug', null)
+            );
+
+            // Output JSON
+            execute_view_die('views/json.php', $view);
         default:
-            flash('Actiunea nu este valida.');
-            redirect(url_home());
+            die_http_error(400, 'Actiune invalida.');
     }
 }
 
-
 ?>
Index: www/controllers/submit.php
===================================================================
--- www/controllers/submit.php	(revision 1033)
+++ www/controllers/submit.php	(working copy)
@@ -6,13 +6,18 @@
 function controller_submit() {
     identity_require_login();
 
+    $view = array(
+        'title' => 'Trimite solutie',
+    );
+
     $values = array();
     $errors = array();
 
     if (request_is_post()) {
         $values = array(
-            'task_id' => getattr($_POST, 'task_id'),
-            'compiler_id' => getattr($_POST, 'compiler_id'),
+            'task_id' => request('task_id'),
+            'compiler_id' => request('compiler_id'),
+            'round_id' => request('round_id'),
             'remote_ip_info' => remote_ip_info(),
         );
 
@@ -25,8 +30,16 @@
         $errors = safe_job_submit($values, identity_get_user());
 
         // The end.
+        if (!isset($errors["round_id"])) {
+            $_SESSION["_ia_last_submit_round"] = $values["round_id"];
+        }
         if ($errors) {
-            log_print_r($errors);
+            if (!isset($errors["task_id"])) {
+                list($rounds, $default_round) =
+                    array_values(task_get_submit_options($values["task_id"]));
+                $view["rounds"] = $rounds;
+                $view["default_round"] = $default_round;
+            }
             flash_error('NU am salvat solutia trimisa! Unul sau mai multe campuri
                          nu au fost completate corect.');
         } else {
@@ -45,12 +58,9 @@
         }
     }
 
-    $view = array(
-            'title' => 'Trimite solutie',
-            'tasks' => $tasks,
-            'form_errors' => $errors,
-            'form_values' => $values,
-    );
+    $view['tasks'] = $tasks;
+    $view['form_errors'] = $errors;
+    $view['form_values'] = $values;
 
     execute_view_die('views/submit.php', $view);
 }
Index: www/macros/macro_tasksubmit.php
===================================================================
--- www/macros/macro_tasksubmit.php	(revision 1033)
+++ www/macros/macro_tasksubmit.php	(working copy)
@@ -34,45 +34,18 @@
         return macro_message("Nu se (mai) pot trimite solutii la aceasta problema.", true);
     }
 
-    // display form
+    list($rounds, $default_round) =
+        array_values(task_get_submit_options($task_id));
+
+    // Display form
     ob_start();
 ?>
 
 <a href="<?= html_escape(url_monitor()."?task=".$task['id']."&user=".$identity_user['username']) ?>">Vezi solutiile trimise de tine</a>
-
-<form enctype="multipart/form-data" action="<?= html_escape(url_submit()) ?>" method="post" class="inlineSubmit" id="task_submit">
-
-<input type="hidden" id="output_only" value="<?= 'output-only' == $task['type'] ? html_escape($task['id']) : '' ?>" />
-<input type="hidden" name="task_id" value="<?= html_escape($task['id']) ?>" id="form_task" />
-
-<ul class="form">
-
-    <li id="field_solution">
-        <label for="form_solution">Fisier solutie</label>
-        <input type="file" name="solution" id="form_solution" />
-    </li>
-
-    <li id="field_compiler">
-        <label for="form_compiler">Compilator</label>
-        <select name="compiler_id" id="form_compiler">
-            <option value="-">[ Alege ]</option>
-            <option value="c">GNU C</option>
-            <option value="cpp">GNU C++</option>
-            <option value="fpc">FreePascal</option>
-            <?php if (user_is_admin($identity_user)) { ?>
-                <option value="py">Python (FOARTE EXPERIMENTAL!)</option>
-            <?php } ?>
-        </select>
-    </li>
-
-    <li id="field_submit">
-        <input type="submit" class="button" value="Trimite solutia" id="form_submit" />
-    </li>
-</ul>
-</form>
-
-
 <?php
+    $inline_form = true;
+    include(IA_ROOT_DIR . "www/views/submit_form.php");
+
     $buffer = ob_get_contents();
     ob_end_clean();
 
Index: www/static/css/screen.css
===================================================================
--- www/static/css/screen.css	(revision 1033)
+++ www/static/css/screen.css	(working copy)
@@ -1081,6 +1081,10 @@
 }
 
 .inlineSubmit #form_compiler {
+    width: 7em;
+}
+
+.inlineSubmit #form_round {
     width: 9em;
 }
 
@@ -1504,3 +1508,10 @@
 .round-running {
     background: url("../images/run.png");
 }
+
+.submit-warning {
+    color: red;
+    text-align: center;
+    margin: 0;
+    padding: 0;
+}
Index: www/static/js/submit.js
===================================================================
--- www/static/js/submit.js	(revision 1033)
+++ www/static/js/submit.js	(working copy)
@@ -15,6 +15,7 @@
     var fTask = $('form_task');
 
     Submit_CompilerDisplay = $('field_compiler').style.display;
+    Submit_RoundDisplay = $('field_round').style.display;
 
     connect(fSolution, 'onchange', Submit_UpdateSolution);
     if ('hidden' != fTask.type) {
@@ -73,11 +74,49 @@
     // toggle displaying compiler select box
     if (Submit_HasCompiler(t.value)) {
         $('field_compiler').style.display = Submit_CompilerDisplay;
-    }
-    else {
+    } else {
         $('field_compiler').style.display = 'none';
     }
 
+    if (t.value) {
+        $('field_round').style.display = Submit_RoundDisplay;
+        var d = doXHR(BASE_HREF + 'json/task-get-rounds?task_id=' + escape(t.value), {method: 'POST'});
+
+        var ready = function(xhr) {
+            var data = evalJSONRequest(xhr);
+            var rounds = data["rounds"];
+            var default_round = data["default"];
+
+            $('form_round').innerHTML = '';
+            warning_container = $('field_round_warning');
+            if (warning_container) {
+                if (rounds.length != 1) {
+                    warning_container.innerHTML = '<p class="submit-warning">Această problemă face parte din mai multe concursuri. Selectează-l pe cel la care participi!</p>';
+                } else {
+                    warning_container.innerHTML = '';
+                }
+            }
+            for (key in rounds) {
+                var option = document.createElement("option");
+                option.value = rounds[key]["id"];
+                if (rounds[key]["id"] == default_round) {
+                    option.selected = "selected";
+                }
+                var text = document.createTextNode(rounds[key]["title"])
+                option.appendChild(text);
+                $('form_round').appendChild(option);
+            }
+        }
+
+        var error = function(error) {
+            window.alert('Eroare! Nu pot determina rundele. Incearcati din nou.');
+        }
+
+        d.addCallbacks(ready, error);
+    } else {
+        $('field_round').style.display = 'none';
+    }
+
     // auto-choose compiler
     if (0 < $('form_solution').value.length) {
         Submit_AutoCompiler();
Index: www/static/js/wikiedit.js
===================================================================
--- www/static/js/wikiedit.js	(revision 1033)
+++ www/static/js/wikiedit.js	(working copy)
@@ -42,7 +42,7 @@
     }
 
     var error = function(error) {
-        window.alert('Eroare! Nu pot face preview. Incearcati din nou.');
+        window.alert('Eroare! Nu pot face preview. Incercati din nou.');
         WikiEdit_ClosePreview();
     }
 
Index: www/views/submit.php
===================================================================
--- www/views/submit.php	(revision 1033)
+++ www/views/submit.php	(working copy)
@@ -24,48 +24,11 @@
 </div>
 </div>
 
-<form enctype="multipart/form-data" action="<?= html_escape(url_submit()) ?>" method="post" class="submit" id="task_submit">
+<?php
+$inline_form = false;
+include("submit_form.php");
+?>
 
-<input type="hidden" id="output_only" value="<?= html_escape(':'.join(':', $output_only_ids).':') ?>" />
-
-<ul class="form hollyfix">
-    <li id="field_task">
-        <label for="form_task">Problema</label>
-        <select name="task_id" id="form_task">
-            <option value="">[ Alegeti problema ]</option>
-<?php foreach ($tasks as $task) {  ?>
-            <option value="<?= html_escape($task['id']) ?>"<?= fval('task_id') == $task['id'] ? ' selected="selected"' : '' ?>><?= html_escape($task['title']) ?></option>
-<?php } ?>
-        </select>
-        <?= ferr_span('task_id') ?>
-    </li>
-
-    <li id="field_solution">
-        <label for="form_solution">Fisier solutie</label>
-        <input type="file" name="solution" id="form_solution" />
-        <?= ferr_span('solution', false) ?>
-    </li>
-
-    <li id="field_compiler">
-        <label for="form_compiler">Compilator</label>
-        <select name="compiler_id" id="form_compiler">
-            <option value="-">[ Alegeti compilator ]</option>
-            <option value="c"<?= 'c' == fval('compiler_id') ? ' selected="selected"' : '' ?>>GNU C</option>
-            <option value="cpp"<?= 'cpp' == fval('compiler_id') ? ' selected="selected"' : '' ?>>GNU C++</option>
-            <option value="fpc"<?= 'fpc' == fval('compiler_id') ? ' selected="selected"' : '' ?>>FreePascal</option>
-            <?php if (user_is_admin($identity_user)) { ?>
-                <option value="py"<?= 'py' == fval('compiler_id') ? ' selected="selected"' : '' ?>>Python (FOARTE EXPERIMENTAL!)</option>
-            <?php } ?>
-        </select>
-        <?= ferr_span('compiler_id') ?>
-    </li>
-
-    <li id="field_submit">
-        <input type="submit" class="button important" value="Trimite solutia" id="form_submit" />
-    </li>
-</ul>
-</form>
-
 <?php wiki_include('template/trimite-solutii') ?>
 
 <?php include('footer.php'); ?>
Index: www/views/submit_form.php
===================================================================
--- www/views/submit_form.php	(revision 0)
+++ www/views/submit_form.php	(revision 0)
@@ -0,0 +1,100 @@
+<?php
+function solution_field() {
+?>
+    <li id="field_solution">
+        <label for="form_solution">Fisier</label>
+        <input type="file" name="solution" id="form_solution" />
+        <?= ferr_span('solution', false) ?>
+    </li>
+<?php
+}
+
+function compiler_field() {
+?>
+    <li id="field_compiler">
+        <label for="form_compiler">Compilator</label>
+        <select name="compiler_id" id="form_compiler">
+            <option value="-">[ Alegeti compilator ]</option>
+            <option value="c"<?= 'c' == fval('compiler_id') ? ' selected="selected"' : '' ?>>GNU C</option>
+            <option value="cpp"<?= 'cpp' == fval('compiler_id') ? ' selected="selected"' : '' ?>>GNU C++</option>
+            <option value="fpc"<?= 'fpc' == fval('compiler_id') ? ' selected="selected"' : '' ?>>FreePascal</option>
+            <?php if (user_is_admin(identity_get_user())) { ?>
+                <option value="py"<?= 'py' == fval('compiler_id') ? ' selected="selected"' : '' ?>>Python (FOARTE EXPERIMENTAL!)</option>
+            <?php } ?>
+        </select>
+        <?= ferr_span('compiler_id') ?>
+    </li>
+<?php
+}
+
+function multiple_round_warning($rounds) {
+    if (count($rounds) > 1) {
+?>
+    <p class="submit-warning">Această problemă face parte din mai multe concursuri. Selectează-l pe cel la care participi!</p>
+<?php
+    }
+}
+
+function round_field($rounds, $default_round, $warning_field = true) {
+?>
+    <li id="field_round">
+<?php if ($warning_field) { ?>
+        <div id="field_round_warning"><?php multiple_round_warning($rounds); ?></div>
+<?php } ?>
+        <label for="form_round">Concurs</label>
+        <select name="round_id" id="form_round">
+<?php foreach ($rounds as $round) { ?>
+            <option value="<?= html_escape($round['id']) ?>"<?= ($round['id'] == $default_round) ? ' selected="selected"' : ''?>><?= html_escape($round['title']) ?></option>
+<?php } ?>
+        </select>
+    </li>
+<?php
+}
+
+function task_field($tasks) {
+?>
+    <li id="field_task">
+        <label for="form_task">Problema</label>
+        <select name="task_id" id="form_task">
+            <option value="">[ Alegeti problema ]</option>
+<?php foreach ($tasks as $task) {  ?>
+            <option value="<?= html_escape($task['id']) ?>"<?= fval('task_id') == $task['id'] ? ' selected="selected"' : '' ?>><?= html_escape($task['title']) ?></option>
+<?php } ?>
+        </select>
+        <?= ferr_span('task_id') ?>
+    </li>
+<?php
+}
+?>
+<form enctype="multipart/form-data" action="<?= html_escape(url_submit()) ?>" method="post" class="<?= $inline_form ? "inlineSubmit" : "submit" ?>" id="task_submit">
+
+<ul class="form">
+<?php
+    if (!isset($rounds)) {
+        $rounds = array();
+        $default_round = NULL;
+    }
+    if ($inline_form) {
+?>
+    <input type="hidden" name="task_id" value="<?= html_escape($task['id']) ?>" id="form_task" />
+<?php
+        solution_field();
+        round_field($rounds, $default_round, false);
+        compiler_field();
+    } else {
+        task_field($tasks);
+        round_field($rounds, $default_round);
+        solution_field();
+        compiler_field();
+    }
+?>
+    <li id="field_submit">
+        <input type="submit" class="button" value="Trimite" id="form_submit" />
+    </li>
+</ul>
+</form>
+<?php
+    if ($inline_form) {
+        multiple_round_warning($rounds);
+    }
+?>
Index: www/views/utilities.php
===================================================================
--- www/views/utilities.php	(revision 1033)
+++ www/views/utilities.php	(working copy)
@@ -41,6 +41,10 @@
 function ferr($param_name, $escape_html = true) {
     global $view;
 
+    if (!isset($view["form_errors"])) {
+        return '';
+    }
+
     if ($escape_html) {
         return html_escape(getattr($view['form_errors'], $param_name));
     } else {
@@ -52,6 +56,10 @@
 function ferr_span($param_name, $escape_html = true) {
     $error = ferr($param_name, $escape_html);
 
+    if (!isset($view["form_errors"])) {
+        return '';
+    }
+
     if ($error) {
         return '<span class="fieldError">' . $error . '</span>';
     } else {
