I'm doing this form home so I can't give you any specific code examples but here's the general idea.
The best method I've found is to have your main doc create a lockdoc when it is put into edit mode. Prior to creating the lockdoc, in querymodechange, check for the existance of a lockdoc for the doc being edited (described below). If it exists, pop up a message saying who is editing the doc, when did they start, etc. and cancel change to edit mode.
These lockdocs are stored in a sorted view by doc ID of the DOC BEING EDITED or some other unique feature. The lock doc should also capture the user's name, time lockdoc went into edit and other relevant info.
When the user is done editing (eg; close event), have your script access the lockdoc view and delete the lockdoc. If user's don't have delete permission in ACL, change the lockdoc form name to frmDelete or something like that and then run a daily agent to clean up all docs with form=frmDelete.
Another method is using a 'BeingEdited' field on the doc being edited but this does not work that well because you have to save the doc as soon as you set BeingEdited to yes. Then if the user decides not to save the doc, you have to restore all field values and save it again. Lots of unnecessary saves which messes up history fields, etc.
Hope this helps

YakUp