function checkDateInput(o,f){
 var d=o.value;
 if(d=="")return false;
 if(!validateDate(d,f,dError)){
  o.value="";
  return false;
 }
 return true;
}
function handleArrival(e,def,tid,cal){
 if(checkDateInput(e,def.dtype)){
  var d=parseDate(e.value,def.dtype);
  var c1=new Date(d[2],d[1],d[0],12,0,0);
  var c2=new Date(def.ddate.Y,def.ddate.M,def.ddate.D,12,0,0);
  if(c1<c2){
   errorOut(def.e,1,formatDate(def.dtype,def.ddate.D,def.ddate.M,def.ddate.Y));
   e.value=formatDate(def.dtype,def.ddate.D,def.ddate.M,def.ddate.Y);
   return;
  }
  var t=new Date(d[2],d[1],d[0]/1+1,12,0,0);
  cal.restrictDate(t.getDate(),t.getMonth(),t.getFullYear());
  e=getElement(document,"id",tid,null);
  if(Def(e)){
   if(checkDateInput(e,cal.def.dtype)){
    var d1=parseDate(e.value,cal.def.dtype);
    var t1=new Date(d1[2],d1[1],d1[0],12,0,0);
    if(t1>=t){
     e.focus();
     return;
    }
   }
   t=new Date(d[2],d[1],d[0]/1+7,12,0,0);
   e.value=formatDate(def.dtype,t.getDate(),t.getMonth(),t.getFullYear());
   e.focus();
  }
 }
}
function handleDeparture(e,def){
 if(checkDateInput(e,def.dtype)){
  var d=parseDate(e.value,def.dtype);
  var c1=new Date(d[2],d[1],d[0],12,0,0);
  var c2=new Date(def.ddate.Y,def.ddate.M,def.ddate.D,12,0,0);
  if(c1<c2){
   errorOut(def.e,1,formatDate(def.dtype,def.ddate.D,def.ddate.M,def.ddate.Y));
   e.value=formatDate(def.dtype,def.ddate.D,def.ddate.M,def.ddate.Y);
   return;
  }
 e.focus();
 }
}
function handleSDWDCalendar(e){
e=evnt.dom(e);
switch (e.trgt.id){
 case "calendar_img": c1.popup(e);
  break;
 case "calendar_img1": c2.popup(e);
  break;
 }
}

function SDWDCalendar(def){
 this.def=def;
 this.hidewidget=true;
 this.ctlid=false;
 this.ctl=null;
 this.widget=new SDWDWidget(this.def.wdef);
 this.preloaded=new Array();
};

