var d,m,y,jd,aky,akm,akd,akjd;

dat=new Date();
aky = dat.getYear()%100;
aky += (aky>97? 1900 : 2000);
akm = parseInt(dat.getMonth()+1);
akd = dat.getDate();
akjd= getJD(aky,akm,akd)+0.5;

function div(a,b){
   var q=a/b;
   if (q<0)
      return Math.ceil(q); else
      return Math.floor(q);
   } //div

function twoDigs(x){
   x=Math.floor(x);
   if (x<10) return ("0"+x); else return x;
   }  // just positive

function wt(y,m,d){
/* 1986-1999 by Thomas K&ouml;hler. All rights reserved. */
   var w = y%28;
   w += Math.ceil(w/4);
   if ( (m==2)||(m==3)||(m==11) ) w+=3;
   if ( (m==4)||(m==7) ) w+=6;
   if ( m==5 ) w+=1;
   if ( m==6 ) w+=4;
   if ( m==8 ) w+=2;
   if ( (m==9)||(m==12) ) w+=5;
   if (m>2) w+=sj(y);
   if (y>1582){
      var y1=Math.floor((y-1)/100);  // ??
      w+=Math.floor(y1/4)+15-y1;
      }
   else w+=3;
/* julianisch */
   if ( (y<1582) || ((y==1582)&&(m<10)) ) w+=3;
   if ( (y==1582)&&(m==10)&&(d<15) ) w+=3;
/* /julianisch */
   w=(w+d+7004)%7; // 1 -> Mo, auch grosse Zahlen
   if (w==0) w=7;
   return(w);
   }  // wt


function maxTag(y,m){
   var mt=0;
   if (m<8){
      if (m%2==1) mt=31; else mt=30;
      }
   if (m>7){
      if (m%2==0) mt=31; else mt=30;
      }
   if (m==2) mt=28+sj(y);
   return(mt);
   } //maxTag



function sj(y){
var x=0;
   if ( (y<1583)&&(y%4==0) ) x=1;
   if ( (y>1582)&&(y%4==0)&&( (y%100!=0)||(y%400==0) ) ) x=1;
   return x;
   }


function change(what,how){
var d1form=document.f;
   y=parseInt(d1form.y.value);
   m=parseInt(d1form.m.value);
   if (what==2){
      if (how==1){
         m++;
         if (m>12){y++; anzYear=y; m=1;}
         }
      if (how==-1){
         m--;
         if (m<1){y--; anzYear=y; m=12;}
         }
      anzMonth=m;
      }
   if (what==1){
      if (how==1) y++;
//      if (y>4099) {y=4099; dform.y.value=4099};
      if (how==-1) y--;
      anzYear=y;
      }
   d1form.y.value=y;
   d1form.m.value=m;
   d=parseInt(d1form.d.value);

   d1form.jd.value=getJD(y,m,d)+0.5;
   } //change

function getJD(Y,M,D){
   var B=0;
   if (M<3) {Y--; M+=12;}
   if ( (Y>1582)||( (Y==1582)&&(M>10) )||( (Y==1582)&&(M==10)&&(D>14) ) ){
      var A=Math.floor(Y/100);
      B=2-A+Math.floor(A/4);
      }
   return(Math.floor(365.25*(Y+4716))+Math.floor(30.6001*(M+1))+D+B-1524.5);
   }

function ymdDate(y,m,d){   //  replaces the Date()-Obj 4 old Browsers
   this.y=y;
   this.m=m;
   this.d=d;
   var evt="";
   var hilite=-1;
   var start=-1;
   var explain="";
   this.DoM=Math.floor(d);
   this.hour=Math.floor( (d-this.DoM)*24 );
   this.min=Math.round( (d-this.DoM)*24*60-this.hour*60 );
   if (this.min==60){
      this.min=0; this.hour++;
      }  // don't care 4 midnite
   }


function parseDate(JD){
   var A,a;
   JD+=0.5;
   var Z=Math.floor(JD);
   var F=JD-Z;
   if (Z<2299161)
      A=Z;
   else{
      a=div( (Z-1867216.25),36524.25 );
      A=Z+1+a-div(a,4);
      }
   var B=A+1524;
   var C=div((B-122.1),365.25);
   var D=Math.floor(365.25*C);
   var E=Math.floor((B-D)/30.6001);
   var J_DoM=B-D-Math.floor(30.6001*E)+F;  //  real
   var J_Month=(E<14)? E-1 : E-13;
   var J_Year=(J_Month>2)? C-4716 : C-4715;
   return new ymdDate(J_Year,J_Month,J_DoM);
   } // parseDate


function today(){
  var d1form=document.f;
   y=aky; anzYear=y;
   m=akm; anzMonth=m;
   d=akd; anzday=d;
   d1form.y.value=aky;
   d1form.m.value=akm;
   d1form.d.value=akd;
   d1form.jd.value=akjd;
   } // today

