var main, i18n, errnumi18n, errmsgi18n, smimei18n, IE, NN, NSCP, appVersion, nbsp, crlf, security, mac, win ,sun, SMIME

function getSMIMEErrorText(errnum){
  return errmsgi18n[eval("'"+errnum+"'")]
}

function initSMIME(){
  SMIME = new Array()
  
  if (main.cfgFrame!=null)
    SMIME.enable=main.cfgFrame.smimeEnabled==true
  else
    SMIME.enable=false

  if (SMIME.enable==false)
    return

  if(!main.SMIMECapableBrowser()) {
    SMIME.enable=false
    return
  }

  if(main.applet_init!=true)
    SMIME.enable=false
}

function init(path, title) { 
  var s = navigator.userAgent
  IE = NN = NSCP = 0
  appVersion = parseFloat(navigator.appVersion)
  if (s == '')
    IE = 3
  else if (s.indexOf('MSIE') != -1)
    IE = appVersion = parseFloat(s.substring(s.indexOf('MSIE') + 5, s.length))
  else if (s.indexOf('Opera') != -1)
    IE = appVersion
  else if (s.indexOf('Mozilla') != -1)
    NN = appVersion
  var t = s.match(/(Netscape\d?\/)(\S*)/)
  if (t && t[2])
    NSCP = parseFloat(t[2])
  mac = s.indexOf('Mac') != -1
  s = s.toLowerCase()
  win = s.indexOf('win') != -1 || s.indexOf('16bit') != -1
  sun = s.indexOf('sun') != -1
  if (win && NN < 5)
    crlf = '\r\n'
  else if (mac)
    crlf = '\r'
  else
    crlf = '\n'
  if (!mac && handleError)
    window.onerror = handleError
   
  if(NN >= 4.0 && NN < 5 && handleResize)
  {
    window.onresize = handleResize
  }
  
  security = false
  if (NN) document.domain = document.domain
  main = eval(path)
  initSMIME()
  i18n = main.i18n
  smimei18n = main.smimei18n
  errnumi18n = main.errnumi18n
  errmsgi18n = main.errmsgi18n
  if (i18n['dir'])
    document.dir = i18n['dir']  

  nbsp = i18n['nbsp']
  if (!nbsp)
    nbsp = '&nbsp'
  if (title)
    document.write('<title>' + eval(title) + '</title>')
}

// smime routine
function getAppletBaseHREF()
{
  var href = ""

  if (navigator.platform.search('Win32') != -1)
    href = 'applet_fs_win.html'
  else if (navigator.platform.search('SunOS') != -1) {
    // not fully implemented, need to determine ISA
    href = 'applet_fs_sunsparc.html'
  } else if (navigator.platform.search('MacPPC') != -1)
    // Mac 68k not supported
    href = 'applet_fs_mac.html'
  else if (navigator.platform.search('Linux') != -1)
    // only x86 supported for linux client
    href = 'applet_fs_linx86.html'
  else
    alert(i18n['unsupported platform']) // need to lookup from i18n

  return href
}

function handleError(msg, url, line) {
  alert(i18n['js error'] + '\n\n' + url + ', ' +
    i18n['line'] + ' ' + line + '\n\n' + msg)
 
  return true
}

function handleResize()
{
  if(NN >= 4.0 && NN < 5) // Double check -- function might be called from other resize handlers
  {
    for(i=0; i<window.frames.length; i++)
      reload(window.frames[i]) 
  }
}

function size(n, type) {
  var i, s, ss

  n = parseInt(n)
  if (n == 0) {
    return '0K'
  } else if (!type || type == 'b') {
    if (n >= 1024 * 1024) {
      n /= 1024.0 * 1024
      ss = 'MB'
    } else {
      n /= 1024.0
      if (n < 1)
        n = 1
      ss = 'K'
    }
  } else if (type == 'k') {
    if (n >= 1024) {
      n /=  1024.0
      ss = 'MB'
    } else {
      ss = 'K'
    }
  } else {
    ss = 'MB'
  }
  if (n < 10)
    s = (n + .0499) + ''
  else
    s = (n + .499) + ''
  i = s.indexOf('.')
  if (i == -1)
    return n + ss
  else if (i == 1 && ss == 'MB')
    return s.substring(0, 3) + ss
  else
    return s.substring(0, i) + ss
}

function html(str) {
  var c, i, s = ''

  if (str == null)
    return s
  for (i = 0; i < str.length; i++ ) {
    c = str.charAt(i)
    if (c == ' ' && str.charAt(i+1) == ' ') // Changes for 1.14 in Tip
      s += '&nbsp;'
    else if (c == '"')
      s += '&quot;'
    else if (c == '<')
      s += '&lt;'
    else if (c == '>')
      s += '&gt;'
    else if (c == '&')
      s += '&amp;'
    else
      s += c
  }
  return s
}

