
var currentMenu = null;
var currentMenuParent = null;
var currentMenuItem = null;
var skipMenuClose = false;

function submitForm(form)
{
	if (!form.onsubmit || form.onsubmit())
		form.submit();
}

function validateFormEditBox(editBox, message)
{
	if (editBox.value == "")
	{
		var label = editBox;
		while (message == null || message == "")
		{
			if (label.previousSibling == null)
				label = label.parentNode;
			else
				label = label.previousSibling;
				
			message = getInnerTextEx(label);
		}
		
		message = message.replace(/:/g, '.');
		message = message.replace(/\* /g, '');
		alert(message);
		
		editBox.focus();
		return false;
	}

	return true;
}

function validateFormCheckBoxesGroup(parentNode, groupPrefix, message)
{
	var checkBoxes = parentNode.getElementsByTagName("INPUT");
	for (index = 0; index < checkBoxes.length; index++)
	{
		checkBox = checkBoxes.item(index);
		if (checkBox.type != "checkbox" || checkBox.name.substr(0, groupPrefix.length) != groupPrefix)
			continue;

		if (checkBox.checked)
			return true;
	}

	alert(message);
	return false;
}

function validateFormComboBox(comboBox, message)
{
	var comboBoxValue = comboBox[comboBox.selectedIndex].value;
	if (comboBoxValue == "" || comboBoxValue == null)
	{
		if (message == null || message == "")
		{
			message = comboBox[comboBox.selectedIndex].text;
			message = message.replace(/\[/g, '');
			message = message.replace(/\]/g, '.');
		}
		
		alert(message);
		comboBox.focus();
		return false;
	}

	return true;
}

function findParentElementWithTag(currentElement, findTagWithName1, findTagWithName2)
{
	if (currentElement == null)
		return null;
	
	if (currentElement.tagName == findTagWithName1 || currentElement.tagName == findTagWithName2)
		return currentElement;
	
	return findParentElementWithTag(currentElement.parentElement, findTagWithName1, findTagWithName2);
}

function isChildOfElement(findWhat, findWhere)
{
	if (findWhere == null)
		return false;

	var childrenArray = findWhere.children;
	if (childrenArray == null || childrenArray.length == 0)
		return false;
	
	var childNumber = 0;
	for (childNumber = 0; childNumber < childrenArray.length; childNumber++)
	{
		var childElement = childrenArray[childNumber];
		if (childElement == findWhat)
			return true;
		
		if (isChildOfElement(findWhat, childElement))
			return true;
	}
	
	return false;
}

function isEventBlockedByMenu()
{
	if (currentMenu == null)
		return false;
	
	if (isChildOfElement(window.event.srcElement, currentMenu))
		return false;
	
	return true;
}

function menuOpen(menuId, menuParent, menuItem)
{
	if (currentMenu != null)
	{
		dtControlReset(currentMenuItem);
		currentMenu.style.display = "none";
	}
	
	currentMenu = document.getElementById(menuId);
	if (currentMenu == null)
		return;

	if (menuItem == null)
		menuItem = menuParent;
	if (typeof(onMenuOpen) == "function")
		onMenuOpen(currentMenu, menuItem);
	
	skipMenuClose = true;
	currentMenuParent = menuParent;
	currentMenuItem = menuItem;
	
	var left = 0;
	var top = 0;
	var width = menuParent.offsetWidth;
	var height = menuParent.offsetHeight;
	do
	{
		left += menuParent.offsetLeft;
		top += menuParent.offsetTop;
		
		menuParent = menuParent.offsetParent;
	}
	while (menuParent != null);

	currentMenu.style.left = Math.max(left, left + width - parseInt(currentMenu.style.width) + 3);
	currentMenu.style.top = top + height + 2;
	currentMenu.style.display = "";
}

function menuClose()
{
	if (currentMenu == null)
		return;

	if (skipMenuClose == true)
	{
		skipMenuClose = false;
		return;
	}
	
	if (typeof(onMenuClose) == "function")
		onMenuClose(currentMenu);

	ControlReset(currentMenuItem);

	currentMenu.style.display = "none";
	currentMenu = null;
	currentMenuParent = null;
}

function getCurrentButton()
{
	return findParentElementWithTag(window.event.srcElement, "TD", "TABLE");
}

function getButtonLink()
{
	var button = getCurrentButton();
	if (button == null)
		return;
		
	button.className = button.id + "1";

	var a;
	if (button.getElementsByTagName)
		a = button.getElementsByTagName("A");
	else
		a = button.all.tags("A");
	if (a == null)
		return null;
	else
		return a[0];
}

