window.onresize=function(){
	setCombobox();
}
window.onload=function(){
	setCombobox();
}

// To include a combo box on any page where Prototype is available:
// 1:  Include this javascript file.
// 2:  On any dropdown box (ASP or HTML), use the CSS Class 'comboBox', and surround it in a span element.
// 3:  At the end of the page, include the javascript command:  setCombobox()
// 4:  To retrieve the selected value from the combo box, use:  
//			VB: Request.Form(comboBoxId.ClientID).UniqueID / C#: Request.Form[comboBoxId.ClientID]
//				The ClientID may need to be changed from having underscores to having colons.
// Note:  The dropdown box must have a manual width, or the 'size' attribute will cause problems.

var nTop;
var nLeft;
var detect = navigator.userAgent.toLowerCase();
var lastfocus;

// Find every select with the comboBox class, then attach the needed elements.

function setCombobox() {
	combos=$$('.comboBox');
 	for(i=0; i<combos.length; i++) {
		textfield = $("txt" + combos[i].id);
		if(textfield == null) {
			inittextfield(combos[i]);
			//Use iframe hack for Internet Explorer
			if(!(detect.indexOf("opera") + 1) && (detect.indexOf("msie") + 1)) {
				initIframe(combos[i]);
			}
		}
	}
}

function inittextfield(ctrl) {

	selectWidth = ctrl.offsetWidth;  

   textfield = document.createElement("input");
	textfield.id = "txt" + ctrl.id;
	textfield.className = "comboText";
	textfield.style.zIndex = "99999";
	textfield.style.border = "none";
	textfield.value = ctrl.options[ctrl.selectedIndex].text;
	textfield.style.position = "absolute";
	
	//Account for Browser Interface Differences Here
	if((detect.indexOf("safari") + 1)) {
		selectButtonWidth = 22
		textfield.style.left = "0px";
	}
	else if((detect.indexOf("firefox") + 1)) {
		selectButtonWidth = 21;
		textfield.style.left = "2px";
	}
	else {
		selectButtonWidth = 23;
		textfield.style.left = "0px";
		textfield.style.top = "1px";
	}
	
	textfield.style.width = (selectWidth - selectButtonWidth) + "px";
	ctrl.parentNode.appendChild(textfield);
	
	ctrl.onchange=function() {
		val = this.options[this.selectedIndex].text;	
		document.getElementById("txt" + this.id).value = val;
	}
	
	ctrl.onblur=function() {
		lastfocus = "";
	}
	
	textfield.onblur=function() {
		lastfocus = this.id;
		ctrlname = this.id.substring(3, this.id.length);
		thisctrl = $(ctrlname);
		textvalue = this.value;
		if (thisctrl.selectedIndex >= 0)
		{
			ctrlmatches = false;
			ctrlvalue = thisctrl.options[thisctrl.selectedIndex].text;
			if (ctrlvalue.toUpperCase() == textvalue.toUpperCase())
			{
				ctrlmatches = true;
			}
			else if (thisctrl.length > 0)
			{
				var i=0;
				for (i=0;i<thisctrl.length;i++)
				{
					if (thisctrl.options[i].text.toUpperCase() == textvalue.toUpperCase())
					{
						thisctrl.selectedIndex = i;
						ctrlmatches = true;
						if (textvalue.length > 0)
						{
							fireDropdownChange(thisctrl);
						}
						break;
					}
				}
			}
			if (ctrlmatches == false)
			{
				ctrllength = thisctrl.length;
				if (thisctrl.getAttribute("custom") == "true")
				{
					thisctrl.options[ctrllength - 1].value = textvalue;
					thisctrl.options[ctrllength - 1].text = textvalue;
				}
				else
				{
					thisctrl.length = ctrllength + 1;
					thisctrl.options[ctrllength].value = textvalue;
					thisctrl.options[ctrllength].text = textvalue;
					thisctrl.setAttribute("custom", "true");
				}
				thisctrl.selectedIndex = thisctrl.length - 1;
			}
		}
		else
		{
			thisctrl.length = 1;
			thisctrl.options[0].value = textvalue;
			thisctrl.options[0].text = textvalue;
			thisctrl.setAttribute("custom", "true");			
		}
	}

}

// Internet Explorer hack requires an empty iFrame to force the textbox over the dropdown box.   

function initIframe(ctrl) {
   hackFrame = new Element('iframe');
   hackFrame.setAttribute("src", "ComboBoxPlaceholder.html");
	hackFrame.setAttribute("scrolling", "0");
	hackFrame.setAttribute("tabindex", "-1");
	hackFrame.id = "frame" + ctrl.id;
	ctrl.parentNode.insertBefore(hackFrame, textfield);
	hackFrame.absolutize();
	hackFrame.clonePosition(textfield);
	hackFrame.style.left = "2px";
	
	hackFrame.onfocus=function() {
		ctrlname = "txt" + this.id.substring(5);
		handleIFrameTabindex(ctrlname)
	}
}

function handleIFrameTabindex(ctrlname)
{
	// IFrames don't correctly render a negative tabindex in IE.  This fixes that problem.
	if (ctrlname != lastfocus)
	{
		$(ctrlname).focus();	
	}
	else
	{
		var dropDownCtrl = $(ctrlname);
		var frm = dropDownCtrl.form;
		
		for (var i = 2; i < frm.elements.length; i++)
		{
			if (frm.elements[i] == dropDownCtrl)
			{
				i -= 2;
				frm.elements[i].focus();
				break;
			}
		}
	}
	lastfocus = "";
}

function fireDropdownChange(ctrl)
{
	if (document.createEventObject){
		// dispatch for IE
		var evt = document.createEventObject();
		return ctrl.fireEvent('onchange',evt)
	}
	else{
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent('change', true, true ); // event type,bubbling,cancelable
		return !ctrl.dispatchEvent(evt);
	}
}