﻿var WEBMAIL_URL = "https://webmail.dkdm.dk/gw/webacc";

//
// This function should be called to open webmail
//
function OpenWebmail(language) {
  WebmailLanguage = language;
  //Calls the security AJAX Webservice - if included - to get the username/password
  if (DKDM.Intranet.Security.SecurityAJAXService != null)
    DKDM.Intranet.Security.SecurityAJAXService.GetCurrentUser(MailLogin_GetCurrentUserCallback);
  else
    window.open(WEBMAIL_URL);
}

var WebmailLanguage = "da";

function MailLogin_GetCurrentUserCallback(result) {
  if (!result.WasSuccessful)
  {
    window.open(WEBMAIL_URL);
    return;
  }

  var mailForm = document.createElement("form");
  mailForm.target = "_blank";
  mailForm.action = WEBMAIL_URL;
  mailForm.method = "post";
  document.body.appendChild(mailForm);
  
  var input;
  input = document.createElement("input");
  input.name = "error";
  input.value = "login";
  mailForm.insertBefore(input, null);  
  input = document.createElement("input");
  input.name="User.displayDraftItems";
  input.value="1";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="merge";
  input.value="webacc";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="action";
  input.value="User.Login";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="Url.hasJavaScript";
  input.value="1";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="Low.bandwidth";
  input.value="0";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="User.interface";
  input.value="css";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="User.Theme.index";
  input.value="1";
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="User.id";
  input.value=result.Credentials.UserName;
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="User.password";
  input.value=result.Credentials.Password;
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.name="User.lang";
  input.value=WebmailLanguage;
  mailForm.insertBefore(input, null);
  input = document.createElement("input");
  input.type="submit";
  input.value="Login";
  mailForm.insertBefore(input, null);

  mailForm.submit();

  document.body.removeChild(mailForm);
}
