function OnPopupListEntity(nEntityType, sElemId)
{
  var
    sValueUser, sFilter, nMinSearchRank, sUrl, oEntity, nNewEntityId, nOldEntityIdsOnLoadedFunc;

  sValueUser = document.getElementById(sElemId + '_user').value;
  sFilter = IsBlank(document.getElementById(sElemId).filter, '');
  nMinSearchRank = IsBlank(document.getElementById(sElemId).getAttribute('minsearchrank'), '');
  sUrl = '../entity/pe-list-entity-frame.asp?et=' + escape(nEntityType) + '&s=' + escape(sValueUser) + '&flt=' + escape(sFilter) + '&msr=' + escape(nMinSearchRank);
  if (nEntityType == -1)
  {
    sUrl += '&f=' + escape(sElemId);
  }
  oEntity = window.showModalDialog(sUrl,document,'dialogWidth:520px;dialogHeight:440px;center:yes;help:no;resizable:yes;status:no;');
  nNewEntityId = (oEntity == null ? null : oEntity.m_nId);
  nOldEntityId = IsBlank(document.getElementById(sElemId).value, null);
  if (oEntity != null)
  {
    document.getElementById(sElemId).value = oEntity.m_nId;
    document.getElementById(sElemId + '_alt').value = oEntity.m_sName;
    document.getElementById(sElemId + '_user').value = oEntity.m_sName;
    document.getElementById(sElemId + '_user').style.color = '';
    
    if (nNewEntityId != nOldEntityId)
    {
      sOnLoadedFunc = IsBlank(document.getElementById(sElemId).onloaded, null);
      if (sOnLoadedFunc != null) 
      {
        eval(String(sOnLoadedFunc) + '(sElemId, oEntity);');
      }
    }
  }
  document.getElementById(sElemId).setAttribute('waitingForServerResponse', '');
}

function OnListEntityChange(nEntityType, sElemId)
{
  var
    sValueStr, sValueUser, sFilter, nMinSearchRank, sUrl, oEntity;
    
  sValueStr = document.getElementById(sElemId + '_alt').value;
  sValueUser = document.getElementById(sElemId + '_user').value;
  sFilter = IsBlank(document.getElementById(sElemId).filter, '');
  nMinSearchRank = IsBlank(document.getElementById(sElemId).getAttribute('minsearchrank'), '');
  if (IsBlank(sValueUser, null) == null)
  {
    OnEntityLoaded(sElemId, sValueUser, null);
  }
  else
  {
    if (sValueStr != sValueUser)
    {
      document.getElementById(sElemId).setAttribute('waitingForServerResponse', (new Date()).getTime());
      CreateIFrame().src = '../entity/load-entity.asp?f=' + escape(sElemId) + '&s=' + escape(sValueUser) + '&et=' + escape(nEntityType) + '&flt=' + escape(sFilter) + '&msr=' + escape(nMinSearchRank);
    }
  }
}

function OnEntityLoaded(sElemId, sElemValue, oEntity)
{
  var
    nNewEntityId, nOldEntityId, sEntityName, sOnLoadedFunc;
    
  document.getElementById(sElemId).setAttribute('waitingForServerResponse', '');
  nNewEntityId = (oEntity == null ? null : oEntity.m_nId);
  nOldEntityId = IsBlank(document.getElementById(sElemId).value, null);
  if (document.getElementById(sElemId + '_user').value == sElemValue)
  {
    if (oEntity == null)
    {
      document.getElementById(sElemId).value = '';
      document.getElementById(sElemId + '_alt').value = '';
      document.getElementById(sElemId + '_user').style.color = '#FF4040';
    }
    else
    {
      sEntityName = (oEntity.m_nDefaultAttributeIndex == null ? '<entity>' : oEntity.m_aoAttributes[oEntity.m_nDefaultAttributeIndex].m_sValueDefault);
      document.getElementById(sElemId).value = oEntity.m_nId;
      document.getElementById(sElemId + '_alt').value = sEntityName;
      document.getElementById(sElemId + '_user').value = sEntityName;
      document.getElementById(sElemId + '_user').style.color = '';
    }
  }
  if (nNewEntityId != nOldEntityId && nNewEntityId != null)
  {
    sOnLoadedFunc = IsBlank(document.getElementById(sElemId).onloaded, null);
    if (sOnLoadedFunc != null) 
    {
      eval(String(sOnLoadedFunc) + '(sElemId, oEntity);');
    }
  }
}

function OnEntityAttrLoaded(sElemId, nEntityId, sEntityName)
{
  var
    oElem;

  oElem = document.getElementById(sElemId);
  if (oElem != null && nEntityId != null)
  {
    oElem.value = nEntityId;
    document.getElementById(sElemId + '_alt').value = sEntityName;
    document.getElementById(sElemId + '_user').value = sEntityName;
    document.getElementById(sElemId + '_user').style.color = '';
  }
}

function OnListEntityKeyPress(oElem)
{
  oElem.style.color = '';
}

function OnListEntityKeyDown(e)
{
  var
    nKeyCode, oElem;
    
  nKeyCode = (window.event ? window.event.keyCode : e.which);
  oElem = (window.event ? window.event.srcElement : e.target);
  if (nKeyCode == 8 || nKeyCode == 46)
  {
    OnListEntityKeyPress(oElem);
  }
}

function OnEntityChange(sElemId, nNewEntityId)
{
  var
    sChildFormIds, anChildFormIds, nIndex, nFormIndex;

  sChildFormIds = IsBlank(document.getElementById(sElemId).childformids, '');
  if (sChildFormIds != '')
  {
    anChildFormIds = sChildFormIds.split(',');
    if (anChildFormIds.length > 0)
    {
      for (nIndex = 0; nIndex < anChildFormIds.length; nIndex++)
      {
        ClearFormValues(anChildFormIds[nIndex]);
      }
      //Request new data for each form
      if (IsBlank(nNewEntityId, null) != null)
      {
        for (nFormIndex = 0; nFormIndex < anChildFormIds.length; nFormIndex++)
        {
          CreateIFrame().src = '../entity/load-form.asp?f=' + escape(anChildFormIds[nFormIndex]) + '&e=' + escape(nNewEntityId);
        }
      }
    }
  }
}

