var g_bExitedBooth = false;

function DisplayUserProfile(iInstanceID, iUserKey, iUserType, bDisableButtons)
{
  var iWidth = 600, iHeight = 600;
  var oWnd = GetTopWnd();
  if (oWnd)
    {
      iWidth = oWnd.g_iProfileDisplayWidth;
      iHeight = oWnd.g_iProfileDisplayHeight;
    }
  var cParams = GenerateCenteredWindowLeftTop(iWidth,iHeight)+"toolbar=no,scrollbars=yes,status=no,width=" + iWidth + ",height=" + iHeight + ",resizable=yes,menubar=no,location=no,center=yes,dialog=yes,modal=yes,dependent=yes";
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";F:UP!29010&DisplayUserKey=" + iUserKey + "&DisplayUserType=" + iUserType;
  if (bDisableButtons)
    cURL += "&DisableInteractiveButtons=1";
  cURL = RandomizeURL(cURL);
  var oDlg = open(cURL,"ProfileDisplayWnd" + iUserKey + "_" + iUserType,cParams);
  if (oDlg == null || oDlg.closed)
    PopupBlockerError();
  else
    oDlg.focus();
}

function DisplayExhibitorProfile(iInstanceID, iExhibitorKey)
{
  var cParams = GenerateCenteredWindowLeftTop(600,600)+"toolbar=no,scrollbars=yes,status=no,width=600,height=600,resizable=yes,menubar=no,location=no,center=yes";
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";F:UP!29012&ExhibitorKey=" + iExhibitorKey;
  cURL = RandomizeURL(cURL);
  var oDlg = open(cURL,"ExhibitorDisplayWnd"+iExhibitorKey,cParams);
  if (oDlg == null || oDlg.closed)
    PopupBlockerError();
  else
    oDlg.focus();
}

function UserProfileCallBack(cRetval)
{
  if (cRetval && cRetval != "")
    eval(cRetval);
}

function SendUserMail(iInstanceID, iUserKey, iUserType, iCreateMode, iForwardReplyMessageKey)
{
  var cCreateMode = "0";
  if (iCreateMode != null)
    cCreateMode = "" + iCreateMode;
  var cForwardReplyMessageKey = "";
  if (iForwardReplyMessageKey != null)
    cForwardReplyMessageKey = "" + iForwardReplyMessageKey;

  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";D:29125&ToUserKey=" + iUserKey + "&ToUserType=" + iUserType + "&CreateMode=" + cCreateMode + "&ForwardReplyMessageKey=" + cForwardReplyMessageKey;
  SetupPopupWindow(open(cURL,"_blank",GenerateCenteredWindowLeftTop(600,400) + ",toolbar=no,scrollbars=no,status=no,width=600,height=400,resizable=yes,menubar=no,location=no"));  
}

function SendUserVCard(iInstanceID, iUserKey, iUserType)
{
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";D:29160&ToUserKey=" + iUserKey + "&ToUserType=" + iUserType;
  SetupPopupWindow(open(cURL,"_blank",GenerateCenteredWindowLeftTop(500,250) + ",toolbar=no,scrollbars=no,status=no,width=500,height=250,resizable=yes,menubar=no,location=no"));  
}

function GoToUserPost(iInstanceID, iBlogPostKey, iBlogSpaceKey)
{
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";D:28035&BlogPostKey=" + iBlogPostKey + "&BlogSpaceKey=" + iBlogSpaceKey;
  SetupPopupWindow(open(cURL, "_blank", GenerateCenteredWindowLeftTop(750, 825) + ",toolbar=no,scrollbars=yes,status=no,width=750,height=825,resizable=yes,menubar=no,location=no"));  
}

function SendUserResume(iInstanceID, iBoothKey)
{
  var cParams = GenerateCenteredWindowLeftTop(420,250)+",width=420,height=250,toolbar=no,status=no,menubar=no,location=no,center=yes";
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";D:15160";
  if (iBoothKey)
    cURL += "&BoothKey=" + iBoothKey;
  SetupPopupWindow(open(cURL,"_blank",cParams));
}

function UserInitiatePrivateChat(iWithUserKey, iWithUserType)
{
  var cURL = "Server.nxp?LASCmd=AI:1;F:SF!29200&InvitedUserKey=" + iWithUserKey + "&InvitedUserType=" + iWithUserType;
  SetupPopupWindow(open(cURL,"",GetChatWndLayout(true)));
}

