
var cssFileName="i/xselect.css";
var arrXSelect=new Array();


var doc=(document.all)?document.all:document;
var isIE=(document.all)?true:false;

function fLoadXSelect()
{

	fLoadCSS(cssFileName);
	fPreparePage();
}

function fLoadCSS(pCSSFileName)
{
		if(document.all)
		{
			var oStyle=document.createElement('LINK');
			oStyle.type="text/css";
			oStyle.rel="StyleSheet";
			oStyle.href=pCSSFileName;
			document.appendChild(oStyle);
		}

		var oStyle=document.createElement('LINK');
		oStyle.id='xselectCSS';
		oStyle.type="text/css";
		oStyle.rel="StyleSheet";
		oStyle.href=pCSSFileName;
		document.body.appendChild(oStyle);
}

function fPreparePage()
{


	var arrDiv=document.getElementsByTagName("div");
	var select;

	for (var i=0;i<arrDiv.length;i++ )
	{
		if (arrDiv[i].className=="select")
		{

			select=new xSelect(arrDiv[i]);

			select.id=arrXSelect.push(select);
			select.btnClicked=false;
		}
	}
}

function xSelect(obj)
{
	var sName=obj.getAttribute("name");
	var oUL=obj.getElementsByTagName("ul")[0];
	this.current=false;
	this.create(sName,obj,oUL);
}

xSelect.prototype.create=function(pName,container,ul)
{
	var oInput=document.createElement("INPUT");
	oInput.setAttribute("type","text");
	oInput.setAttribute("size","18");
	oInput.style.cursor="pointer";
	oInput.style.cursor="default";
	oInput.style.paddingLeft=5;
	oInput.readOnly=true;
	oInput.className="select";
	container.insertBefore(oInput,ul);
	
	var btn=this.drawButton(oInput.offsetHeight);
	container.insertBefore(btn,ul);

	var oHidden=document.createElement((isIE)?"<INPUT name='"+pName+"'>":"INPUT");
	oHidden.setAttribute("name",pName);
	oHidden.setAttribute("value","");
	oHidden.setAttribute("type","hidden");
	
	if(container.id)
	{
		oHidden.setAttribute("id",container.id);
		container.removeAttribute("id");
	}

	oHidden.value="";
	container.insertBefore(oHidden,ul);

	with(ul.style)
	{
		display="none";
		padding="0px 0px 0px 0px";
		margin="0px 0px 0px 0px";
		width=oInput.offsetWidth-((isIE)?0:25);
		border="1px solid black";
		listStyleType="none";
		listStyleImage="none";
		if(ul.childNodes>8)
		{
			height=70;	
		}
		else
		{
			height=ul.childNodes.length*14;	
		}
		overflowY="auto";
		overflowX="hidden";
		position="absolute";
		backgroundColor="white";
		color="black";
		zIndex="10000";
		top=oInput.offsetTop+oInput.offsetHeight; 
		left=oInput.offsetLeft; 
		cursor="pointer";
		cursor="default";
	}
	

	xs=this;

	var arrOption=ul.getElementsByTagName("li");

	for (var i=0; i<arrOption.length; i++)
	{
		arrOption[i].style.paddingLeft=5;
		arrOption[i].onmouseover=function(){this.className="optionover"};
		arrOption[i].onmouseout=function(){this.className="optionout"};
		arrOption[i].onclick=
			function(){
				this.className="optionout"
				oInput.value=this.innerHTML;
				oHidden.value=(this.getAttribute("value")==0)?'':this.getAttribute("value");
				ul.style.display="none";			
				xs.btnClicked=false;
				xs.currentValue=this.innerHTML;
				xs.current=false;
			};

		if (arrOption[i].getAttribute("selected"))
		{
			arrOption[i].onclick();
		}
		else
		{
			arrOption[0].onclick();
		}
	}

	/*oInput.onblur=function()
	{
			ul.style.display="none";			
			xs.btnClicked=false;
	}*/

	btn.onfocus=function(){oInput.focus();}

	oInput.onfocus=function()
	{
		
			if (!xs.btnClicked)
			{
				ul.style.display="block";
				xs.btnClicked=true;
				xs.current=ul;
			}
			else
			{
				ul.style.display="none";			
				xs.btnClicked=false;
				xs.current=false;
			}
	}

	window.document.body.onclick=function()
	{
		if((window.event.srcElement.parentElement.className!="select")&&(window.event.srcElement.parentElement.parentElement.className!="select"))
			if(xs.current)
			{
					xs.current.style.display="none";			
					xs.btnClicked=false;		
			}

	}
	
}


xSelect.prototype.drawButton=function(pWidth)
{
	var oButton=document.createElement("BUTTON");

	with(oButton.style)
	{
		height=pWidth-((isIE)?3:6);
		width=pWidth-4;
		position="relative";
		left=2-pWidth;
		top=(isIE)?-1:4;
/*		backgroundImage="url(i/select-button.gif)";
		backgroundRepeat="no-repeat";
		backgroundPosition="center center";
*/
	}
	var oImg=document.createElement("IMG");
	oImg.setAttribute("src","i/select-button.gif");

	oButton.appendChild(oImg);

	return oButton;
}



window.onload=fLoadXSelect;

