title: Functions loading in incorrect order
body: <p>I have a login page.When the user clicks on a login button a "terms and condition" dialog will pop up,however if the username and password is incorrect an "invalid" dialog box will pop up.BUT the problem is when an incorrect detail is entered the "terms and condition" dialog pops up first and then only after you click the dialog box does the "invalid" dialog box show up.I need the invalid dialog box to pop up on an invalid entry before the terms and condition dialog.Can you advise as to what i am doing wrong in my code?</p>
  
  <pre><code>onClick: function (event) {
          if (event)
              event.preventDefault();
          if ($(event.currentTarget).attr('id') == "btnLogin") {
              $('#txtLogin').val($('#username').val());
              $('#txtPassword').val($('#password').val())
          }
          if (($('#txtLogin').val() === "") || ($('#txtPassword').val() === "") || ($('#txtLogin').val() === "Number") || ($('#txtPassword').val() === "Password")) {
              if ($(event.currentTarget).attr('id') == "btnLogin") {
                  $('#dismiss').click();
              } 
              $("&lt;div&gt;Please enter user name and password&lt;/div&gt;").dialog({
                  position: 'top',
                  modal: true,
                  resizable: false,
                  title: "Error",
                  buttons: { "Okay": function () { $(this).dialog("close"); } }
              });
              return;
          }
  
          CheckLogon($('#txtLogin').val(), $('#txtPassword').val(),checkLogonSuccess, onError); //checklogonSuccess calls terms and conditions and on error calls invalid details.i tried adding it as this CheckLogon($('#txtLogin').val(), $('#txtPassword').val(), onError,checkLogonSuccess,); but it doesnt do anything.
      },
  
  onError: function (ex) {
              $('#btnLogin').show();
  
  
              $("&lt;div&gt;" + ex._message +"&lt;/div&gt;").dialog({
                  position: 'top',
                  modal: true,
                  resizable: false,
                  title: "Error",
                  buttons: { "Okay": function () { $(this).dialog("close"); } }
              });
  
          }
  
  
  
  TermsSuccess: function (result, context) {
  
              var topTerms = findSetByInArray(result.Data, 'ParentId', 0);
              var termsHTML = '&lt;div id="terms"&gt;&lt;ul class="termsList"&gt;';
              for (var i = 0; i &lt; topTerms.length; i++) {
                  var cls = (topTerms[i].isNew) ? 'newTerm' : 'Term';
                  termsHTML += '&lt;li id=' + topTerms[i].ID + ' class=' + cls + '&gt;'
                  termsHTML += topTerms[i].PageIndex + '. ' + topTerms[i].Detail;
                  termsHTML += betMethods.getChildrenTerms(result.Data, topTerms[i].ID, topTerms[i].PageIndex + '. ');
                  termsHTML += '&lt;/li&gt;';
              }
              termsHTML += '&lt;/ul&gt;&lt;/div&gt;';
              $('#dismiss').click();
  
              $(termsHTML).dialog({
                  modal: true,
                  resizable: false,
                  width: 600,
                  height: 600,
                  closeOnEscape: false,
                  open: function (event, ui) {
                      $(this).parent().children().children('.ui-dialog-titlebar-close').hide();
                  },
                  title: "Terms &amp; Conditions",
                  buttons: [{
                      text: "Decline",
                      "class": 'btnDialog',
                      click: function () {
                          $(this).dialog("close");
  
  
                      }
                  },
                          {
                              text: "Accept",
                              "class": 'btnDialog',
                              click: function () {
                                  betEvents.btnAccept_onClick();
                                  $(this).dialog("close");
                              }
                          }]
              });
          },
          checkLogonSuccess: function (result, context) {
              GetTerms(result,TermsSuccess, onError);
          },
  
  CheckLogon(string account, string password)
          {
              Stopwatch stopwatch = Stopwatch.StartNew();
              DateTime? result = null;
  
              var aNumber = number;
              if (Context.Enabled)
              {
                  var profile = Context.Current.ValidateLogin(account, password);
  
                  if (profile == null)
                      throw new SecurityException("Invalid username / password");
  
                  var key = profile._UserID.ToString();
                  var mapping = DependencyResolver.GetService&lt;DataContext&gt;().AccountUserNameMappings.FirstOrDefault(x =&gt; x.Key == key);
                  if (mapping == null)
                      aNumber = null;
                  else
                      aNumber = mapping.ANumber;
              }
  }
  </code></pre>