function calax_logout(formname)
{
  AJS.$f(formname, "SID").value = "";
}

CJS.calax = new CJS.Class({
  formname : "unbekannt",
  cal_name : "def_cal",

  init : function(options)
  {
    AJS.update(this, options);
  },

  getvarname : function(varname)
  {
    return this.formname + '_' + this.cal_name + "_" + varname;
  },

  existsVar : function (varname)
  {
    if (!document.forms[this.formname])
      return false;
    var vn = this.getvarname(varname);
    return (AJS.$f(this.formname, vn)) ? true : false;
  },

  getvar : function (varname, _default, num)
  {
    if (!this.existsVar(varname))
      return _default;

    if (num)
      return parseInt(AJS.$f(this.formname, this.getvarname(varname)).value, 10);
    else
      return AJS.$f(this.formname, this.getvarname(varname)).value;
  },

  setvar : function (varname, varval)
  {
    var vn = this.getvarname(varname);

    if (!this.existsVar(varname))
    {
      var o = AJS.INPUT({ type : "hidden",
                          name : vn,
                         value : varval });

      AJS.appendToTop(document.forms[this.formname], o);
    }
    else
      AJS.$f(this.formname, vn).value = varval;
    return true;
  }
});

CJS.calax.list = CJS.calax.extend({
  rowname : "evid",
  line : 0,
  limit : 25,

  init : function(options)
  {
    this.parent(options);
  },

  list_fold : function(i, aufklappen)
  {
    if (i < 0)
      i = this.line;
    var vn = this.getvarname(this.rowname) + "_" + i;
    var e = AJS.$(vn);
    if (e)
      this.setvar("show_id_" + e.attributes["id2"].nodeValue, aufklappen ? 1 : 0);
    toggleVisibleFade(vn, aufklappen, false, false, 10);

    return false;
  },

  list_fold_toggle : function(i, id, nofade)
  {
    var vn = this.getvarname(this.rowname) + "_" + i;
    //alert(vn);
    var e = AJS.$(vn);
    if (e)
      this.setvar("show_id_" + id, e.style.display == "none" ? 1 : 0);
    if (nofade || AJS.isIe())
      toggleVisibleNoFade(vn, -1, "block");
    else
      toggleVisibleFade(vn, -1, "block");
    return false;
  },

  list_fold_all : function()
  {
    for (var i=0; i < this.limit; i++)
      this.list_fold(i, 0);

    return false;
  },

  list_unfold_all : function()
  {
    for (var i=0; i < this.limit; i++)
      this.list_fold(i, 1);
    return false;
  },

  list_first : function()
  {
    this.setvar("page", 0);
    this.list_reload();
    return false;
  },

  list_last : function(lastpage)
  {
    this.setvar("page", lastpage);
    this.list_reload();
    return false;
  },

  list_next : function()
  {
    var p = this.getvar("page", 0, true);
    this.setvar("page", p+1);
    this.list_reload();
    return false;
  },

  list_prev : function()
  {
    var p = this.getvar("page", 0, true);
    if (p > 0)
      p--;
    this.setvar("page", p);
    this.list_reload();
    return false;
  },

  list_reload : function(show_id)
  {
    try
    {
      if (!this.existsVar("page"))
        this.setvar("page", 0);
      if (show_id)
        this.setvar("show_id", show_id);

      postAXAH('../db/cal_ajaxbox.php3?ajax=cal_render&formname=' + this.formname + '&cal_name=' + this.cal_name, this.formname, "calview_" + this.formname + "_" + this.cal_name);
    }
    catch(e)
    {
      return false;
    }
    return false;
  }
});

CJS.calax.time = CJS.time.extend({
  init : function(options)
  {
    var s = "";
    if (options.elm)
    {
      this.name = options.elm.name;
      s = options.elm.value;
      options.elm = null;
    }
    this.parent(options);

    if (s)
      this.fromString(s);
  }
});

CJS.calax.date = CJS.date.extend({
  init : function(options)
  {
    var s = "";
    if (options.elm)
    {
      this.name = options.elm.name;
      s = options.elm.value;
      options.elm = null;
    }
    this.parent(options);

    if (s)
      this.fromString(s);
  }
});

