/************************************
*	js模拟Select					*

*	Author:Daviv					*

*	Date:2007-4-28					*

*	Blog:http://www.iwcn.net		*

*	Email:jxdawei#163.com			*
************************************/

var childCreate=false;

var _seleck_option = '';

var _seleck_on_other = true;  //鼠标是否移动到页面其他位置

var _seleck_on_other_div = false;  //鼠标是否移动到页面其他select位置

var _seleck_option1 = '';   //鼠标是否移动到页面其他select的名称

var all_seleck_obj = new Array();



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);
	//alert(obj.name+"==top=="+offset.top);
	//alert(obj.name+"==left=="+offset.left);
	//alert(obj.name+"==width=="+offset.width);
	//alert(obj.name+"==height=="+offset.height);
	
	//第二步：将真的select隐藏
	obj.style.display="none";

	//第三步：虚拟一个div出来代替select
	var iDiv = document.createElement("div");
		iDiv.id="selectof" + obj.name;
		
		iDiv.style.position = "absolute";
		iDiv.style.width=offset.width + "px";
		iDiv.style.height=offset.height + "px";
		iDiv.style.top=offset.top-3 + "px";
		iDiv.style.left=offset.left + "px"; 
		iDiv.style.background="#fff url(/htzq/images/dot.gif) no-repeat right 7px";
		iDiv.style.border="1px solid #e0e0e0";
		iDiv.style.fontSize="12px";
		iDiv.style.lineHeight=offset.height + "px";
		iDiv.style.textIndent="4px";
	document.body.appendChild(iDiv);

	all_seleck_obj.push(obj.name+"");

	//第四步：将select中默认的选项显示出来
	var tValue=obj.options[obj.selectedIndex].innerHTML;
	iDiv.innerHTML=tValue;

	//第五步：模拟鼠标点击
	iDiv.onmouseover=function(){//鼠标移到 
		if(_seleck_option == '' || (document.getElementById(_seleck_option) == null) || (document.getElementById(_seleck_option) != null && document.getElementById(_seleck_option).style.display=='none')){//当其他所有的都关闭后才能对另一个进行操作
			_seleck_on_other = false;//鼠标在其上 
			_seleck_option = "selectchild" +obj.name; 
			iDiv.style.background="#fff url(/htzq/images/dot.gif) no-repeat right 7px";  
		} 

		if(_seleck_option != '' && _seleck_option != "selectchild" +obj.name){
				 _seleck_on_other_div = true;  //鼠标是否移动到页面其他select位置 
				 _seleck_option1 = "selectchild" +obj.name;   //鼠标是否移动到页面其他select的名称 
		}
		
	}
	iDiv.onmouseout=function(){//鼠标移走
		//alert("鼠标移走");
		if(_seleck_option == "selectchild" + obj.name ){//当其他所有的都关闭后才能对另一个进行操作
			_seleck_on_other = true;//鼠标在其他地方
			iDiv.style.background="#fff url(/htzq/images/dot.gif) no-repeat right 7px";
		}
		 _seleck_on_other_div = false;  
	}
	document.onclick=function(){//当鼠标点击其他地方时 关闭 
			// alert(_seleck_option+"======"+_seleck_on_other);
		if(_seleck_option != '' && _seleck_on_other && (document.getElementById(_seleck_option)!=null&&document.getElementById(_seleck_option).style.display=="")){
			//alert(_seleck_option+"======"+_seleck_on_other); 
			document.getElementById(_seleck_option).style.display="none";
			childCreate=false;
			_seleck_option = ''; 
			_seleck_on_other = true;
			 
			if(_seleck_on_other_div){ 
					_seleck_on_other = false;//鼠标在其上 
					_seleck_option = _seleck_option1 ; 
					iDiv.style.background="#fff url(/htzq/images/dot.gif) no-repeat right 7px";   
			}

		}
	}
	iDiv.onclick=function(){//鼠标点击 
		
	  if(_seleck_option == "selectchild" + obj.name ){//当其他所有的都关闭后才能对另一个进行操作
		//alert(_seleck_option);
		
		if (document.getElementById("selectchild" + obj.name)){
		//判断是否创建过div
		//alert(childCreate);
			if (childCreate){
				//判断当前的下拉是不是打开状态，如果是打开的就关闭掉。是关闭的就打开。
				document.getElementById("selectchild" + obj.name).style.display="none"; 
				childCreate=false;
			}else{
				document.getElementById("selectchild" + obj.name).style.display="";
				_seleck_option = "selectchild" + obj.name;
				childCreate=true;
			}
		}else{
			//初始一个div放在上一个div下边，当options的替身。
			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 *18 + "px";
			//cDiv.style.top=(offset.top+offset.height+2) + "px";
			//cDiv.style.left=offset.left + "px"; 
			cDiv.style.top=(document.getElementById("selectof" + obj.name).offsetTop+offset.height+2) + "px";
			cDiv.style.left=document.getElementById("selectof" + obj.name).offsetLeft + "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;
			for (var i=0;i<obj.options.length;i++){
				//将原始的select标签中的options添加到li中
				var lLi=document.createElement("li");
				lLi.id=obj.options[i].value;
				lLi.style.textIndent="4px";
				lLi.style.height="16px";
				lLi.style.lineHeight="16px";
				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="#999";
					this.style.color="#fff";
				}
				liObj[j].onmouseout=function(){
					this.style.background="white";
					this.style.color="#333";
				}
				liObj[j].onclick=function(){
					//做两件事情，一是将用户选择的保存到原始select标签中，要不做的再好看表单递交后也获取不到select的值了。
					obj.options.length=0;
					//alert(obj.name);
					//alert(obj.value); 
					obj.options[0]=new Option(this.innerHTML,this.id);   
					//alert(this.id);
					//obj.value = this.id + ''; 
					//var str= this.id;
					//alert(str); 
					//if(obj1 == 'city'){
					//alert('dddddddddddddddddddddddddddddd'+document.getElementById('mytd'));
					//new MyAjax('yykh_yyb.jsp?city='+this.value,document.getElementById('mytd'));clearDzdh();
					//}
					if(obj.name=="type"){
                    if(this.id==3 || this.id==10){
					document.getElementById("titlename").innerHTML="资金帐号：";
					}else{
					document.getElementById("titlename").innerHTML="用 户 名：";
					}
					 if(this.id==10){
					document.getElementById("titlepssword").innerHTML="网站密码：";
					}else{
					 document.getElementById("titlepssword").innerHTML="密&nbsp;&nbsp;&nbsp;&nbsp;码：";
					}
					}
					//document.getElementById(obj.name).value =  str ;
					//alert(document.getElementById(obj.name).value);  
					//alert(this.innerHTML+'======>'+this.id+'<=====');
					//同时我们把下拉的关闭掉。
					document.getElementById("selectchild" + obj.name).style.display="none";
					childCreate=false;
					iDiv.innerHTML=this.innerHTML;
					doSelectOnChange(obj.name);
				}
			}
		}
	}
	}
}