function OnChildEntityListLoaded(sElemId, nAttributeId, aoListItem)
{
  var
    oElem, nIndex;

  oElem = document.getElementById(sElemId);
  oElem.length = aoListItem.length + 1;
  oElem.options[0].text = "";
  oElem.options[0].value = "";
  for (nIndex = 0; nIndex < aoListItem.length; nIndex++)
  {
    oElem.options[nIndex + 1].value = aoListItem[nIndex].m_nEntityId;
    oElem.options[nIndex + 1].text = aoListItem[nIndex].m_sEntityName;
  }
  oElem.selectedIndex = 0;
  OnEntityChange(oElem.name, null);
}

function ClearFormValues(nFormId)
{
  var
    aoElem, nIndex, oElem, sElemTagName;

  nFormId = DebraceUniqueIdentifier(nFormId);
  aoElem = (document.all ? document.all : document.body.getElementsByTagName('*'));
  for (nIndex = 0; nIndex < aoElem.length; nIndex++)
  {
    oElem = aoElem[nIndex];
    if (String(oElem.id).indexOf('_caption') == -1)
    {
      sElemTagName = String(oElem.tagName).toUpperCase();
      switch (sElemTagName)
      {
      case 'DIV':
        if (String(oElem.id).substring(0, nFormId.length + 2) == 'f' + nFormId + '_')
        {
          oElem.innerText = '';
        }
        break;
      case 'INPUT':
        if (String(oElem.name).substring(0, nFormId.length + 2) == 'f' + nFormId + '_')
        {
          oElem.value = '';
        }
        break;
      case 'TEXTAREA':
        if (String(oElem.name).substring(0, nFormId.length + 2) == 'f' + nFormId + '_')
        {
          oElem.innerText = '';
        }
      case 'SELECT':
        if (String(oElem.name).substring(0, nFormId.length + 2) == 'f' + nFormId + '_')
        {
          oElem.selectedIndex = 0;
        }
        break;
      }
    }
  }
}

function OnFormLoaded(oForm, oEntity)
{
  oElemEntityId = document.getElementById('f' + DebraceUniqueIdentifier(oForm.m_nId) + '_EntityId');
  if (oElemEntityId != null)
  {
    oElemEntityId.value = (oEntity == null ? '' : oEntity.m_nId);
  }
  for (nFormFieldIndex = 0; nFormFieldIndex < oForm.m_aoFields.length; nFormFieldIndex++)
  {
    oFormField = oForm.m_aoFields[nFormFieldIndex];
    nAttributeIndex = GetEntityAttributeIndex(oEntity, oFormField.m_oAttribute.m_nId);
    xValue = (nAttributeIndex == null ? null : oEntity.m_aoAttributes[nAttributeIndex].m_xValue);
    xValueStr = (nAttributeIndex == null ? null : oEntity.m_aoAttributes[nAttributeIndex].m_sValueDefault);
    sElemId = 'f' + DebraceUniqueIdentifier(oForm.m_nId) + '_ff' + DebraceUniqueIdentifier(oFormField.m_nId);
    if (oForm.m_bReadOnly)
    {
      oElem = document.getElementById(sElemId);
      if (oElem != null)
      {
        oElem.innerText = IsNull(xValueStr, '');
      }
    }
    else
    {
      switch (oFormField.m_oAttribute.m_sType)
      {
      case 'Str':
        oElem = document.getElementById(sElemId);
        if (oElem != null)
        {
          oElem.value = IsNull(xValue, '');
        }
        break;
      case 'Text':
        oElem = document.getElementById(sElemId);
        if (oElem != null)
        {
          oElem.innerText = IsNull(xValue, '');
        }
        break;
      case 'LookupList':
        oElem = document.getElementById(sElemId);
        if (oElem != null)
        {
          oElem.value = IsNull(xValue, '');
        }
        break;
      }
    }
  }
}

function OnValidate(oSrcElem, oFormElem)
{
  var
   sSrcElementId, aoFormElems, nIndex, sWaiting, oDefaultElemId, oFieldElem, sAttributeName, 
   oElem, sValidateFunction, sValidateFunction2, bSuccess;

  //Check to see if any entity boxes are waiting for a server response. If so wait up to 2 seconds
  sSrcElementId = oSrcElem.id;
  if (IsBlank(sSrcElementId, '') != '' && document.getElementById(sSrcElementId) == oSrcElem)
  {
    aoFormElems = (oFormElem.elements ? oFormElem.elements : oFormElem.all);
    for (nIndex = 0; nIndex < aoFormElems.length; nIndex++)
    {
      sWaiting = IsBlank(aoFormElems[nIndex].getAttribute('waitingForServerResponse'), '');
      if (sWaiting != '')
      {
        //if waiting less than 2 seconds then keep waiting
        if (Number(sWaiting) + 2000 > (new Date()).getTime())
        {
          window.setTimeout('document.getElementById("' + sSrcElementId + '").click();', 250);
          return;
        }
      }
    }
  }

  //Check if there is a field for the default attribute, if so ensure it has value
  oDefaultElemId = document.getElementById('DefaultElemId');
  if (oDefaultElemId != null)
  {
    oFieldElem = document.getElementById(oDefaultElemId.value);
    if (oFieldElem != null && 
        oFieldElem.tagName != 'DIV' &&
        (oFieldElem.tagName != 'INPUT' || oFieldElem.type != 'hidden'))
    {
      if (Trim(IsBlank(oFieldElem.value, '')) == '')
      {
        sAttributeName = document.getElementById(oDefaultElemId.value + '_caption').innerHTML;
        if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
        {
          sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
        }
        alert('Please specify the "' + sAttributeName + '".');
        try
        {
          oFieldElem.focus();
        }
        catch (e)
        {
        }
        return false;
      }
    }
  }
  aoFormElems = (oFormElem.elements ? oFormElem.elements : oFormElem.all);
  for (nIndex = 0; nIndex < aoFormElems.length; nIndex++)
  {
    oElem = aoFormElems[nIndex];
    sValidateFunction = (oElem.getAttribute ? oElem.getAttribute('validatefunction') : null);
    if (IsBlank(sValidateFunction, null) != null)
    {
      bSuccess = eval(String(sValidateFunction) + '(\'' + aoFormElems[nIndex].name + '\');');
      if (!bSuccess)
      {
        return false;
      }
    }
    sValidateFunction2 = aoFormElems[nIndex].getAttribute('validatefunction2');
    if (IsBlank(sValidateFunction2, null) != null)
    {
      bSuccess = eval(String(sValidateFunction2) + '(\'' + aoFormElems[nIndex].name + '\');');
      if (!bSuccess)
      {
        return false;
      }
    }
  }   
  return true;
}

