/************************************/
function lengthOfBytes(pStr)
{//Calculate string use byte as unit¡£
 // if ASCII code is bigger than 256 than it will two bytes.
	var rLen=0;
	if(pStr==null)return rLen;
	    var index=pStr.indexOf("&nbsp;");
		while(index>=0)
		{
			pStr=pStr.substr(0,index)+" "+pStr.substr(index+6);
			index=pStr.indexOf("&nbsp;");
		}
		var aLen=pStr.length;
		for(var i=0;i<aLen;i++)
		{
			var ch=pStr.charCodeAt(i);
			ch=parseInt(ch);
			if(ch>256)
			{
			  rLen+=2;
			}else rLen+=1;
		}
		
	return rLen;
}
var childCreate=false;

function Offset(e)
//Get the label's absolute position.
{
	if (!e) {
		return;
	}
	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){

	if (!obj) {
		return;
	}
	//First£ºGet the position of select
	var offset=Offset(obj);
	//Second£ºHidden the real select
	obj.style.display="none";

	//Third£ºCreate a div take place of select
	var iDiv = document.createElement("div");
		iDiv.id="selectof" + obj.name;
		iDiv.style.position = "absolute";		
		iDiv.style.width=offset.width + "px";
        iDiv.style.overflow="hidden";
		iDiv.style.height=offset.height + "px";
		iDiv.style.top=offset.top + "px";
		iDiv.style.left=offset.left + "px";
		iDiv.style.background="#fff url(../../../../static/img/icon_select.gif) no-repeat top right";
		iDiv.style.border="1px solid #cccccc";
		iDiv.style.fontSize="12px";
		iDiv.style.lineHeight=offset.height + "px";
		iDiv.style.textIndent="4px";
	document.body.appendChild(iDiv);

	//Fourth£ºDisplay the default value in select.
	var tValue=obj.options[obj.selectedIndex].innerHTML;
	iDiv.innerHTML=tValue;

	//Fifth£ºSimulate mouse click
	iDiv.onmouseover=function(){//mouseover
		iDiv.style.background="#fff url(../../../../static/img/icon_select_focus.gif) no-repeat top right";
	}
	iDiv.onmouseout=function(){//mouseout
		iDiv.style.background="#fff url(../../../../static/img/icon_select.gif) no-repeat top right";
	}
	iDiv.onclick=function(){//onclick
		if (document.getElementById("selectchild" + obj.name)){
		//Judge whether you have created div
			if (childCreate){
				//Judge the dropdow whether it is open,if it is opened,please close it.if it is closed,please open it.
				document.getElementById("selectchild" + obj.name).style.display="none";
				childCreate=false;
			}else{
				document.getElementById("selectchild" + obj.name).style.display="";
				childCreate=true;
			}
		}else{
			//Create a div about the first div,replace of option¡£
			var cDiv = document.createElement("div");
			cDiv.id="selectchild" + obj.name;
			cDiv.style.position = "absolute";
			cDiv.style.overflow = "scroll";
			cDiv.style.height="200px"
			cDiv.style.top=(offset.top+offset.height+2) + "px";
			cDiv.style.left=offset.left + "px";
			cDiv.style.background="#ffffff";
			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;
			var maxlen=0;
			for (var i=0;i<obj.options.length;i++){
				//Transfer the original option value to "li"
				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 templen=lengthOfBytes(lLi.innerHTML);				
				
				if(templen>maxlen)
					maxlen=templen;				
			}     
			maxlen=parseInt(maxlen*6.2);
			if(maxlen<offset.width)
				maxlen=offset.width;
			cDiv.style.width=maxlen+"px";
			
			var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
			for (var j=0;j<obj.options.length;j++){
				//Add mouse action for li 
				liObj[j].onmouseover=function(){
					this.style.background="gray";
					this.style.color="white";
				}
				liObj[j].onmouseout=function(){
					this.style.background="white url(/static/img/select_bg.gif) no-repeat right 16px";
					this.style.color="black";
				}
				liObj[j].onclick=function(){
					//Do two things,first,save the selected value into the original select.
					obj.options.length=0;
					obj.options[0]=new Option(this.innerHTML,this.id);
					//Close the dropdown at the same time¡£
					document.getElementById("selectchild" + obj.name).style.display="none";
					childCreate=false;
					iDiv.innerHTML=this.innerHTML;
				    document.forms[0].submit();
				}
			}
		}
	}
}


