var combodropimage='/imagens/NI_setaCombo.png' //path to "drop down" image
var combodropoffsetY=0 //offset of drop down menu vertically from default location (in px)
var combozindex=1000
var drop=false
var select_activa = ''

function findPosY(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return curtop;
}

function accao(selectid,i){
	var selectbox=document.getElementById(selectid)
	var textbox=document.getElementById('dhtml_tb_'+selectid)
	
	if(selectbox.selectedIndex != i){
		selectbox.selectedIndex=i
		textbox.value=selectbox[selectbox.selectedIndex].text

	
		if (selectbox.onchange) {
			selectbox.onchange();
		}
	}
}

function fechaDrop(){
	if(select_activa != ''){

	    var dhtmlDropFecha=document.getElementById(select_activa)
		dhtmlDropFecha.parentNode.getElementsByTagName("div")[0].style.display="none"
		drop = false
		select_activa = ''
	}
}

function actualizaDrop(selectid){
		var selectbox=document.getElementById(selectid)
		var dhtmltbbox=document.getElementById("dhtml_tb_"+selectid)
		var dhtmldrop=document.getElementById('dhtml_drop'+selectid)
		var opcoes=''
			
		for (var i=0; i<selectbox.options.length; i++)
			opcoes += '<a href="javascript:accao(\''+selectid+'\','+i+');">'+selectbox.options[i].text+'</a>'

		dhtmldrop.innerHTML = opcoes;

	dhtmltbbox.value=selectbox[selectbox.selectedIndex].text
	
}

function dhtmlselect(selectid, selectwidth, optionwidth, urlImage){
	
	var selectbox=document.getElementById(selectid)
	
	if (typeof urlImage!="undefined")
		combodropimage = urlImage;
	
	document.onclick = fechaDrop;
	
	if (combodropimage!=""){
	combodropimagehtml='<img id="dhtml_img'+selectid+'" class="downimage" src="'+combodropimage+'" title="Select an option" />'
	}

	document.write('<div class="dhtmlselect" id="dhtml_'+selectid+'"> <input class="dhtml_input" id="dhtml_tb_'+selectid+'" readonly value="'+selectbox[selectbox.selectedIndex].text+'"> '+combodropimagehtml+'<div id="dhtml_drop'+selectid+'" class="dropdown">')
  
  var dhtmltbbox=document.getElementById("dhtml_tb_"+selectid)
  var dhtmlImg=document.getElementById('dhtml_img'+selectid)
	var dhtmlDrop=document.getElementById('dhtml_drop'+selectid)

	for (var i=0; i<selectbox.options.length; i++)
		document.write('<a href="javascript:accao(\''+selectid+'\','+i+');">'+selectbox.options[i].text+'</a>')
	document.write('</div></div>')
	
	//Se a combo box ficar com tamanho maior que o permitido, mostrar scroll
	var tamanhoDrop = selectbox.options.length  * 13;

	if ((document.body.clientHeight - findPosY(dhtmltbbox)) < tamanhoDrop) {
		dhtmlDrop.style.height="100px"
		dhtmlDrop.style.overflow="auto"
	}

	selectbox.style.display="none"
	var dhtmlselectbox=document.getElementById("dhtml_"+selectid)
	dhtmlselectbox.style.zIndex=combozindex
	combozindex--
	if (typeof selectwidth!="undefined"){
		dhtmlselectbox.style.width=parseInt(selectwidth)
		dhtmltbbox.style.width=parseInt(selectwidth)-16
	}
	if (typeof optionwidth!="undefined")
		dhtmlselectbox.getElementsByTagName("div")[0].style.width=optionwidth

	dhtmlImg.onclick=function() {
		if (select_activa!='dhtml_drop'+selectid){
			fechaDrop()
		}
		
		var selectbox=document.getElementById(selectid)
		
		if(!selectbox.disabled){
		
			if (!drop) {
				this.parentNode.getElementsByTagName("div")[0].style.display="block"
				drop = true
			} else {
				this.parentNode.getElementsByTagName("div")[0].style.display="none"
				drop=false
				select_activa=''
			}
		}
	}
	
	dhtmltbbox.onclick=function() {
		if (select_activa!='dhtml_drop'+selectid){
			fechaDrop()
		}
		if(!drop){
			this.parentNode.getElementsByTagName("div")[0].style.display="block"
			drop = true
		} else
		{
			this.parentNode.getElementsByTagName("div")[0].style.display="none"
			drop=false
			select_activa=''
		}
	}	

	dhtmlselectbox.onmouseout=function(){
		if(select_activa =='' && drop)
			select_activa='dhtml_drop'+selectid
	}
}