I assume that you are using a database for this. I would go about this by creating a field called say 'lock' for each id. The initial value should be set to 0 (meaning it is not locked).
When a user clicks on the id then it will query the database to find the value in the 'lock' field. If the value is 0 then it is not locked and the user can edit the document. At the same time the locked value is set to 1 implying it is locked for editing.
When the user finishes adding a comment then the lock value is set back to 0 again ready for another user to edit.
Only problem that may arrise is if the connection is interrupted without the value of locked being set back to 0 again. Could cause a deadlock if you're not careful.
I'm also not sure how it would work with interleaving. It's a difficult one as connections are concurrent and if the lock value 1 is not issued quick enough then it is possible to get two connections at the same time.
The more I think about it the harder it gets. Possibly someone else will be able to help more but this will hopefully get you started.