function ComboBox()
{
	// How many pixels to the right/left of the cursor to show the popup
	// Values between 3 and 12 are best
	this.offsetx = 0;

	// How many pixels to the below the cursor to show the popup
	// Values between 3 and 12 are best
	this.offsety = 10;

	this.ns4 = (document.layers)? true:false
	this.ie4 = (document.all)? true:false

	// Microsoft Stupidity Check
	if (this.ie4) {
		if (navigator.userAgent.indexOf('MSIE 5') > 0) {
			this.ie5 = true;
		} else {
			this.ie5 = false;}
	}
//	 else {
//		this.ie5 = false;
//	}

	this.x = 0;
	this.y = 0;
	this.snow = 0;
	this.sw = 0;
	this.cnt = 0;
	this.dir = 1;
	this.currentWidth = 100;
	
	this.mouseMove = function(e)
	{
		if (comboBox.ns4) { comboBox.x = e.pageX; comboBox.y = e.pageY; }
		if (comboBox.ie4) { comboBox.x = event.x; comboBox.y = event.y; }
		if (comboBox.ie5) { comboBox.x = event.x + document.body.scrollLeft; y = event.y + document.body.scrollTop; }
	}
	
	this.getPopupDiv = function()
	{
		var overDiv = document.getElementById( 'comboDiv' );

		if ( overDiv != null && overDiv != 'undefined' )
			return overDiv;
		else
		{

			overDiv = document.createElement('div');
			overDiv.id = 'comboDiv';
			overDiv.style.visibility = 'hidden';
			overDiv.style.position = 'absolute';
			document.body.appendChild(overDiv);
			
			return overDiv;
		}
	}
	
	this.GetAbsolutePos = function(el)
	{
		var SL = 0, ST = 0;
		var is_div = /^div$/i.test(el.tagName);
		if ( is_div && el.scrollLeft )
			SL = el.scrollLeft;
		if ( is_div && el.scrollTop )
			ST = el.scrollTop;
		var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		if ( el.offsetParent )
		{
			var tmp = this.GetAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	} 

/*
	if ((this.ns4) || (this.ie4))
	{
		document.onmousemove = this.mouseMove;
		if (this.ns4) document.captureEvents(Event.MOUSEMOVE);
	}
*/
}

var comboBox = new ComboBox();

// Public functions to be used on pages.

ComboBox.selectVariant = function( idToStore, textToStore )
{
	document.getElementById( idToStore ).value = textToStore;
	ComboBox.killEvent();
	ComboBox.hidePopupDiv();
}

// Caption popup
ComboBox.showPopupDiv = function(idToLink, width, idText)
{
	
	ComboBox.hidePopupDiv();

	var controlToLink = document.getElementById( idToLink );
  comboBox.offsety = controlToLink.offsetHeight;
  if ( width != "" )
		comboBox.currentWidth = width;
	else
	{
		comboBox.currentWidth = controlToLink.clientWidth;
		width = controlToLink.clientWidth;
	}

  var text = document.getElementById( idText ).value;
  var txt = "<table width="+width+" border='0' cellpadding='0' cellspacing='0' style='background-color:White;'><tr><td >"+text+"</td></tr></table>";

  ComboBox.layerWrite(txt);
  
  ComboBox.disp( controlToLink );
	ComboBox.killEvent();
  ComboBox.addEvent( document, 'click', ComboBox.hidePopupDiv );
  ComboBox.HideShowCovered( comboBox.getPopupDiv(), 'hidden' );
  comboBox.getPopupDiv().style.height = '215px';
  comboBox.getPopupDiv().style.width = (parseInt(width.replace('px',''))+16)+'px';
  comboBox.getPopupDiv().style.overflow = 'auto';
  comboBox.getPopupDiv().style.className ='bordered';
}

ComboBox.mailToEscape = function( mailTo )
{
	location.href = 'mailto:' + mailTo;
	ComboBox.killEvent();
}

ComboBox.killEvent = function()
{
	if ( window.event )
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} 
}

ComboBox.HideShowCovered = function( el, vis )
{
	var tags = new Array("applet", "iframe", "select");

	var p = comboBox.GetAbsolutePos(el);
	var EX1 = p.x;
	var EX2 = el.offsetWidth + EX1;
	var EY1 = p.y;
	var EY2 = el.offsetHeight + EY1;
	for (var k = tags.length; k > 0; )
	{
		var ar = document.getElementsByTagName(tags[--k]);
		var cc = null;

		for (var i = ar.length; i > 0;)
		{
			cc = ar[--i];

			p = comboBox.GetAbsolutePos(cc);
			var CX1 = p.x;
			var CX2 = cc.offsetWidth + CX1;
			var CY1 = p.y;
			var CY2 = cc.offsetHeight + CY1;
			if (!((CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)))
				cc.style.visibility = vis;
		}
	}
}

// Clears popups if appropriate
ComboBox.hidePopupDiv = function()
{
  if (comboBox.cnt >= 1) { comboBox.sw = 0 };
  {
    if (comboBox.sw == 0) {
      comboBox.snow = 0;
      ComboBox.hideObject(comboBox.getPopupDiv().style);
    } else {
      comboBox.cnt++;
    }
  }
  ComboBox.removeEvent( document, 'click', ComboBox.hidePopupDiv );
  ComboBox.HideShowCovered( comboBox.getPopupDiv(), 'visible' );
}

ComboBox.addEvent = function( el, evname, func )
{
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else { // Opera (or old browsers)
		el["on" + evname] = func;
	}
}

ComboBox.removeEvent = function( el, evname, func )
{
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else { // Opera (or old browsers)
		el["on" + evname] = null;
	}
}

// Common calls
ComboBox.disp = function ( controlToLink ) {
{
    if (comboBox.snow == 0)  {
      if (comboBox.dir == 1)
      { // Right
		var position = comboBox.GetAbsolutePos( controlToLink );
        ComboBox.moveTo( comboBox.getPopupDiv().style, position.x+comboBox.offsetx, position.y+comboBox.offsety );
      }
      
      ComboBox.showObject(comboBox.getPopupDiv().style);
      comboBox.snow = 1;
    }
  }
// Here you can make the text goto the statusbar.
}

// Writes to a layer
ComboBox.layerWrite = function (txt) {
   
  if (comboBox.ns4) {
    var lyr = comboBox.getPopupDiv().document;
    lyr.write(txt);
    lyr.close();
  } else if (comboBox.ie4) {
    comboBox.getPopupDiv().innerHTML = txt;
  }
  else
  {
    comboBox.getPopupDiv().innerHTML = txt;
  }
}

// Make an object visible
ComboBox.showObject = function (obj) {
  if (comboBox.ns4) obj.visibility = "show";
  else if (comboBox.ie4) obj.visibility = "visible";
		else obj.visibility = "visible";
  
}

// Hides an object
ComboBox.hideObject = function (obj) {
  if (comboBox.ns4) obj.visibility = "hide";
  else if (comboBox.ie4) obj.visibility = "hidden";
  else obj.visibility = "hidden";
}

// Move a layer
ComboBox.moveTo = function (obj, xL, yL) {
  obj.left = xL;
  obj.top = yL;
}