function UserCreatePrivateChat(iWithUserKey, iWithUserType, cInviteMessage)
{
  var cURL = "Server.nxp?LASCmd=AI:1;F:SF!29202&InvitedUserKey=" + iWithUserKey + "&InvitedUserType=" + iWithUserType;
  if (cInviteMessage && cInviteMessage.length > 0)
    cURL += "&InviteMessage=" + encodeURIComponent(cInviteMessage);
  SetupPopupWindow(open(cURL,"",GetChatWndLayout(false)));
}

function JoinPrivateChat(iChatSessionUserKey)
{
  var cURL = "Server.nxp?LASCmd=AI:1;F:SF!29210&ChatSessionUserKey=" + iChatSessionUserKey + "&DeclineMsg=";
  SetupPopupWindow(open(cURL,"",GetChatWndLayout(false)));
}

function UpdateTwitterStatus(iInstanceID, cTwitterStartingText, cTwitterShowDefaultValue)
{
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";D:28101&TwitterStartingText=" + encodeURIComponent(cTwitterStartingText) + "&TwitterShowDefaultValue=" + encodeURIComponent(cTwitterShowDefaultValue);
  SetupPopupWindow(open(cURL,"Tweet",GenerateCenteredWindowLeftTop(475,230) + ",width=475,height=230,directories=no,channelmode=no,titlebar=no,toolbar=no,scrollbars=no,status=no,resizable=no,menubar=no,location=no,center=no,dialog=yes,modal=yes,dependent=yes"));  
}

function GetChatWndLayout(bInvite)
{
  var cRetval = "";
  var iShowChatInviteMessage = 0;
  if (bInvite)
    {
      var oTop = GetTopWnd();
      if (oTop)
        {
          var iUserType = oTop.g_iUserType;
          iShowChatInviteMessage = oTop.g_iShowChatInviteMessage;
          if (iShowChatInviteMessage == 1 && iUserType == 0)
            iShowChatInviteMessage = 0;
        }
    }
  var iWidth = (iShowChatInviteMessage > 0 ? 400 : 525);
  var iHeight = (iShowChatInviteMessage > 0 ? 280 : 410);
  var iLeft = 20;
  var iTop = 20;
  if (iLeft + iWidth > screen.width)
    iLeft = screen.width - iWidth;
  if (iTop + iHeight > screen.height)
    iTop = screen.height - iHeight;
  cRetval = "left=" + iLeft + ",top=" + iTop + ",height=" + iHeight + ",width=" + iWidth + ",resizable=yes,status=no,scrollbars=no,toolbar=no,menubar=no,location=no,dialog=yes,modal=no,dependent=no";
  return cRetval;
}

function ChatWithNextAvailableBoothRep(iBoothKey)
{
  var cURL = "Server.nxp?LASCmd=AI:1;D:29250&BoothKey=" + iBoothKey;
  SetupPopupWindow(open(cURL,"",GetChatWndLayout(true)));
}

function AddDocumentLinkToMyBriefcase(iAttachmentKey, iInstanceID)
{
  var cURL = "Server.nxp?LASCmd=AI:"+iInstanceID+";S:29170;O:BriefcaseDocumentAddedResult.htm&AttachmentKey=" + iAttachmentKey;
  SetupPopupWindow(open(cURL,"_blank",GenerateCenteredWindowLeftTop(400,250) + ",toolbar=no,scrollbars=no,status=no,width=400,height=250,resizable=yes,menubar=no,location=no,center=yes"));  
}

function AddToConnectionList(iInstanceID, iUserKey, iUserType) { AddToBuddyList(iInstanceID, iUserKey, iUserType); }
function AddToBuddyList(iInstanceID, iUserKey, iUserType)
{
  var cURL = RandomizeURL("Server.nxp?LASCmd=AI:" + iInstanceID + ";F:SF!29341;O:BuddyAdded.htm&BuddyUserKey=" + iUserKey + "&BuddyUserType=" + iUserType);
  var cFeatures = GenerateCenteredWindowLeftTop(400,300) + ",width=400,height=300,toolbar=no,scrollbars=no,status=no,resizable=yes,menubar=no,location=no,center=yes,dialog=yes,modal=yes,dependent=yes";
  SetupPopupWindow(open(cURL,"_blank",cFeatures));  
}

function DoUserFinder(iInstanceID, cSrcTitle, bDefaultToOnline, iChatSessionKey)
{
  var cURL = "Server.nxp?LASCmd=AI:" + iInstanceID + ";O:UserFinder.htm";
  if (bDefaultToOnline)
    cURL += "&DefaultToOnline=1";
  if (iChatSessionKey)
    cURL += "&ChatSessionKey=" + iChatSessionKey;
  cURL = RandomizeURL(cURL);
  var cParams = GenerateCenteredWindowLeftTop(720,650)+"toolbar=no,width=720,height=650,resizable=yes,menubar=no,location=no,center=yes,scrollbars=no";
  open(cURL,"UserFinder"+cSrcTitle,cParams);
}