function encode(str) {
  var c, i, s = ''
  if (NN >= 5 && navigator.userAgent.indexOf('Safari')==-1)
    str = encodeURIComponent(str)
  else
    str = escape(str, 1)

  for (i = 0; i < str.length; i++) {
    c = str.charAt(i)
    if (c == '+')
      s += '%2B'
    else if (c == '&')
      s += '%26'
    else
      s += c
  }
  return s 
}

function split(s, d) {
  var a = new Array()
  var i, j

  if (d == '') {
    for (i = 0; i < s.length; i++)
      a[a.length] = s.charAt(i)
  } else if (s == '') {
    a[0] = s
  } else {
    for (i = 0; i < s.length; i = j + d.length) {
      j = s.indexOf(d, i)
      if (j == -1)
        j = s.length
      a[a.length] = s.substring(i, j)
    }
  }
  return a 
}

function join(a, d) {
  var i, s = ''

  if( typeof(a) == 'string' && IE > 3)
  {
    if (a.length) {
      s = a.charAt(0)
      for (i = 1; i < a.length; i++)
        s += d + a.charAt(i)
    }
  }
  else
  {
    if (a.length) {
      s = a[0]
      for (i = 1; i < a.length; i++)
        s += d + a[i]
    }
  }
  return s
}

function gethref(s) {
  if (main.laurel) {
    var i = s.lastIndexOf('.html')
    if (i != -1) s = s.substring(0, i) + '_lr' + s.substring(i)
  }
  return s
}

function load(f, href, reload) {
  href +=  (href.indexOf('?')==-1 ? '?' : '') + 
           (href.indexOf(main.clientargs())==-1 ? main.clientargs() : '')
 
  if(href.indexOf('cfg.msc') > -1)
     href += "&sid=";
  
  if (!reload) href = gethref(href)
  if (NN > 0 || (IE >= 4 && !mac))
    f.location.replace(href)
  else
    f.location.href = href
}

function reload(f) {
  load(f, main.srcHREF+'frame.html', true)
}

function compressArray(l) {
  var i, a = new Array()
  
  for (i = 0; i < l.length; i++) {
    if ((l[i] != null && l[i] != '') || l[i] == '0')
      a[a.length] = l[i]
  }
  return a
}

function orphaned() {
  a = split(window.location.search.substring(1, window.location.search.length), '&')
  argv = new Array(a.length)
  for (i = 0; i < argv.length; i++) {
  j = a[i].indexOf('=')
  if (j == -1)
    argv[a[i]] = true
  else
    argv[a[i].substring(0, j)] = a[i].substring(j + 1, a[i].length)
}

  if(argv['view'] != 'compose')
    return ((IE >= 4 || NN > 2) && window.opener && window.opener.closed) || main.sortno == null
  else
    return ((IE >= 4 || NN > 2) && parent.closed) || main.sortno == null
}

function hint(s) {
  window.status = (s ? s : '')
  return true
}

function adjust() {

  if (IE >= 4) {
    var i, a = split(document.body.rows, ',')

    for (i = 0; i < a.length; i++) {
      if (a[i] != '*' && a[i] != '0')
        a[i] = frames[i].document.body.scrollHeight
    }
    document.body.rows = join(a, ',')
  }
}

// window handling funcs
function Window(url, name, args) {
  this.url = gethref(url)
  this.name = name
  this.args = args
  this.close = main.__close
  this.isClosed = main.__isClosed

  var r = open(this.url, name, args)
  return r
}

function isasciistring(s) {
  for (var i = 0; i < s.length; i++) {
    if (s.charAt(i) > '~')
      return false
  }
  return true
}

function isvalidemailaddress(s) {
  if (s.length == 0)
    return true
  if (!isasciistring(s))
    return false
  if (s.search('@') == -1)
    return false
  return true
}

function getArg(arg, s, nocase, init) {
  var i
  s = s.substring(s.indexOf('?') + 1)
  var a = split(s, '&')
  arg = encode(arg)
  for (i = 0; i < a.length; i++) {
    j = a[i].indexOf('=')
    if (j != -1 && (a[i].substring(0, j) == arg ||
         (nocase && a[i].substring(0, j).toLowerCase() == arg.toLowerCase())))
      return unescape(a[i].substring(j+1, a[i].length))
  }
  if (init)
    return ''
  else
    return null
}

// Some native objects (Windows) can enter infinite loops when being cloned.
// That's bad.
// &! We need a way to guarantee that recursion in cloneObject is not infinite.
function dontClone(o) {
  if (!o)
    return true
  if (NN > 0)
    return o.toString && o.toString() == '[object Window]'
  else
    return (o['clientInformation'] != null)
  return false
}

// IE doesn't return 'function' when using the typeof operator so we have to hack it.
function isFunction(o) {
  if (!o)
    return false
  if (NN > 0) {
    return typeof o == 'function'
  }
  if (typeof o != 'object')
    return false
  var tmp = o.toString()
  return tmp.indexOf('function') != -1
}

