var childCreate=false;

function Offset(e)
{
	var t = e.offsetTop;
	var l = e.offsetLeft;
	var w = e.offsetWidth;
	var h = e.offsetHeight-2;

	while(e=e.offsetParent)
	{
		t+=e.offsetTop;
		l+=e.offsetLeft;
	}
	return {
		top : t,
		left : l,
		width : w,
		height : h
	}
}

function loadSelect(obj) {
    try { if (obj == null) return; } catch (e) { }
    var offset = Offset(obj);
    var iDiv = document.getElementById("selectof" + obj.name);
    if (iDiv != null) {
        iDiv.style.top = offset.top + "px";
        iDiv.style.left = offset.left + "px";
        return;
    }
	
	obj.style.visibility = "hidden";
	iDiv = document.createElement("div");
		iDiv.id="selectof" + obj.name;
		iDiv.style.position = "absolute";
		iDiv.style.textAlign = "left";
		iDiv.style.width=offset.width + "px";
		iDiv.style.height = "20px";
		iDiv.style.cursor = "pointer";
		iDiv.style.top=offset.top + "px";
		iDiv.style.left=offset.left + "px";
		iDiv.style.background="url(images/icon_select.gif) no-repeat right 4px";
		iDiv.style.border = "1px solid gray";
		iDiv.style.backgroundColor = "white";
		iDiv.style.fontSize="12px";
		iDiv.style.lineHeight=offset.height + "px";
		iDiv.style.textIndent="4px";
	    obj.parentNode.appendChild(iDiv);

	var tValue=obj.options[obj.selectedIndex].innerHTML;
	iDiv.innerHTML=tValue;

	iDiv.onmouseover = function() {//鼠标移到
	    iDiv.style.backgroundImage = "url(images/icon_select_focus.gif)";
	    iDiv.style.backgroundRepeat = "no-repeat";
	    iDiv.style.backgroundPosition = "right 4px";
	}
	iDiv.onmouseout=function(){//鼠标移走
	iDiv.style.backgroundImage = "url(images/icon_select.gif)";
	iDiv.style.backgroundRepeat = "no-repeat";
	iDiv.style.backgroundPosition = "right 4px";
	}
	iDiv.onclick=function(){//鼠标点击
		if (document.getElementById("selectchild" + obj.name)){
			if (childCreate){
				document.getElementById("selectchild" + obj.name).style.display="none";
				childCreate=false;
			}else{
				document.getElementById("selectchild" + obj.name).style.display="";
				childCreate=true;
			}
		}else{
			var cDiv = document.createElement("div");
			cDiv.id="selectchild" + obj.name;
			cDiv.style.position = "absolute";
			cDiv.style.width=offset.width + "px";
			cDiv.style.height=obj.options.length *20 + "px";
			cDiv.style.top=(offset.top+offset.height+2) + "px";
			cDiv.style.left=offset.left + "px";
			cDiv.style.background="#f7f7f7";
			cDiv.style.border="1px solid silver";

			var uUl = document.createElement("ul");
			uUl.id="uUlchild" + obj.name;
			uUl.style.listStyle="none";
			uUl.style.margin="0";
			uUl.style.padding="0";
			uUl.style.fontSize="12px";
			cDiv.appendChild(uUl);
			document.body.appendChild(cDiv);		
			childCreate=true;
			for (var i=0;i<obj.options.length;i++){
				var lLi=document.createElement("li");
				lLi.id=obj.options[i].value;
				lLi.style.textIndent="4px";
				lLi.style.height="20px";
				lLi.style.lineHeight="20px";
				lLi.innerHTML=obj.options[i].innerHTML;
				uUl.appendChild(lLi);
			}
			var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
			for (var j=0;j<obj.options.length;j++){
				liObj[j].onmouseover=function(){
					this.style.background="gray";
					this.style.color="white";
				}
				liObj[j].onmouseout=function(){
					this.style.background="white";
					this.style.color="black";
				}
				liObj[j].onclick=function(){
					obj.options.length=0;
					obj.options[0]=new Option(this.innerHTML,this.id);
					document.getElementById("selectchild" + obj.name).style.display="none";
					childCreate=false;
					iDiv.innerHTML=this.innerHTML;
				}
			}
		}
	}
}