function OnValidateEntity(sElemId)
{
  var
    oElem, oElemUser, sAttributeName;
    
  oElem = document.getElementById(sElemId);
  oElemUser = document.getElementById(sElemId + '_user');
  if (IsBlank(oElem.value, null) == null && IsBlank(oElemUser.value, null) != null)
  {
    document.getElementById(sElemId + '_button').click();
    return false;
  }
  if (oElem.required == 1 && IsBlank(oElem.value, null) == null)
  {
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    alert('Please specify the "' + sAttributeName + '".');
    oElemUser.focus();
    return false;
  }
  return true;
}

function OnValidateStr(sElemId)
{
  var
    oElem, sAttributeName;
    
  oElem = document.getElementById(sElemId);
  if (Trim(IsBlank(oElem.value, '')) == '')
  {
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    alert('Please specify the "' + sAttributeName + '".');
    oElem.focus();
    return false;
  }
  return true;
}

function OnValidateFName(sElemId)
{
  var
    oElem, sValue, sAttributeName;
    
  oElem = document.getElementById(sElemId);
  sValue = Trim(IsBlank(oElem.value, ''));
  if (sValue == '' || sValue != GetValidFileName(sValue))
  {
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    alert('Please specify a valid file name (without the path) in the "' + sAttributeName + '" box.\r\n\r\n' +
          'NB: a file name cannot contain any of these characters  \\  /  :  *  ?  "  <  >  |');
    oElem.focus();
    return false;
  }
  return true;
  
}

function OnValidateInt(sElemId, bRequired)
{
  var
    oElem, sInt, sAttributeName, sMsgSuffix, nInt, nMinValue, nMaxValue, sMsg;
    
  oElem = document.getElementById(sElemId);
  sInt = IsBlank(oElem.value, '');
  if (sInt != '' || bRequired == true)
  {
    sInt = sInt.replace(/,/g, '');
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    sMsgSuffix = (bRequired == true ? '' : ', or clear the field');
    if (IsNumber(sInt))
    {
      nInt = Number(sInt);
      nMinValue = IsBlank(oElem.getAttribute('minvalue'), null);
      nMaxValue = IsBlank(oElem.getAttribute('maxvalue'), null);
      if ((nMinValue != null && nInt < nMinValue) || 
          (nMaxValue != null && nInt > nMaxValue))
      {
        sMsg = (nMinValue != null && nMaxValue != null ?
                'Please type a number between ' + nMinValue + ' and ' + nMaxValue + ' in the "' + sAttributeName + '" box' + sMsgSuffix + '.' :
                (nMinValue != null ? 'Please type a number greater than ' + nMinValue + ' in the "' + sAttributeName + '" box' + sMsgSuffix + '.' :
                                     'Please type a number less than ' + nMaxValue + ' in the "' + sAttributeName + '" box' + sMsgSuffix + '.'));
        alert(sMsg);
        oElem.focus();
        return false;                                     
      }
    }
    else
    {
      alert('Please type a number in the "' + sAttributeName + '" box' + sMsgSuffix + '.');
      oElem.focus();
      return false;
    }
  }
  return true;
}

function OnValidateIntRequired(sElemId)
{
  return OnValidateInt(sElemId, true);
}

function OnValidateFloat(sElemId, bRequired)
{
  var
    oElem, sFloat, oCaption, sAttributeName, sMsgSuffix;
    
  oElem = document.getElementById(sElemId);
  sFloat = IsBlank(oElem.value, '');
  if (sFloat != '' || bRequired == true)
  {
    sFloat = sFloat.replace(/,/g, '');
    sSymbol = IsBlank(oElem.getAttribute('symbol'), null);
    sFloat = (sSymbol != null ? sFloat.replace(sSymbol, '') : sFloat);
    if (!IsNumber(sFloat))
    {
      oCaption = document.getElementById(sElemId + '_caption');
      sAttributeName = (oCaption == null ? 'numeric' : oCaption.innerHTML);
      if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
      {
        sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
      }
      sMsgSuffix = (bRequired == true ? '' : ', or clear the field');
      alert('Please type a number in the "' + sAttributeName + '" box' + sMsgSuffix + '.');
      oElem.focus();
      return false;
    }
  }
  return true;
}

function OnValidateFloatRequired(sElemId)
{
  return OnValidateFloat(sElemId, true);
}

function OnValidateDate(sElemId, bRequired)
{
  var
    oElem, sAttributeName, sMsgSuffix;
    
  oElem = document.getElementById(sElemId);
  if (StrToDate(oElem.value) == null &&
      (IsBlank(oElem.value, null) != null || bRequired == true))
  {
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    sMsgSuffix = (bRequired == true ? '' : ', or clear the field');
    alert('Please type a date in the "' + sAttributeName + '" box' + sMsgSuffix + '.');
    oElem.focus();
    return false;
  }
  return true;
}

function OnValidateDateRequired(sElemId)
{
  return OnValidateDate(sElemId, true);
}

function OnValidateTime(sElemId)
{
  var
    oElem, sAttributeName;
    
  oElem = document.getElementById(sElemId);
  if (IsBlank(oElem.value, null) != null && StrToTime(oElem.value) == null)
  {
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    alert('Please type a time in the "' + sAttributeName + '" box, or clear the field.');
    oElem.focus();
    return false;
  }
  return true;
}

function OnValidateEmail(sElemId)
{
  var
    oElem, sAttributeName;
    
  oElem = document.getElementById(sElemId);
  if (IsBlank(oElem.value, null) != null && !(new RegExp(/^.+@[^\.].*\.[a-z]{2,}$/i).test(oElem.value)))
  {
    sAttributeName = document.getElementById(sElemId + '_caption').innerHTML;
    if (sAttributeName.lastIndexOf(':') == sAttributeName.length - 1)
    {
      sAttributeName = sAttributeName.substr(0, sAttributeName.length - 1);
    }
    alert('Please type a valid email address in the "' + sAttributeName + '" box, or clear the field.');
    oElem.focus();
    return false;
  }
  return true;
}