CJS.calax.timeselector = new CJS.Class({
  target : null,
  box : null,
  locked : false,
  cb_out : null,
  cb_onSelectTime : null,
  cb_show : null,
  cb_hide : null,

  start_time : null, // Die Box beginnt mit dieser Zeit
  steps : 30,
  sel_time : null, // Dies ist die aktuell eingestellte Zeit

  init : function(target, options)
  {
    this.target = target;
    AJS.update(this, options);
    var me = this;

    if (!this.cb_out)
      this.cb_out = function(s) { alert(s); };

    if (!this.start_time)
      this.start_time = this.createTimeObject();

    if (!this.steps)
      this.steps = 30;

    if (!this.cb_onSelectTime)
      this.cb_onSelectTime = this.onSelectTime;

    if (!this.cb_show)
      this.cb_show = this.show;

    if (!this.cb_hide)
      this.cb_hide = this.hide;

    AJS.addEventListener(AJS.getBody(), "click",  function () { me.cb_hide(); me.unlock(); });
    AJS.addEventListener(this.target, "click", function () { me.lock(); me.cb_show("click"); });
    AJS.addEventListener(this.target, "focus", function () { me.cb_show("focus"); });
    AJS.addEventListener(this.target, "blur",  function () { me.cb_hide(); });
  },

  createTimeObject : function()
  {
    return new CJS.calax.time({ "elm" : this.target,
                                "cb_out" : this.cb_out
                              });
  },

  show : function(mode)
  {
    if (!this.target)
      return false;

    if (mode=="click" && this.box)
    {
      if (this.byevent == "focus")
      {
        this.byevent = mode;
        return;
      }
      this.byevent = mode;
      this.hide();
      return;
    }
    this.hide(true);

    this.box = AJS.DIV({style : "position: absolute; border: 0px solid #048;",
                        "class" : "DP_popup_div"
                       })

    // Ab wann die Zeit beginnt
    this.box.start_time = this.createTimeObject();
    this.box.start_time.set(this.start_time);
    if (!this.box.start_time.checkTime())
      this.box.start_time.now();

    this.box.steps = this.steps;
    if (!this.box.steps)
      this.box.steps = 30;

    this.box.owner = this;

    var pos = AJS.absolutePosition(this.target);
    var s = { "z-index": 1000,
              zIndex : 1000,
              width : this.target.offsetWidth,
              height : 10 * this.target.offsetHeight,
              left : pos.x,
              top : pos.y + this.target.offsetHeight
             };
    AJS.update(this.box.style, s);
    this.paint();

    AJS.insertAfter(this.box, this.target);
    this.byevent = mode;
  },

  hide : function(force)
  {
    if (this.box && (!this.locked || force))
    {
      AJS.removeElement(this.box);
      this.box = null;
      return true;
    }
    return false;
  },

  lock : function()
  {
    this.locked = true;
  },

  unlock : function()
  {
    this.locked = false;
  },

  onSelectTime : function(new_val)
  {
    this.target.value = new_val;
    this.cb_hide(true);
  },

  paint : function()
  {
    if (!this.box)
      return false;

    var me = this;
    var sel = AJS.SELECT({ size : 10 })

    AJS.addEventListener(sel, "mousedown", function () { me.lock(); });
    AJS.addEventListener(sel, "mouseup", function () { me.unlock(); });
    AJS.addEventListener(sel, "blur", function () { me.unlock(); });

    var z = this.box.start_time;
    var nn = 24*60 / this.box.steps;
    for (var i=0; i < nn; i++)
    {
      AJS.ACN(sel, AJS.OPTION({value: z.toString()}, z.toString()));
      z.addMinutes(this.box.steps);
    }

    var s = { "font-size" : "10px",
              backgroundColor : "#ffd",
              zIndex : 1000,
              width : Math.max(50, this.target.offsetWidth),
              height : 10 * this.target.offsetHeight,
              display : "inline"
            };
    AJS.update(sel.style, s);

    var tmp = this.createTimeObject();
    if (!tmp.checkTime() || !this.target.value)
    {
      tmp.now();
      tmp.minute = 0;
      sel.value = tmp.toString();
    }
    else
      sel.value = this.target.value;

    AJS.addEventListener(sel, "change", function () { me.cb_onSelectTime(sel.value); });
    AJS.RCN(this.box, sel);
  }
});

