function RoundEditFocus(controlId)
{
	if (event.srcElement.tagName == "INPUT" || event.srcElement.tagName == "TEXTAREA") return;
	var box = document.getElementById(controlId);
	if (!box)
	{
		status = controlId + " could not be focused.";
		return;
	}
	if (box.type == 'hidden')	// in case of combobox
		box = box.parentElement.firstChild;	// move to display box
	if (!box.disabled)
	{	
		box.focus();
		SelectAllContents(box);
	}
}

function SelectAllContents(element)
{
	if (element.createTextRange)
	{
		var tr = element.createTextRange();
		tr.select();
	}
}

function RoundEditKeyUp()
{
	if (event.srcElement.tagName == "TEXTAREA") return;
	if (event.keyCode == 13)
	{
		var submit = FindSubmit(event.srcElement);
		if (submit.tagName == "FORM")
			submit.submit();
		else
		{
			submit.value = "1";
			return submit.click();
		}
		event.returnValue = false;
		return false;
	}
}

function FindSubmit(elementScope)
{
	var inputs = elementScope.getElementsByTagName("INPUT");
	for(var i in inputs)
		if (inputs[i].type && inputs[i].type.toLowerCase() == "submit")
			return inputs[i];
	if (elementScope.tagName == "FORM")
		return elementScope;
	return FindSubmit(elementScope.parentElement);
}

function ShowOptions(img,div,popup)
{
	img. src= '/App_Themes/Up 16 n p.png';
	div.style.minHeight = div.style.height;
	popup.style.display = '';
	if (div.offsetHeight)	// (only works in IE)
	{
		// save some values in container expando object
		div.oldParent = div.parentElement;
		div.oldParentHeight = div.parentElement.style.height;
		div.oldParentWidth = div.parentElement.style.width;
		div.oldWidth = div.style.width;
		div.oldHeight = div.style.height;
		div.oldLeft = div.style.left;
		div.oldTop = div.style.top;

		// freeze the parent element's width and height at the current sizes
		div.parentElement.style.height = div.parentElement.offsetHeight + "px";
		div.parentElement.style.width = div.parentElement.offsetWidth + "px";
		
		// calculate where the div used to be relative to the BODY element
		var x = 0, y = -1, e = div;
		while (e)
		{
			x += e.offsetLeft;
			y += e.offsetTop;
			e = e.offsetParent;
			if (e.tagName == "BODY") break;
		}
		
		// grow the container vertically
		div.style.height = div.style.maxHeight;
		if (div.offsetHeight > 30 + popup.scrollHeight) // trim height to minimum
			div.style.height = (30 + popup.scrollHeight) + 'px';
		
		// freeze the container width
		div.style.width = div.offsetWidth + 'px';
		
		// move the container in the DOM hierarchy to become a child of the BODY element
		document.body.insertAdjacentElement("beforeEnd", div);
		div.style.left = x + "px";
		div.style.top = y + "px";
	}
	else // other browsers
		div.style.height = div.style.maxHeight;
	event.returnValue = false;
	return false;
}

function HideOptions(img,div,popup,displayBox)
{
	popup.style.display = 'none';
	displayBox.focus();
	img. src= '/App_Themes/Down 16 n p.png';
	div.style.height = div.style.minHeight;
	if (div.offsetHeight)
	{
		div.oldParent.insertAdjacentElement("afterBegin", div);
		div.parentElement.style.height = div.oldParentHeight;
		div.parentElement.style.width = div.oldParentWidth;
		div.style.width = div.oldWidth;
		div.style.left = div.oldLeft;
		div.style.top = div.oldTop;
	}
}

function RoundComboboxToggle()
{
	var div = event.srcElement.parentElement.parentElement;
	var img = div.getElementsByTagName("IMG")[0];
	var displayBox = div.getElementsByTagName("INPUT")[0];
	var valueBox = div.getElementsByTagName("INPUT")[1];
	var popup = GetFirstChildWithId(div, "popup");
	if (!popup.firstChild)
		return;
	if (popup.style.display)	// are the options hidden at present?
		return ShowOptions(img,div,popup);
	else
		return HideOptions(img,div,popup,displayBox);
}

function GetFirstChildWithId(parent,id)
{
	var nodes = parent.childNodes;
	for(var i in nodes)
	{
		var e = nodes[i];
		if (e.id == id || e.name == id)
			return e;
	}
	for(var i in nodes)
	{
		var e = nodes[i];
		if (e.childNodes)
		{
			e = GetFirstChildWithId(e, id);
			if (e)
				return e;
		}
	}
	return null;
}

function RoundComboboxSelect(value)
{
	var option = event.srcElement;
	var display = option.innerText;
	var div = option.parentElement.parentElement;
	var inputs = div.getElementsByTagName("INPUT");
	var displayBox = inputs[0];
	var valueBox = inputs[1];
	valueBox.value = value;
	displayBox.value = display;
	RoundComboboxToggle();
	return false;
}
