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 7.0") + 1) {
			nTop = findPosY(combos[i]); 
		}
		nLeft = findPosX(combos[i]);
	
		textfield = $(combos[i].id + "_text");
		inittextfield(combos[i], textfield);
		if(detect.indexOf("msie 7.0") + 1) {
			initIframe(combos[i]);
			textfield.style.top = nTop + "px";
		}
		textfield.style.left = nLeft + "px";
		//Use iframe hack for Internet Explorer
		if(detect.indexOf("msie 7.0") + 1) {
			initIframe(combos[i]);
			hackFrame = $("frame" + combos[i].id);
			hackFrame.style.top = nTop + "px";
			hackFrame.style.left = nLeft + "px";
		}
	}
}

function inittextfield(ctrl, textfield) {

	selectWidth = ctrl.offsetWidth;  
	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 = "6px";
		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 if((detect.indexOf("msie 9.0") + 1)){
		selectButtonWidth = 23;
		textfield.style.marginTop = "7px";
		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";
	textfield.style.display = "inline";

	formItemLocation = ctrl.id.indexOf("VehicleInfo_");
	formItemLocation2 = ctrl.id.indexOf("VehicleInfo_Model");
	if (formItemLocation == -1 || formItemLocation2 > -1)
	{
		ctrl.onchange=function() {
			changeCombobox(this);
		}
	}

	ctrl.onfocus=function() {
		this.selectedIndex = -1;
	}

	ctrl.onblur=function() {
		lastfocus = "";
	}
}

// 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 = this.id.substring(5) + "_text";
		handleIFrameTabindex(ctrlname)
	}
	textfield.insert({before: hackFrame});
}

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 7.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;
	textfield= $(ctrl.id + "_text");
	textfield.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 = $(ctrl.id + "_text");
	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();
	}
}