function OnValidateImageList(sElemId)
{
  var
    oLimboOpsElem, asOps, oShell, asTextarea, nIndex, oTextarea, sFileName;
   
  oLimboOpsElem = document.getElementById(sElemId + '_limboops');
  if (oLimboOpsElem != null)
  {
    asOps = new Array();
    oShell = document.getElementById(sElemId + '_shell');
    aoTextarea = oShell.getElementsByTagName('textarea');
    for (nIndex = 0; nIndex < aoTextarea.length; nIndex++)
    {
      oTextarea = aoTextarea[nIndex];
      if (oTextarea.id == 'EntityFileName')
      {
        sFileName = String(oTextarea.value).replace(/\r\n/g, ' ').replace(/\r/g, ' ').replace(/\n/g, ' ');
        if (sFileName != oTextarea.getAttribute('originalvalue'))
        {
          asOps.push('op=ren\te=' + oTextarea.parentNode.parentNode.getAttribute('entityid') + '\tfilename=' + sFileName);
        }
      }
    }
    oLimboOpsElem.value = asOps.join('\r\n');
  }
  return true;
}

function OnFormResize()
{
  var
    oContainer, nFormX, nVisibleClientWidth, nScale, aoElem, nIndex, oElem, nDefX, nX, nDefDX, nDX;

  oContainer = document.getElementById('FormContainer');
  if (oContainer != null && typeof(g_nFormDX) != 'undefined')
  {
    nFormX = oContainer.offsetLeft;
    nVisibleClientWidth = document.body.offsetWidth - 64;
    
    if (nVisibleClientWidth < nFormX + g_nFormDX)
    {
      nVisibleClientWidth = nFormX + g_nFormDX;
    }
    if (g_nFormMaxDX != null && nVisibleClientWidth > nFormX + g_nFormMaxDX)
    {
      nVisibleClientWidth = nFormX + g_nFormMaxDX;
    }
    nScale = (nVisibleClientWidth - nFormX) / g_nFormDX;
    aoElem = (document.all ? document.all : document.body.getElementsByTagName('*'));
    for (nIndex = 0; nIndex < aoElem.length; nIndex++)
    {
      oElem = aoElem[nIndex];
      nDefX = IsBlank(oElem.getAttribute('defx'), null);
      if (nDefX != null)
      {
        nX = Math.round(Number(nDefX) * nScale);
        oElem.style.left = nX + 'px';
        nDefDX = IsBlank(oElem.getAttribute('defdx'), null);
        if (nDefDX != null)
        {
          nDX = Math.round(nDefDX * nScale);
          if (nDX > 0)
          {
            oElem.style.width = nDX + 'px';
          }
        }
      }
    }
  }
}

function GetOutlook()
{
  try
  {
    return (new ActiveXObject("Outlook.Application"));
  }
  catch (e)
  {
    alert('This feature requires Microsoft Outlook.\r\n\r\nYou computer either doesn\'t have Microsoft Outlook installed, or your browser security settings are preventing it from being used.\r\n\r\nSystem message: "' + e.message + '"');
    return null;
  }
}

function GetClientFileObjFileLauncherX()
{
  try
  {  
    return (new ActiveXObject('ClientFileObj.FileLauncherX'));
  }
  catch (e)
  {
    ClientFileObjRequired(false);
    return null;
  }
}

function ClientFileObjRequired(bOnlyUpdateRequired)
{
  var
    sMsg;
    
  if (bOnlyUpdateRequired)
  {
    sMsg = 'The component that processes this request needs to be updated before continuing.\r\n\r\n' +
           'Would you like to update this component now?'
  }
  else
  {
    sMsg = 'Your computer doesn\'t have the necessary component installed to open this file (or security settings allowing its use).\r\n\r\n' +
           'Would you like to install this component now?'
  }
  if (confirm(sMsg))
  {
    document.location = '../desktop/support.asp?sm=1';
  }
}

function OnAttachmentClick(sUrl, nAttachmentEntityId, sName, nSize, sInfo)
{
  var
    oLauncher;
  
  oLauncher = GetClientFileObjFileLauncherX();
  if (oLauncher != null)
  {  
    if (IsBlank(sInfo, '') == '')
    {
      oLauncher.Open(sUrl, nAttachmentEntityId, sName, nSize);
    }
    else
    {
      oLauncher.OpenWithInfo(sUrl, nAttachmentEntityId, sName, nSize, sInfo);
    }
  }
}

function OnToolbarMouseUp(sEditorId)
{
  document.getElementById(sEditorId).focus();
}

function OnToolbarClick(sEditorId, sOp)
{
  document.getElementById(sEditorId).focus();
  document.execCommand(sOp, false, null);
  return false;
}

function OnToolbarMouseOver(oTb)
{
  oTb.className = 'toolbar-hover';
}

function OnToolbarMouseOut(oTb)
{
  oTb.className = 'toolbar';
}

function OnToolbarMouseDown(oTb)
{
  oTb.className = 'toolbar-down';
}

function OnToolbarMouseUp(oTb)
{
  oTb.className = 'toolbar-hover';
}

function OnInsertLinkToEntity(nEntityType, sElemId)
{
  var
    sUrl, oEntity;

  document.getElementById(sElemId).focus();
  sUrl = '../entity/pe-list-entity-frame.asp?et=' + escape(nEntityType) + '&s=&flt=&msr=100';
  oEntity = window.showModalDialog(sUrl,document,'dialogWidth:520px;dialogHeight:440px;center:yes;help:no;resizable:yes;status:no;');
  if (oEntity != null)
  {
    CreateLinkToEntity(oEntity, sElemId);
  }
}

