/* hafas_standard_calendar_utilties.js */
/* built: 05.11.2007 by MHEI */

function e() {
   var test = arguments[0];
   var holla = document.getElementById(test);
   return holla;
}

// some more Date object enhancements (see hafas_utilities.js):
Date.prototype.shiftD = function(value) {
   this.setDate(this.getDate()+value);
}

Date.prototype.shiftM = function(value) {
   this.setMonth(this.getMonth()+value);
}

Date.prototype.shiftY = function(value) {
   this.setYear(this.getYear()+value);
}

function parseUserDateInput (userInput) {
       if (isString(userInput)) {
          var matchReg = /(\d+)\D+(\d+)\D+(\d+)\D*/;
          matchReg.exec( userInput );

          var day   = RegExp.$1;
          var month = RegExp.$2;
          var year  = RegExp.$3;

          day *= 1; month *= 1; year *= 1;
          // attention: month is 1-12-ranged!
          // ~~~
          if (month!="") {
             month -= 1;
             if (month<0) {
                month = 11;
             } else if (month>11) {
                month = 0;
             }
          }
          if (year!="") {
             if (year<100) {
                if (year<50) {
                   year+=2000;
                } else {
                   year+=1900;
                }
             } else if (year < 1000) {
                if (year<200) {
                   year+=1900;
                } else {
                   year+=1000;
                }
             }
          }
          if  ((year != 0) && (day != 0)) {
            return new Date(year, month, day);
          }

       } else if (isDate(userInput)) {
         return userInput;
       }

       return null;
}

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';
}

// extracted from prototype framework - BEGIN
var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}
// extracted from prototype framework - END

function inherits(base, extension)
{
   for ( var property in base )
   {
      try
      {
         extension[property] = base[property];
      }
      catch( warning ){}
   }
}

function makeObservable (obj, observer) {
   inherits(new Observable(), obj);
   if (observer != undefined) {
      obj.addObserver(observer);
   }
}

Array.prototype.forEach = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        fn.call(scope, this[i], i, this);
    }
};

Array.prototype.filter = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            continue;
        }
        a.push(this[i]);
    }
    return a;
};

Observer = Class.create();
Observer.prototype = {

      initialize: function() {
         // intentionally left blank
      },
      observe: function() {
      }

}

function Observable() {
    this.fns = [];
}

Observable.prototype = {
    addObserver : function(fn) {
        this.fns.push(fn);
    },
    removeObserver : function(fn) {
        this.fns = this.fns.filter(
            function(el) {
                if ( el !== fn ) {
                    return el;
                }
            }
        );
    },
    notify : function(o, thisObj) {
        var scope = thisObj || window;
        this.fns.forEach(
            function(el) {
                //el.call(scope, o);
                el.observe(o);
            }
        );
    }
};

