/*
Object.prototype.attachEvent = function (sEvent, fnHandler, bUseCapture) {
    this.addEventListener(sEvent.indexOf('on') == 0 ? sEvent.replace('on', '') : sEvent, fnHandler, bUseCapture);
}
*/

function attachEventOld(el, arg2, arg3, arg4) {
  if (el.addEventListener) {
    el.addEventListener(arg2.replace('on', ''), arg3, arg4);
  } else {
    el.attachEvent(arg2, arg3);
  }
}

calendar1 = false;
calendar2 = false;
// bugIE = ((parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf('MSIE') + 5,navigator.userAgent.indexOf('MSIE') + 6)) < 7) && navigator.userAgent.indexOf('Windows') > -1 && navigator.userAgent.indexOf('MSIE') && window.opera != true) ? true : false;
bugIE = navigator.userAgent.indexOf('MSIE')>-1 && navigator.userAgent.indexOf('Windows')>-1; 
// bugIE = true; 
// alert(bugIE)

var Menu =
{
  timeout: false,
  
  select: function(display)
  {
    display = (display) ? display : 'none';
    if (document.getElementById('innerSearch'))
    {
      selectFields = document.getElementsByTagName('select');
      if (typeof(selectFields) == 'object')
      {
        /*
        var i = 0;
        for ( i; i < selectFields.length; i++ )
        {
          selectFields[i].style.display = display;
        }
        */
  
        // if ( typeof(calendar1)!="undefined" && typeof(calendar2)!="undefined" ) {   if (calendar1 && calendar2) { clearT();calendar1.hide();calendar2.hide(); } }
      }
    }
  },

  clearTimeout: function()
  {
    clearTimeout(Menu.timeout)
  },

  setTimeout: function()
  {
    Menu.timeout = setTimeout(Menu.close,500);
  },

  init: function()
  {
    mObj = document.getElementById('menu');
    Menu.images();

    mLinks = mObj.getElementsByTagName('a');

    for (i = 0; i < mLinks.length; i++)
    {
      if (mLinks[i].parentNode.parentNode.id == 'menu')
      {
        mLinks[i].onmouseover = function()
        {
          obj = this;
          obj.blur();
          Menu.clearTimeout();
          if ( typeof(calendar1)!="undefined" && typeof(calendar2)!="undefined" ) {   if (calendar1 && calendar2) { clearT();calendar1.hide();calendar2.hide(); } }
          Menu.show(obj);
          // if ( bugIE ) Menu.select('none');
          Menu.select('none');
        }
      }
    }

    mObj.onmouseout = Menu.setTimeout;

    function setMenu(childs,services)
    {
      var i = 0;
      for (i; i < childs.length; i++)
      {
        if (childs[i].nodeType == 1 && childs[i].parentNode.parentNode.id != 'menu')
        {
          childs[i].onmouseover = Menu.clearTimeout;
        }

        if (childs[i].childNodes.length > 0)
        {
          setMenu(childs[i].childNodes);
        }
      }
    }

    setMenu(mObj.childNodes);

    Menu.close();
  },

  images: function()
  {
    Menu.imgs = mObj.getElementsByTagName('img');
    Menu.imgPath = Menu.imgs[0].src.substring(0, Menu.imgs[0].src.lastIndexOf('/')+1);
    Menu.imgNames = [];
    Menu.imgDefault = [];
    Menu.imgActive = [];
    Menu.imgHover = [];

    for (i = 0; i < Menu.imgs.length; i++ )
    {
      currentIMG = Menu.imgs[i];
      Menu.imgNames.push(Menu.imgName(currentIMG));
    }

    for (i = 0; i < Menu.imgNames.length; i++)
    {
      Menu.imgDefault[Menu.imgNames[i]] = new Image();
      Menu.imgDefault[Menu.imgNames[i]].src = Menu.imgPath+Menu.imgNames[i]+'.gif';

      Menu.imgHover[Menu.imgNames[i]] = new Image();
      Menu.imgHover[Menu.imgNames[i]].src = Menu.imgPath+Menu.imgNames[i]+'-hover.gif';

      Menu.imgActive[Menu.imgNames[i]] = new Image();
      Menu.imgActive[Menu.imgNames[i]].src = Menu.imgPath+Menu.imgNames[i]+'-a.gif';
    }
  },

  imgName: function(currentIMG)
  {
    currentIMGname = currentIMG.src.substring(currentIMG.src.lastIndexOf('/')+1,currentIMG.src.lastIndexOf('.gif'));
    if ( currentIMG.src.indexOf('-hover.gif') > -1 ) currentIMGname = currentIMG.src.substring(currentIMG.src.lastIndexOf('/')+1,currentIMG.src.lastIndexOf('-hover.gif'));
    if ( currentIMG.src.indexOf('-a.gif') > -1 ) currentIMGname = currentIMG.src.substring(currentIMG.src.lastIndexOf('/')+1,currentIMG.src.lastIndexOf('-a.gif'));
    return currentIMGname;
  },


  show: function(t)
  {
    subMenus = t.parentNode.parentNode.getElementsByTagName('li');
    currentLI = t.parentNode;
    currentMenu = t.parentNode.getElementsByTagName('ul')[0];

    Menu.close();

    if ( currentMenu )
    {
      currentIMG = currentLI.getElementsByTagName('img')[0];
      Menu.imgName(currentIMG);
      currentIMG.src = Menu.imgHover[currentIMGname].src;
      currentLI.className = 'selected';
      currentMenu.style.width = parseInt(currentIMG.width + 37) + 'px';
      if (typeof(parent.bugIE)!="undefined" && typeof(parent.searchSelect)!="undefined") {
        if (parent.bugIE && parent.searchSelect) {
          var curUL = currentLI.getElementsByTagName("UL")[0];
          curUL.myX = curUL.offsetLeft;
          curUL.myY = curUL.offsetTop;
          var el = curUL;
          while(el.offsetParent, el=el.offsetParent) {
            curUL.myX += el.offsetLeft;
            curUL.myY += el.offsetTop;
          }
          var x1 = curUL.myX;
          var x2 = x1 + curUL.offsetWidth;
          var x3 = searchSelect.myX;
          var x4 = searchSelect.myX + searchSelect.offsetWidth;
          var cross1 = x3>x1 && x3<x2;
          var cross2 = x4>x1 && x4<x2;
          var cross3 = x1<x3 && x2>x4;
          // alert(x1 +', '+ x2 +', '+ x3 +', '+ x4 +'\n'+ cross1 +', '+ cross2 +', '+ cross3);
          var cross = (cross1 || cross2 || cross3) && curUL.myY+curUL.offsetHeight>searchSelect.myY;
          if (!cross3) searchSelect.style.visibility = 'visible';
          if (!cross) {
            searchSelectParent.style.left = "0px";
            searchSelect.style.marginLeft = "0px";
            searchSelectParent.style.width = "174px";
          } else if (cross1) {
            var dx = x2 - x3;
            searchSelectParent.style.left = dx + "px";
            searchSelect.style.marginLeft = -1*dx + "px";
          } else if (cross2) {
            var dx = x4 - x1;
            dx += 1;
            searchSelectParent.style.left = "0px";
            searchSelect.style.marginLeft = "0px";
            searchSelectParent.style.width = 174-dx +"px";
          } else if (cross3) {
            searchSelect.style.visibility = 'hidden';
          }
        }
      }
    }
  },

  close: function()
  {
    // if ( bugIE ) Menu.select('inline');
    Menu.select('inline');
    menus = mObj.getElementsByTagName('li');
    for (i = 0; i < menus.length; i++)
    {
      if (menus[i].parentNode.id == 'menu')
      {
        currentIMG = menus[i].getElementsByTagName('img')[0];
        Menu.imgName(currentIMG);
        if ( currentIMG.parentNode.className == 'active' ) currentIMG.src = Menu.imgActive[currentIMGname].src;
        else currentIMG.src = Menu.imgDefault[currentIMGname].src;
        menus[i].className = '';
      }
    }
    if (bugIE && typeof(searchSelect)!="undefined") {
      searchSelectParent.style.left = "0px";
      searchSelect.style.marginLeft = "0px";
      searchSelectParent.style.width = "174px";
    }
  }
}