function controlReset(element)
{
	if (!element)
		return;

	if (element.id == "footerButton" || element.id == "sideBarButton" || element.id == "sideBarSubButton")
	{
		element.className = element.id + "0";
		
		element.onmouseenter = onButtonMouseEnter;
		element.onmouseleave = onButtonMouseLeave;
		element.onclick = onButtonClick;
	}

	if (element.id == "bitmapButton")
	{
		element.onmouseenter = onBitmapButtonMouseEnter;
		element.onmouseleave = onBitmapButtonMouseLeave;
		element.onclick = onButtonClick;
	}
}

function controlsResetForParent(item)
{
	if (item == null)
		item = document;

	var elements = item.getElementsByTagName("*");
	for (index = 0; index < elements.length; index++)
		controlReset(elements.item(index));
}

function controlsReset()
{
	controlsResetForParent();
}

function getInnerTextEx(element)
{
	if (typeof element == "string" || typeof element == "undefined")
		return element;

	if (element.innerText)
		return element.innerText;

	var innerText = "";
	var children = element.childNodes;
	var length = children.length;
	for (var index = 0; index < length; index++)
	{
		switch (children[index].nodeType)
		{
			case 1: //ELEMENT_NODE
				innerText += getInnerTextEx(children[index]);
				break;

			case 3: //TEXT_NODE
				innerText += children[index].nodeValue;
				break;
		}
	}

	return innerText;
}

var modifiersShiftKey = null;

function onEventEx(e, h)
{
	if (e == null)
		return;
		
	window.event = e;
	window.event.x = e.x;
	window.event.y = e.y;
	
	modifiersShiftKey = window.event.shiftKey;
	
	var t  = e.target;
	while (t)
	{
		window.event.fromElement = t;
		window.event.srcElement = t;
		window.event.toElement = t;
		
		if (h(t))
			return;
			
		t = t.parentNode;
	}
}

function onHandleMouseOver(e)
{
	onEventEx(e, function (t) { if (t.onmouseenter) { t.onmouseenter(); return true; } else return false; });
}

function onHandleMouseOut(e)
{
	onEventEx(e, function (t) { if (t.onmouseleave) { t.onmouseleave(); return true; } else return false; });
}

function onHandleClick(e)
{
	onEventEx(e, function (t) { if (t.onclick) { t.onclick(); return true; } else return false; });
}

function onBitmapButtonMouseEnter()
{
	var button = getCurrentButton();
	if (isEventBlockedByMenu() || !button)
		return;
		
	var images = button.getElementsByTagName("IMG");
	images[0].style.display = "none";
	images[1].style.display = "";

	var a = getButtonLink();
	if (a)
	{
		if (a.title)
			window.status = a.title;
		else
			window.status = a.href;
	}
}

function onBitmapButtonMouseLeave()
{
	var button = getCurrentButton();
	if (isEventBlockedByMenu() || !button)
		return;

	var images = button.getElementsByTagName("IMG");
	images[0].style.display = "";
	images[1].style.display = "none";
	
	window.status = "";
}

function onButtonMouseEnter()
{
	if (isEventBlockedByMenu())
		return;
		
	var button = getCurrentButton();
	if (button)
		button.className = button.id + "1";

	var a = getButtonLink();
	if (a)
	{
		if (a.title)
			window.status = a.title;
		else
			window.status = a.href;
	}
}

function onButtonMouseLeave()
{
	if (isEventBlockedByMenu())
		return;
		
	var button = getCurrentButton();
	if (button)
		button.className = button.id + "0";
	
	window.status = "";
}

function onButtonClick()
{
	var button = getCurrentButton();
	var command = button.scope;

	if (button.popup == null)
		button = findParentElementWithTag(button, "TABLE");

	if (button != null && button.popup != null && window.event.srcElement.tagName != "A")
	{
		var menuParent = currentMenuParent;
		if (currentMenu)
		{
			menuClose();
			button.className = button.id + "1";
		}

		if (menuParent != button)
			menuOpen(button.popup, button);
		
		return;
	}

	if (command == null)
		return;

	if (command.substr(0, 11) == "javascript:")
		eval(command);
	else
	if (command != null && command != "")
	{
		var newWindow = false;
		if (modifiersShiftKey != null)
			newWindow = modifiersShiftKey;
		else
			newWindow = window.event.shiftKey;
			
		if (newWindow)
			window.open(command);
		else
			window.location.href = command;
	}
}

function onDocumentLoaded()
{
	if (window.document.captureEvents)
	{
		window.document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.CLICK);
		window.document.onmouseover = onHandleMouseOver;
		window.document.onmouseout = onHandleMouseOut;
		window.document.onclick = onHandleClick;
	}

	controlsReset();

	window.document.onclick = menuClose;
	window.document.oncontextmenu = menuClose;
}
