Ticket availability

How do I create a form to book tickets for an event indicating the maximum total quantity (also visible to the visitor) and block the selection when they are sold out?

e.g. Registration site