// *************************************
CJS.calax.dateselector = new CJS.Class({
  target : null,
  box : null,
  locked : false,
  cb_out : null,
  cb_show : null,
  cb_hide : null,
  cb_onSelectDate : null,

  init : function(target, options)
  {
    this.target = target;
    AJS.update(this, options);

    if (!this.cb_out)
      this.cb_out = function(s) { alert(s); };

    if (!this.cb_show)
      this.cb_show = this.show;

    if (!this.cb_hide)
      this.cb_hide = this.hide;

    if (!this.cb_onSelectDate)
      this.cb_onSelectDate = this.onSelectDate;

    if (!this.cb_onToday)
      this.cb_onToday = this.onToday;

    var me = this;
    AJS.addEventListener(this.target, "click", function () { me.cb_show("click"); });
    AJS.addEventListener(this.target, "focus", function () { me.cb_show("focus"); });
    AJS.addEventListener(this.target, "blur",  function () { me.cb_hide(); });
  },

  createDateObject : function()
  {
    return new CJS.calax.date({ "elm" : this.target,
                                "cb_out" : this.cb_out
                              });
  },

  show : function(mode)
  {
    if (!this.target)
      return false;
    if (mode=="click" && this.box)
    {
      if (this.byevent == "focus")
      {
        this.byevent = mode;
        return;
      }
      this.byevent = mode;
      this.hide();
      return;
    }
    this.hide(true);

    var pos = AJS.absolutePosition(this.target);
    this.box = AJS.DIV({ "class" : "DP_popup_div",
                         style : "position: absolute;"
                       })

    this.box.owner = this;
    this.box.sel_dat = this.createDateObject();
    if (!this.box.sel_dat.checkDate() || !this.target.value)
      this.box.sel_dat.today();
    this.box.cur_dat = this.box.sel_dat.clone();

    this.paint();

    var s = { "z-index": 1000,
              zIndex : 1000,
              left : pos.x + ((this.target.offsetWidth) - ((this.target.offsetWidth * 3)/2)) + "px",
              top : pos.y + this.target.offsetHeight,
              width : (this.target.offsetWidth * 5) + "px"
             };
    AJS.update(this.box.style, s);

    AJS.insertAfter(this.box, this.target);
    this.byevent = mode;
  },

  hide : function(force)
  {
    if (this.box && (!this.locked || force))
    {
      AJS.removeElement(this.box);
      this.box = null;
      return true;
    }
    return false;
  },

  lock : function()
  {
    this.locked = true;
  },

  unlock : function()
  {
    this.locked = false;
  },

  onPrevMonth : function()
  {
    if (!this.box)
      return false;

    this.box.cur_dat.tag=1;
    this.box.cur_dat.monat--;
    if (this.box.cur_dat.monat < 1)
    {
      this.box.cur_dat.monat=12;
      this.box.cur_dat.jahr--;
    }
    this.paint();
    //this.cb_out('Prev Month');
  },

  onNextMonth : function()
  {
    if (!this.box)
      return false;
    this.box.cur_dat.tag = 1;
    this.box.cur_dat.monat++;
    if (this.box.cur_dat.monat > 12)
    {
      this.box.cur_dat.monat=1;
      this.box.cur_dat.jahr++;
    }
    this.paint();
    //this.cb_out('Next Month');
  },

  onPrevYear : function()
  {
    if (!this.box)
      return false;

    this.box.cur_dat.tag=1;
    this.box.cur_dat.jahr--;
    this.paint();
    //this.cb_out('Prev Month');
  },

  onNextYear : function()
  {
    if (!this.box)
      return false;
    this.box.cur_dat.tag = 1;
    this.box.cur_dat.jahr++;
    this.paint();
    //this.cb_out('Next Month');
  },

  onSelectDay : function(day, mon_offset)
  {
    //this.cb_out('xxx: ' + day + " / " + mon_offset);
    if (!this.box)
      return;
    this.box.sel_dat = this.box.cur_dat.clone();
    this.box.sel_dat.tag = day;
    if (mon_offset < 0)
    {
      this.box.sel_dat.monat--;
      if (this.box.sel_dat.monat < 1)
      {
        this.box.sel_dat.monat=12;
        this.box.sel_dat.jahr--;
      }
    }
    else if (mon_offset > 0)
    {
      this.box.sel_dat.monat++;
      if (this.box.sel_dat.monat > 12)
      {
        this.box.sel_dat.monat=1;
        this.box.sel_dat.jahr++;
      }
    }
    this.cb_onSelectDate(this.box.sel_dat);
  },

  onToday : function()
  {
    this.box.sel_dat = this.box.cur_dat.clone();
    this.box.sel_dat.today();
    this.cb_onSelectDate(this.box.sel_dat)
  },

  onSelectDate : function(newdate)
  {
    this.target.value = newdate.toString();
    //this.box.cur_dat = newdate.clone();
    //this.paint();
    this.hide(true);
  },

  paint : function()
  {
    if (!this.box)
      return false;

    me = this;
    var tab1 = AJS.TABLE({"class" : "DP_popup_monthtable",
                          id : "when-sddp_div_tbl",
                          cols : 7,
                          cellSpacing : 0,
                          cellPadding : 0,
                          style : "-moz-user-select: none; cursor: pointer;"
                         });
    var tab = AJS.TBODY();
    AJS.ACN(tab1, tab);
    AJS.addEventListener(tab, "mousedown", function () { me.lock(); });
    AJS.addEventListener(tab, "mouseup", function () { me.unlock(); });

    var tr = AJS.TR({"class" : "DP_popup_heading",
                     id : "when-sddp_div_header"
                    });
    AJS.ACN(tab, tr);

    var td = AJS.TD({"class" : "DP_popup_prev",
                     id : "when-sddp_div_mhl",
                     //onmousedown : function() { me.onPrevMonth(); },
                     colSpan : 1,
                     unselectable : "on"
                    }, "«");
    AJS.ACN(tr, td);
    AJS.addEventListener(td, "mousedown", function () { me.onPrevMonth(); });

    var Monat = new Array("Januar", "Februar", "März", "April", "Mai", "Juni",
                          "Juli", "August", "September", "Oktober", "November", "Dezember");
    var m = Monat[this.box.cur_dat.monat-1];

    var span_prev = AJS.SPAN({"class" : "DP_popup_cur",
                          nowrap : "nowrap",
                          unselectable : "on"
                         }, "<");

    var span_next = AJS.SPAN({"class" : "DP_popup_cur",
                            nowrap : "nowrap",
                            unselectable : "on"
                           }, ">");

    var j = String(this.box.cur_dat.jahr);
    td = AJS.TD({"class" : "DP_popup_cur",
                  id : "when-sddp_div_mhc",
                  colSpan : 5,
                  nowrap : "nowrap",
                  unselectable : "on"
                 }, m, " ", span_prev, j, span_next);
    AJS.ACN(tr, td);

    td = AJS.TD({"class" : "DP_popup_next",
                 id : "when-sddp_div_mhr",
                 unselectable : "on",
                 colspan : 1
                }, "»");
    AJS.ACN(tr, td);
    AJS.addEventListener(span_prev, "mousedown", function () { me.onPrevYear(); });
    AJS.addEventListener(span_next, "mousedown", function () { me.onNextYear(); });
    AJS.addEventListener(td, "mousedown", function () { me.onNextMonth(); });

    // beginn wochentage-zeile
    tr = AJS.TR({"class" : "DP_popup_days",
                 id : "when-sddp_div_dow"
                });
    AJS.ACN(tab, tr);

    var wotas = new Array("M", "D", "M", "D", "F", "S", "S");
    for(var i=0; i < wotas.length; i++)
    {
      td = AJS.TD({"class" : "DP_popup_dayh",
                 style : "border-bottom:1px solid #A3B9DF",
                    id : "when-sddp_div_day_" + String(i),
                    unselectable : "on"
                  }, wotas[i]);
      AJS.ACN(tr, td);
    }

    var heute = new CJS.date;
    heute.today();
    var heute_month = this.box.cur_dat.jahr == heute.jahr && this.box.cur_dat.monat == heute.monat;

    var dt = this.box.cur_dat.toDate();
    var current_month = this.box.cur_dat.jahr == this.box.sel_dat.jahr && this.box.cur_dat.monat == this.box.sel_dat.monat;
    dt.setDate(1);
    var wota = (dt.getDay()+6)%7; // Wochentag
    var anz = this.box.cur_dat.getDaysInMonth();
    var li = wota + anz -1;

    var t = 0;
    var mon = wota;
    if (Math.floor(li/7) < 5)
      mon+= 7;
    var moff = mon + anz-1;
    var cur = current_month ? mon + this.box.sel_dat.tag - 1 : -1;
    var now = heute_month ? mon + heute.tag - 1 : -1;

    var prevmonth = this.box.cur_dat.clone();
    prevmonth.tag=1;
    prevmonth.prevDay();
    var nr = prevmonth.getDaysInMonth()-mon;
    var mon_offset = 0;

    var c = "";
    for(var z=0; z < 7; z++)
    {
      // beginn wochentage-zeile
      tr = AJS.TR({id : "when-sddp_div_week_" + String(z)});
      AJS.ACN(tab, tr);

      for (var s=0; s< 7; s++)
      {
        c = "";
        if (z==0)
          c+= "DP_popup_day_top";
        c+= " DP_popup_onmonth";
        if (t==cur && current_month)
        {
          if (s > 4)
            c+= " DP_popup_weekend_selected";
          else
            c+= " DP_popup_weekday_selected";
        }
        else if (t==now && heute_month)
        {
          if (s > 4)
            c+= " DP_popup_weekend_today";
          else
            c+= " DP_popup_weekday_today";
        }
        else
        {
          if (s > 4)
            c+= " DP_popup_weekend";
          else
            c+= " DP_popup_weekday";
        }
        if (s==0)
          c+= " DP_popup_day_left";
        else if (s==6)
          c+= " DP_popup_day_right";

        if (t >= mon && t <= moff)
          c+= " DP_popup_onmonth";
        else
          c+= " DP_popup_offmonth";

        nr++;
        if (t == mon || t == moff+1)
          nr = 1;
        mon_offset = t < mon ? -1 : (t > moff ? 1 : 0);
        t++;
        if (AJS.isMozilla())
        {
          td = AJS.TD({"class" : c,
                        id : "when-sddp_div_day_" + String(z) + "_" + String(s),
                        value1 : nr,
                        value2 : mon_offset,
                        unselectable : "on"
                      }, String(nr));
          AJS.ACN(tr, td);
          td.value1 = nr;
          td.value2 = mon_offset;
          AJS.addEventListener(td, "mousedown", function () { me.onSelectDay(this.value1, this.value2); });
        }
        else
        {
          td = AJS.TD({"class" : c,
                        id : "when-sddp_div_day_" + String(z) + "_" + String(s),
                        value1 : nr,
                        value2 : mon_offset,
                        onmousedown : function() { me.onSelectDay(this.value1, this.value2); },
                        unselectable : "on"
                      }, String(nr));
          AJS.ACN(tr, td);
        }
      }
    }

    tr = AJS.TR({"class" : "DP_popup_heading",
                 id : "when-sddp_div_dow"
                });
    AJS.ACN(tab, tr);

    td = AJS.TD({"class" : "DP_popup_cur",
               style : "border-bottom:1px solid #A3B9DF;text-align:center;",
                  id : "when-sddp_div_today",
                  colSpan : 7,
                  width : "100%",
                  unselectable : "on"
                }, AJS.SMALL("[ Heute ]"));
    AJS.addEventListener(td, "mousedown", function () { me.onToday(); });
    AJS.ACN(tr, td);

    var tab2 = AJS.TABLE({
      cellSpacing : 0,
      cellPadding : 0
      });
    var tb = AJS.TBODY();
    AJS.ACN(tab2, tb);

    tr = AJS.TR({});
    AJS.ACN(tb, tr);

    td = AJS.TD({"class" : "ol"});
    AJS.ACN(tr, td);
    td = AJS.TD({"class" : "o"});
    AJS.ACN(tr, td);
    td = AJS.TD({"class" : "or"});
    AJS.ACN(tr, td);

    tr = AJS.TR({});
    AJS.ACN(tb, tr);

    td = AJS.TD({"class" : "l"});
    AJS.ACN(tr, td);
    td = AJS.TD({"text-align" : "center",
                 "padding" : 0,
                 "class" : "DP_popup_monthtable",
                 style : "-moz-user-select: none; cursor: pointer; border 1px solid #f00;",
                 "margin" : "0px"});

    AJS.ACN(td, tab1);
    AJS.ACN(tr, td);
    td = AJS.TD({"class" : "r"});
    AJS.ACN(tr, td);

    tr = AJS.TR({});
    AJS.ACN(tb, tr);

    td = AJS.TD({"class" : "ul"});
    AJS.ACN(tr, td);
    td = AJS.TD({"class" : "u"});
    AJS.ACN(tr, td);
    td = AJS.TD({"class" : "ur"});
    AJS.ACN(tr, td);

    AJS.RCN(this.box, tab2);
  }
});

