dai un'occhiata qui
magari puoi prendere il codice

http://www.webreference.com/js/column53/demo.html