//code found at http://www.geocities.com/nbilalis/combobox.htm
function ComboBox(name) {
	
// The parameter "name" must have the same value as the name we give to the object.
// If not errors will be raised as this parameter is used to reference the Object prperties and function.

// Also an ID is randomly calculated which is used as a prefix for the name of the HTML controls.
// This ID is random for the following reason. IE and other browsers remember values entered i na textfield.
// If the textfield stays the same a popup comes out w/ previous given values that covers our layer.
	
	this.obj = name;

//////////////////
//  Properties  //
//////////////////

	this.filter			= true;
	
	this.height			= "100";
	this.width			= "367";
	
	this.fontName		= "Verdana";
	this.fontSize		= "12px";
	
	this.fontColor		= "black";
	this.bgColor		= "white";
	this.borderColor	= "#7F9DB9";
	this.fontColor		= "black";
	
	this.hiliteFColor	= "white";
	this.hiliteBColor	= "gray";

	this.blurTimer		= null;	
	this.scrollTimer	= null;
	this.stopTimer 		= false;
////////////////////////////
//	Read-only Properties  //
////////////////////////////
	
	this.getLength		= function() {return _noe;};
	
	this.hasFocusIndex	= function() {return _hasfocusIndex;};
	this.selectedIndex	= function() {return _selectedIndex;};
	
/////////////////////////
//  Private Variables  //
/////////////////////////
	
	var _noe		= 0;
	var _elements	= new Array();
	
	
	var _filter		= null;
	var _contents	= "";

	var _noe_f		= 0
	var _indexes	= [];
	
	var isIE=(document.all)?true:false;
	
	var _blurTimeout = 100;
	var _scrollTimeout = 50;
	
	//preloaded images
	var normalButton = new Image();
	var overButton = new Image();
	var downButton = new Image();

	normalButton.src = "/images/downArrow.gif";
	overButton.src = "/images/downArrow_over.gif";
	downButton.src = "/images/downArrow_down.gif";
///////////////////////
//  State Variables  //
///////////////////////

	var _selectedIndex	= -1;
	var _hasfocusIndex	= -1;

	var _expanded		= false;
	var _alreadyExpanded = false;

///////////////////////
//  Event Variables  //
///////////////////////	

//	var _filterChanged = false; 

////////////////////////
//	Public Functions  //
////////////////////////

	this.add = function(text, value) { _elements[_noe++] = [text, (value)?value:text]; }
	 
	this.getText  = function(index) { return (index<_noe)?_elements[index][0]:""; }
	this.getValue = function(index) { return (index<_noe)?_elements[index][1]:""; }

	this.toString = function() {
		// overLIB command output
		var _command = this.obj + ".handle(event);";
		
		// Final output
		var _out = 	"<style type='text/css'>" +
						"<!--" +
						"input#" + this.obj + "_textbox {width:" + (this.width-20) + "; border:0px;padding:0px;margin:0px;height:13px;}" +
												
						"#" + this.obj + "_scroll_div {height:" + this.height + ";overflow:auto;}" +
						"#" + this.obj + "_scroll_div div {white-space:nowrap;cursor:pointer;cursor:hand;padding:2px}" +
						"#" + this.obj + "_button {width:15px;height:14px;border:0;}" +
						
						"." + this.obj + "_over {color:" + this.hiliteFColor + ";background-color:" + this.hiliteBColor + ";}" +
						"." + this.obj + "_out  {color:" + this.fontColor + ";background-color:" + this.bgColor + ";}" +
						"-->" +
					"</style>" +
					"<div style='width:" + this.width + "; border:1px solid " + this.borderColor + ";' onmouseover='" + this.obj + "_button.src=\"" + overButton.src + "\"' onmousedown='" + this.obj + "_button.src=\"" + downButton.src + "\"' onmouseup='" + this.obj + "_button.src=\"" + overButton.src + "\"' onmouseout='" + this.obj + "_button.src=\"" + normalButton.src + "\"'>" +
						"<table border='0' cellpadding='0' cellspacing='0' width='100%'>" +
							"<tr bgcolor='#FFFFFF' valign='center'>" +
								"<td><input type='text' id='" + this.obj + "_textbox' name='" + this.obj + "_textbox' onkeyup=\"" + _command + "\" onblur=\"" + _command + "\" autocomplete='off' onkeypress='return disableEnter(event);' style=\"height:18px;\" /></td>" +
								"<td><input type='hidden' name='" + this.obj + "_'/></td>" +
								"<td><img name='" + this.obj + "_button' src='/images/downArrow.gif' onclick=\"" + _command + "\" hspace='1' vspace='1' /></td>" +
							"</tr><tr>" +
								"<td colspan='3'><a name='" + this.obj + "_anchor'></a></td>" +
						"</tr></table>" +
					"</div>";
		
		// Handlers
		_objTextbox 	= MM_findObj(this.obj + "_textbox");

		return _out;
	}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////
//	Private Functions  //
/////////////////////////

// These function are in fact declared as public, that is, there are visible from outside.
// There are declared this way so that they can reference the current object.
// Otherwise the "this" operator can not be used and will produce an "undefined" error.
	
	this.expand = function() {
		overlib("<div id=" + this.obj + "_scroll_div onscroll='" + this.obj + ".scroll();' onmousedown='" + this.obj + ".scroll();'></div>", WIDTH, this.width, FGCOLOR, this.bgColor, BGCOLOR, this.borderColor, CELLPAD, 0, STICKY, ANCHOR, this.obj + "_anchor", ANCHORALIGN, "LL", "UL");
		_expanded = true;
		
		var objTextfield = MM_findObj(this.obj + "_textbox");
		this.setContents(objTextfield.value);	
		
		//Setup the IFrame to display a solid background behind the DIV
	    //only needed in IE
		if (isIE)
		  this.createBG(); 
	}
	this.scrollMouseUp = function() {
		this.stopTimer = false;
		var objTextfield = MM_findObj(this.obj + "_textbox");
		objTextfield.focus();
	}
	
	this.collapse = function() {
		 nd(); nd();
		_expanded = false;
		
		 //Need to hide the background IFrame
		var IfrRef = document.getElementById( "dropdown_auto_bg" );
		if (IfrRef) 		
		  IfrRef.style.display = 'none';
	}

	this.scroll = function() {
		this.stopTimer = true;
	    clearTimeout(this.scrollTimer);
		this.scrollTimer = setTimeout(this.obj + ".scrollTimeout()", _scrollTimeout);
		
		var objTextfield = MM_findObj(this.obj + "_textbox");
		objTextfield.focus();
	}		
			
	this.scrollTo = function(index) {
		var _el = MM_findObj(this.obj + "_td_" + index);
		if (_el != null && _el.scrollIntoView)
			_el.scrollIntoView();
	}

	this.select = function(index) {
		if(_elements[_indexes[index]]) {
			var _value = (index<_noe_f)?_elements[_indexes[index]][0]:"";
		
			_selectedIndex = index;
			MM_findObj(this.obj + "_textbox").value = _value;
			MM_findObj(this.obj + "_value").value = _elements[_indexes[index]][1];

			filter = _value;
		} else {
			MM_findObj(this.obj + "_textbox").value = "";
			MM_findObj(this.obj + "_value").value = "";
		}
		
		this.collapse();
	}

	this.setContents = function(filter) {
		MM_setTextOfLayer(this.obj + "_scroll_div", '', this.getContents(filter));
	}

	this.getContents = function(filter) {
		if (filter != _filter) {
			_contents = ""; 
		
			var _temp = new Array();
			var _re = new RegExp("(" + filter + ")", "ig")
			
			_noe_f = 0; _indexes = [];
			for (var i=0; i<_noe; i++) {
				if (!this.filter || _elements[i][0].toLowerCase().indexOf(filter.toLowerCase())>=0) {
					_temp.push("<div id='" + this.obj + "_td_" + i + "' title='" + this.obj + "_td_" + i + "' class='" + this.obj + ((i==_hasfocusIndex)?"_over":"_out") + "' onmouseover='" + this.obj + ".over(" + _noe_f + ");' onmouseout='" + this.obj + ".out(" + _noe_f + ");' onclick='" + this.obj + ".select(" + _noe_f + ");'>" + _elements[i][0].replace(_re, "$1".bold()) + "</div>");
					_indexes[_noe_f++] = i; 
				}
			}
			_contents = _temp.join(''); 

		_filter = filter;

		//	alert("different");
		} else {
		//	alert("same");
		}
		
		return _contents;
	}
	
	
	this.handle = function(event) {
		var keyCode = (String(event.which)=="undefined")?event.keyCode:event.which;

	//	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	//	alert(keyCode);
	//	alert(event.srcElement);
		switch (String(keyCode)) {
			case "13" :	this.select(_hasfocusIndex);	break;	// Enter
		//	case "27" :									break;	// Escape
		//	case "32" :									break;	// Space
			case "38" :	this.focusPrevious();			break;	// Arrow Up
			case "40" :	this.focusNext();				break;	// Arrow Down
			default   :	
						var objTextfield = MM_findObj(this.obj + "_textbox");
						switch (event.type) {
						  case "click" : 
						    if (!_expanded) {
							  clearTimeout(this.blurTimer);
							  this.expand();
							  objTextfield.focus();
							} else {
							  this.collapse();
							}
							
							break;
						  case "keyup"	: 
						    if (objTextfield.value == "") {
							  _hasfocusIndex = -1;
							  this.collapse();
							} else {
							  this.expand();
							  if (_noe_f>0) this.over(0);
						  	}

							break;
						  case "blur" 	: 
						    if (!this.stopTimer) {
						    clearTimeout(this.blurTimer);
						    this.blurTimer = setTimeout(this.obj + ".blurTimeout();", _blurTimeout);
							}
							
							break;
						  default : break;
						}

						break;
		}
	}
	
	this.scrollTimeout = function() {
	  this.stopTimer = false;
	}
	
	this.blurTimeout = function() {
	  if (!this.stopTimer) {
	    if (_hasfocusIndex > -1) {
		  this.select(_hasfocusIndex);}
	    else
		  this.collapse();
	  }
	}
	
	//creates a solid white background behind the popup menu
	this.createBG = function() {
		var popupBox = document.getElementById("overdiv")
		var tmpIfrRef = document.getElementById( "dropdown_auto_bg" );
						
		//only insert the IFrame if one does not already exist
		if(!tmpIfrRef)
 		  popupBox.insertAdjacentHTML("afterEnd","<IFRAME name=\"dropdown_auto_bg\" id=\"dropdown_auto_bg\" style=\"DISPLAY: none; LEFT: 0px; POSITION: absolute; TOP: 0px\" src=\"javascript:false;\" frameBorder=\"0\" scrolling=\"no\"></IFRAME>");

		var IfrRef = document.getElementById( "dropdown_auto_bg" );
		IfrRef.style.width = popupBox.offsetWidth;
		IfrRef.style.height = popupBox.offsetHeight;
		IfrRef.style.top = popupBox.style.top;
		IfrRef.style.left = popupBox.style.left;
		IfrRef.style.zIndex = popupBox.style.zIndex - 1;
		IfrRef.style.display = "block";
	}
		
	this.over = function(index)	{ MM_findObj(this.obj + "_td_" + _indexes[index]).className = this.obj + "_over"; _hasfocusIndex = index;}
	this.out  = function(index)	{ MM_findObj(this.obj + "_td_" + _indexes[index]).className = this.obj + "_out" ; _hasfocusIndex = -1;}
	
	this.focusPrevious = function() {
		this.out(_hasfocusIndex);
		_hasfocusIndex = (_hasfocusIndex > 0)? _hasfocusIndex-1 : _noe_f-1;
		this.over(_hasfocusIndex);
		
		this.scrollTo(_indexes[_hasfocusIndex]);
	}
	
	this.focusNext = function() {
		this.out(_hasfocusIndex);
		_hasfocusIndex = (_hasfocusIndex < _noe_f-1)? _hasfocusIndex+1 : 0;
		this.over(_hasfocusIndex);
		
		this.scrollTo(_indexes[_hasfocusIndex]);
	}

	this.debug = function() {
		window.status = (_expanded?"Expanded":"Collapsed") + " " + _indexes.join(",") ;
	}


}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
   		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_setTextOfTextfield(objName,x,newText) { //v3.0
  var obj = MM_findObj(objName); if (obj) obj.value = newText;
}