function isArray(o) {
  if (!o || typeof o != 'object')
    return false
  if (o.length)
    return true
  else {
    // arrays of length 0 do not have a zeroth element, but other objects might.
    for (p in o)
      return false
    var a = new Array()
    return o.toString() == a.toString()
  }
}

function cloneObject(o) {
  if (!o) return null
  var i, p, no = new Object()
  for (p in o) {
    // alert(p + ' = ' + o[p])
    if (typeof o[p] == 'object' && !isFunction(o[p])) {
      if (!isArray(o[p])) {
        // alert(p + ' = ' + o[p] + ' (' + typeof o[p] + ').\n')
        // clone object here.
        if (!dontClone(o[p]))
          no[p] = cloneObject(o[p])
        else
          no[p] = o[p]
      } else {
        // clone array object.
        no[p] = new Array()
        for (i = 0; i < o[p].length; i++) {
          if (typeof o[p][i] == 'object')
            no[p][i] = cloneObject(o[p][i])
          else
            no[p][i] = o[p][i]
        }
      }
    } else
      no[p] = o[p]
  }
  
  // for..in seems to skip toString in Transfer object, possibly elsewhere.
  if (o && o['toString'])
    no['toString'] = o['toString']
  return no
}

function isWS(c) {
  return c == ' ' || c == '\b' || c == '\f' || c == '\n' || c == '\r' || c == '\t'
}

function trim(s) {
  s += ''; // make sure that s is a string.
  s = split(s, '')
  for (var i = 0; i < s.length; i++) {
    if (isWS(s[i]))
      s[i] = ''
    else
      break
  }
  for (var i = s.length - 1; i > -1; i--) {
    if (isWS(s[i]))
      s[i] = ''
    else
      break
  }
  return join(compressArray(s), '')
}

function adjustAddressFrame() {
  if (IE >= 4) {
    var i, a = split(document.body.rows, ',')

    for (i = 0; i < a.length; i++) {
      if (a[i] != '*' && a[i] != '0')
        a[i] = frames[i].document.body.scrollHeight
    }
    document.body.rows = join(a, ',')
  }
  if(NN>2)
  {
    if(main.isRefreshing)
    {
      main.start()
    }
  }
}
// Comment Added by Webmail Team India to check check-in from Bangalore

function hasGC(s) {
  return s.indexOf(',') != -1 ||
    s.indexOf(';') != -1 ||
    s.indexOf(':') != -1 ||
    s.indexOf('<') != -1 ||
    s.indexOf('>') != -1 ||
    s.indexOf('@') != -1 ||
    s.indexOf('(') != -1 ||
    s.indexOf(')') != -1
}

function encCn(n,g) {
  n = split(n, '')
  for (var i = 0; i < n.length; i++) {
    if ( !g && (IE > 0 || NN >= 5) && (n[i] == '&' || n[i] == '#'))
      n[i] = encode(n[i])
    if (n[i] == '"')
      n[i] = ''
  }
  return join(n, '')
}

function s_EmailEntry(n, m, g) {
  if (!m)
    return nbsp
  var s
  n = encCn(main.unescape_crlf(n),g)
  n = (!g && (IE > 0 || NN >= 5)) ? main.i18n_squote(n) : n
  var m1 = (!g && (IE > 0 || NN >= 5)) ? main.i18n_squote(m) : m
  n = hasGC(n) ? '\"' + n + '\"' : n
  if (g) 
    s = encode(n + ' <' + m + '>')
  else
    s = '<A href="javascript:main.compose(\'new\', \'t=' + encode(n + ' <' + split(m1,'; ')[0] + '>') + '\')">' + main.unescape_crlf(m) + '</A>'
  return s
}

function s_EmailGroup(n, un,g) {
  var i , u = main.pabFrame.people, s = ''
  for (i = 0; i < u.length; i++) {
    if (u[i].mail && u[i].mail[0] && indexOfGroup(u[i], un) > -1)
      s += (s ? ',' : '') + s_EmailEntry(u[i].cn, u[i].mail[0], true)
  }
  if(!g) return '<A href="javascript:main.compose(\'new\',\'p=' + un + '\')">Email ' + n + '</A>'
  return s
}

function displayName(filename)
{
  var f, ff
  
  ff = filename
  if (navigator.userAgent.toLowerCase().indexOf('win') != -1 ||
        navigator.userAgent.toLowerCase().indexOf('16bit') != -1) {
    f = filename
    if (f.lastIndexOf('\\') == -1) {
      if (f.indexOf(':') != -1) {
        ff  = f.substring(f.lastIndexOf(':') + 1)
      }
    } else {
      ff = f.substring(f.lastIndexOf('\\') + 1)
    }
  }
  return ff
}

