Extracted javascript function (@bgotink)