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, bRecursive)
{
  var value = false;
  if ( IsObject( w))
  {
    if ( bRecursive)
    {
      if ( w.textContent)
        value = w.textContent; // For all browsers
      else
        value = w.innerText; // For IE
    }
    else
      value = w.lastChild.nodeValue;
  }
  return value;
}

function SetItemValue( w, value)
{
  if ( IsObject( w))
  {
    w.value = value;
  }
}
function SetItemInnerHtml( w, value)
{
  if ( IsObject( w))
  {
    w.innerHTML = value;
  }
}
function SetItemText( w, value)
{
  if ( IsObject( w))
  {
    if ( w.lastChild)
      w.lastChild.nodeValue = value;
    else
    {
      var textNode = w.document.createTextNode( value);
      w.appendChild( textNode);
    }
  }
}

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 SetItemListInnerHtml( arItems, value)
{
  if ( IsObject( arItems))
  {
    for (var i=0; i<arItems.length; i++) 
    {
      SetItemInnerHtml( 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 SetDisabled( item, bDisabled)
{
  if ( item)
  {
    if ( bDisabled)
      item.disabled = true;
    else
      item.disabled = false;
  }
}

function SetFocus( item)
{
  if ( item)
    item.focus();
}

function SetContentEditable( item, bEditable)
{
  if ( item)
  if ( 'contentEditable' in item)
  {
    TurnSpellCheck( item);
    item.contentEditable = bEditable;
  }
}

function GetContentDocument( item)
{
  if ( item.ownerDocument)
    return item.ownerDocument;
  // To iframes:
  if ( item.contentDocument)
    return item.contentDocument;
  return item.contentWindow.document;
}

function TurnSpellCheck( item, bOn)
{
  if ( bOn == null)
    bOn = false;
  var doc = GetContentDocument( item);
  if ( doc)
  {
    var body = doc.body;
    if ( body)
    if ('spellcheck' in body)
      body.spellcheck = false;
  }
}

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
  {
    // This line causes error message in FF, but works correctly
    w.location.replace( strUrl);
  }
  catch (e)
  {
    // This line causes error message in FF, but works correctly
    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, bKeepDefault)
{
  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();
      if ( !bKeepDefault)
        e.preventDefault();
    }
    else
    {
      if ( !bKeepDefault)
        e.returnValue = false;
      e.cancelBubble = true;
    }
  }
}

function EventItem( event)
{
  var item = null;
  if ( event.target)
  {
    item = event.target;
  }
  else
  {
    item = event.srcElement;
  }
  return item;
}


function RemoveAllChild( node)
{
  if ( node)
  while ( node.firstChild)
  {
    RemoveAllChild( node.firstChild)
    node.removeChild( node.firstChild);
  };
}

function IsChild( item, parentObject)
{
  var currentObject = ParentNode( item);
  var bResult = false;
  while ( currentObject && !bResult)
  {
    if ( currentObject == parentObject)
      bResult = true;
    else
      currentObject = ParentNode( currentObject);
  }
  return bResult;
}

function FindElementByTag( object, strTagName, nCount)
{
  var arItem = new Array();
  var bNext = true;
  for ( var j = 0;(( j < object.childNodes.length) && bNext); j++)
  {
    var item = object.childNodes[j];
    if ( item.tagName)
    if ( item.tagName.toLowerCase() == strTagName.toLowerCase())
    {
      arItem.push( item);
      if ( nCount)
      {
        nCount--;
        if ( nCount == 0)
          bNext = false;
      }
    }
  }
  return arItem;
}

function IsInputTag( item, bTextInputOnly)
{
  var bIs = false;
  var strLcTagName = item.tagName.toLowerCase();
  if ( strLcTagName == 'input')
  {
    if ( bTextInputOnly)
    {
      if ( item.type == 'text')
        bIs = true;
    }
    else
      bIs = true;
  }
  else
  if ( strLcTagName == 'textarea')
    bIs = true;
  return bIs;
}