function MM_setTextOfLayer(objName,x,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write((newText)); document.close();}
    else innerHTML = unescape(newText);
}

function MWJ_findObj( oName, oFrame, oDoc ) {
    /* this function is slightly bigger than the DreamWeaver
    function but is more efficient as it can also find
    anchors, frames, variables, functions, and check through
    any frame structure

    if not working on a layer, document should be set to the
    document of the working frame
    if the working frame is not set, use the window object
    of the current document
    WARNING: - cross frame scripting will cause errors if
    your page is in a frameset from a different domain */
    if( !oDoc ) { if( oFrame ) { oDoc = oFrame.document; } else {
        oDoc = window.document; } }

    //check for images, forms, layers
    if( oDoc[oName] ) { return oDoc[oName]; }

    //check for pDOM layers
    if( oDoc.all && oDoc.all[oName] ) { return oDoc.all[oName]; }

    //check for DOM layers
    if( oDoc.getElementById && oDoc.getElementById(oName) ) {
        return oDoc.getElementById(oName); }

    //check for form elements
    for( var x = 0; x < oDoc.forms.length; x++ ) {
        if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }

    //check for anchor elements
    //NOTE: only anchor properties will be available,
    //NOT link properties!
    for( var x = 0; x < oDoc.anchors.length; x++ ) {
        if( oDoc.anchors[x].name == oName ) {
            return oDoc.anchors[x]; } }

    //check for any of the above within a layer in layers browsers
    for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) {
        var theOb = MWJ_findObj( oName, null, oDoc.layers[x].document );
            if( theOb ) { return theOb; } }

    //check for frames, variables or functions
    if( !oFrame && window[oName] ) { return window[oName]; }
    if( oFrame && oFrame[oName] ) { return oFrame[oName]; }

    //if checking through frames, check for any of the above within
    //each child frame
    for( var x = 0; oFrame && oFrame.frames &&
      x < oFrame.frames.length; x++ ) {
        var theOb = MWJ_findObj( oName, oFrame.frames[x],
          oFrame.frames[x].document ); if( theOb ) { return theOb; } }

    return null;
}

function disableEnter(event) {
	var keyCode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
	return (String(keyCode)!="13");
}		

// If Push and pop is not implemented by the browser
if (!Array.prototype.push) {
	Array.prototype.push = function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
};
if (!Array.prototype.pop) {
	Array.prototype.pop = function array_pop() {
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
};