var inSelect=false;
function replaceAllSelects() {  
	//点任意位置关闭弹出窗口
	document.onclick=function(){
		if( !inSelect ){
			if(currentSelect!=null){currentSelect.style.display='none';}
			currentSelect=null;
		}
	}
	
    selects = document.getElementsByTagName('select');  
  
    for(var i=0; i < selects.length; i++) {  
		var selectWidth=selects[i].clientWidth;  
		var selectArea = document.createElement('div');  
		var textInput = document.createElement('input');  
		selectArea.id = "mySelect"+i;  
		selectArea.className = "zSelect";  
		
		textInput.type = "text";  
		textInput.className = "inputText";  
		textInput.readOnly=true;  
		textInput.style.width=selectWidth+"px";  
		textInput.style.height=selects[i].clientHeight+4+"px";  
		
		textInput.id = "mySelectText"+i;  
		textInput.value = selects[i].options[0].text;  
		//textInput.onclick="showOptions("+i+")";
		new function(){
			var ii=i;
			textInput.onclick=function(){ showOptions(ii) ;};
  	
		}
		
		selectArea.appendChild(textInput);  

		selects[i].style.display='none';   

		selects[i].parentNode.insertBefore(selectArea, selects[i]);  

		var optgroup = document.createElement('div');  
		optgroup.className = "optgroup";  
		optgroup.style.width=selectWidth+20+"px";  
		optgroup.style.display = "none";  
		optgroup.id = "optgroup"+i;  
		var optionsDiv = document.createElement('div');  
		optionsDiv.className = "optionsDiv";  
		optionsDiv.id = "optionsDiv"+i;  
		
		optionsDiv.style.left=getAbsoluteLeft(selects[i])+"px";
		optionsDiv.style.top=getAbsoluteTop(selects[i])+"px";
		
		optgroup.appendChild(optionsDiv);  
		if(selects[i].id=="")selects[i].id="select"+i;  

		selectArea.appendChild(optgroup);  
		for(var j=0; j < selects[i].options.length; j++) {  
			var optionHolder = document.createElement('p');  
			var optionLink = document.createElement('a');  
			var optionTxt = document.createTextNode(selects[i].options[j].text);  
			optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+j+","+i+");";  
			optionLink.appendChild(optionTxt);  
			optionHolder.appendChild(optionLink);  
			optionsDiv.appendChild(optionHolder);  
			if(selects[i].options[j].selected){selectMe(selects[i].id,j,i);}  
		}  
	}  
}  

function setInSelect(){
	inSelect=true;
	setTimeout(function(){
		inSelect=false;
		},500);
}

var currentSelect=null;
function showOptions(g) {
	setInSelect();
	
	if(currentSelect!=null){currentSelect.style.display='none';}
	
	var elem = document.getElementById("optgroup"+g);  
	currentSelect=elem;
	elem.style.display=elem.style.display=='none'?'block':'none';  
}  
function selectMe(selectFieldId,linkNo,selectNo) {  
	setInSelect();
	optionLinks = document.getElementById("optionsDiv"+selectNo).getElementsByTagName("a");  
	for(var k = 0; k < optionLinks.length; k++) {  
		if(k==linkNo) {  
		optionLinks[k].className = "selected";  
		}  
		else {  
		optionLinks[k].className = "";  
		}  
	}  
	selectField = document.getElementById(selectFieldId);  
	for(var k = 0; k < selectField.options.length; k++) {  
		if(k==linkNo) {  
			selectField.options[k].selected = "selected";  
		}else {  
			selectField.options[k].selected = "";  
		}  
	}  
	var newText = selectField.options[linkNo].text;  
	document.getElementById("mySelectText"+selectNo).value=newText;  
	
	//隐藏
	var elem = document.getElementById("optgroup"+selectNo);  
	elem.style.display='none';  
}  

//获取控件左绝对位置

function getAbsoluteLeft(o) {
	//o = document.getElementById(objectId)
	oLeft = o.offsetLeft            
	while(o.offsetParent!=null) {
	oParent = o.offsetParent    
	oLeft += oParent.offsetLeft
	o = oParent
	}
	return oLeft
}
//获取控件上绝对位置
function getAbsoluteTop(o) {
	//o = document.getElementById(objectId);
	oTop = o.offsetTop;
	while(o.offsetParent!=null)
	{  
	oParent = o.offsetParent 
	oTop += oParent.offsetTop  // Add parent top position
	o = oParent
	}
	return oTop
}

window.onload=replaceAllSelects;  
