Js tree menu

i have a js tree menu, but there is a problem about closing submenus. hope you help me about this. here is the sample–>> treemenu.html


<script type="text/javascript">
function tab(){
        uls = document.getElementsByTagName('ul');
        for(i = 0; i < uls.length; i++){
                if (uls*.id == 'tab'){
                        var lis = uls*.getElementsByTagName('li');
                        for (j = 0; j < lis.length; j++){
                                if(lis[j].lastChild.tagName == 'UL'){
                                        lis[j].onclick = function() {
                                                this.lastChild.style.display = 'block';
                                        }
                                }    
                        }
                }
        }
}
</script>
<ul id="tab">
<li>1<ul><li>1.1<ul><li>1.1.1</li><li>1.1.2</li></ul></li><li>1.2</li></ul></li>
<li>2<ul><li>2.1</li></ul></li>
<li>3</li>
</ul>