
init('parent')

var SMIMEApplet // Cross Platform (IE & Mozilla) SMIMEApplet loading -- 05/11/2004 ROHIT POTNIS
var waitforapplet // Refers to the applet load status window in applet_init and applet_start
var timerid

function getPlatformParam() {
  var agt=navigator.userAgent.toLowerCase()
  var param = ""

  if (agt.indexOf('msie') != -1 && navigator.platform.search('Win32') != -1) {
    param = parent.cfgFrame.smimePlatformWin
  } else if (agt.indexOf('mozilla') != -1 || agt.indexOf('netscape') != -1) {
    if (navigator.platform.search('SunOS') != -1) {
      param = parent.cfgFrame.smimePlatformSun
    } else if (navigator.platform.search('MacPPC') != -1) {
      // Mac 68k not supported
      param = parent.cfgFrame.smimePlatformMac
    } else if (navigator.platform.search('linux') != -1) {
      param = parent.cfgFrame.smimePlatformLinux
    } else if (navigator.platform.search('Win32') != -1) {
      param = parent.cfgFrame.smimePlatformWin
    }
  } else {
    alert(i18n['unsupported platform']) // need to lookup from i18n
  }
//    alert(navigator.platform)
  return param
}

/*
  Initialize cross platform (IE & Mozilla) SMIMEApplet loading
*/
function setupAppletForBrowser()
{
  SMIMEApplet = document.applets[0]

  if (navigator.appName.indexOf("Netscape")>=0){
    SMIMEApplet = window.document.NS_SMIMEApplet
  }
  else if (navigator.appName.indexOf("Microsoft")>=0){
    SMIMEApplet = document.IE_SMIMEApplet
  }
}

function initCertificate()
{
  var signingCert
  signingCert = parent.getUserSignCertID()
  if(signingCert != '')
    certid = SMIMEApplet.setSignCertificate(signingCert, "")
}

