function TopParent( w)
{
	var topParent = null;
	while ((w)&&(w != topParent))
	{
		topParent = w;
		w = w.parent;
	}
	return topParent;
}
function IsObject( w)
{
	var bIs = true;
	if (!w)
	{
		bIs = false;
	}
	//alert( "IsObject = "+bIs);
	return bIs;
}
function GetIdItem( strId, w)
{
	if ( IsObject( w))
	{
		return w.document.getElementById( strId);
	}
	return document.getElementById( strId);
}

function GetItemValue( w)
{
	//alert( "GetItemValue( "+w+")");
	var value = false;
	if ( IsObject( w))
	{
		value = w.value;
	}
	return value;
}

function GetItemText( w)
{
	var value = false;
	if ( IsObject( w))
	{
		value = w.lastChild.nodeValue;
	}
	return value;
}

function SetItemValue( w, value)
{
	if ( IsObject( w))
	{
		w.value = value;
	}
}
function SetItemText( w, value)
{
	if ( IsObject( w))
	{
		w.lastChild.nodeValue = value;
	}
}

function IsChecked( w)
{
	if ( IsObject( w))
	{
		return w.checked;
	}
}
function GetCheckedItem( arItems)
{
	var item = null;
	if ( IsObject( arItems))
	{
		for (var i=0; (( item == null) && ( i<arItems.length)); i++) 
		{
			if ( IsChecked( arItems[i]))
				item = arItems[i];
		}
	}
	return item;
}

function FirstCheckedId( arId, w)
{
	var nId = null;
	if ( IsObject( arId))
	{
		for (var i=0; (( nId == null) && ( i<arId.length)); i++) 
		{
			if ( IsChecked( GetIdItem( arId[i], w)))
				nId = arId[i];
		}
	}
	return nId;
}

function SetItemChecked( w, bChecked)
{
	if ( IsObject( w))
	{
		var bCheckedValue;
		bCheckedValue = false;
		if ( bChecked)
			bCheckedValue = true;
		w.checked = bCheckedValue;
		//alert( "SetItemChecked = "+w.checked);
	}
}
function SetItemAction( w, script)
{
	w.action = script;
}

function SetItemActionPosition( w, strPosition)
{
	if ( IsObject( w))
	{
		var strAction = w.action;
		var nIndexOfPosition = strAction.indexOf( "#");
		if ( nIndexOfPosition >= 0)
		{
			strAction = strAction.substr( 0, nIndexOfPosition);
		}
		if ( strPosition)
			strAction += "#" + strPosition;
		w.action = strAction;
	}
}

function GetNamedItems( strName, w)
{
	//alert( "GetNamedItems( "+strName+", "+w+")");
	if ( IsObject( w))
	{
		return w.document.getElementsByName( strName);
	}
	return document.getElementsByName( strName);
}

function GetFirstItemByName( strName, w)
{
	//alert( "GetFirstItemByName( "+strName+", "+w+")");
	var arItems = GetNamedItems( strName, w);
	if ( arItems.length > 0)
	{
		return arItems[0];
	}
	return null;
}

function SetItemListValue( arItems, value)
{
	if ( IsObject( arItems))
	{
		for (var i=0; i<arItems.length; i++) 
		{
			SetItemValue( arItems[i], value);
		}
	}
}
function SetItemListText( arItems, value)
{
	if ( IsObject( arItems))
	{
		for (var i=0; i<arItems.length; i++) 
		{
			SetItemText( arItems[i], value);
		}
	}
}
function SetItemListChecked( arItems, bChecked)
{
	//alert( "SetItemListChecked( )");
	//alert( "SetItemListChecked( "+arItems+", "+bChecked+")");

	if ( IsObject( arItems))
	{
		for (var i=0; i<arItems.length; i++) 
		{
			SetItemChecked( arItems[i], bChecked);
		}
	}
}

function AllChildCheckBox( itemRoot)
{
	var arCheckBox = null;
	if ( IsObject( itemRoot))
	{
		var arStack = new Array();
		var arCheckBox = new Array();

		var item = itemRoot;
		do
		{
			var child = item.firstChild;
			while ( child)
			{
				if ( child.type == 'checkbox')
					arCheckBox.push( child);
				else
				if ( child.tagName)
					arStack.push( child);
				child = child.nextSibling;
			}
			var item = arStack.pop();
		}
		while ( item);
	}
	return arCheckBox;
}