function initCollapseNewsLine(id, sectionTag, sectionClass, subSectionTag, headerTag, headerApplyClass, contentApplyClass) {
  var i, i2, sec, sec2, p, h, hCopy;
  if (!sectionTag) sectionTag = "div";
  if (!sectionClass) sectionClass = "docsItem";
  if (!subSectionTag) subSectionTag = "p";
  if (!headerTag) headerTag = "strong";
  if (!headerApplyClass) headerApplyClass = "newsLineCollapsedHeader";
  if (!contentApplyClass) contentApplyClass = "newsLineCollapsedContent"
  var line = document.getElementById(id);
  var divs = line.getElementsByTagName(sectionTag);
  var sections = new Array();
  i = -1;
  while(divs[++i]) {
    if (!divs[i].className || divs[i].className.indexOf(sectionClass)==-1) continue;
    sections[sections.length] = {
      el: divs[i],
      paragraphs: divs[i].getElementsByTagName(subSectionTag),
      headers: []
    }
  }
  i = -1;
  while(sections[++i]) {
    sec = sections[i];
    i2 = -1;
    while(sec.paragraphs[++i2]) {
      p = sec.paragraphs[i2];
      h = p.getElementsByTagName(headerTag)[0];
      if (!h) continue;
      hCopy = document.createElement("div");
      hCopy.className = headerApplyClass;
      hCopy.appendChild(h.cloneNode(true));
      p.parentNode.insertBefore(hCopy, p);
      p.style.display = i==0 ? "block" : "none";
      p.className = contentApplyClass;
      h.parentNode.removeChild(h);
      sec.headers[i2] = hCopy;
      sec.headers[i2].onclick = function() {
        var el = this;
        while(el.nextSibling, el=el.nextSibling, el.nodeType!=1) { }
        if (!el.style.display || el.style.display=="block") {
          el.style.display = "none";
        } else {
          el.style.display = "block";
        }
      }
      sec.headers[i2].onmouseover = function() {
        var strong = this.getElementsByTagName("strong")[0];
        strong.className = "hover";
      }
      sec.headers[i2].onmouseout = function() {
        var strong = this.getElementsByTagName("strong")[0];
        strong.className = "";
      }
    }
  }
  i= i2= sec= sec2= p= h= hCopy= line= divs= sections= null;
}

function newsPeriodSubmit(form, separator) {
  var from = form.elements[0];
  var to = form.elements[1];
  if (!separator) separator = "/";
  if (chDateFormat(from.value) && chDateFormat(to.value)) {
    from.value = from.value.replace(/\./g, separator);
    to.value = to.value.replace(/\./g, separator);
    return true;
  } else {
    if ( !chDateFormat(from.value) ) {
      from.style.color = "red";
      from.onkeydown = from.onmouseup = function() { this.style.color = "black"; }
    } else {
      to.style.color = "red";
      to.onkeydown = to.onmouseup = function() { this.style.color = "black"; }
    }
    return false;
  }  
  function chDateFormat(str) {
    var d = str.substring(0,2);
    var m = str.substring(3,5);
    var y = str.substring(6,10);
    return typeof(parseInt(d))=="number" && typeof(parseInt(m))=="number" && typeof(parseInt(y))=="number";
  }
}
