if (typeof(getEle) != 'function') {
  if (document.getElementById) { 
    getEle = function(name) {
      return document.getElementById(name);
    };
  } else if (document.all) {
    getEle = function(name) {
      return document.all[name];
    };
  } else if (document.layers) {
    getEle = function(name) {
      return document.layers[name];
    };
  }
}

var m_kept_alive = (new Date()).getTime();

function m_keep_alive(e) {
  var now = (new Date()).getTime();

  if (now - m_kept_alive > 120000) {
    m_kept_alive = now;

    var img = getEle('tdcossofix');

    if (img) {
      var src = img.src;
      src = src.replace(/\?.*$/, '');
      src += '?' + now;
      img.src = src;
    }
  }
}

document.onclick=m_keep_alive;
document.onkeydown=m_keep_alive;
if (document.layers) document.captureEvents(Event.KEYDOWN);

function m_table_even_odd(id) {
  evenOddTableRows(id, 'even', 'odd');
}

var shim;

function shimmy(aState, aDiv)
{
  if (is.IE) {
    if (!shim) {
      shim = document.createElement('iframe');
      shim.id = 'o-mail-shim';
      shim.scrolling = 'no';
      shim.frameborder= '1';
      shim.style.position = 'absolute';
      shim.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=0)';
    }
    if (shim) {
      if (aState) {
	if (aDiv) {
	  shim.style.width = aDiv.offsetWidth;
	  shim.style.height = aDiv.offsetHeight;
	  shim.style.top = aDiv.offsetTop;
	  shim.style.left = aDiv.offsetLeft;
	  shim.style.zIndex = aDiv.style.zIndex - 1;
	  aDiv.parentNode.insertBefore(shim, aDiv);
	}
      } else {
	var parent = shim.parentNode;
	if (parent) {
	  parent.removeChild(shim);
	}
      }
    }
  }
}

function initIEmButtonMenu(id) {
  if (document.all&&document.getElementById) {
    var mouseoverfunc = function() {
      this.className+=" over";
      this.onmouseout = mouseoutfunc;
      this.onmouseover = null;
      var dropdown = this.getElementsByTagName("ul")[0];
      if (dropdown) {
	shimmy(true, dropdown);
	this.shimmied = true;
      }
    };
    var mouseoutfunc = function() {
      this.className=this.className.replace(RegExp('\\bover\\b', 'g'), "");
      this.onmouseover = mouseoverfunc;
      this.onmouseout = null;
      if (this.shimmied) {
	shimmy(false);
	this.shimmied = false;
      }
    };
    var navRoot = document.getElementById(id);
    for (var i=0; i<navRoot.childNodes.length; i++) {
      var node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=mouseoverfunc;
      }
    }
  }
}

function cloneMenuBar() {
  var actionbar, marker;
  if ((marker = getEle('m-bar-marker')) &&
      (actionbar = getEle('mTopMenuBar')) &&
      actionbar.parentNode == marker.parentNode) {
    var winHeight = 0;
    if (self.innerHeight) {
      // all except Explorer
      winHeight = window.innerHeight;
    } else if( document.documentElement &&
	       document.documentElement.clientHeight) {
      // Explorer 6 Strict Mode
      winHeight = document.documentElement.clientHeight;
    } else if (document.body) {
      // other Explorers
      winHeight = document.body.clientHeight;
    }
    
    if (winHeight < 100) {
      winHeight = 200;
    }
    
    if (marker.offsetTop - actionbar.offsetTop > winHeight) {
      var newbar = actionbar.cloneNode(true);
      if (newbar) {
	newbar.id = 'mBottomMenuBar';
	var ULs = newbar.getElementsByTagName('ul');
	for (var n = 0; n < ULs.length; n++) {
	  if (ULs[n].id == 'mButtonMenu') {
	    ULs[n].id = 'mButtonMenu2';
	    break;
	  }
	}
	marker.appendChild(newbar);
	marker.id = 'mBottomMenuBarContainer';
	initIEmButtonMenu("mButtonMenu2");
      }
    }
  }
}

function mShowSwitch(id1, id2) {
  var divid1 = getEle(id1);
  var divid2 = getEle(id2);
  if (divid1.style.display == 'none') {
    divid1.style.display = divid2.style.display;
    divid2.style.display = 'none';
  } else {
    divid2.style.display = divid1.style.display;
    divid1.style.display = 'none';
  }
}

function m_tablebg(id, bg) {
  var tid = getEle(id);
  if (tid) {
    if (bg)
      tid.className += " bg-greyfade-" + bg;
    else
      tid.className += " bg-greyfade-2";
    evenOddTableRows(id, 'ot-tr1', 'ot-tr2');
  }
}

function m_pagetitle(title) {
  if (typeof(title) == "string" && typeof(m_footpaths) == "object" && m_footpaths.length > 0) {
    for (var i = 0; i < m_footpaths.length; i++) {
      var m_footpath = m_footpaths[i];
      if (typeof(m_footpath) == "undefined") {
	break;
      }
      if (m_footpath[0].match(/^\(.*\)$/)) {
	m_footpath[0] = title;
	break;
      }
    }
    m_setfootpaths();
  }
}

{
  var m_page_hierarchy;

  m_get_page_hierarchy = function() {
    if (typeof(m_page_hierarchy) != 'undefined')
      return m_page_hierarchy;

    m_page_hierarchy = [];

    if (typeof(m_footpaths) == "object" && m_footpaths.length > 0) {
      for (var i = 0; i < m_footpaths.length; i++) {
	var m_footpath = m_footpaths[i];
	if (typeof(m_footpath) == "undefined") {
	  break;
	}
	m_page_hierarchy.push(typeof(m_footpath[4]) == "undefined" ? m_footpath[0] : m_footpath[4]);
      }
    } else
      m_page_hierarchy.push('(unknown)');

    return m_page_hierarchy;
  };
}