function initializeApplet() {
  var smimeMailHost
  var platformParam
  var port = ''
  var res
  var errno, errstr
  var logging
  var maxPostSize, maxMsgSize
  var usingHTTPS = false

  setupAppletForBrowser()
//  smimeMailHost = parent.cfg['mailhost'] // used to talk directly to  SMTP
  smimeMailHost = window.location.hostname // Fix for bug 5072543
  platformParam = getPlatformParam()
  maxPostSize = parent.cfgFrame.maxpostsize
  maxMsgSize = parent.cfgFrame.maxmessagesize

//  alert("Platform: " + platformParam)
//  alert("parent.msgHREF: " + parent.msgHREF)

  tmp = parent.msgHREF.indexOf(':')
//  alert("parent.msgHREF: " + parent.msgHREF.substring(0,tmp))
  if(parent.msgHREF.substring(0,tmp) == "https") {
    smimeMailHost = "https://" + smimeMailHost
    usingHTTPS = true
  } else
    smimeMailHost = "http://" + smimeMailHost
//  alert("mailhost: " + smimeMailHost)

  tmp = parent.msgHREF.lastIndexOf(':') // at least one : is present
  if ((parent.msgHREF.substring(0,tmp) != "http")   &&
    (parent.msgHREF.substring(0,tmp) != "https")) {
    // derive port from parent.msgHREF
    port = parent.msgHREF.substring(tmp,parent.msgHREF.length) // includes ":"
    tmp = port.lastIndexOf('/')    // Trailing slash?
    if (tmp > 0) port = port.substring(0,tmp)
    //alert ('port='+port)
  }
//    port = ""

  if (port != '')
    smimeMailHost = smimeMailHost + port

  logging = false
  if (parent.cfgFrame.smimeLogging==true)     // set by appletlogging==0/1 in smime.conf
    logging = true // easy enable for all users
  if(!logging)
    if(parent.cfg['meSMIMEDebug'] == 'on')
      logging = true

  // set up the parameters here, most results not checked because they set a variable and can't fail
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_LOGGING, logging)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_WMAPSID, parent.sid)
  if (parent.cookie != '') {
    res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_COOKIE, parent.cookie)
  }
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_SENDMETHOD, "http")
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_THROW, "false")
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_ENCRYPT_SENDER, "true")
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_SENDURL,  smimeMailHost + "/" + parent.cfgFrame.smimeSendcmd)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_CRL_URL,  smimeMailHost + "/" + parent.cfgFrame.smimeCRLcmd)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_CERT_URL, smimeMailHost + "/" + parent.cfgFrame.smimeCertcmd)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_OCSP_URL, smimeMailHost + "/" + parent.cfgFrame.smimeOCSPcmd)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_CARD_HTMLLOGIN, "true")
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_MAX_POSTSIZE, maxPostSize)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_MAX_MSGSIZE, maxMsgSize)
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_CRLCHECK_OVER_SSL, parent.cfgFrame.smimeCRLcheckoverSSL)

  if (usingHTTPS == true) { // Fix to bug 5096224
    if (port=='') port = 443
  } else {
    // If you have no https, use the port from the server. This will work if there is no proxy;
    // if there is, then the configuration of the proxy must match to work right.
    port = parent.cfgFrame.smimeCRLSSLport
  }
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_WMAP_SSLPORT, port)

  // get the SSL root CA's
  if(parent.cfgFrame.smimeSSLRootCerts.length > 0) {
    var sslRootCACerts = ''
    sslRootCACerts = parent.cfgFrame.smimeSSLRootCerts[0]
    // loop executes 1 less time than number of elements
    for(i=1;i < parent.cfgFrame.smimeSSLRootCerts.length;++i)
      sslRootCACerts = sslRootCACerts + ';' + parent.cfgFrame.smimeSSLRootCerts[i]

    res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_SSL_ROOTCA_CERTS, sslRootCACerts)
    if(!res) {
      errno  = SMIMEApplet.getLastErrorNumber()
      errstr = getSMIMEErrorText(errno)
      if (errno!=0) {
        alert(errstr)
      } else {
        alert(i18n['SSL CA Root failed'])
      }
    }
  }
  // get the Trusted root CA's
  if(parent.cfgFrame.smimeTrustedCerts.length > 0) {
    var trustedRootCACerts = ''
    trustedRootCACerts = parent.cfgFrame.smimeTrustedCerts[0]
    // loop executes 1 less time than number of elements
    for(i=1;i < parent.cfgFrame.smimeTrustedCerts.length;++i)
      trustedRootCACerts = trustedRootCACerts + ';' + parent.cfgFrame.smimeTrustedCerts[i]

    res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_TRUSTED_ROOTCA_CERTS, trustedRootCACerts)
    if(!res) {
      errno  = SMIMEApplet.getLastErrorNumber()
      errstr = getSMIMEErrorText(errno)
      if (errno!=0) {
        alert(errstr)
      } else {
        alert(i18n['trusted Root failed'])
      }
    }
  }

  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_TIMESTAMP, parent.cfgFrame.smimeTimeStampDelta)

  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_LOGIN_PROMPT, i18n['login_prompt'])
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_LOGIN_ACCEPT, i18n['login_accept'])
  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_LOGIN_CANCEL, i18n['login_cancel'])
  setupAppletUIText()

  res = SMIMEApplet.setAppletParameter(SMIMEApplet.PARAM_FINAL_INIT, "MUST BE LAST SET PARAMETER CALL")

  if (platformParam=="") {
    alert(i18n['Applet config failed'])
  } else {
    res = SMIMEApplet.setTokenConfiguration(platformParam)
    if (res) {
      initCertificate()
    } else {
      errno  = SMIMEApplet.getLastErrorNumber()
      errstr = getSMIMEErrorText(errno)
      if (errno!=0) {
        alert(errstr)
      } else {
        alert(i18n['Applet config failed'])
      }
    }
  }
}