/////////////////////////////////////////////////////////////////////////////////
// taskbar functions

function SetupPopupWindow(oWin)
{
  try {
  if (oWin == null || oWin.closed)
    PopupBlockerError();
  }
  catch(e){}
}

function GetTaskBarWnd()
{
  return null;
}

function GetLayoutWnd(sID)
{
  oTop = GetTopWnd();
  if (oTop && oTop.g_bTopWnd && sID == "TopMenuFrame")
    return oTop;
  else if (oTop)
    return oTop.frames[sID];
  else
    return null;
}

function GetTopWnd(oWnd)
{
  var bUserFinderFrame = false;
  if (!oWnd)
    oWnd = window;
  try {
    if (oWnd.g_bTopWnd)
      return oWnd;
    if (oWnd.top && oWnd.top.g_bTopWnd)
      return oWnd.top;
    if (oWnd.opener && oWnd.opener != oWnd)
      return GetTopWnd(oWnd.opener);
    else if (oWnd.parent && oWnd.parent != oWnd)
      return GetTopWnd(oWnd.parent);
    else
      return null;  
  }
  catch(e) {
    bUseFinderFrame = true;
  }
  if (bUseFinderFrame)
    {
      try {
        var oWnd = open("/cfr/StdHTML/Loading.htm","dsfShellFinderFrame");
        if (oWnd)
          return oWnd.parent;
      }
      catch(e) {}
    }
  return null;
}

function GetMenuBarWnd() {
return GetLayoutWnd("TopMenuFrame");
}

function GetSectionBarWnd(){
return GetLayoutWnd("SectionBarFrame");
}

function GetBannerAdWnd(){
return GetLayoutWnd("BannerAdFrame");
}

function GetShowFloorWnd() {
return GetLayoutWnd("ShowFloorFrame");
}

function GetMarqueeWnd(){
return GetLayoutWnd("MarqueeFrame");
}


function ShowFloorFindTaskBar() {
return GetTaskBarWnd();
}


function AddToTaskBar(iWindowType, cTitle, bChatWin) {
}


function RemoveFromTaskBar(bChatWin) {
}


function FlashTaskBar() {
}


function FlashTaskBarStop() {
}

/////////////////////////////////////////////////////
// Tree Synchronization

function SyncTree(iBarIndex,iTreeKey,cPath,bExpandTargetNode)
{
  var oFrame = GetLayoutWnd("SectionBarFrame");
  if (oFrame == null)
    return;

  var cFindFcn = "TreeFindAndHighlightTreePath(\"" + cPath + "\"," + bExpandTargetNode + ")";
  oFrame.BarClicked(iBarIndex+1);
  var oTreeSelect = oFrame.frames[iBarIndex].frames[0].document.getElementById("PivotTree");
  if (oTreeSelect && oTreeSelect.value != iTreeKey)
    {
      oTreeSelect.value = iTreeKey;
      if (oTreeSelect.onchange)
        oTreeSelect.onchange();
      cFindFcn = "SyncTree(" + iBarIndex + "," + iTreeKey + ",\"" + cPath + "\"," + bExpandTargetNode + ")";
      oFrame.setTimeout(cFindFcn,3000);
    }
  else
    oFrame.frames[iBarIndex].frames[1].TreeFindAndHighlightTreePath(cPath,bExpandTargetNode);
}

/////////////////////////////////////////////////////
// general helper / compatibility functions

function SaveBtnHighlight(aEvent, bHighlight)
{
  var oEvent = new EventObj(aEvent);
  oEvent.srcElement.className = "Save" + (bHighlight ? "Highlight" : "");
}

function PopupBlockerError()
{
  var cMsg = "It appears that a popup blocker has\ndisabled this function. Please ensure\n" +
             "that your popup blockers have been\nturned off or that the show has been\n" +
             "whitelisted.  For more information,\nclick on the Help button or visit the\n" +
             "help booth in the show.";

  var oTop = GetTopWnd();
  if (oTop && oTop.g_oMessages)
    if (oTop.g_oMessages.PopUpError)
      cMsg = oTop.g_oMessages.PopUpError;

  alert(cMsg);
}

/////////////////////////////////////////////////////
// Order entry search helper function