function CreateLinkToEntity(oEntity, sElemId)
{
  var 
    oEditor, oControlRange, oImg, oA, sInnerText, sOuterHtml;

  oEditor = document.getElementById(sElemId);
  if (oEntity != null && oEditor != null)
  {
    if (window.getSelection)
    {
      sInnerText = window.getSelection().toString();
      if (IsBlank(sInnerText, null) == null)
      {
        sInnerText = oEntity.m_sName;
        if (IsBlank(sInnerText, null) == null)
        {
          sInnerText = 'link';
        }
      }
      sOuterHtml = '<a href="../entity/?e=' + escape(oEntity.m_nId) + '">' + ForDisplay(sInnerText) + '</a>';
      InsertHtml(sOuterHtml);
    }
    else
    if (document.selection.type.toLowerCase() == 'control')
    {
      oControlRange = document.selection.createRange();
      if (oControlRange.length == 1 && oControlRange(0).tagName == 'IMG')
      {
        oImg = oControlRange(0);
        oA = document.createElement('A');
        oA.href = '../entity/?e=' + escape(oEntity.m_nId);
        oImg.insertAdjacentElement('beforeBegin', oA);
        oA.insertAdjacentElement('afterBegin', oImg);
        if (oA.parentElement.tagName == 'A')
        {
          oA.parentElement.removeNode(false);
        }
      }
    }
    else
    {
      sInnerText = document.selection.createRange().text;
      if (IsBlank(sInnerText, null) == null)
      {
        sInnerText = oEntity.m_sName;
        if (IsBlank(sInnerText, null) == null)
        {
          sInnerText = 'link';
        }
      }
      sOuterHtml = '<a href="../entity/?e=' + escape(oEntity.m_nId) + '">' + ForDisplay(sInnerText) + '</a>';
      InsertHtml(sOuterHtml);
    }
  }
}

function InsertHtml(sHtml)
{
  //Hidden inputs are used in a two step process to insert arbitrary html at the caret position.  
  //Undoing after a call to InsertHtml leaves a "<input type=hidden" element behind.
  //When in contentEditable mode, hidden inputs are visible, hence the class selector.
  var
    oControlRange, sElemId;
    
  if (document.selection && document.selection.type.toLowerCase() == 'control')
  {
    oControlRange = document.selection.createRange();
    oControlRange(0).insertAdjacentHTML('afterEnd', sHtml);
  }
  else
  {
    try
    {
      document.execCommand('InsertHTML', false, sHtml);
    }
    catch (e)
    {
      sElemId = 'idInsertHtml';
      RemoveElementsWithId(sElemId);
      document.execCommand('InsertInputHidden', false, sElemId);
      document.getElementById(sElemId).outerHTML = sHtml;
    }
  }
}

function RemoveElementsWithId(sElemId)
{
  var
    oElem;
    
  oElem = document.getElementById(sElemId);
  while (oElem != null)
  {
    oElem.removeNode(false);
    oElem = document.getElementById(sElemId);
  }
}