function writeJD(){  // in2 the form
   var df=document.f;
   var y0=parseInt(df.y.value);
   var m0=parseInt(df.m.value);
   var d0=parseInt(df.d.value);
   var myjd=getJD(y0,m0,d0)+0.5;
   df.jd.value=myjd;
   }

function getDoWName(n){
   if (n==1) return "Mo"; else
   if (n==2) return "Di"; else
   if (n==3) return "Mi"; else
   if (n==4) return "Do"; else
   if (n==5) return "Fr"; else
   if (n==6) return "Sa"; else
   return "So";
   }

function getDoWLongName(n){
   if (n==1) return "Montag"; else
   if (n==2) return "Dienstag"; else
   if (n==3) return "Mittwoch"; else
   if (n==4) return "Donnerstag"; else
   if (n==5) return "Freitag"; else
   if (n==6) return "Samstag"; else
   return "Sonntag";
   }



function getMonthName(mon){
   if (mon==1) return('Januar'); else
   if (mon==2) return('Februar'); else
   if (mon==3) return('M&auml;rz'); else
   if (mon==4) return('April'); else
   if (mon==5) return('Mai'); else
   if (mon==6) return('Juni'); else
   if (mon==7) return('Juli'); else
   if (mon==8) return('August'); else
   if (mon==9) return('September'); else
   if (mon==10) return('Oktober'); else
   if (mon==11) return('November'); else
   return('Dezember');
   }

function KW1(y,m,d){
   if (y<1976) return "";

   var DoYear=getJD(y,m,d+1)-getJD(y,1,1);
   var temp=wt(y,1,1);
   var K=div((DoYear+5+temp),7);
   if (temp>4) K--;
   if ((K==0)&&(y>1976)){
      temp=wt(y-1,1,1);
      K=div( (370+sj(y-1)+temp),7 );
      if (temp>4) K--;
      }
   if ( (m==12)&&(d>28)&&(wt(y,12,31)<4) )  K=1;
   return K;
   }


function getJMoon(y,m,full){
   if (full==0) y+=(m-0.5)/12;  // NM
   if (full==2) y+=(m-1)/12;    // VM
   var k=0;
   if (full==0)  // NM
      k=Math.floor((y-2000)*12.3685);
   else if (full==2)  // VM
      k=Math.floor((y-2000)*12.3685)+0.5;
   var T=k/1236.85;
   var T2=T*T,T3=T2*T,T4=T2*T2;
   var JD=2451550.09765+29.530588853*k
       +0.0001337*T2
       -0.00000015*T3
       +0.00000000073*T4;
   var E=1-0.002516*T-0.0000074*T2;
   var M=(2.5534+29.10535669*k-0.0000218*T2-0.00000011*T3)%360;
   var M1=(201.5643+385.81693528*k+0.1017438*T2+0.00001239*T3-0.000000058*T4)%360;
   var F=(160.7108+390.67050274*k-0.0016341*T2-0.00000227*T3+0.000000011*T4)%360;
   var Z=(124.7746-1.5637558*k+0.0020691*T2+0.00000215*T3)%360;
   if (M<0) M+=360; if (M1<0) M1+=360; if (F<0) F+=360; if (Z<0) Z+=360;
   M=M/180*Math.PI; M1=M1/180*Math.PI; F=F/180*Math.PI; Z=Z/180*Math.PI;
   var korr=0;
   if (full==0){
      korr+= -0.4072*Math.sin(M1)
             +0.17241*E*Math.sin(M)
             +0.01608*Math.sin(2*M1)
             +0.01039*Math.sin(2*F)
             +0.00739*E*Math.sin(M1-M)
             -0.00514*E*Math.sin(M1+M)
             +0.00208*E*E*Math.sin(2*M)
             -0.00111*Math.sin(M1-2*F)
             -0.00057*Math.sin(M1+2*F);
             }
   if (full==2){
      korr+= -0.40614*Math.sin(M1)
             +0.17302*E*Math.sin(M)
             +0.01614*Math.sin(2*M1)
             +0.01043*Math.sin(2*F)
             +0.00734*E*Math.sin(M1-M)
             -0.00515*E*Math.sin(M1+M)
             +0.00209*E*E*Math.sin(2*M)
             -0.00111*Math.sin(M1-2*F)
             -0.00057*Math.sin(M1+2*F);
             }
   JD+=korr;
   var dT=0;
   if ( (y<1667)||(y>1950) ) dT=60/86400;
   JD-=dT;
   return (JD);
   }   // getJMoon


