window.onresize=function(){
	setCombobox();
}
window.onload=function(){
	setCombobox();
}

// This is a specialized version of Combobox.js only for use on the Service Scheduler.

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++) {
 		combos[i].tabIndex = -1;
 		formItemLocation = combos[i].id.indexOf("VehicleInfo_");
		if (formItemLocation > -1)
		{
			formLabelId = "FormLabel_" + combos[i].id.substring(formItemLocation + 12);
			$(formLabelId).tabIndex = -1;
		} 	
 		if((detect.indexOf("msie") + 1)&&!(detect.indexOf("msie 8.0") + 1)) {
			nTop = findPosY(combos[i]); 
		}
		nLeft = findPosX(combos[i]);
	
		textfield = $("txt" + combos[i].id);
		if(textfield == null) {
			inittextfield(combos[i]);
			//Use iframe hack for Internet Explorer
			if((detect.indexOf("msie") + 1)&&!(detect.indexOf("msie 8.0") + 1)) {
				initIframe(combos[i]);
			}
		}
		else{
			if((detect.indexOf("msie") + 1)&&!(detect.indexOf("msie 8.0") + 1)) {
				textfield.style.top = nTop + "px";
			}
			textfield.style.left = nLeft + "px";
			if((detect.indexOf("msie") + 1)&&!(detect.indexOf("msie 8.0") + 1)) {
				hackFrame = $("frame" + combos[i].id);
				hackFrame.style.top = nTop + "px";
				hackFrame.style.left = nLeft + "px";
			}
		}
	}
}

function inittextfield(ctrl) {

	selectWidth = ctrl.offsetWidth;  

   textfield = document.createElement("input");
	textfield.id = "txt" + ctrl.id;
	textfield.className = "comboText";
	textfield.style.zIndex = "99999";
	
	if (ctrl.selectedIndex >= 0)
	{
		textfield.value = ctrl.options[ctrl.selectedIndex].text;
	}
	else
	{
		textfield.value = "";
	}
	
	textfield.style.position = "absolute";
    textfield.style.left = nLeft + "px";
	textfield.style.border = "none";
	
	//Account for Browser Interface Differences Here
	if((detect.indexOf("safari") + 1)) {
		selectButtonWidth = 22
		textfield.style.marginTop = "0px";
		textfield.style.marginLeft = "1px";
		textfield.style.top = nTop + "px";
	}
	else if((detect.indexOf("firefox") + 1)) {
		selectButtonWidth = 20;
		textfield.style.marginTop = "6px";
		textfield.style.paddingBottom = "2px";
		textfield.style.marginLeft = "1px";	
		textfield.style.top = nTop + "px";		
	}
	else if((detect.indexOf("msie 8.0") + 1)){
		selectButtonWidth = 23;
		textfield.style.marginTop = "6px";
		textfield.style.marginLeft = "2px";
		textfield.style.top = "auto";
	}
	else {
		selectButtonWidth = 23;
		textfield.style.marginTop = "2px";
		textfield.style.marginLeft = "2px";
		textfield.style.top = nTop + "px";	
	}
	
	textfield.style.width = (selectWidth - selectButtonWidth) + "px";
    
	ctrl.parentNode.appendChild(textfield);	
	
	formItemLocation = ctrl.id.indexOf("VehicleInfo_");
	formItemLocation2 = ctrl.id.indexOf("VehicleInfo_Model");
	if (formItemLocation == -1 || formItemLocation2 > -1)
	{
		ctrl.onchange=function() {
			changeCombobox(this);
		}
	}
		
	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) {
	textWidth = textfield.offsetWidth;
	textHeight = textfield.offsetHeight;
   hackFrame = document.createElement("iframe");
   hackFrame.setAttribute("src", "ComboBoxPlaceholder.html");
	hackFrame.setAttribute("scrolling", "0");
	hackFrame.setAttribute("tabindex", "-1");
	hackFrame.id = "frame" + ctrl.id;
	hackFrame.style.position = "absolute";
	hackFrame.style.width = textWidth + "px";
	hackFrame.style.height = textHeight + "px";
	hackFrame.style.top = nTop + "px";
	hackFrame.style.left = nLeft + "px";
	hackFrame.style.marginTop = "2px";
	hackFrame.style.marginLeft = "2px";
	hackFrame.onfocus=function() {
		ctrlname = "txt" + this.id.substring(5);
		handleIFrameTabindex(ctrlname)
	}
	ctrl.parentNode.insertBefore(hackFrame, textfield);
}

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 findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		if((detect.indexOf("firefox") + 1)) {
			// If the item's name ends as: 'CustomerInfo_Xxxxx', assume that there is a 'FormLabel_Xxxxx' label.
			// and use its right margin to adjust the left positioning of the combobox in Firefox.
			formLabelId = "null";
			formItemLocation = obj.id.indexOf("CustomerInfo_");
			if (formItemLocation > -1)
			{
				formLabelId = "FormLabel_" + obj.id.substring(formItemLocation + 13);
			}
			formItemLocation = obj.id.indexOf("VehicleInfo_");
			if (formItemLocation > -1)
			{
				formLabelId = "FormLabel_" + obj.id.substring(formItemLocation + 12);
			}
			if (formLabelId != "null")
			{
				rightMargin = $(formLabelId).getStyle('margin-right');
				rightNumber = rightMargin.substring(0, rightMargin.indexOf("px"));
				curleft -= rightNumber;
				curleft -= 5;
			}
		}
		
		curleft += obj.offsetLeft;
		formItemLocation = obj.id.indexOf("VehicleInfo_");
		if((detect.indexOf("msie") + 1)&&!(detect.indexOf("msie 8.0") + 1)) {
			obj = obj.offsetParent;
			if (obj.offsetParent)
			{
				curleft += obj.offsetLeft;	
				obj = obj.offsetParent;
				if (obj.offsetParent)
				{
					curleft += obj.offsetLeft;	
					obj = obj.offsetParent;
					if (obj.offsetParent && formItemLocation > -1)
					{
						curleft += obj.offsetLeft;	
					}
				}
			}
		}
	}
	else if (obj.x)
	{
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		curtop += obj.offsetTop;
		formItemLocation = obj.id.indexOf("VehicleInfo_");
		if((detect.indexOf("safari") + 1)) {
			curtop += 2;
		}
		if((detect.indexOf("msie") + 1)) {
			obj = obj.offsetParent;
			if (obj.offsetParent)
			{
				curtop += obj.offsetTop;	
				obj = obj.offsetParent;
				if (obj.offsetParent)
				{
					curtop += obj.offsetTop;	
					obj = obj.offsetParent;
					if (obj.offsetParent && formItemLocation > -1)
					{
						curtop += obj.offsetTop;	
					}
				}
			}
		}
	}
	else if (obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

function changeCombobox(ctrl)
{
	val = ctrl.options[ctrl.selectedIndex].text;	
	$("txt" + ctrl.id).value = val;
}

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);
	}
}

function focusCombobox(ctrl)
{
	txtctrl = $("txt" + ctrl.id);
	if((detect.indexOf("msie") + 1)) {
		// IE loads up differently after a callback -- wait 1/10 of a second before trying to gain focus.
		var t=setTimeout("txtctrl.focus()",100);
	}
	else
	{
		txtctrl.focus();
	}
}