/* hafas_utilities.js */
/* built: 16.05.2006 by CAG */
/* added: 21.02.2007 by MHEI */
/*        Date object enhancmends */


// Get absolute x-positon of any element
function getPosX(element){
  var curleft = 0;
  if (element.offsetParent){
     while (element.offsetParent){
        curleft += element.offsetLeft
        element = element.offsetParent;
     }
  }
  else if (element.x) curleft += element.x;
  return curleft;
}

// Get absolute y-position of any element
function getPosY(element){
  var curtop = 0;
  if (element.offsetParent) {
     while (element.offsetParent){
        curtop += element.offsetTop
        element = element.offsetParent;
     }
  }
  else if (element.y) curtop += element.y;
  return curtop;
}

// IE6 FIX : only focus an element which is not hidden or in a hidden container
function safeFocus(element) {
  var parent = element.parentNode;
  var bFocus = true;
  // while parent node exists and isn't body or html tag
  while (parent) {
    if ((parent.tagName == "BODY") || (parent.tagName == "HTML")) {
      break;
    }
    // The parent node is hiddden this so this element is no good.
    if ((parent.style.display == "none") || (parent.style.visibility == "hidden")) {
      bFocus = false;
      break;
    }
    // climb up DOM tree
    parent = parent.parentNode;
  }
  if (bFocus) {
    element.focus();
  }
}


// Date object enhancements:
Date.prototype.getDaysInMonth = function() {
   return new Date(this.getFullYear(),this.getMonth()+1,1,-1).getDate();
}

Date.prototype.getWeek = function() {
  var d=new Date(this);
  var DoW=d.getDay();
  d.setDate(d.getDate()-(DoW+6)%7+3); // Nearest Thu
  var ms=d.valueOf(); // GMT
  d.setMonth(0);
  d.setDate(4); // Thu in Week 1
  return Math.round((ms-d.valueOf())/(7*864e5))+1;
}

/* built 2007-01-05  mib A&F           */
/* Show Mouseover in 'Teilsuche' links */

/* Tooltip for partsearch arrows including mouse over for ps icon */
var hafasTTip = {
   i:null,
   t:null,

   show: function (id, name){
        a=document.getElementById(id);
        if(a == null)
           return;
        i=document.getElementById('i'+id);
        if(i == null)
           return;
        t=document.getElementById('HFSTooltip');
        if(t == null)
           return;
        x=a.offsetLeft;
        y=a.offsetTop;
        var tmp = a;
        a = a.offsetParent;
        while(((tmp = tmp.parentNode) != null) && (tmp.id != "HFS")){
           if(tmp == a)
             {
             x += a.offsetLeft;
             y += a.offsetTop;
             a = a.offsetParent;
             }
           }
        t.innerHTML = name ;
        t.style.top = y+28+"px";
        t.style.left = x+5+"px";
        t.style.display = 'block';
        if(i)hafasTTip.cImg(1);
        },

   hide: function (id){
        t.style.display = 'none';
        if(i)hafasTTip.cImg(0);
        },

   cImg: function (stat){/* image mouse over */
        if(stat == 1)
           i.src = i.src.substring(0,i.src.indexOf('.png'))+'_o.png';
        else
           i.src = i.src.substring(0,i.src.indexOf('_o.png'))+'.png';
        }
   }


function decodeEntities( at ){
  var t = at.replace(/&ouml;/g, 'ö');
  t = t.replace(/&auml;/g, 'ä');
  t = t.replace(/&uuml;/g, 'ü');
  t = t.replace(/&Auml;/g, 'Ä');
  t = t.replace(/&Uuml;/g, 'Ü');
  t = t.replace(/&Ouml;/g, 'Ö');
  t = t.replace(/&#252;/g, 'ü');
  t = t.replace(/&#246;/g, 'ö');
  t = t.replace(/&#228;/g, 'ä');
  t = t.replace(/&#226;/g, 'â');
  t = t.replace(/&#223;/g, 'ß');
  t = t.replace(/&#196;/g, 'Ä');
  t = t.replace(/&#214;/g, 'Ö');
  t = t.replace(/&#220;/g, 'Ü');
  t = t.replace(/&#248;/g, 'ø');
  t = t.replace(/&#216;/g, 'Ø');
  t = t.replace(/&#229;/g, 'å');
  t = t.replace(/&#197;/g, 'Å');
  t = t.replace(/&#230;/g, 'æ');
  t = t.replace(/&#198;/g, 'Æ');
  t = t.replace(/&#233;/g, 'é');
  t = t.replace(/&#232;/g, 'è');
  t = t.replace(/&#156;/g, 'œ');
  t = t.replace(/&#140;/g, 'Œ');
  t = t.replace(/&#x0028;/g, '(');
  t = t.replace(/&#x0029;/g, ')');
  t = t.replace(/&#/g, '');
  return t;
}

