section.begin
   Name = EventMultiplayerLogin
   Code : struct.begin
      [*] = ;procedure FillInputBoxList(elmHnd, focused : Integer; bforward : Boolean; var list : TIntegerList);
      [*] = ;begin
      [*] = ;   if (GetGUIElementVisible(elmHnd)) then
      [*] = ;   begin
      [*] = ;      var className : String = GetObjectClassNameByHandle(elmHnd);
      [*] = ;      if (SameText(className, 'TXEditControl')) then
      [*] = ;      gIntegerList.Add(elmHnd)
      [*] = ;      else
      [*] = ;      begin
      [*] = ;         var i : Integer;
      [*] = ;         for i:=0 to GetGUIElementChildrenCount(elmHnd)-1 do
      [*] = ;         begin
      [*] = ;            var elmChildHnd : Integer = GetGUIElementChildrenByIndex(elmHnd, i);
      [*] = ;            FillInputBoxList(elmChildHnd, focused, bforward, list);
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;function CheckValidCharsInStringByFilter(text, filter : String) : Boolean;
      [*] = ;begin
      [*] = ;   Result := True;
      [*] = ;   var i, j : Integer;
      [*] = ;   for i:=1 to StrLength(text) do
      [*] = ;   begin
      [*] = ;      var bres : Boolean;
      [*] = ;      var tchar : String = SubStr(text, i, 1);
      [*] = ;      for j:=1 to StrLength(filter) do
      [*] = ;      begin
      [*] = ;         var filterchar : String = SubStr(filter, j, 1);
      [*] = ;         if (filterchar=tchar) then
      [*] = ;         begin
      [*] = ;            bres := True;
      [*] = ;            break;
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;      if (not bres) then
      [*] = ;      begin
      [*] = ;         Result := False;
      [*] = ;         break;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;function IsEmailValidFormat(email : String) : Boolean;
      [*] = ;begin
      [*] = ;   if (StrExists(email, '@')) and
      [*] = ;   (StrExists(email, '.')) and
      [*] = ;   (not StrExists(email, '@.')) and
      [*] = ;   (not StrExists(email, ' ')) and
      [*] = ;   (not StrExists(email, '.@')) and
      [*] = ;   (not StrExists(email, '..')) and
      [*] = ;   (SubStr(email, 0, 1)<>'.') and
      [*] = ;   (SubStr(email, StrLength(email), 1)<>'.') and
      [*] = ;   CheckValidCharsInStringByFilter(email, gc_gui_charfilter_mail) then
      [*] = ;   Result := True
      [*] = ;   else
      [*] = ;   Result := False;
      [*] = ;end;
      [*] = ;
      [*] = ;const cTabChar    = 9;  // tab
      [*] = ;const cSpaceChar  = 32; // space
      [*] = ;const cQuoteChar  = 34; // "
      [*] = ;const cEscChar    = 27; // Escape
      [*] = ;const cReturnChar = 13; // Enter or Return
      [*] = ;const cUpChar     = 38; // key up
      [*] = ;const cDownChar   = 40; // key down
      [*] = ;const cShiftChar  = 16; // shift
      [*] = ;const cCtrlChar   = 17; // ctrl
      [*] = ;
      [*] = ;procedure GetInputBoxValueToVar(elmpath, elmname : String; var value : String);
      [*] = ;begin
      [*] = ;   var parent : Integer = GetGUIElementIndexByNameParent(elmpath, _gui_GetTop);
      [*] = ;   if (parent<>0) then
      [*] = ;   begin
      [*] = ;      var childHnd : Integer = _gui_FindChildHandleByName(parent, elmname);
      [*] = ;      if (childHnd<>0) and GetGUIElementVisible(childHnd) then
      [*] = ;      begin
      [*] = ;         var inputboxHnd : Integer = GetGUIElementIndexByNameParent('text', childHnd);
      [*] = ;         var className : String = GetObjectClassNameByHandle(inputboxHnd);
      [*] = ;         //if (SameText(className, 'TXEditControl')) then
      [*] = ;         value := GetGUIElementText(inputboxHnd);
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;var elmHnd : Integer = GetIntValueByName('ElementHandle');
      [*] = ;var press : String = GetValueByName('Press');
      [*] = ;var tag : Integer = GetIntValueByName('Tag');
      [*] = ;
      [*] = ;var status : String = GetValueByName('Status');
      [*] = ;if (status='shortcut') or (status='key') then // to prevent invalid handle here
      [*] = ;elmHnd := 0;
      [*] = ;
      [*] = ;const currentstatename = 'EventMultiplayerLogin';
      [*] = ;var elmname, showstate, eventstate : String;
      [*] = ;_misc_GetGUIShowEventStateNames(currentstatename, elmname, showstate, eventstate);
      [*] = ;
      [*] = ;const cTagRegister = 102;
      [*] = ;const cTagLogin = 103;
      [*] = ;const cTagCheckEmail = 104;
      [*] = ;const cTagForgot = 105;
      [*] = ;const cTagChangeProfile = 106;
      [*] = ;
      [*] = ;var inputhnd : Integer = GetGUIElementIndexByNameParent(elmname+'.input.text', _gui_GetTop);
      [*] = ;
      [*] = ;if (GetGUIFocusedElementHandle<>0) then
      [*] = ;SetGUIEventStateOnKeyDown(eventstate)
      [*] = ;else
      [*] = ;SetGUIEventStateOnKeyDown('');
      [*] = ;
      [*] = ;//log('key.tag='+IntToStr(tag));
      [*] = ;//log('key.press='+press);
      [*] = ;//log('key.status='+status);
      [*] = ;
      [*] = ;case status of
      [*] = ;   'select' : begin
      [*] = ;      tag := GetGUIListBoxItemTag(elmHnd, GetGUIListBoxItemIndex(elmHnd));
      [*] = ;      gConnectIPIndex := tag;
      [*] = ;   end;
      [*] = ;   'shortcut' : begin
      [*] = ;      //Log('Console key: '+GetValueByName('Key'));
      [*] = ;      case GetValueByName('Key') of
      [*] = ;         'escape' : tag := gc_gui_event_tagClose;
      [*] = ;         'return' : tag := gc_gui_hotkey_btnEnter;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   'button' : begin
      [*] = ;      //if (press='c') then
      [*] = ;      //SetGUIElementFocusedHandle(elmHnd);
      [*] = ;      //else
      [*] = ;      //if (press='d') then
      [*] = ;      //SetGUIEventStateOnKeyDown(eventstate);
      [*] = ;   end;
      [*] = ;   'key' : begin
      [*] = ;      if (press='d') then
      [*] = ;      begin
      [*] = ;         case tag of
      [*] = ;            cTabChar : begin
      [*] = ;               var focused : Integer = GetGUIFocusedElementHandle;
      [*] = ;               if (focused<>0) then
      [*] = ;               begin
      [*] = ;                  var bforward : Boolean = (KeyPressed(cShiftChar)<>cShiftChar);
      [*] = ;                  gIntegerList.Clear;
      [*] = ;                  FillInputBoxList(_gui_GetTop, focused, bforward, gIntegerList);
      [*] = ;                  var curind : Integer = gIntegerList.IndexOf(focused);
      [*] = ;                  var maxind : Integer = gIntegerList.GetCount-1;
      [*] = ;                  var newind : Integer;
      [*] = ;                  if (bforward) then
      [*] = ;                  begin
      [*] = ;                     if curind>=maxind then
      [*] = ;                     newind := 0
      [*] = ;                     else
      [*] = ;                     newind := curind+1;
      [*] = ;                  end
      [*] = ;                  else
      [*] = ;                  begin
      [*] = ;                     if curind<=0 then
      [*] = ;                     newind := maxind
      [*] = ;                     else
      [*] = ;                     newind := curind-1;
      [*] = ;                  end;
      [*] = ;                  var newhnd : Integer = gIntegerList.Get(newind);
      [*] = ;                  SetGUIElementFocusedHandle(newhnd);
      [*] = ;               end;
      [*] = ;            end;
      [*] = ;            cReturnChar : begin
      [*] = ;               if press='u' then
      [*] = ;               begin
      [*] = ;                  press := 'c';
      [*] = ;                  tag := gc_gui_hotkey_btnEnter;
      [*] = ;               end;
      [*] = ;            end;
      [*] = ;            cEscChar : begin
      [*] = ;               press := 'c';
      [*] = ;               tag := gc_gui_hotkey_btnEscape;
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if press='u' then
      [*] = ;      if tag=cReturnChar then
      [*] = ;      begin
      [*] = ;         press := 'c';
      [*] = ;         tag := gc_gui_hotkey_btnEnter;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;
      [*] = ;if (press='c') then
      [*] = ;begin
      [*] = ;   if (tag=gc_gui_hotkey_btnEnter) then
      [*] = ;   case gConnectLoginState of
      [*] = ;      gc_ConnectLoginState_YourEmail : tag := cTagCheckEmail;
      [*] = ;      gc_ConnectLoginState_InvalidEmail : tag := cTagRegister;
      [*] = ;      gc_ConnectLoginState_ValidEmail : tag := cTagLogin;
      [*] = ;   end;
      [*] = ;   var elmPath : String = elmname+'.main';
      [*] = ;   case tag of
      [*] = ;      gc_gui_event_tagShow : ExecuteState(showstate);
      [*] = ;      gc_gui_event_tagHide : begin
      [*] = ;         _gui_SetGUIElementVisibleByName(elmname, False);
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;      end;
      [*] = ;      gc_gui_event_tagClose, gc_gui_hotkey_btnEscape : begin
      [*] = ;         _gui_SetGUIElementVisibleByName(elmname, False);
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;         ExecuteState('ShowMainMenu');
      [*] = ;         gint_gui_newsshowvisible := 0;
      [*] = ;         ExecuteState('ShowNews');
      [*] = ;         gInternetShell.prevstate := -1;
      [*] = ;         if (gstring_gui_connectserver<>'') or (not IsLanPublicServerMode) then
      [*] = ;         _misc_ConnectIPIndexIncrease;
      [*] = ;         gstring_gui_connectserver := '';
      [*] = ;         gint_gui_connectserverstate := 0;
      [*] = ;         LanShutdown;
      [*] = ;      end;
      [*] = ;      cTagLogin : begin
      [*] = ;         var key : String;
      [*] = ;         _gui_ShowConnectStateMessage(gc_ConnectState_Connecting);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'pass', gProfileTmp.netpass);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'key', gProfileTmp.netcdkey);
      [*] = ;         LanSetMyEmail(gProfileTmp.netemail);
      [*] = ;         LanSetMyPassword(gProfileTmp.netpass);
      [*] = ;         LanSetMyCDKey(gProfileTmp.netcdkey);
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;
      [*] = ;         if (gConnectIPList[gConnectIPIndex]<>gConnectHost) then
      [*] = ;         begin
      [*] = ;            gstring_gui_connectserver := gProfileTmp.netemail+':'+gProfileTmp.netpass+':'+gProfileTmp.netcdkey+'@'+gConnectIPList[gConnectIPIndex];
      [*] = ;            gInternetShell.prevstate := -1;
      [*] = ;            gint_gui_connectserverstate := 1;
      [*] = ;            //LanShutdown;
      [*] = ;            gint_gui_connectserverstate := 2;
      [*] = ;            var logindata, hostdata : String;
      [*] = ;            var pos : Integer = StrPos(':', gstring_gui_connectserver);
      [*] = ;            pos := StrPosEx('@', gstring_gui_connectserver, pos);
      [*] = ;            if (pos>0) then
      [*] = ;            begin
      [*] = ;               logindata := SubStr(gstring_gui_connectserver, 1, pos-1);
      [*] = ;               hostdata := SubStr(gstring_gui_connectserver, pos+1, StrLength(gstring_gui_connectserver));
      [*] = ;               var email, pass, cdkey, s4 : String;
      [*] = ;               _misc_GetDelimiterString(logindata, 58, email, pass, cdkey, s4);
      [*] = ;               var i : Integer;
      [*] = ;               for i:=0 to gConnectIPCount-1 do
      [*] = ;               begin
      [*] = ;                  if (gConnectIPList[i]=hostdata) then
      [*] = ;                  begin
      [*] = ;                     gConnectIPIndex := i;
      [*] = ;                     break;
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               var hostip, hostport : String;
      [*] = ;               pos := StrPos(':', hostdata);
      [*] = ;               if (pos>1) then
      [*] = ;               begin
      [*] = ;                  hostip := SubStr(hostdata, 1, pos-1);
      [*] = ;                  hostport := SubStr(hostdata, pos+1, StrLength(hostdata));
      [*] = ;                  LanPublicServerEnter(hostip, 31523);
      [*] = ;               end;
      [*] = ;            end;
      [*] = ;         end
      [*] = ;         else
      [*] = ;         LanPublicServerLogin;
      [*] = ;      end;
      [*] = ;      cTagCheckEmail : begin
      [*] = ;         _gui_ShowConnectStateMessage(gc_ConnectState_Auth);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'mail', gProfileTmp.netemail);
      [*] = ;         LanSetMyEmail(gProfileTmp.netemail);
      [*] = ;         LanPublicServerUserExist(gProfileTmp.netemail);
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;      end;
      [*] = ;      cTagForgot : begin
      [*] = ;         if (gProfileTmp.netemail<>'') then
      [*] = ;         begin
      [*] = ;            LanPublicServerForgotPassword(gProfileTmp.netemail);
      [*] = ;            Log('Forgotten password sent to '+gProfileTmp.netemail);
      [*] = ;            _gui_ShowConnectStateMessage(gc_ConnectState_PassWasSentToMail);
      [*] = ;         end;
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;      end;
      [*] = ;      cTagRegister : begin
      [*] = ;         var mail, pass, passconfirm, nick, key : String;
      [*] = ;         GetInputBoxValueToVar(elmPath, 'mail', mail);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'nick', nick);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'pass', pass);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'passconfirm', passconfirm);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'key', key);
      [*] = ;
      [*] = ;         gConnectState := 0;
      [*] = ;         if (nick='') or (pass='') then
      [*] = ;         gConnectState := gc_ConnectState_FillAllFields
      [*] = ;         else
      [*] = ;         begin
      [*] = ;            if (StrLength(nick)<4) or (StrLength(nick)>16) then
      [*] = ;            gConnectState := gc_ConnectState_InvalidNickLength
      [*] = ;            else
      [*] = ;            if (not CheckValidCharsInStringByFilter(nick, gc_gui_charfilter_nick)) then // add restricted characters list here
      [*] = ;            gConnectState := gc_ConnectState_InvalidNickChar
      [*] = ;            else
      [*] = ;            if (StrLength(pass)<5) or (StrLength(pass)>16) then
      [*] = ;            gConnectState := gc_ConnectState_InvalidPassLength
      [*] = ;            else
      [*] = ;            if (not CheckValidCharsInStringByFilter(pass, gc_gui_charfilter_pass)) then
      [*] = ;            gConnectState := gc_ConnectState_InvalidPassChar
      [*] = ;            else
      [*] = ;            if (pass<>passconfirm) then
      [*] = ;            gConnectState := gc_ConnectState_PassDoesntMatch
      [*] = ;            else
      [*] = ;            begin
      [*] = ;               // Register
      [*] = ;               if IsEmailValidFormat(mail) then
      [*] = ;               begin
      [*] = ;                  _gui_ShowConnectStateMessage(gc_ConnectState_Connecting);
      [*] = ;                  gConnectState := 0;
      [*] = ;                  gProfileTmp.netemail := mail;
      [*] = ;                  gProfileTmp.netpass := pass;
      [*] = ;                  gProfileTmp.netcdkey := key;
      [*] = ;                  LanSetMyEmail(mail);
      [*] = ;                  LanSetMyPassword(pass);
      [*] = ;                  LanSetMyCDKey(key);
      [*] = ;                  LanSetMyNick(nick);
      [*] = ;                  LanSetMyCountry('');
      [*] = ;                  // combine info
      [*] = ;                  var previnfo, newinfo : String;
      [*] = ;                  _misc_LanCombinePlayerInfo(previnfo, newinfo);
      [*] = ;                  LanSetMyProfInfo(newinfo);
      [*] = ;                  LanPublicServerRegister;
      [*] = ;               end
      [*] = ;               else
      [*] = ;               _gui_ShowConnectStateMessage(gc_ConnectState_InvalidEmail);
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;         if (gConnectState<>0) then
      [*] = ;         _gui_ShowConnectStateMessage(gConnectState);
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;      end;
      [*] = ;      cTagChangeProfile : begin
      [*] = ;         var mail, pass, passconfirm, nick, key : String;
      [*] = ;         GetInputBoxValueToVar(elmPath, 'mail', mail);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'nick', nick);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'pass', pass);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'passconfirm', passconfirm);
      [*] = ;         GetInputBoxValueToVar(elmPath, 'key', key);
      [*] = ;
      [*] = ;         gConnectState := 0;
      [*] = ;         if (nick='') or (pass='') then
      [*] = ;         gConnectState := gc_ConnectState_FillAllFields
      [*] = ;         else
      [*] = ;         begin
      [*] = ;            if (StrLength(nick)<4) or (StrLength(nick)>16) then
      [*] = ;            gConnectState := gc_ConnectState_InvalidNickLength
      [*] = ;            else
      [*] = ;            if (not CheckValidCharsInStringByFilter(nick, gc_gui_charfilter_nick)) then // add restricted characters list here
      [*] = ;            gConnectState := gc_ConnectState_InvalidNickChar
      [*] = ;            else
      [*] = ;            if (StrLength(pass)<5) or (StrLength(pass)>16) then
      [*] = ;            gConnectState := gc_ConnectState_InvalidPassLength
      [*] = ;            else
      [*] = ;            if (not CheckValidCharsInStringByFilter(pass, gc_gui_charfilter_pass)) then
      [*] = ;            gConnectState := gc_ConnectState_InvalidPassChar
      [*] = ;            else
      [*] = ;            if (pass<>passconfirm) then
      [*] = ;            gConnectState := gc_ConnectState_PassDoesntMatch
      [*] = ;            else
      [*] = ;            begin
      [*] = ;               if (LanMyInfoName<>nick) or (gProfile.netpass<>pass) then
      [*] = ;               begin
      [*] = ;                  if (nick<>'') and (LanMyInfoName<>nick) then
      [*] = ;                  LanSetMyNick(nick);
      [*] = ;                  if (pass<>'') and (gProfile.netpass<>pass) then
      [*] = ;                  begin
      [*] = ;                     LanSetMyPassword(pass);
      [*] = ;                     gProfile.netpass := pass;
      [*] = ;                     gProfileTmp.netpass := pass;
      [*] = ;                     _profile_SaveUserProfile;
      [*] = ;                  end;
      [*] = ;                  LanPublicServerRegister;
      [*] = ;                  gbool_lan_changeprofile := True;
      [*] = ;                  LanShutdown;
      [*] = ;               end;
      [*] = ;               _gui_ShowConnectStateMessage(gc_ConnectState_Hide);
      [*] = ;               gConnectLoginState := gc_ConnectLoginState_Hide;
      [*] = ;               _gui_SendTagToState('EventMultiplayerLogin', gc_gui_event_tagHide);
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;         if (gConnectState<>0) then
      [*] = ;         _gui_ShowConnectStateMessage(gConnectState);
      [*] = ;         SetGUIElementFocusedHandle(0);
      [*] = ;         SetGUIEventStateOnKeyDown('');
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
   struct.end
section.end

