var window_onload_fired = false;
function setWindowOnLoadFlag() {
	window_onload_fired = true;
}
if (window.attachEvent)
	window.attachEvent("onload", setWindowOnLoadFlag);
function dropdown_menu_hack( theSelect ) { 
	if (theSelect.runtimeStyle.behavior.toLowerCase() == "none") {
		return; 
	}
	theSelect.runtimeStyle.behavior="none";
	if (theSelect && theSelect.length <= 0)
	{
		return;
	}
	if (window_onload_fired) {
		expandingDropdownInit();
	}
	else {
		window.attachEvent('onload',expandingDropdownInit ); 
	}
	function expandingDropdownInit() {
		if (!this.doesExist) {
			this.doesExist = 1;
			this.divFrame = new DivFrame();
		}
		if (!theSelect.doesExist) {
			theSelect.doesExist = 1;
			theSelect.divFrame = this.divFrame;
			selectInit(theSelect);
		}
	}
	
	function selectInit(theSelect) {
		theSelect.contentOptions = new Array();
		theSelect.contentIndex = theSelect.selectedIndex;
		theSelect.contentHTML = theSelect.outerHTML;
		theSelect.contentIndexSelected = theSelect.contentIndex;  
		for(var i=0;i<theSelect.options.length;i++)
		{
			theSelect.contentOptions [theSelect.contentOptions.length] =
			{
				"value": theSelect.options[i].value,
				"text": theSelect.options[i].innerHTML
			}
			if(!theSelect.options[i].selected){theSelect.options[i].removeNode(true);i--;};
		}
		theSelect.showMenu = function showMenu() {
			theSelect.contentIndexSelected = theSelect.contentIndex; 
			if (theSelect.divFrame.menuIsHidden())
				theSelect.contentIndexOpened = theSelect.contentIndex; 
			function selectMenu(obj)
				{
					var o = document.createElement("option");
					o.value = theSelect.contentOptions[obj.selectedIndex].value;
					o.innerHTML = obj.innerHTML;
					while(theSelect.options.length>0){theSelect.options[0].removeNode(true);}
					theSelect.appendChild(o);
					theSelect.title =  o.innerHTML;  
					theSelect.contentIndex = obj.selectedIndex;
					theSelect.contentIndexSelected = theSelect.contentIndex; 
					theSelect.writeToCell(); 
					this.divFrame.hide(theSelect);
				} 
			  
			var mb = this.divFrame.front; 
			mb.style["margin"] = "0px";
			mb.style["padding"] = "0px";
			var t = theSelect.contentHTML;
			t = t.replace(/<select/gi,'<ul');
			t = t.replace(/<option/gi,'<li');
			t = t.replace(/<\/option/gi,'</li');
			t = t.replace(/<\/select/gi,'</ul');
			mb.innerHTML = t;
			theSelect.UL = mb.all.tags("ul")[0];
			theSelect.UL.style.cssText = "list-style:none; margin:0; padding:0; width:100%;";
			mb.options = theSelect.UL.getElementsByTagName("li");
			mb.style.width = "0";
			mb.style.direction = theSelect.style.direction;
			mb.dir = theSelect.dir;
			mb.style.fontFamily = theSelect.currentStyle.fontFamily;
			mb.style.fontSize = theSelect.currentStyle.fontSize;
			function unselectedOption( obj ) {
				if (theSelect.style.backgroundColor)
					obj.style.backgroundColor = theSelect.style.backgroundColor;
				else
					obj.style.backgroundColor = "Window";
				if (theSelect.style.color)
					obj.style.color = theSelect.style.color;
				else
					obj.style.color = "WindowText";
			} 
			function selectedOption (obj) {
				obj.style.background="Highlight";
				obj.style.color="HighlightText";
				mb.options.selected = obj;
			} 
			for(var i=0;i<mb.options.length;i++)
			{
				mb.options[i].selectedIndex = i;
				mb.options[i].style.cssText = "list-style:none; margin:0; padding:1px 2px; width:100%; cursor:default; white-space:nowrap; border-style: none;";
				unselectedOption (mb.options[i]);
				mb.options[i].onmousemove = function()
					{ 
						var ev = window.event;
						if (ev.screenX == theSelect.divFrame.prevScreenX && ev.screenY == theSelect.divFrame.prevScreenY)
							return false;
						theSelect.divFrame.prevScreenX = ev.screenX;
						theSelect.divFrame.prevScreenY = ev.screenY;
						if( mb.options.selected ) {
							unselectedOption (mb.options.selected);
						}
						selectedOption (this);
						theSelect.contentIndex = this.selectedIndex;
					} 
				mb.options[i].onmousedown = function() {
				  selectMenu(this);
				  return true;
				}
				if(i == theSelect.contentIndex)
				{
					selectedOption (mb.options[i]);
				}
			} 
			var mw = theSelect.offsetWidth;
			mw = Math.max(  mw, ( mb.scrollWidth) ) + parseInt(mb.style.borderLeftWidth) + parseInt(mb.style.borderRightWidth) + (theSelect.divFrame.IEStrict ? 0 : (parseInt(mb.offsetWidth) - parseInt(mb.clientWidth)) );
			var tbox = mb.options[0].getBoundingClientRect();
			var lh = (tbox.bottom - tbox.top ); 
			var mh =  (mb.options.length) * lh;
			var mx = (this.divFrame.ie5)?-3:0;
			var my = theSelect.offsetHeight;
			var docH =   document.documentElement.offsetHeight ;
			var bottomH = docH  - theSelect.getBoundingClientRect().bottom ; 
			mh = Math.min(mh, Math.max(( docH - theSelect.getBoundingClientRect().top - 50),100) )
						+ (theSelect.divFrame.IEStrict?0:parseInt(mb.style.borderTopWidth) + parseInt(mb.style.borderBottomWidth));
			if(( bottomH < mh) )
			{
				mh = Math.max( (bottomH - lh),10);
				if( mh <100 ) 
				{
					my = -100 ;
				}
				mh = Math.max(mh,100);
			}
			theSelect.divFrame.show( mx , my ,  mw, mh , theSelect);
			mb.onblur = function() { 
				if (theSelect.divFrame.menuIsDisplayed())
					theSelect.divFrame.hide(theSelect);
				}
			mb.onkeydown = function(){ 
				theSelect.switchMenu(); 
				return false; 
				}
			mb.onmousewheel = function() { 
				return false;
				} 
			if(mb.options.selected)
			{
			  var r = mb.options.selected.getBoundingClientRect();
			  var opH;
			  if (r)
			    opH = r.bottom - r.top + 2;
			  else
			    opH = 20;
			  
			  if (mb.options.selected.offsetTop + opH > mb.scrollTop + mh)
			    mb.scrollTop = mb.options.selected.offsetTop + opH - mh;
			  
			  else if (mb.options.selected.offsetTop < mb.scrollTop)
			    mb.scrollTop = mb.options.selected.offsetTop;
			  
			}
			  
			window.onresize = function () { 
				mb.onblur();
			}
	}  
	theSelect.switchMenu = function switchMenu(ev) 
	{
		var event;
		if (ev)
			event = ev;
		else if (window.event) 
			event = window.event;
		else if (document.frames && document.frames[fid] && document.frames[fid].event)
			event = document.frames[fid].event;
		else
			event = null;
	  if (!(event)) {
	    return false;
	  }
		window.dropdownTimeoutFunc  = function() {
			theSelect.click();
		}
		var prv = false;
		var nxt = false;
		var selectionMade = false;
		var tabPressed = false;
		if (event.keyCode)
		{
			prv = (event.keyCode == 38 || event.keyCode == 37); 
			nxt = (event.keyCode == 40 || event.keyCode == 39); 
			if ( event.altKey && (event.keyCode == 38 || event.keyCode == 40) ) {
				if (theSelect.divFrame.menuIsDisplayed()) {
					theSelect.divFrame.hide( theSelect );
					theSelect.writeToCell();
				}
				else { 
					window.setTimeout("(dropdownTimeoutFunc ())",0);
				}
				return false;
			}
			else if (event.keyCode == 33)
				{ theSelect.contentIndex -= 8; }
			else if (event.keyCode == 34)
				{ theSelect.contentIndex += 8; }
			else if (nxt) 
				{ theSelect.contentIndex++; }
			else if (prv) 
				{ theSelect.contentIndex--; }
			else if (event.keyCode == 27) 
				{ 
				}
			else if (event.keyCode == 13) 
				{
					selectionMade = true;
				}
			else if (event.keyCode == 9)  
				{
					tabPressed = true;
					selectionMade = true;
				}
			else 
				{
					return true;
				}
		} 
		else if (event.wheelDelta)
		{
			if (event.wheelDelta >= 120)
				theSelect.contentIndex--;
			else if (event.wheelDelta <= -120)
				theSelect.contentIndex++;
		}
		else 
			{	return true; }
		if (theSelect.contentIndex > (theSelect.contentOptions.length-1) ){ 
			theSelect.contentIndex = theSelect.contentOptions.length-1;
			if (nxt || prv || theSelect.contentIndex == theSelect.contentIndexSelected)
				return false;
		}	else if (theSelect.contentIndex < 0) {
			theSelect.contentIndex = 0;
			if (nxt || prv || theSelect.contentIndex == theSelect.contentIndexSelected)
				return false;
		}
		
		theSelect.contentIndexSelected = theSelect.contentIndex;
		var o = document.createElement("option");
		if ( event.keyCode == 27 && theSelect.divFrame.menuIsDisplayed()) {
			theSelect.contentIndexSelected = theSelect.contentIndexOpened;
			theSelect.divFrame.hide( theSelect );
		}
		o.value = theSelect.contentOptions[theSelect.contentIndex].value;
		o.innerHTML = theSelect.contentOptions[theSelect.contentIndex].text;
		while(theSelect.options.length>0){theSelect.options[0].removeNode(true);}
		theSelect.appendChild(o);
		theSelect.title =  o.innerHTML;  
		if ( event.keyCode == 27) { 
			theSelect.writeToCell();
			return false;
		}
		if (theSelect.divFrame.menuIsDisplayed()){
			if (selectionMade) {
				theSelect.divFrame.hide( theSelect );
				theSelect.writeToCell();
				if (tabPressed) {
					return true;
				}
			}
			else
				theSelect.showMenu(); 
		}
		else {
			if (!tabPressed) {
				theSelect.writeToCell();
				theSelect.fireEvent("onchange"); 
			}
		}
		return false; 
	} 
	theSelect.writeToCell = function writeToCell(someText)
	{
		var theId = null;
		if (theSelect.id) theId = theSelect.id;
		else if (theSelect.name) theId = theSelect.name;
		if (!theId) return;
		if (!someText)
			someText = theSelect.contentOptions[theSelect.contentIndex].text;
		var cell = document.getElementById(theId + "Output");
		if (!cell) return;
		else
			cell.innerHTML = someText;
	}
		theSelect.onkeydown = function () {
		  theSelect.switchMenu();
		  return true;
		}
		theSelect.onmousedown = function () {
			if (window.event.button != 1)
				return;
			window.dropdownTimeoutFunc  = function() {
				theSelect.click();
			}
			if (theSelect.divFrame.menuIsHidden())
				window.setTimeout("(dropdownTimeoutFunc ())", 0);
			return false;
		}
		theSelect.onmousewheel = function () { 
		  
		  return false;
		}
		theSelect.onclick = function () {
			theSelect.showMenu();
			return true;
		}
		
	}
	function DivFrame() {
		this.fid = "dropdown_menu_hack_" + Date.parse(new Date()); 
		this.fbackid = this.fid+"_back"; 
		this.ie5 = (document.namespaces==null);
		this.IEStrict = (document.documentElement && document.documentElement.clientHeight);
		if (!this.fback) {
			this.fback = document.createElement("iframe");
			this.fback.src = "javascript:false;";
			this.fback.name = this.fbackid;
			this.fback.scrolling = "no";
			this.fback.border = "0";
			this.fback.frameBorder = "0";
			this.fback.vSpace = "0";
			this.fback.hSpace = "0";
			this.fback.marginHeight = "0";
			this.fback.marginWidth = "0";
			this.fback.style.cssText = "border:0px solid WindowFrame; padding:0px; margin:0px; visibility:hidden; top:0px; left:0px; width:0px; height:0px; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=100);";
		}
		if (!this.front) {
		  this.front = document.createElement("div");
			this.front.id=this.fid;
			this.front.style.cssText = "display:block; width:100px; height:100px; background:Window; color:WindowText; z-index:999999; top:0px; left:0px; position:absolute; border:1px solid WindowFrame; margin:0; padding:0; visibility:hidden; tab-index:999; background-color:Window;";
			if (!this.IEStrict)
				this.front.style.overflowY = "scroll";
			else
				this.front.style.overflowY = "auto";
		}
		this.fback.style.zIndex = (this.front.style.zIndex - 1);
		document.body.appendChild(this.fback);
		document.body.appendChild(this.front);
		this.show = function(px,py,pw,ph,baseElement)
				{ 
					py = py + baseElement.getBoundingClientRect().top + Math.max( document.body.scrollTop, document.documentElement.scrollTop) - 2;
					px = px + baseElement.getBoundingClientRect().left + Math.max( document.body.scrollLeft, document.documentElement.scrollLeft) - 2;
					this.front.style.width = pw + "px";
					this.front.style.height = ph + "px";
					this.front.style.left = px;
					this.front.style.top = py;
					if (this.IEStrict)
						this.fback.style.width = Math.max( (parseInt(this.front.scrollWidth) + 2 * parseInt(this.front.style.borderWidth) ), parseInt(this.front.offsetWidth) ) + "px";
					else
						this.fback.style.width = parseInt(this.front.offsetWidth) + "px";
					this.fback.style.height = (parseInt(this.front.offsetHeight) + (this.IEStrict?0:0) ) + "px";
					this.fback.style.left = parseInt(this.front.style.left) ;
					this.fback.style.top = parseInt(this.front.style.top);
					this.fback.style.visibility="visible";
					this.front.style.visibility="visible";
					this.front.focus();
				}
		this.hide = function hide(theSelect) {
			if (this.menuIsHidden()) return;
			if(window.event && window.event.srcElement
				&& window.event.srcElement.tagName &&
			  window.event.srcElement.tagName.toLowerCase()=="select") { return true; }
			this.front.style.visibility="hidden";
			this.fback.style.visibility="hidden";
			this.front.style.width = "0";
			this.front.style.height = "0";
			
			var sel = theSelect;
			if (sel) {
				sel.focus();
				sel.contentIndex = sel.contentIndexSelected;
				if (sel.contentIndex != sel.contentIndexOpened) {sel.fireEvent("onchange");}
			}
		}
		this.menuIsHidden = function () {
			return (this.front.style.visibility.toLowerCase() == "hidden");
		}
		this.menuIsDisplayed = function () {
			return (!this.menuIsHidden());
		}
	}
} 