function setupAppletUIText(){
  /* Localization variables for File chooser to be set by Javascript */
  SMIMEApplet.setUIParam(SMIMEApplet.FILE_CHOOSER_HEADING, smimei18n['FILE_CHOOSER_HEADING'])
  SMIMEApplet.setUIParam(SMIMEApplet.FILE_CHOOSER_ERROR, smimei18n['FILE_CHOOSER_ERROR'])
  SMIMEApplet.setUIParam(SMIMEApplet.WARN_DUP_FILE_OVERWRITE, smimei18n['WARN_DUP_FILE_OVERWRITE'])
  SMIMEApplet.setUIParam(SMIMEApplet.OK_OPTION , smimei18n['OK_OPTION'])
  SMIMEApplet.setUIParam(SMIMEApplet.OVERWRITE_PERMISSION_DENIED, smimei18n['OVERWRITE_PERMISSION_DENIED'])
  SMIMEApplet.setUIParam(SMIMEApplet.OVERWRITE_WARNING_TITLE, smimei18n['OVERWRITE_WARNING_TITLE'])
  SMIMEApplet.setUIParam(SMIMEApplet.SAVE_ATTACH_MESSAGE, smimei18n['SAVE_ATTACH_MESSAGE'])
  SMIMEApplet.setUIParam(SMIMEApplet.FILE_HASHCODE_NONEXISTENT, smimei18n['FILE_HASHCODE_NONEXISTENT'])
  SMIMEApplet.setUIParam(SMIMEApplet.ATTACHMENT_FAILURE, smimei18n['ATTACHMENT_FAILURE'])
  SMIMEApplet.setUIParam(SMIMEApplet.LOOK_IN_LABEL_TEXT, smimei18n['LOOK_IN_LABEL_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.FILE_TYPE_LABEL_TEXT, smimei18n['FILE_TYPE_LABEL_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.UP_FOLDER_TOOLTIP, smimei18n['UP_FOLDER_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.HELP_BUTTON_TOOLTIP, smimei18n['HELP_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.UPDATE_BUTTON_TOOLTIP, smimei18n['UPDATE_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.CANCEL_BUTTON_TOOLTIP, smimei18n['CANCEL_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.OPEN_BUTTON_TOOLTIP, smimei18n['OPEN_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.SAVE_BUTTON_TOOLTIP, smimei18n['SAVE_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.HELP_BUTTON_TEXT, smimei18n['HELP_BUTTON_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.UPDATE_BUTTON_TEXT, smimei18n['UPDATE_BUTTON_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.CANCEL_BUTTON_TEXT, smimei18n['CANCEL_BUTTON_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.OPEN_BUTTON_TEXT, smimei18n['OPEN_BUTTON_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.SAVE_BUTTON_TEXT, smimei18n['SAVE_BUTTON_TEXT'])
  SMIMEApplet.setUIParam(SMIMEApplet.DETAILS_VIEW_BUTTON_TOOLTIP, smimei18n['DETAILS_VIEW_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.LIST_VIEW_BUTTON_TOOLTIP, smimei18n['LIST_VIEW_BUTTON_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.NEW_FOLDER_TOOLTIP, smimei18n['NEW_FOLDER_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.HOME_FOLDER_TOOLTIP, smimei18n['HOME_FOLDER_TOOLTIP'])
  SMIMEApplet.setUIParam(SMIMEApplet.FILENAME_LABEL_TEXT, smimei18n['FILENAME_LABEL_TEXT'])
  /* -------------------------------------------------------- */
}

function showAppletLoadStatus() {
//position the window over parent window
  var cw = 400
  var ch = 100
  var x = 0, y = 0
  if (IE){
    x = (window.screenLeft + document.body.offsetWidth-cw)/2
    y = (window.screenTop +  document.body.offsetHeight-ch)/2
  }
  else if (NN){
    x = (window.screenX + window.innerWidth-cw)/2
    y = (window.screenY + window.innerHeight-ch)/2
  }
  var strhtml = "javascript:document.write('<html><font color=\"#ff0000\"><b>" +
                i18n['wait_for_applet']+"<//b><//font><//html>')"

  strhtml = "javascript:document.write('<html><font color=red><b>" +
  i18n['wait_for_applet']+ "<//b><//font><//html>')"
  waitforapplet = window.open(strhtml,"statwin","status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=100")
  return false
}

function hideAppletLoadStatus(){
  waitforapplet.close()
  clearTimeout(timerid)
}

function applet_init() {
  window.setTimeout("initializeApplet()",0)//Bugfix 5070672
  parent.applet_init=true
//  alert('Applet Initialized')
}

function applet_wrongvm() {
  alert('Wrong VM')
}

function applet_start() {
//  alert('Applet Started')
  parent.cfgCB_2();
}

function applet_stop() {
//  alert('Applet Stopped')
}

function applet_destroy() {
  parent.applet_destroy=true
//  alert('Applet Destroyed')
}


//
//Get password for a Crypto card
//
//called by the applet if SMIMEApplet.PARAM_CARD_HTMLLOGIN == true
//
function applet_reportCardLogin(sCard, sPrompt){
  var result = ""
//  result = prompt(sCard + " : " + sPrompt, result)
  result = prompt(sPrompt, result)
  return result
}

//
//Called by the applet when a crypto card status changes
//
function applet_reportTokenStatus(sBool, sMessage){
  alert(sMessage)
}

//
//Called by the applet if a crypto card encounters a problem
//
function applet_reportProviderError(sProv, sMessage){
  alert(sProv + " : " + sMessage)
}

//
//Fills select (combo box) with a list of certificates
//
function getCerts(){
  var strCerts = SMIMEApplet.getSignCertificates(parent.cfg['mail'])
  var arrCerts = strCerts.split(";")
  for(idxCert =0; idxCert<arrCerts.length; idxCert++ ){
    //selectcertform.CertSelect.options[idxCert]=new Option(arrCerts[idxCert], idxCert, (idxCert==0), false )
  }
}