CJS.calax.editor = CJS.calax.extend({
  cal : null,
  line : 0,
  e_date_von : null,
  e_time_von : null,
  e_date_bis : null,
  e_time_bis : null,

  init : function(cal, options, show_options)
  {
    this.parent(options);
    this.cal = cal;
    if (options)
    {
      var me = this;

      this.e_date_von = AJS.$(options.date_von);
      TypeChecker.decorate(this.e_date_von, 'date');
      CJS.calax.decorate(this.e_date_von, "dateselector", { cb_out : this.out,
                                                            cb_onSelectDate : function(newdate) { me.onSelectDate(me.e_date_von, newdate); }
                                                          });

      this.e_time_von = AJS.$(options.time_von);
      TypeChecker.decorate(this.e_time_von, 'time');
      CJS.calax.decorate(this.e_time_von, "timeselector", { cb_out : this.out,
                                                            start_time : "00:00",
                                                            cb_onSelectTime : function(new_val) { me.onSelectTime(me.e_time_von, new_val); }
                                                          });
      this.e_date_bis = AJS.$(options.date_bis);
      TypeChecker.decorate(this.e_date_bis, 'date');
      CJS.calax.decorate(this.e_date_bis, "dateselector", { cb_out : this.out,
                                                            cb_onSelectDate : function(newdate) { me.onSelectDate(me.e_date_bis, newdate); }
                                                          });

      this.e_time_bis = AJS.$(options.time_bis);
      TypeChecker.decorate(this.e_time_bis, 'time');
      CJS.calax.decorate(this.e_time_bis, "timeselector", { cb_out : this.out,
                                                            start_time : this.e_time_von.value,
                                                            cb_onSelectTime : function(new_val) { me.onSelectTime(me.e_time_bis, new_val); }
                                                          });
    }
    if (show_options && show_options.url)
      this.win = this.showEditorByOptions(show_options);
  },

  createTimeObject : function(elm)
  {
    return new CJS.calax.time({ "elm" : elm, "owner" : this });
  },

  createDateObject : function(elm)
  {
    return new CJS.calax.date({ "elm" : elm, "owner" : this });
  },

  checkElements : function()
  {
    if (this.e_date_von && this.e_time_von && this.e_date_bis && this.e_time_bis)
      return true;
    return false;
  },

  showEditor : function(caption, url, height, width)
  {
    var options =
    {
      caption: caption,
      height: height,
      width: width,
      url: url
    }
    return this.showEditorByOptions(options);
  },

  showEditorByOptions : function(options)
  {
    var default_options =
    {
      owner : this,
      caption: "Editor - unbenannt",
      center_win: true,
      height: 500,
      width: 500,
      fullscreen: false,
      callback_fn: null,
      url : false
    }

    var o = AJS.update(default_options, options);
    this.win = new GB_Window(o);
    return this.win.show(o.url);
  },

  validateDateByTime : function(bis_is_fix)
  {
    var vd = this.createDateObject();
    var bd = this.createDateObject();
    if (vd.fromString(this.e_date_von.value) &&  bd.fromString(this.e_date_bis.value))
    {
      if (vd.compare(bd))
      {
        var vt = this.createTimeObject(this.e_time_von);
        var bt = this.createTimeObject(this.e_time_bis)
        if (vt.toString() > bt.toString())
        {
          if (bis_is_fix)
          {
            vd.prevDay();
            this.e_date_von.value = vd.toString();
          }
          else
          {
            bd.nextDay();
            this.e_date_bis.value = bd.toString();
          }
        }
      }
    }
  },

  onSelectTime : function(elm, new_val)
  {
    if (!this.checkElements())
      return false;

    var timediff = this.getTimeDiff();

    elm.timeselector.onSelectTime(new_val);

    // von zeit hat sich geändert
    if (elm == this.e_time_von)
    {
      var v = this.createTimeObject(this.e_time_von);
      v.addMinutes(timediff);
      this.e_time_bis.value = v.toString();
    }

    this.validateDateByTime();
    this.e_time_bis.timeselector.start_time = this.e_time_von.value;
  },

  onSelectDate : function(elm, newdate)
  {
    if (!this.checkElements())
      return false;

    var vd = this.createDateObject();
    var bd = this.createDateObject();

    var von_mode = (elm == this.e_date_von) ? true : false;

    // Von-Datum verändert? Dann Bis-Datum auch verschieben
    if (von_mode)
    {
      // zunächst bisherige diff berechnen
      vd.fromString(this.e_date_von.value);
      bd.fromString(this.e_date_bis.value);
      var daysdiff = vd.dateDiff(bd); // Tage Unterschied vor der Änderung

      // Von datum nun ändern, diff addieren und bis-Datum setzen
      elm.dateselector.onSelectDate(newdate);

      vd.fromString(this.e_date_von.value);
      vd.addDays(daysdiff);
      this.e_date_bis.value = vd.toString();
    }
    else
      elm.dateselector.onSelectDate(newdate);

    vd.fromString(this.e_date_von.value);
    bd.fromString(this.e_date_bis.value);
    if (vd.toDate().getTime() > bd.toDate().getTime())
    {
      if (von_mode)
        this.e_date_bis.value = this.e_date_von.value;
      else
        this.e_date_von.value = this.e_date_bis.value;
    }
    this.validateDateByTime(!von_mode);
  },

  getTimeDiff : function()
  {
    var v = this.createTimeObject(this.e_time_von);
    var b = this.createTimeObject(this.e_time_bis);

    var dur = b.getTimeInMinutes() - v.getTimeInMinutes();
    if (dur < 0)
      dur+=24*60;

    return dur;
  },

  out : function(s, clear)
  {
    var dbg_elm = AJS.$("debug_out");
    if (dbg_elm)
    {
      if (clear)
        AJS.setHTML(dbg_elm, s);
      else
        AJS.setHTML(dbg_elm, dbg_elm.innerHTML + s + "<br>");
      AJS.fx.highlight(dbg_elm, {duration: 500})
    }
    else
      alert(s);
  }
});

CJS.calax.decorate = function(elm, typ, options)
{
  if (typeof elm == 'string')
    elm = AJS.$(elm);

  if (!elm)
    return false;

  options = options || {};
  var me = this;

  switch (typ)
  {
    case "dateselector":
      elm.dateselector = new CJS.calax.dateselector(elm, options);
      break;

    case "timeselector":
      elm.timeselector = new CJS.calax.timeselector(elm, options);
      break;

    default:
      break;
  }
}
