/************************************
*	js模拟Select					*

*	Author:Daviv					*

*	Date:2007-4-28					*

*	Blog:http://www.iwcn.net		*

*	Email:jxdawei#163.com			*
************************************/

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){

	//第一步：取得Select所在的位置
	var offset=Offset(obj);
	//第二步：将真的select隐藏
	obj.style.display="none";

	//第三步：虚拟一个div出来代替select
	var iDiv = document.createElement("div");
		iDiv.id="eselect";
		iDiv.style.top=offset.top + "px";
		iDiv.style.left=offset.left + "px";
	document.body.appendChild(iDiv);

	//第四步：将select中默认的选项显示出来
	var tValue=obj.options[obj.selectedIndex].innerHTML;
	iDiv.innerHTML=tValue;
    
	document.getElementById("eselect").onclick=function(){
	        document.getElementById("select_icon").onclick();
	}
	document.getElementById("eselect").onmouseover=function(){
			document.getElementById("select_icon").className="focu";
	}
	document.getElementById("eselect").onmouseout=function(){
			document.getElementById("select_icon").className="";
	} 
	document.getElementById("select_icon").onclick=function(){//鼠标点击
		if (document.getElementById("eoptions")){
		//判断是否创建过div
			if (childCreate){
				//判断当前的下拉是不是打开状态，如果是打开的就关闭掉。是关闭的就打开。
				document.getElementById("eoptions").style.display="none";
				childCreate=false;
			}else{
				document.getElementById("eoptions").style.display="";
				childCreate=true;
			}
		}else{
			//初始一个div放在上一个div下边，当options的替身。
			var cDiv = document.createElement("div");
			cDiv.id="eoptions";
			cDiv.style.height=obj.options.length *16 + "px";
			cDiv.style.top=(offset.top+offset.height+2) + "px";
			cDiv.style.left=offset.left + "px";

			var uUl = document.createElement("ul");
			uUl.id="uUlchild" + obj.name;
			cDiv.appendChild(uUl);
			document.body.appendChild(cDiv);		
			childCreate=true;
			for (var i=0;i<obj.options.length;i++){
				//将原始的select标签中的options添加到li中
				var lLi=document.createElement("li");
				lLi.id=obj.options[i].value;
				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++){
				//为li标签添加鼠标事件
				liObj[j].onmouseover=function(){
					this.style.background="#07519A";
					this.style.color="#FFF";
				}
				liObj[j].onmouseout=function(){
					this.style.background="#FFF";
					this.style.color="#000";
				}
				liObj[j].onclick=function(){
					//做两件事情，一是将用户选择的保存到原始select标签中，要不做的再好看表单递交后也获取不到select的值了。
					obj.options.length=0;
					obj.options[0]=new Option(this.innerHTML,this.id);
					MailChange(this.id,'');
					//同时我们把下拉的关闭掉。
					document.getElementById("eoptions").style.display="none";
					childCreate=false;
					iDiv.innerHTML=this.innerHTML;
				}
			}
		}
	}
}