function SetAllChildChecked( itemRoot, bChecked)
{
	SetItemListChecked( AllChildCheckBox( itemRoot), bChecked);
}

function SetValueByName( strInputName, strValue)
{
	SetItemListValue( GetNamedItems( strInputName), strValue);
	return true;
}

function GetClassName( item)
{
	var strClassName = '';
	if ( item)
		strClassName = item.className;
	return strClassName;
}
function SetClassName( item, strClassName)
{
	if ( item)
		item.className = strClassName;
	return true;
}

function SetClassNameById( nId, strClassName, w)
{
  var item;
  SetClassName( GetIdItem( nId, w), strClassName);
  return true;
}

function SetClassNameByName( strName, strClassName, w)
{
  var arItems;
	arItems = GetNamedItems( strName, w);
	for (var i=0; i<arItems.length; i++) 
	{
		SetClassName( arItems[i], strClassName);
	}
  return true;
}

function SetDisplayStyle( item, strDisplay)
{
	if ( item)
		item.style.display = strDisplay;
	return true;
}

function SetDisplayStyleById( nId, strDisplay, w)
{
  var item;
  SetDisplayStyle( GetIdItem( nId, w), strDisplay);
  return true;
}

function SetTarget( item, strTarget)
{
	if ( item)
		item.target = strTarget;
	return true;
}

function SetTargetById( nId, strTarget, w)
{
  var item;
  SetTarget( GetIdItem( nId, w), strTarget);
  return true;
}

function SetFocus( item)
{
	if ( item)
		item.focus();
}

function DoSubmit( strFormId)
{
	var form = GetIdItem( strFormId);
	form.submit();
}

function FirstId( strIdList, strDelimiter)
{
	var strResult = strIdList;
	var nIndex = strIdList.indexOf( strDelimiter);
	if ( nIndex >= 0)
		strResult = strIdList.substr( 0, nIndex);
	return strResult;
}

function CheckedInputValue( strInputName)
{
	var strValue = false;
	var item = GetCheckedItem( GetNamedItems( strInputName, null));
	if ( item)
	{
		strValue = GetItemValue( item);
	}
	return strValue;
}

function SetAttribute( node, strName, strValue)
{
	if ( node)
	{
		node.setAttribute( strName, strValue);
	}
}

function ParentNode( item, nStep)
{
	if (( nStep == null) || ( nStep <= 0))
		nStep = 1;
	var i = 0;
	for ( i = 0; (( item) && ( i < nStep)); i++)
	{
		item = item.parentNode;
	}
	return item;
}

function AddChild( parent, node, beforeNode)
{
	if ( parent)
	if ( node)
	{
		if ( beforeNode)
			parent.insertBefore( node, beforeNode);
		else
			parent.appendChild( node);
	}
}

function RemoveFromParent( node)
{
	if ( node)
	if ( node.parentNode)
	{
		node.parentNode.removeChild( node);
	}
}

function SetOpenerLocation( strLocation)
{
	var bChanged = false;
	if ( strLocation != null)
	if ( strLocation != '')
	if ( top.opener)
	{
		try
		{
			top.opener.location = strLocation;
			bChanged = true;
		}
		catch (e)
		{
			bChanged = false;
		}
	}
	return bChanged;
}

function ReplaceLocation( strUrl, w)
{
	if ( w == null)
		w = window;
	try
	{
		w.location.replace( strUrl);
	}
	catch (e)
	{
		w.location = strUrl;
	}
}


function AddEventHandler( strEventName, hOnEvent, obj, bCapture)
{
	if (obj == null)
		obj = window;
	if ( bCapture == null)
		bCapture = false;
	if ( obj.addEventListener)
		obj.addEventListener( strEventName, hOnEvent, bCapture);
	else
	if ( obj.attachEvent)
	{
		obj.attachEvent( 'on'+strEventName, hOnEvent);
	}
}

function CancelEventBuble( e)
{
	if ( e == null)
	if ( window.event)
	{
		// IE if event set by "document.onmousedown = handler" or something like this
		e = event;
	}
	if ( e)
	{
		if ( e.stopPropagation)
		{
			e.stopPropagation();
			e.preventDefault();
		}
		else
		{
			e.returnValue = false;
			e.cancelBubble = true;
		}
	}
}


function RemoveAllChild( node)
{
	if ( node)
	while ( node.firstChild)
	{
		RemoveAllChild( node.firstChild)
		node.removeChild( node.firstChild);
	};
}