function moonDate(y,m,full,which){
   var N1Date=parseDate(getJMoon(y,m,full));
   if (N1Date.m!=m)
      N1Date=parseDate(getJMoon(y,m+0.5,full));
   var N2Date=parseDate(getJMoon(y,m+1,full));
   var V1Date=parseDate(getJMoon(y,m,2));
   if (V1Date.m!=m)
      V1Date=parseDate(getJMoon(y,m+0.5,2));
   var V2Date=parseDate(getJMoon(y, m+1,2));

   if (full==0){ // Neumond
      if ( (N1Date.m==m)&&(which==1) )
         return N1Date; else
         if ( (N2Date.m==m)&&(which==2)&&(N2Date.d!=N1Date.d) )
            return N2Date; else
            return new ymdDate(0,0,-99);
      }
   if (full==2){ // Vollmond
      if ( (V1Date.m==m)&&(which==1) )
         return V1Date; else
         if ( (V2Date.m==m)&&(which==2)&&(V2Date.d!=V1Date.d) )
            return V2Date; else
            return new ymdDate(0,0,-99);
      }
   }  // moonDate

function HoursMinutes(time) { // fuer SASU
 var h = Math.floor(time), min = Math.round(60*(time-h));
 if(min==60){min=0; h++; }
 h%=24;
 return h+":"+((min>9)? min : "0"+min);
 }

function MTable(Y0,M0,size0){
   document.write("<TABLE border cellspacing=1 cellpadding="+(size0-1)+" bgcolor="+( (size0==3)? '#D0E0D0' : '#F0FFF0' )+">");
   document.write("<TR bgcolor="+( (M0==anzMonth)? '#FFB060' : '#C0D0C0' )+"><TD colspan=8><FONT size=2><I><B>"+getMonthName(M0)+" " + ( (Y0<1)?(twoDigs(1-Y0)+" vor Chr."):((Y0<100)?("00"+twoDigs(Y0)):Y0) )+ "</B></I></FONT></TD></TR>");
   document.write("<TR bgcolor='#B0E060'><TH bgcolor='#C0D0C0'><FONT size='1' color='#1010F0'>" + ( (Y0>1975)?"KW" : "" ) + "</FONT></TH><TH width='40'><FONT size="+size0+"><I>Mo</TH><TH width='40'><FONT size="+size0+"><I>Di</TH><TH width='40'><FONT size="+size0+"><I>Mi</TH><TH width='40'><FONT size="+size0+"><I>Do</TH><TH width='40'><FONT size="+size0+"><I>Fr</TH><TH width='40'><FONT size="+size0+"><I>Sa</TH><TH width='40'><FONT size="+size0+"><font color='red'><I>So</TH></TR><TR>");
   var WT1=wt(Y0,M0,1); MTM=maxTag(Y0,M0);
   var maxI=Math.ceil( (MTM+WT1-1)/7 )*8+1;
   var w1=2-WT1; //   w1==1 on the i-th position
   var aKW=parseInt(KW1(Y0,M0,1))-1;

      VDay1=moonDate(Y0,M0,2,1).DoM;
      VDay2=moonDate(Y0,M0,2,2).DoM;
      NDay1=moonDate(Y0,M0,0,1).DoM;
      NDay2=moonDate(Y0,M0,0,2).DoM;

      if ( (M0==anzMonth) && (VDay1<NDay1) ) VMis1st=1;

   for (i=1; i<maxI; i++){
      if ( (Y0==1582)&&(M0==10)&&( (w1>4)&&(w1<15) ) ) w1+=10;
      document.write("<TD align='right'"+( ((w1==d)&&(i%8!=1)&&(M0==anzMonth))? "bgcolor='#D0D0FF'>" : ">" ) );
      if (i%8==0) document.write("<FONT color='#FF0000'>");

      if (maxhiDate > 0){ // not showed in kalJahr
      for (j=1; j<=maxhiDate; j++)
      if ( (hiDate[j].m==M0)&&(Math.floor(hiDate[j].d)==w1)&&(i%8!=0)&&(i%8!=1) ){
         if (hiDate[j].hilite==2) document.write("<FONT color='#FF0000'><B>"); else
         if (hiDate[j].hilite==1) document.write("<FONT color='#00B000'><B>");
         }
      }// maxhi >0

      if (i%8!=1){
         if ( (w1==VDay1)||(w1==VDay2) ) document.write("<IMG src='../img/vm.gif'>&nbsp;");
         if ( (w1==NDay1)||(w1==NDay2) ) document.write("<IMG src='../img/nm.gif'>&nbsp;");
         }
      if (i%8==1) document.write("<FONT size='1' color='#1010F0'>"+ ( (aKW<52)? ++aKW : KW1(Y0,M0,w1) )+"</FONT>");
      else{
			if (size0==2) document.write("<FONT size='2'>");
         if ( (w1>0)&&(w1<=MTM) ) document.write(w1);
         else
         document.write("&nbsp;");
         }
      document.write("</FONT></TD>");
      if (i%8!=1) w1++;
      if (i%8==0) document.write("</TR><TR>");
      }
   document.write("</TR></TABLE>");
   } // MTable()

