var zIndexForSelectContainerDiv=1;

function hsInitAdvSelect(id)
{
	var oInput = document.getElementById(id+"_input");
	var oSel = document.getElementById(id);
	var oDiv = document.getElementById(id+"_div");
	var oImg = document.getElementById(id+"_img");

	oDiv.style.position = "absolute";
	oDiv.style.display = "none";
	oDiv.style.zIndex = 100;
	oDiv.style.left = oInput.offsetLeft+"px";
	oDiv.style.top = oInput.offsetHeight + "px";
	oInput.style.cursor = "default";

	if (oSel.selectedIndex != -1)
	{	
	    oInput.value = oSel.options[oSel.selectedIndex].text;
	}
	else
	{
	    if(oSel.options[0]) oInput.value = oSel.options[0].text;
	}
	
	oInput.onfocus = function() {
		hsShowAdvSelect(oSel.id);
	}
	oInput.onblur = function() {
		hsHideAdvSelect(oSel.id);
	}
	oSel.onblur = function() {
		hsHideAdvSelect(oSel.id);
	}
	oSel.oldOnChange = oSel.onchange;
	oSel.onclick = function() {
		hsClickAdvSelect(oSel.id);
		if (oSel.oldOnChange) oSel.oldOnChange();
	}
	oSel.onchange=null;
	oSel.onfocus = function() { if(!document.all) { document.activeElement=this; } }
	oSel.onkeypress = function() {
		if(window.event && (window.event.keyCode == 13))
		{
		    hsClickAdvSelect(oSel.id);
		    if (oSel.oldOnChange) oSel.oldOnChange();
		}		
	}
	oImg.onmousedown = function() {
		var s = oImg.src;
		oImg.src = s.replace(/bg.gif/g, "bg_mo.gif");
	}
	oImg.onmouseup = function() {
		var s = oImg.src;
		oImg.src = s.replace(/bg_mo.gif/g, "bg.gif");
	}
	oImg.onclick = function() {
		hsShowAdvSelect(oSel.id);
	}
}

function hsHideAdvSelect(id)
{
  if(document.activeElement && (document.activeElement.id!=id)&&((document.activeElement.id+"_img")!=id)&&((document.activeElement.id+"_input")!=id))
  {
	hideField(id+"_div");
  }
}

function hsShowAdvSelect(id)
{
	var oInput = document.getElementById(id+"_input");
	var oDiv = document.getElementById(id+"_div");
	var oSel = document.getElementById(id);
	
	oDiv.style.left = oInput.offsetLeft+"px";
	oDiv.style.top = oInput.offsetHeight + "px";
	oDiv.parentNode.style.zIndex = ++zIndexForSelectContainerDiv;

	var i = oSel.options.length;
	if (i>10)i=10;
	if (i==1)i=2;
	oSel.size=i;

	showField(id+"_div");
	for(i=0;i<oSel.options.length;i++)
	{
		if(oSel.options[i].text==oInput.value)oSel.selectedIndex=i;
	}
	if(oSel)oSel.focus();
}

function hsClickAdvSelect(id)
{
	var o=document.getElementById(id);
	if(o)
	{
		var i=document.getElementById(id+"_input");
		i.value=o.options[o.selectedIndex].text;
	}	
	hideField(id+"_div");
}