function OnCommitFormFieldHtml(sElemName)
{
  var
    oEditor, oValue, oValueAlt, sText, sHtml;
    
  RemoveElementsWithId('idInsertHtml');
  oEditor = document.getElementById(sElemName + '_editor');
  oValue = document.getElementById(sElemName);
  oValueAlt = document.getElementById(sElemName + '_alt');
  sText = HtmlToPlainText(oEditor.innerHTML);
  oValue.value = sText;
  sHtml = oEditor.innerHTML;
  sHtml = sHtml.replace(/href="http.+?\/entity\/\?e=/gi, 'href="../entity/?e=');
  oValueAlt.value = sHtml;
  return true;
}

function OnEntityListAddClick(nEntityType, sElemId)
{
  var
    oElem, oSubHtml, oRemoveA, nMinSearchRank, sUrl, oEntity,
    oTable, oTrHeader, oTrNew, nRowIndex, sClassName, oTd,
    nColIndex, sValue, nAttributeId;

  oElem = document.getElementById(sElemId + '_shell');
  oSubHtml = document.getElementById(sElemId + '_subhtml');
  oRemoveA = oSubHtml.getElementsByTagName('A')[0];
  nMinSearchRank = IsBlank(oElem.getAttribute('minsearchrank'), '');
  sUrl = '../entity/pe-list-entity-frame.asp?et=' + escape(nEntityType) + '&s=&msr=' + escape(nMinSearchRank) + '&load=1';
  oEntity = window.showModalDialog(sUrl,document,'dialogWidth:520px;dialogHeight:440px;center:yes;help:no;resizable:yes;status:no;');
  if (oEntity != null)
  {
    oTable = oElem.getElementsByTagName('TABLE')[0];
    oTrHeader = oTable.getElementsByTagName('TR')[0];
    oTrNew = null;
    for (nRowIndex = 2; nRowIndex < oTable.rows.length; nRowIndex++)
    {
      if (oTable.rows[nRowIndex].getAttribute('entityid') == oEntity.m_nId)
      {
        oTrNew = oTable.rows[nRowIndex];
      }
    }
    if (oTrNew == null)
    {
      oTrNew = oTable.insertRow(-1);
      oTrNew.setAttribute('entityid', oEntity.m_nId);
    }
    else
    {
      while(oTrNew.cells.length > 0)
      {
        oTrNew.deleteCell();
      }
    }
    sClassName = (oTrNew.rowIndex == 2 ? '' : 'bt-light');
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    oTd.style.padding = 0;
    oTd.innerHTML = '<input type=checkbox onclick="OnEntityListRowSelect(\'' + sElemId + '\');" tabindex=' + oRemoveA.tabIndex + '>';
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    oTd.style.padding = 1;
    oTd.style.paddingBottom = 0;
    oTd.innerHTML = '<img src="../imgs/entitytype-' + oEntity.m_nTypeId + '.gif" width=14 height=16>';
    for (nColIndex = 2; nColIndex < oTrHeader.cells.length; nColIndex++)
    {
      oTd = oTrNew.insertCell(-1);
      oTd.className = sClassName;
      sValue = '';
      nAttributeId = oTrHeader.cells[nColIndex].attributeid;
      for (nAttrIndex = 0; nAttrIndex < oEntity.m_aoAttributes.length; nAttrIndex++)
      {
        if (oEntity.m_aoAttributes[nAttrIndex].m_nId == nAttributeId)
        {
          sValue = oEntity.m_aoAttributes[nAttrIndex].m_sValueDefault;
          break;
        }
      }
      oTd.innerHTML = ForDisplayF(sValue);
    }
    oTable.rows[1].style.display = 'none';
    OnEntityListChanged(sElemId, oEntity.m_nId, 'added');
  }
  window.event.srcElement.blur();
}

function OnEntityListRowSelect(sElemId)
{
  var
    oSubHtml, oRemoveA, oRemoveSpan, oElem, oTable, bChecked, nRowIndex, oCb;
    
  oSubHtml = document.getElementById(sElemId + '_subhtml');
  oRemoveA = oSubHtml.getElementsByTagName('A')[0];
  oRemoveSpan = oSubHtml.getElementsByTagName('SPAN')[0];
  oElem = document.getElementById(sElemId + '_shell');
  oTable = oElem.getElementsByTagName('TABLE')[0];
  bChecked = false;
  for (nRowIndex = oTable.rows.length - 1; nRowIndex >= 2; nRowIndex--)
  {
    oCb = oTable.rows[nRowIndex].getElementsByTagName('INPUT')[0];
    if (oCb != null && oCb.checked)
    {
      bChecked = true;
      break;
    }
  }
  oRemoveSpan.style.display = (bChecked ? 'none' : '');
  oRemoveA.style.display = (bChecked ? '' : 'none');
  if (window.event)
  {
    window.event.srcElement.blur();
  }
}

function OnEntityListEditClick(nEntityType, sElemId, nPopupWidth, nPopupHeight, bShowColumnHeader)
{
  var
    oElem, sUrl, aoEntity, oTable, aoCol, bCheckBoxes, bShowIcons, nIndex, nEntityId, oEntity, oTrNew, sClassName, oTd;

  oElem = document.getElementById(sElemId + '_shell');
  sUrl = '../entity/pe-list-entity-grid.asp?et=' + escape(nEntityType) + '&f=' + escape(sElemId);
  aoEntity = window.showModalDialog(sUrl,document,'dialogWidth:' + nPopupWidth + 'px;dialogHeight:' + nPopupHeight + 'px;center:yes;help:no;resizable:yes;status:no;');
  if (aoEntity != null)
  {
    oTable = oElem.getElementsByTagName('TABLE')[0];
    aoCol = oTable.getElementsByTagName('COL');
    bCheckBoxes = aoCol[0].getAttribute('checkboxes') == 1;
    bShowIcons = aoCol[1].getAttribute('showicons') == 1;
    for (nIndex = oTable.rows.length - 1; nIndex >= 2; nIndex--)
    {
      nEntityId = oTable.rows[nIndex].getAttribute('entityid');
      oTable.deleteRow(nIndex);
      OnEntityListChanged(sElemId, nEntityId, 'removed');
    }
    for (nIndex = 0; nIndex < aoEntity.length; nIndex++)
    {
      oEntity = aoEntity[nIndex];
      oTrNew = oTable.insertRow(-1);
      oTrNew.setAttribute('entityid', oEntity.m_nId);
      sClassName = (oTrNew.rowIndex == 2 || !bShowColumnHeader ? '' : 'bt-light');
      oTd = oTrNew.insertCell(-1);
      oTd.style.display = (bCheckBoxes ? '' : 'none');
      oTd = oTrNew.insertCell(-1);
      oTd.style.display = (bShowIcons ? '' : 'none');
      oTd.className = sClassName;
      oTd.style.padding = 1;
      oTd.style.paddingBottom = 0;
      oTd.innerHTML = '<img src="../imgs/entitytype-' + oEntity.m_nTypeId + '.gif" width=14 height=16>';
      oTd = oTrNew.insertCell(-1);
      oTd.className = sClassName;
      oTd.style.width = '100%';
      oTd.innerHTML = ForDisplayF(oEntity.m_sName);
      oTable.rows[1].style.display = 'none';
      OnEntityListChanged(sElemId, oEntity.m_nId, 'added');
    }
    if (oTable.rows.length == 2)
    {
      oTable.rows[1].style.display = '';
    }
  }
  if (window.event)
  {
    window.event.srcElement.blur();
  }
}

function OnEntityListRemoveClick(sElemId)
{
  var
    oElem, oTable, nRowIndex, oCb, oTr, nColIndex;
    
  oElem = document.getElementById(sElemId + '_shell');
  oTable = oElem.getElementsByTagName('TABLE')[0];
  for (nRowIndex = oTable.rows.length - 1; nRowIndex >= 2; nRowIndex--)
  {
    nEntityId = oTable.rows[nRowIndex].getAttribute('entityid');
    oCb = oTable.rows[nRowIndex].getElementsByTagName('INPUT')[0];
    if (oCb != null && oCb.checked)
    {
      oTable.deleteRow(nRowIndex);
      OnEntityListChanged(sElemId, nEntityId, 'removed');
    }
  }
  if (oTable.rows.length == 2)
  {
    oTable.rows[1].style.display = '';
    OnEntityListRowSelect(sElemId);
  }
  else
  {
    oTr = oTable.rows[2];
    for (nColIndex = 0; nColIndex < oTr.cells.length; nColIndex++)
    {
      oTr.cells[nColIndex].className = '';
    }
  }
  window.event.srcElement.blur();
}

function OnEntityListChanged(sElemId, nEntityId, sOp)
{
  var
    sSeparator, oElem, sElemValue;

  sSeparator = String.fromCharCode(14);
  if (IsUniqueIdentifier(nEntityId))
  {     
    oElem = document.getElementById(sElemId);
    sElemValue = String(IsBlank(oElem.value, ''));
    if (sOp == 'added')
    {
      if (sElemValue.indexOf(nEntityId) == -1)
      {
        if (sElemValue != '')
        {
          sElemValue += String.fromCharCode(14);
        }
        sElemValue += nEntityId;
        oElem.value = sElemValue;
      }
    }
    else
    if (sOp == 'removed')
    {
      sElemValue = sElemValue.replace(nEntityId, '');
      sElemValue = sElemValue.replace(sSeparator + sSeparator, sSeparator);
      if (sElemValue.substr(0, 1) == sSeparator)
      {
        sElemValue = sElemValue.substr(1);
      }
      if (sElemValue.lastIndexOf(sSeparator) == sElemValue.length - 1)
      {
        sElemValue = sElemValue.substr(0, sElemValue.length - 1);
      }
      oElem.value = sElemValue;
    }
  }
}

function OnImageListAddClick(nEntityType, nLimboEntityId, nUploadSize, nListSize, sElemId, nAttributeId)
{
  var
    oElem, oSubHtml, oRemoveA, nMinSearchRank, sUrl, oEntity,
    oTable, oTrHeader, oTrNew, nRowIndex, sClassName, oTd,
    nColIndex, sValue, nAttributeId;

  oElem = document.getElementById(sElemId + '_shell');
  oSubHtml = document.getElementById(sElemId + '_subhtml');
  oRemoveA = oSubHtml.getElementsByTagName('A')[0];
  sUrl = '../entity/pe-image-frame.asp?et=' + escape(nEntityType) + '&size=' + nUploadSize + '&pe=' + escape(nLimboEntityId) + '&postarg=' + escape('a=' + nAttributeId);
  oEntity = window.showModalDialog(sUrl,document,'dialogWidth:580px;dialogHeight:490px;center:yes;help:no;resizable:yes;status:no;');
  if (oEntity != null && IsUniqueIdentifier(oEntity.m_nLimboAttachmentId))
  {
    oTable = oElem.getElementsByTagName('TABLE')[0];
    oTrHeader = oTable.getElementsByTagName('TR')[0];
    oTrNew = oTable.insertRow(-1);
    oTrNew.setAttribute('entityid', oEntity.m_nLimboAttachmentId);
    sClassName = (oTrNew.rowIndex == 2 ? '' : 'bt-light');
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    oTd.style.padding = 0;
    oTd.innerHTML = '<input type=checkbox onclick="OnImageListRowSelect(\'' + sElemId + '\');" tabindex=' + oRemoveA.tabindex + '>';
    for (nColIndex = 1; nColIndex < oTrHeader.cells.length; nColIndex++)
    {
      oTd = oTrNew.insertCell(-1);
      oTd.className = sClassName;
      sValue = '&nbsp;';
      nAttributeId = oTrHeader.cells[nColIndex].getAttribute('attributeid');
      if (nAttributeId == oEntity.m_nFileAttributeId)
      {
        sValue = '<div style="width: ' + nListSize + 'px; overflow: hidden;"><img src="../entity/pe-image-src.asp?la=' + escape(oEntity.m_nLimboAttachmentId) + '"></div>';
      }
      else
      {
        for (nAttrIndex = 0; nAttrIndex < oEntity.m_aoAttributes.length; nAttrIndex++)
        {
          if (oEntity.m_aoAttributes[nAttrIndex].m_nId == nAttributeId)
          {
            if (oTrHeader.cells[nColIndex].getAttribute('filenameattribute') == 1)
            {
              sValue = '<textarea id="EntityFileName" originalvalue="' + ForInput(IsBlank(oEntity.m_aoAttributes[nAttrIndex].m_sValueDefault, '')) + '" style="width: 100%; height: 71px;">' + ForDisplay(oEntity.m_aoAttributes[nAttrIndex].m_sValueDefault) + '</textarea>';
              oTd.style.padding = '20px';
            }
            else
            {
              sValue = ForDisplayF(oEntity.m_aoAttributes[nAttrIndex].m_sValueDefault);
            }
            break;
          }
        }
      }
      oTd.innerHTML = sValue;
    }
    oTable.rows[1].style.display = 'none';
  }
  if (window.event)
  {
    window.event.srcElement.blur();
  }
}

function OnImageListAddWithThumbClick(nEntityType, nLimboEntityId, sElemId, nAttributeId)
{
  var
    oElem, oSubHtml, oRemoveA, sUrl, oEntity,
    oTable, oTrHeader, oTrNew, sClassName, oTd,
    sValue, nAttributeId;

  oElem = document.getElementById(sElemId + '_shell');
  oSubHtml = document.getElementById(sElemId + '_subhtml');
  oRemoveA = oSubHtml.getElementsByTagName('A')[0];
  sUrl = '../entity/pe-image-add.asp?et=' + escape(nEntityType) + '&pe=' + escape(nLimboEntityId) + '&a=' + escape(nAttributeId);
  oEntity = window.showModalDialog(sUrl,document,'dialogWidth:640px;dialogHeight:270px;center:yes;help:no;resizable:yes;status:no;');
  if (oEntity != null && IsUniqueIdentifier(oEntity.m_nLimboAttachmentId))
  {
    oTable = oElem.getElementsByTagName('TABLE')[0];
    oTrHeader = oTable.getElementsByTagName('TR')[0];
    oTrNew = oTable.insertRow(-1);
    oTrNew.setAttribute('entityid', oEntity.m_nLimboAttachmentId);
    sClassName = (oTrNew.rowIndex == 2 ? '' : 'bt-light');
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    oTd.style.padding = 0;
    oTd.innerHTML = '<input type=checkbox onclick="OnImageListRowSelect(\'' + sElemId + '\');" tabindex=' + oRemoveA.tabindex + '>';
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    sValue = '<img src="../entity/pe-image-src.asp?t=1&la=' + escape(oEntity.m_nLimboAttachmentId) + '">';
    oTd.innerHTML = sValue;
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    if (oTrHeader.cells[2].getAttribute('filenameattribute') == 1)
    {
      oTd.innerHTML = '<textarea id="EntityFileName" originalvalue="' + ForInput(IsBlank(oEntity.m_sCaption, '')) + '" style="width: 100%; height: 71px;">' + ForDisplay(oEntity.m_sCaption) + '</textarea>'
      oTd.style.paddingRight = '20px';
    }
    else
    {
      oTd.innerText = IsBlank(oEntity.m_sCaption, ' ');
    }
    oTable.rows[1].style.display = 'none';
  }
  if (window.event)
  {
    window.event.srcElement.blur();
  }
}

function OnImageListAddVideoClick(nEntityType, nLimboEntityId, sElemId, nAttributeId)
{
  var
    oElem, oSubHtml, oRemoveA, sUrl, oEntity,
    oTable, oTrHeader, oTrNew, sClassName, oTd,
    nAttributeId;

  oElem = document.getElementById(sElemId + '_shell');
  oSubHtml = document.getElementById(sElemId + '_subhtml');
  oRemoveA = oSubHtml.getElementsByTagName('A')[0];
  sUrl = '../entity/pe-image-video.asp?et=' + escape(nEntityType) + '&pe=' + escape(nLimboEntityId) + '&a=' + escape(nAttributeId);
  oEntity = window.showModalDialog(sUrl,document,'dialogWidth:600px;dialogHeight:540px;center:yes;help:no;resizable:yes;status:no;');
  if (oEntity != null && IsUniqueIdentifier(oEntity.m_nLimboAttachmentId))
  {
    oTable = oElem.getElementsByTagName('TABLE')[0];
    oTrHeader = oTable.getElementsByTagName('TR')[0];
    oTrNew = oTable.insertRow(-1);
    oTrNew.setAttribute('entityid', oEntity.m_nLimboAttachmentId);
    sClassName = (oTrNew.rowIndex == 2 ? '' : 'bt-light');
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    oTd.style.padding = 0;
    oTd.innerHTML = '<input type=checkbox onclick="OnImageListRowSelect(\'' + sElemId + '\');" tabindex=' + oRemoveA.tabindex + '>';
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    oTd.innerHTML = '<img src="../imgs/thumb-video.gif" width=120 height=90>';
    oTd = oTrNew.insertCell(-1);
    oTd.className = sClassName;
    if (oTrHeader.cells[2].getAttribute('filenameattribute') == 1)
    {
      oTd.innerHTML = '<textarea id="EntityFileName" originalvalue="' + ForInput(IsBlank(oEntity.m_sCaption, '')) + '" style="width: 100%; height: 71px;">' + ForDisplay(oEntity.m_sCaption) + '</textarea>'
      oTd.style.paddingRight = '20px';
    }
    else
    {
      oTd.innerText = IsBlank(oEntity.m_sCaption, ' ');
    }
    oTable.rows[1].style.display = 'none';
  }
  if (window.event)
  {
    window.event.srcElement.blur();
  }
}

function OnImageListRowSelect(sElemId)
{
  OnEntityListRowSelect(sElemId);
}

function OnImageListRemoveClick(nLimboEntityId, sElemId)
{
  var
    oElem, oTable, nRowIndex, oCb, oIFrame, oTr, nColIndex;
    
  oElem = document.getElementById(sElemId + '_shell');
  oTable = oElem.getElementsByTagName('TABLE')[0];
  for (nRowIndex = oTable.rows.length - 1; nRowIndex >= 2; nRowIndex--)
  {
    nEntityId = oTable.rows[nRowIndex].getAttribute('entityid');
    oCb = oTable.rows[nRowIndex].getElementsByTagName('INPUT')[0];
    if (oCb != null && oCb.checked)
    {
      oTable.deleteRow(nRowIndex);
      oIFrame = CreateIFrame();
      oIFrame.src = '../serverop/?op=del&e=' + escape(nEntityId) + '&pe=' + escape(nLimboEntityId);
    }
  }
  if (oTable.rows.length == 2)
  {
    oTable.rows[1].style.display = '';
  }
  else
  {
    oTr = oTable.rows[2];
    for (nColIndex = 0; nColIndex < oTr.cells.length; nColIndex++)
    {
      oTr.cells[nColIndex].className = '';
    }
  }
  OnImageListRowSelect(sElemId);
  if (window.event)
  {
    window.event.srcElement.blur();
  }
}

function ImageListThumbStrip_OnClick(oA)
{
  var
    oI, sSrc, oN, oV, oP, oC, aoT, nIndex;
    
  oI = oA.getElementsByTagName('img')[0];
  sSrc = String(oI.src).replace(/t=1/, 't=0');
  oN = document.getElementById('ImageListThumbStrip_Name');
  oV = document.getElementById('ImageListThumbStrip_Background');
  if (oV != null)
  {
    oV.style.backgroundImage = 'url(' + sSrc + ')';
  }
  else
  {
    oV = document.getElementById('ImageListThumbStrip_Container');
    if (oV != null)
    {
      bIsVideo = oI.getAttribute('video') == '1';
      if (bIsVideo)
      {
        oV.innerHTML = oI.getAttribute('embedhtml');
      }
      else
      {
        oV.innerHTML = '<img src="' + sSrc + '" galleryimg="no">';
      }
    }
  }
  if (oV != null)
  {
    oP = oA.parentNode;
    oC = document.getElementById('ImageListThumbStrip_ThumbContainer');
    aoT = oC.getElementsByTagName('table');
    for (nIndex = 0; nIndex < aoT.length; nIndex++)
    {
      oTd = aoT[nIndex].rows[0].cells[0];
      oTd.className = (oTd == oP ? 'bltrb bg-light ac' : 'bltrb-light bg-light-ex ac');
    }
    if (oN != null)
    {
      oN.innerHTML = IsBlank(oA.getAttribute('imagename'), '');
    }
    oA.blur();
    return false;
  }
  return true;
}

function OnAdvancedSearchClick(nFormId, nEntityTypeId)
{
  var
    sSearchAdv, oFormElem, aoFormElem, nIndex, oElem, nAttributeId, sValue, sUrl;
    
  sSearchAdv = '';
  oFormElem = document.forms[document.forms.length - 1];
  aoFormElem = (oFormElem.elements ? oFormElem.elements : oFormElem.all);
  for (nIndex = 0; nIndex < aoFormElem.length; nIndex++)
  {
    oElem = aoFormElem[nIndex];
    if (String(oElem.name).indexOf('f' + nFormId) == 0)
    {
      nAttributeId = IsBlank(oElem.getAttribute('attributeid'), null);
      sValue = IsBlank(oElem.value, null);
      if (nAttributeId != null && sValue != null)
      {
        if (sSearchAdv != '')
        {
          sSearchAdv += '\t\t';
        }
        sSearchAdv += nAttributeId + '\t' + sValue;
      }
    }
  }
  if (sSearchAdv != '')
  {
    sSearchAdv = 'et\t' + nEntityTypeId + '\t\t' + sSearchAdv;
    sUrl = document.getElementById('AdvSearchUrl').value + escape(sSearchAdv);
    document.location = sUrl;
  }
}

function OnAdvancedSearchToggle(oA, nEntityTypeId)
{
  var
    bShow, oForm;
    
  bShow = String(oA.innerHTML).indexOf('Show') >= 0;
  oA.innerHTML = (bShow ? 'Hide search options' : 'Show search options');
  oForm = document.getElementById('Filter' + nEntityTypeId);
  if (oForm != null)
  {
    oForm.style.display = (bShow ? '' : 'none');
  }
}

