/* 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) {
    try{
      element.focus();
    }
    catch(e){
      // silent - just no focusing done - it failed.
      //alert('Could not focus on '+element.name);
    }
  }
}


// Date object enhancements:
Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];

Date.prototype.isLeapYear = function() {
    var year = this.getFullYear();
    return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
}

Date.prototype.getDaysInMonth = function() {
    Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
    return Date.daysInMonth[this.getMonth()];
}

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;
}

function insertAfter(parent, node, referenceNode){
  if(referenceNode.nextSibling){
    parent.insertBefore(node, referenceNode.nextSibling);
  } else {
    parent.appendChild(node);
  }
}

function isFunction(o) {
  return (typeof(o)=="function");
}

function isObject(a)
{
  return (typeof a == 'object' && !!a) || isFunction(a);
}

function isArray(a)
{
  return isObject(a) && a.constructor == Array;
}

function isDate(a)
{
  return isObject(a) && a.constructor == Date;
}

function isString(a)
{
  return typeof a == 'string';
}

function log(text) {
   if (logwindow == null) {
      var logwindow = window.open("","log","");
   }
   logwindow.document.writeln(text+"<br>");
   logwindow.focus();
}

function logT(text) {
  log(new Date() + ": "+text);
}


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;
}
