#property show_inputs

#import "user32.dll"
  int FindWindowA( string lpClassName, string lpWindowName );
  int SendMessageA( int hWnd, int Msg, int wParam, int lParam );
  int GetAncestor( int hwnd, int gaFlags );
  int GetDlgItem( int hDlg, int nIDDlgItem );
  int PostMessageA( int hWnd, int Msg, int wParam, int lParam[] );
  int GetMenuItemCount( int hMenu );
  int GetMenuStringA( int hMenu, int uIDItem, string lpString, int nMaxCount, int uFlag );
  int GetMenuItemID( int hMenu, int nPos );
  bool SetCursorPos( int PosX, int PosY );
  bool GetCursorPos( int& Pos[] );
#import

#define WM_CLOSE   0x0010
#define WM_NOTIFY  0x004E
#define WM_COMMAND 0x0111

#define MN_GETHMENU   0x01E1
#define MF_BYPOSITION 0x00000400

#define ID_CONTROL 0xE801

#define CURSOR_XY 5000

#define PAUSE 25

extern string ServerString = "Gamma";
extern int CheckInterval = 60;

string TempFunc( int hPopUp, string Str )
{
  if (hPopUp != 0)
    SendMessageA(hPopUp, WM_CLOSE, 0, 0);

  return(Str);
}

string SetServer( string ServerStr, int MaxTime )
{
  if (!IsDllsAllowed())
    return("ERROR - DLL are not allowed.");

  int hMT = GetAncestor(WindowHandle(Symbol(),Period()), 3);
  int hPopUp, hMenu, ItemCount, MenuItemID;
  int Cursor[2], NMHDR[3] = {0, ID_CONTROL, 0x81A4};
  int TickCount = GetTickCount();
  string TmpStr = "";

  NMHDR[0] = GetDlgItem(hMT, ID_CONTROL);

  GetCursorPos(Cursor);
  SetCursorPos(CURSOR_XY, CURSOR_XY);

  PostMessageA(hMT, WM_NOTIFY, ID_CONTROL, NMHDR);

  while (!IsStopped())
  {
    hPopUp = FindWindowA("#32768", TmpStr);

    if ((hPopUp > 0) || (GetTickCount() - TickCount > MaxTime))
      break;

    Sleep(PAUSE);
  }

  SetCursorPos(Cursor[0], Cursor[1]);

  if (hPopUp == 0)
    return(TempFunc(hPopUp, "ERROR - Not found a window pop-up menu."));
  else if (IsStopped())
    return(TempFunc(hPopUp, "Stopped."));

  hMenu = SendMessageA(hPopUp, MN_GETHMENU, 0, 0);

  if (hMenu == 0)
    return(TempFunc(hPopUp, "ERROR - Not found pop-up menu."));

  ItemCount = GetMenuItemCount(hMenu);

  if (ItemCount == 0)
    return(TempFunc(hPopUp, "ERROR - Null Menu."));

  TmpStr = "АбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэюяАбвгдежзийклмнопрстуфхцчшщъыьэю";

  for (int i = 0; i < ItemCount; i++)
    if (GetMenuStringA(hMenu, i, TmpStr, 255, MF_BYPOSITION) != 0)
    {
      MenuItemID = GetMenuItemID(hMenu, i);

      if (StringFind(TmpStr, ServerStr) >= 0)
      {
        if (SendMessageA(hMT, WM_COMMAND, MenuItemID, 0) != 0)
          return(TempFunc(hPopUp, TmpStr));
        else
          return(TempFunc(hPopUp, "ERROR - Unable to select the item."));
      }
    }

  return(TempFunc(hPopUp, "ERROR - The item \"" + ServerStr + "\" was not found in the menu."));
}

void deinit()
{
  Comment("");

  return;
}

void init()
{
  int Pause;
  string Str;

  while (!IsStopped())
  {
    Str = "Previous result (" + TimeToStr(TimeLocal(), TIME_SECONDS) + "): " + SetServer(ServerString, 500);

    Pause = CheckInterval * 1000;

    while (Pause > 0)
    {
      Comment(WindowExpertName() + ": to check the Server (" + ServerString +") still have " + DoubleToStr(1 + Pause / 1000, 0) + " s.\n" + Str);

      Sleep(PAUSE);

      Pause -= PAUSE;

      if (IsStopped())
        break;
    }
  }

  return;
}

void start()
{
  init();

  return;
}