{var SDWDc=SDWDCalendar.prototype;

SDWDc.preload=function(b){
 var h=this.def.headerstyle;
 for(var tmp in h){
  if(!h[tmp].search(/^http:\/\//)){
   this.preloaded[this.preloaded.length]=new Image();
   this.preloaded[this.preloaded.length-1].src=h[tmp];
  }
 }
};
SDWDc.init=function(b,id,off){
 this.ctlid=id;
 evnt.add(b,'click',handleSDWDCalendar,false);
 this.widget.find();
 if(off>0){
  var oneday=24*3600000;
  var tdy=new Date();
  var now=tdy.getTime();
  tdy.setTime(now+off*oneday);
  this.restrictDate(tdy.getDate()/1,tdy.getMonth()/1,tdy.getFullYear()/1);
 }
};
SDWDc.getCss=function(key,d){
 if(Undef(d)){
  var t=eval("this.def."+key);
 }else{
  var t=eval("d."+key);
 }
 return " class=\""+t+"\"";
};

SDWDc.hide=function(){
 if(Def(this.widget)){
  this.widget.hidewidget=true;
  this.widget.hide();
 }
};
SDWDc.show=function(){
 this.widget.show();
};
SDWDc.draw=function(){
 if(!this.date)this.date=new Date();
 var def=this.def;
 var table="<table cellspacing=\"0\" cellpadding=\"0\" width=\""+def.width+"\" border=\"0\">";
 table+="<tbody><tr>";
 table+=this.createButtons();
 table+="</tr>";
 table+="<tr>";
 table+=this.createWeekdays();
 table+="</tr>";
 table+=this.createMonth();
 table+="</tbody></table>";
 if(!setHtml(this.widget.viewport,table))alert("Fehler");
};

SDWDc.createMonth=function(){
 var r="";
 this.date.setHours(12);
 var ct=this.date.getTime();
 var cd=this.date.getDate();
 var cm=this.date.getMonth();
 var oneday=24*3600000;
 var ws=this.def.firstday;
 var wd=this.date.getDay();
 wd=wd==0?7:wd;
 var start=ct-(wd-ws)*oneday;
 this.date.setDate(1);
 this.date.setHours(12);
 var offs=this.date.getDay()-ws;
 offs=offs<0?7+offs:offs;
 this.date.setTime(this.date.getTime()-offs*oneday);
 var css,csst,thisweek,thismonth,weekend,thisday,setref=true;
 var w;
 for(w=0;w<6;w++){
  r+="<tr>";
  for(var day=0;day<7;++day){
   var yy=this.date.getFullYear();
   var dd=this.date.getDate();
   var tt=this.date.getTime();
   var mm=this.date.getMonth();
   if(this.def.drestrict!='no'){
    setref=(yy*10000+mm*100+dd<this.def.ddate.Y*10000+this.def.ddate.M*100+this.def.ddate.D)?false:true;
   }
   thismonth=(mm==cm)?true:false;
   thisweek=(tt>=start&&tt<start+7*oneday)?true:false;
   thisday=(mm==cm&&dd==cd)?true:false;
   weekend=(this.date.getDay()==0||this.date.getDay()==6)?true:false;
   css=setref?(thisday?"cd_css":(thisweek?(weekend?"wecw_css":"tw_css"):(thismonth?(weekend?"we_css":"wd_css"):(weekend?"weom_css":"wdom_css")))):"cres_css";
   csst=setref?(thisday?"cd_css_t":(thisweek?"tw_css_t":(thismonth?(weekend?"we_css_t":"wd_css_t"):(weekend?"weom_css_t":"wdom_css_t")))):"cres_css_t";
   thisref=setref?this.getRef():"";
   r+='<td align="center" width="14%"'+this.getCss(csst)+"><a"+this.getCss(css)+thisref+' onmouseover="window.status=\' \';return true;" onmouseout="window.status=\'\';return true;">';
   r+=this.date.getDate();
   r+="</a></td>";
   var t=this.date.getTime();
   this.date.setTime(t+oneday);
  }
  r+="</tr>";
 }
 this.date.setTime(ct);
 return r;
};


SDWDc.getRef=function(){
 var ref=' href="#" onclick="window.sdwdcalendar.setDay(\''+this.def.dtype+'\','+this.date.getDate()+','+this.date.getMonth()+','+this.date.getFullYear()+');return false;"';
 return ref;
};

SDWDc.createWeekdays=function(){
 var def=this.def;
 var weekend;
 var r="";
 for(var day=0;  day<7;  ++day){
  weekend=day+def.firstday;
  weekend-=(weekend<7?0:7);
  r+='<td width="28" align="center"'+this.getCss("dn_css")+'>'+def.daynames[weekend]+'</td>';
 }
 return r;
};

SDWDc.createButtons=function(){
 var col=20,dh=this.def.headerstyle,r="";
 for(var p in dh)--col;
 if(dh.imgprevy)r+='<td width="28" align="left"'+this.getCss("css",dh)+'><a href="#" onclick="window.sdwdcalendar.setMY('+this.date.getMonth()+','+this.date.getFullYear()+',3);return false;"><img border="0" src="'+dh.imgprevy+'" alt="'+dh.imgprevyt+'" title="'+dh.imgprevyt+'" onmouseover="replaceImage(this,\''+dh.imgprevyo+'\');window.status=\''+dh.imgprevyt+'\';return true;" onmousedown="replaceImage(this,\''+dh.imgprevyd+'\');" onmouseout="replaceImage(this,\''+dh.imgprevy+'\');window.status=\'\';return true;" onmouseup="replaceImage(this,\''+dh.imgprevy+'\');"></a></td>';
 if(dh.imgprevm)r+='<td width="28" align="left"'+this.getCss("css",dh)+'><a href="#" onclick="window.sdwdcalendar.setMY('+this.date.getMonth()+','+this.date.getFullYear()+',1);return false;"><img border="0" src="'+dh.imgprevm+'" alt="'+dh.imgprevmt+'" title="'+dh.imgprevmt+'" onmouseover="replaceImage(this,\''+dh.imgprevmo+'\');window.status=\''+dh.imgprevmt+'\';return true;" onmousedown="replaceImage(this,\''+dh.imgprevmd+'\');" onmouseout="replaceImage(this,\''+dh.imgprevm+'\');window.status=\'\';return true;" onmouseup="replaceImage(this,\''+dh.imgprevm+'\');"></a></td>';
 r+='<td width="84" colspan="'+col+'" align="center"'+this.getCss("css",dh)+'>'+this.def.monthnames[this.date.getMonth()]+'<br>'+this.date.getFullYear()+'</td>';
 if(dh.imgnextm)r+='<td width="28" align="left"'+this.getCss("css",dh)+'><a href="#" onclick="window.sdwdcalendar.setMY('+this.date.getMonth()+','+this.date.getFullYear()+',0);return false;"><img border="0" src="'+dh.imgnextm+'" alt="'+dh.imgnextmt+'" title="'+dh.imgnextmt+'" onmouseover="replaceImage(this,\''+dh.imgnextmo+'\');window.status=\''+dh.imgnextmt+'\';return true;" onmousedown="replaceImage(this,\''+dh.imgnextmd+'\');" onmouseout="replaceImage(this,\''+dh.imgnextm+'\');window.status=\'\';return true;" onmouseup="replaceImage(this,\''+dh.imgnextm+'\');"></a></td>';
 if(dh.imgnexty)r+='<td width="28" align="left"'+this.getCss("css",dh)+'><a href="#" onclick="window.sdwdcalendar.setMY('+this.date.getMonth()+','+this.date.getFullYear()+',2);return false;"><img border="0" src="'+dh.imgnexty+'" alt="'+dh.imgnextyt+'" title="'+dh.imgnextyt+'" onmouseover="replaceImage(this,\''+dh.imgnextyo+'\');window.status=\''+dh.imgnextyt+'\';return true;" onmousedown="replaceImage(this,\''+dh.imgnextyd+'\');" onmouseout="replaceImage(this,\''+dh.imgnexty+'\');window.status=\'\';return true;" onmouseup="replaceImage(this,\''+dh.imgnexty+'\');"></a></td>';
 return r;
};

SDWDc.restrictDate=function(d,m,y){
 this.def.ddate.Y=y;
 this.def.ddate.M=m;
 this.def.ddate.D=d;
};

SDWDc.setCurDate=function(){
 var f=this.def.dtype;
 var q=this.def.drestrict;
 var t;
 var s=this.ctl.value;
 var cc=this.def.ddate;
 var c=new Array(1970,0,1,12,0,0);
 if(s==""||q=="current"){
  this.date=new Date();
  if(q=="current"){
   cc.Y=this.date.getFullYear();
   cc.M=this.date.getMonth();
   cc.D=this.date.getDate();
  }
 if(s=="")return;
 }
 var di=parseDate(s,f);
 if(!di){
  alert(dError[0]);
  return;
 }
 c[0]=di[2];
 c[1]=di[1];
 c[2]=di[0];
 if(q!="no"){
  if(cc.Y>c[0]){
   this.date=new Date(cc.Y,cc.M,cc.D,cc.h,cc.m,cc.s);
   errorOut(this.def.e,2,formatDate(this.def.dtype,this.date.getDate(),this.date.getMonth(),this.date.getFullYear()));
   return;
  }else if(cc.Y==c[0]){
   if(cc.M>c[1]){
    this.date=new Date(cc.Y,cc.M,cc.D,cc.h,cc.m,cc.s);
    errorOut(this.def.e,2,formatDate(this.def.dtype,this.date.getDate(),this.date.getMonth(),this.date.getFullYear()));
    return;
   }else if(cc.M==c[1]){
    if(cc.D>c[2]){
     this.date=new Date(cc.Y,cc.M,cc.D,cc.h,cc.m,cc.s);
     errorOut(this.def.e,2,formatDate(this.def.dtype,this.date.getDate(),this.date.getMonth(),this.date.getFullYear()));
     return;
    }
   }
  }
 }
 this.date=new Date(c[0],c[1],c[2],c[3],c[4],c[5]);
};

SDWDc.popup=function(e){
 window.sdwdcalendar=this;
 this.widget.setXY(e.clientX-50,ua.ie?e.clientY-16:e.clientY);
 this.ctl=getElement(document,"id",this.ctlid,null);
 if(this.ctl)this.setCurDate();
 this.draw();
 this.show();
};

SDWDc.setDay=function(f,d,m,y){
 if(Def(this.ctl)){
  this.ctl.value=formatDate(f,d,m,y);
  if(this.ctl.onchange)this.ctl.onchange();
 }
   this.hide();
};
SDWDc.setMY=function(m,y,f){
 switch(f){
  case 0:{
   ++m;
   if(m==12){
    m=0;
    y++;
   }
   break;
  }
  case 1:{
   --m;
   if(m==-1){
    m=11;
    y--;
   }
   break;
  }
  case 2:{
   ++y;
   break;
  }
  case 3:{
   --y;
   break;
  }
  case 4:{
   break;
  }
 }
 var d=this.def;
 var curday=this.date.getDate();
 if(d.drestrict!="no"){
  if(y<d.ddate.Y){
   errorOut(d.e,1,formatDate(d.dtype,d.ddate.D,d.ddate.M,d.ddate.Y));
   return;
  }else{
   if(y==d.ddate.Y&&m<d.ddate.M){
    errorOut(d.e,1,formatDate(d.dtype,d.ddate.D,d.ddate.M,d.ddate.Y));
    return;
   }
  }
 }
 this.date.setFullYear(y);
 this.date.setMonth(m);
 while (this.date.getDate()<curday) {
  curday--;
  this.date.setMonth(m);
  this.date.setDate(curday);
 }
 this.draw();
};

}

