amended javascript to adopt DRY approach - much less code!