var m_omniture_cosName = '';

function m_omniture_setupvars(s, event) {
  if (event) {
    s.events = 'event10';
    s.eVar19 = s.prop19 = event;
  }
}

function m_omniture_page(cosName, mailplus) {
  if (typeof(s) == 'object' && typeof(s.t) == 'function') {
    m_omniture_cosName = cosName + (mailplus ? '/mailplus' : '');

    var event = getCookie('m_event');
    if (event) {
      delCookie('m_event');
      s.events = 'event10';
      s.eVar19 = s.prop19 = event;
    }
    
    s.eVar24 = s.prop24 = m_omniture_cosName;    

    if (typeof(ot_track) == "function")
      ot_track(m_get_page_hierarchy());
  }
}

function m_omniture_event(event) {
  if (event && typeof(s) == 'object' && typeof(s.tl) == 'function') {
    var old_event = getCookie('m_event');
    if (old_event) {
      var vars = {
	linkTrackVars: "eVar19,prop19,eVar24,prop24,events",
	linkTrackEvents: "event10"
      };

      vars.events = 'event10';
      vars.eVar19 = vars.prop19 = old_event;

      s.tl(true,'o','Webmail event', vars);
    }
    setCookie('m_event', event);
  }
}

var footpaths = [];
var m_setfootpaths_done;

function m_setfootpaths(pre_template) {
  if (m_setfootpaths_done)
    return;
  footpaths = [['Mail','folder.cgi?r=' + Math.random(),'Webmail','']];
  if (typeof(m_footpaths) == "object" && m_footpaths.length > 0) {
    for (var i = 0; i < m_footpaths.length; i++) {
      var m_footpath = m_footpaths[i];
      if (typeof(m_footpath) == "undefined") {
	break;
      }
      if (m_footpath[0].match(/^\(.*\)$/)) {
	// Have to wait and see if we get a m_pagetitle() from template
	if (pre_template)
	  return;
	break;
      }
      footpaths.push([m_footpath[0], m_footpath[1], m_footpath[2], m_footpath[3]]);

      if (m_footpath[1] == "")
	break;
    }
  }
  footpaths[footpaths.length-1][1] = "";

  var page_title = typeof(m_title_prefix) != "undefined" ? m_title_prefix : 'TDC Online';
  for (var i = 0; i < footpaths.length; i++) {
    page_title += ' - ' + footpaths[i][0];
  }

  document.title = page_title;

  if (typeof(footpath) == 'function') {
    footpath(footpaths);
    footpaths=[];
  }
  m_setfootpaths_done = 1;
}

function clearFetching(elem) {
  var fetching = elem;

  while ((fetching = fetching.previousSibling)) {
    if (fetching.className == "o-mail-fetching") {
      fetching.parentNode.removeChild(fetching);
      elem.style.visibility = 'visible';
      break;
    }
  }
}

function iframeHtmlLoaded(iframe) {
  clearFetching(iframe);
  mResizeIframeToContent(iframe);
}

function iframeLoaded(iframe) {
  iframeHtmlLoaded(iframe);
}

function imageLoaded(img) {
  cloneMenuBar();
}

function mResizeIframeToContent(iframeEl) {
  if (iframeEl) {
    var iframeWin = iframeEl.contentWindow;
    if (iframeWin) {
      var iframeBody = iframeWin.document.body;
      if (iframeEl.offsetWidth != iframeBody.scrollWidth)
	iframeEl.style.width = iframeBody.scrollWidth + 'px';

      var newHeight = iframeBody.scrollHeight;
      // Ugly hack. For "wide" mails in IE, scrollHeight seems to sometimes
      // not have "settled" yet, the first time we ask for it?!?!?
      newHeight = iframeBody.scrollHeight;

      var scrollbarHackFF = 0;
      if (iframeEl.clientHeight != iframeBody.clientHeight) {
	// Horizontal scrollbar will probably cause Firefox catch-22
	scrollbarHackFF = iframeEl.clientHeight - iframeBody.clientHeight;
      }

      if (iframeEl.offsetHeight != newHeight)
	iframeEl.style.height = newHeight + scrollbarHackFF + 'px';

      if (scrollbarHackFF != 0) {
	var dummy = iframeBody.clientHeight;
        iframeEl.style.height = newHeight + 'px';
      }
    } else {
      iframeEl.style.height = '500px';
    }
    cloneMenuBar();
  }
}

function mAddEventHandler(ele, event, handler) {
  if (ele.addEventListener) {
    ele.addEventListener(event, handler, false);
  }
  else if (ele.attachEvent) {
    ele.attachEvent('on'+event, handler);
  }
  else {
    var oldHandler = eval('ele.on'+event);
    var newHandler;
    if (oldHandler)
      newHandler = function() {
	oldHandler();
	newHandler();
      };
    else
      newHandler = handler;

    eval('ele.on'+event+'=newHandler');
  }
}

function move_darea_to_navigationextra() {
  var mail_darea = getEle('mail-darea');
  if (mail_darea) {
    var navigationextra = getEle('navigationextra');
    if (navigationextra) {
      mail_darea.parentNode.removeChild(mail_darea);
      navigationextra.appendChild(mail_darea);
    }
  }
}
