This would be done with server side php code and not javascript... you can add a recordset above the add to cart code that checks availability... then you can add code to redirect if there is no stock. A redirect before the add to cart would prevent the add to cart from happening and you could add a url parameter to conditionally show an "out of stock" message.