var focused = null;

function formKeyDown(form)
{
	if (event.keyCode == 13)
	{
		if ((form.document.activeElement.type != "textarea") && (form.document.activeElement.type != "button") && (form.document.activeElement.type != "submit") && (form.document.activeElement.type != "reset") && (form.document.activeElement.tagName != "A") && !form.document.activeElement.classid)
			event.keyCode = 9;
	}
	if (event.keyCode == 9)
	{
		var selected = selectNextControl(form, !event.shiftKey);
		
		if (selected) event.returnValue = false;
	}
}

function getNextControl(page, focusedControl)
{
	var nextControl = null;

	for (var i = 0; i < page.all.length; i++) 
	{
		if (page.all[i].disabled) continue;
		
		if (page.all[i].tabIndex > focusedControl.tabIndex)
		{
			if ((nextControl == null) || (nextControl.tabIndex > page.all[i].tabIndex)) nextControl = page.all[i];
		}
	}
	
	if (nextControl == null)
	{
		for (var i = 0; i < page.all.length; i++) 
		{
			if (page.all[i].disabled) continue;
			
			if (page.all[i].tabIndex > 0)
			{
				if ((nextControl == null) || (nextControl.tabIndex > page.all[i].tabIndex)) nextControl = page.all[i];
			}
		}
	}
	
	if (nextControl == null) nextControl = focusedControl;
	
	return nextControl;
}

function getPrevControl(page, focusedControl)
{
	var nextControl = null;

	for (var i = 0; i < page.all.length; i++) 
	{
		if (page.all[i].disabled) continue;
		
		if ((page.all[i].tabIndex > 0) && (page.all[i].tabIndex < focusedControl.tabIndex))
		{
			if ((nextControl == null) || (nextControl.tabIndex < page.all[i].tabIndex)) nextControl = page.all[i];
		}
	}
	
	if (nextControl == null)
	{
		for (var i = 0; i < page.all.length; i++) 
		{
			if (page.all[i].disabled) continue;
			
			if ((page.all[i].tabIndex > 0) && (page.all[i].tabIndex > focusedControl.tabIndex))
			{
				if ((nextControl == null) || (nextControl.tabIndex < page.all[i].tabIndex)) nextControl = page.all[i];
			}
		}
	}
	
	if (nextControl == null) nextControl = focusedControl;
	
	return nextControl;
}

function selectNextControl(page, forward)
{
	var result = focused != null;
	
	if (result)
	{
		var nextControl = forward? getNextControl(page, focused) : getPrevControl(page, focused);
	
		if (nextControl != null) nextControl.focus();
	}
	
	return result;
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? ";expires=" + expires.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure" : "");

}