﻿_FormControl_FieldChange = false;
var _FailedLogins = 0;

function CheckCookies() {
  $.cookie("LoginTest", "1");
  if ($.cookie("LoginTest") == "1") {
    $.cookie("LoginTest", null);
    return true;
  } else {
    Dialog_Info("Cookies Required", 
      "You must have Cookies enabled in order for the WebManager Control Panel<br />"
      + "to function correctly.</b><br /><br />"
      + "Please enable Cookies in your browser and then click Close or click your<br />"
      + "browser's refresh button to log in to WebManager.", true);
    return false;
  }
}

function UpdateSaveChecks() {
  if (gc("chkSaveLogin").checked) {
    gc("chkSavePassword").disabled = false;
  } else {
    gc("chkSavePassword").disabled = true;
    gc("chkSavePassword").checked = false;
  }
}

function ShowLoginInfo() {
  Dialog_Dynamic("WebManager Login Information", _AppUrls.WebManagerS + "Dialogs/LoginInfo.aspx", 800, 300);
}

function Dialog_OkHandler(title) {
  switch (title) {
    case "AutoManager Service Agreement":
      gc("xAcceptAgmt").value = "1";
      Click_Enter();
      break;
    case "Duplicate Login":
      gc("xOverrideDup").value = "1";
      Click_Enter();
      break;
  }
}

function Dialog_CloseHandler(title) {
  switch (title) {
    case "Cookies Required":
      window.location.reload(); break;
    case "AutoManager Service Agreement":
      gc("xAcceptAgmt").value = ""; break;
    case "Duplicate Login":
      gc("xOverrideDup").value = ""; break;
  }
}

function Login_ResultHandler(result) {
  if (result.Success) {
    Go(PageLink(((result.NewUser) ? "Setup" : "Default") + ".aspx", result.SessionId));
  } else {
    g("btnLogin").disabled = false;
    $("#btnLogin").removeClass("gray");

    switch (result.ErrorMessage) {
      case "Service Agreement":
        Dialog_ServiceAgreement();
        break;

      case "Duplicate Login":
        Dialog_Prompt("Duplicate Login",
          "Your account is already logged in from another location.<br/><br/>"
          + "<b><span class=\"field-heading\">Remote IP:</span>" + result.DuplicateIP + "</b><br/><br/>"
          + "Do you want to log out the other session and log in from this location?", true);
        break;

      default:
        $("#tdErrorMessage").html("<b class=\"error\">" + result.ErrorMessage + "</b>");
        $c("txtClientID").focus();

        if (_FailedLogins++ >= 2) {
          ShowLoginInfo();
        }
        break;
    }
  }
}

function Click_Enter() {
  g("btnLogin").disabled = true;
  $("#btnLogin").addClass("gray");
  $("#tdErrorMessage").html("<b class=\"okay\">Logging in...</b>");

  AjaxForm(null, "Login.aspx/Login", "#divLoginForm", Login_ResultHandler);
}

//========================================================================================================================

$(function(){
  if (CheckCookies()) {
    if (_DialogPanelType == null) {
      if (gc("txtClientID").value == "") {
      } else if (gc("txtUsername").value == "") {
        FieldFocus_Start($c("txtUsername"));
      } else if (gc("txtPassword").value == "") {
        FieldFocus_Start($c("txtPassword"));
      } else {
        FieldFocus_Start($c("txtClientID"));
      }
    }
  }

  UpdateSaveChecks();
});