function BoothOrderEntrySearchHelper(cFromProductSearch,cInventoryItemKey)
{
  if (cFromProductSearch == "1" && cInventoryItemKey.substr(0,2) != "$[")
    {
      for (var iLup = 0; iLup < g_aPageURLS.length; iLup++)
       {
         if (g_aPageURLS[iLup].indexOf("F:OE!44050") > -1)
           {
             if (SelectFlashTab)
               SelectFlashTab(iLup);  
             else if (SelectTab)
               SelectTab(iLup);
             break;
           }
       }
    }
}

/////////////////////////////////////////////////////
// Booth helper functions

function ExitBooth(iInstanceID, iBoothVisitKey, oAjaxObj) 
{
  if (g_bExitedBooth)
    return true;
    
  g_bExitedBooth = true;
  if (!iBoothVisitKey)
    return true;
    
  if (oAjaxObj == null)
    oAjaxObj = new InxpoAJAXObject();

  if (oAjaxObj)
    {
      oAjaxObj.SendSyncRequest("GET", "Server.nxp?LASCmd=AI:"+iInstanceID+";$:40010&BoothVisitKey="+iBoothVisitKey, "");
      return true;
    }
  else
    return false;
}

function NotifyStaffersOfBoothVisitor(iBoothVisitKey, iMinimumTimeSpentSecs, bNotifyFirstVisitOnly)
{
  var iFirstVisitOnly = (bNotifyFirstVisitOnly ? 1 : 0);

  if (iMinimumTimeSpentSecs == null || iMinimumTimeSpentSecs == 0)
    NotifyStaffersSendMail(iBoothVisitKey,iFirstVisitOnly);
  else
    setTimeout("NotifyStaffersSendMail(" + iBoothVisitKey + "," + iFirstVisitOnly +")",iMinimumTimeSpentSecs * 1000);
}

function NotifyStaffersSendMail(iBoothVisitKey,iNotifyFirstVisitOnly)
{
  var oAjaxObj = new InxpoAJAXObject();

  if (oAjaxObj)
    oAjaxObj.SendSyncRequest("GET", "Server.nxp?LASCmd=AI:1;$:40016&BoothVisitKey="+iBoothVisitKey+"&NotifyFirstVisitOnly="+iNotifyFirstVisitOnly, "");
  else
    alert("Unable To Create Communication Object.");
}

/////////////////////////////////////////////////////
// Event helper functions

function ProcessEventExitAction(iEventKey, cExitAction, cExitData, cExitActionTarget, iChatUserID)
{
  var cURL = "";
  switch (cExitAction) 
    {
      case "0":
        return;
        break;
      case "1":  // go to booth
        cURL = "Server.nxp?LASCmd=AI:1;F:SF!40000&BoothKey=" + cExitData;
        break;
      case "2":  // go to event lobby
        cURL = "Server.nxp?LASCmd=AI:1;F:SF!42000&EventKey=" + iEventKey + "&ForceToLobby=1";
        break;
      case "3":  // go to event survey
        cURL = "Server.nxp?LASCmd=AI:1;F:SF!30180&EventSurveyKey=" + cExitData;
        break;
      case "4":  // go to show floor
        cURL = "Server.nxp?LASCmd=AI:1;F:SF!30500&ShowFloorTemplateKey=" + cExitData;
        break;
      case "5":  // go to event
        cURL = "Server.nxp?LASCmd=AI:1;F:SF!42000&EventKey=" + cExitData;
        break;
      case "7":  // go to URL
        cURL = cExitData;
        break;
      case "8":  // go to event group chat
        cURL = "Server.nxp?LASCmd=AI:1;F:CHAT!640&ChatRoomKey=" + cExitData;
        break;
      case "9":  // go to event blog space
        cURL = "Server.nxp?LASCmd=AI:1;D:28037&BlogSpaceKey=" + cExitData;
        break;

      default:
        alert("Invalid Exit Action!");
        break;
    }

  if (cURL == "")
    return;

  if (cExitActionTarget == "ShowFloorFrame")
    {
      open(cURL,cExitActionTarget);
      CheckCloseWindow();
    }
  else if (cExitActionTarget == "new" || cExitActionTarget == "_new" || cExitActionTarget == "blank" || cExitActionTarget == "_blank")
    open(cURL);
  else if (cExitActionTarget == "" || cExitActionTarget == "self" || cExitActionTarget == "_self")
    NavigateWindow(window,cURL);
  else if (cExitActionTarget == "parent" || cExitActionTarget == "_parent")
    NavigateWindow(window.parent,cURL);
  else if (cExitActionTarget == "top" || cExitActionTarget == "_top")
    NavigateWindow(window.top,cURL);
  else
    {
      var oLink = document.getElementById("HiddenExitLink");
      if (oLink)
        {
          oLink.href = cURL;
          oLink.target = cExitActionTarget;
          oLink.click();
        }
      else
        alert("Unrecognized Exit Action Target Specified [" + cExitActionTarget + "].\r\nExit Link Tag Not Found! [HiddenExitLink]");
    }
}

