/* 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.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);
}
