Properly handle empty id fields

Sometimes tasks can have empty id fields. Instead of assuming that we
can convert the id field to an integer, use -1 for empty ids. Bug
report and initial patch by Manuel Rego Casasnovas.