function CheckCloseWindow()
{
  try {
    if (!top.g_bTopWnd)
      top.close();
  }
  catch(e){}
}

function DisplayEvent(iEventKey, bLaunchNewWindow, bFullScreen, iNewWindowWidth, iNewWindowHeight) 
{
  var cURL = "Server.nxp?LASCmd=AI:1;F:SF!42000&EventKey=" + iEventKey;
  if (bLaunchNewWindow == 1)
    SetupPopupWindow(open(cURL,"Event"+iEventKey,"toolbar=no,scrollbars=yes,status=yes,width=" + iNewWindowWidth + ",height=" + iNewWindowHeight + ",resizable=yes,menubar=no,location=no,fullscreen=" + bFullScreen));
  else
    open(cURL,"ShowFloorFrame");
}

/////////////////////////////////////////////////////
// Show Floor helper functions

var g_cUCOTotalCountElemID = null;
var g_cUCOAttendeeCountElemID = null;
var g_cUCOExhibitorCountElemID = null;
var g_fUCOCallbackFcn = null;

function ShowCurrentOnlineCount(cTotalCountElemID, cAttendeeCountElemID, cExhibitorCountElemID, fCallbackFcn)
{
  g_cUCOTotalCountElemID = cTotalCountElemID;
  g_cUCOAttendeeCountElemID = cAttendeeCountElemID;
  g_cUCOExhibitorCountElemID = cExhibitorCountElemID;
  g_fUCOCallbackFcn = fCallbackFcn;

  if (g_oAjaxObj)
    {
      g_oAjaxObj.Abort();
      g_oAjaxObj.OnComplete = ShowCurrentOnlineCountComplete;
      g_oAjaxObj.SendRequest("GET", RandomizeURL("Server.nxp?LASCmd=AI:1;F:LBSEXPORT!CSV&SQLID=25"), "");
    }
  else
    {
      alert("Invalid or missing g_oAjaxObj!");
    }
}

function ShowCurrentOnlineCountComplete(cResponse)
{
  var iAttendeeCount = -1;
  var iExhibitorCount = -1;
  var iTotalCount = -1;

  if (cResponse != "" && cResponse.indexOf("Total") > -1)
    {
      aData = cResponse.split("\r\n");
      if (aData.length > 1)
        {
          var aRow = aData[1].split(",");
          if (aRow && aRow.length > 2)
            {
              iAttendeeCount = aRow[0]*1;
              iExhibitorCount = aRow[1]*1;
              iTotalCount = aRow[2]*1;
            }
        }
    }

  if (g_fUCOCallbackFcn)
    {
      g_fUCOCallbackFcn(iAttendeeCount,iExhibitorCount,iTotalCount);
    }
  else
    {
      var oElem;
      if (g_cUCOTotalCountElemID && g_cUCOTotalCountElemID != "")
        {
          oElem = document.getElementById(g_cUCOTotalCountElemID);
          if (oElem)
            oElem.innerHTML = "" + iTotalCount;
        }
        
      if (g_cUCOAttendeeCountElemID && g_cUCOAttendeeCountElemID != "")
        {
          oElem = document.getElementById(g_cUCOAttendeeCountElemID);
          if (oElem)
            oElem.innerHTML = "" + iAttendeeCount;
        }

      if (g_cUCOExhibitorCountElemID && g_cUCOExhibitorCountElemID != "")
        {
          oElem = document.getElementById(g_cUCOExhibitorCountElemID);
          if (oElem)
            oElem.innerHTML = "" + iExhibitorCount;
        }
    }
}

function AttendOtherShow(iShowKey, iShowPackageKey, cDisplayItem, bAC)
{
  if (!iShowKey)
    return;
    
  var cURL = "Server.nxp?LASCmd=AI:1;F:US!14516&NewShowKey=" + iShowKey;
  if (bAC)
    cURL += "&AC=1";
  if (iShowPackageKey)
    cURL += "&ShowPackageKey=" + iShowPackageKey;
  if (cDisplayItem && cDisplayItem != "")
    cURL += "&DisplayItem="+cDisplayItem;
    
  var oTop = GetTopWnd();
  if (oTop)
    NavigateWindow(oTop,cURL);
}

