//
// EbyMonthExt.
//
// You find the documentation in "../doc/EbyMonthExt.htm"
//
// Written by Martin Schädler (ms@alien.de)
//
function EbyMonthExt( nMonth, nYear ) {
        if (!nMonth && !nYear) {
         var actDay = new Date();
         nMonth = actDay.getMonth()+1;
         nYear = actDay.getYear();
         if (nYear<2000) nYear += 1900;
        }
        this.nMonth = nMonth;
        this.nYear = nYear;
        this.nBegin = Date.UTC( nYear, nMonth-1, 1);
        this.nEnd = Date.UTC( nYear, nMonth, 1);
        this.strTableAttr = "";
        this.strTrAttr = "";
        this.strThAttr = "";
        this.strTdAttr = "";
        this.strHeaderPresentation = "{d}";
        this.fHeaderPresentJS = false;
        this.fInclThTag = false;
        this.strCellPresentation = "{d}";
        this.fCellPresentJS = false;
        this.fInclTdTag = false;
        this.strFirstDayOfWeek = "Mo";
        this.intDaySelected = 0;
        this.days = new Array("Su", "Mo","Tu","We","Th","Fr","Sa");
        this.months = new Array("Jan","Feb","Mar","Apr","May","Jun",
                                "Jul","Aug","Sep","Oct","Nov","Dec","Jan");
        return this;
};

EbyMonthExt.prototype.getHtml = function() {
        var fEndReached = false;
        var strHtml="";
        var nColumn = 0;
        var nRow = 0;
        var actDay;
        
        for( nDay=1; !fEndReached; nDay++ )
			{
        	var nAct = Date.UTC( this.nYear, this.nMonth-1, nDay );
            
			if(nAct >= this.nEnd)
				{
                fEndReached = true;
                strHtml += this.getEnd( nColumn );
                }
			else
				{
                actDay = new Date (this.nYear, this.nMonth-1, nDay);
                
                if(this.strFirstDayOfWeek == "Mo")
                	nColumn = (actDay.getDay()==0) ? 6 : actDay.getDay() - 1;
                else
					nColumn = actDay.getDay();
                
				if ( nDay == 1 )
					{
                    strHtml += this.getStart( nColumn );
                    }
                
				if ( nColumn == 0 )
					{
                    nRow++;
					}
				
				strHtml += this.getCell( nRow, nColumn, nDay );
                }
        	}
        
        return strHtml;
};

EbyMonthExt.prototype.getMaxDay = function() {
   return (this.nEnd-this.nBegin)/(1000*60*60*24);
};

EbyMonthExt.prototype.getMonth = function() {
   return this.nMonth;
}

EbyMonthExt.prototype.getMonthName = function() {
   return this.months[ this.nMonth -1 ];
}

EbyMonthExt.prototype.getYear = function() {
   return this.nYear;
}

EbyMonthExt.prototype.nextMonthObj = function() {
   var m = this.nMonth;
   var y = this.nYear;
   if (m==12) {
      m=1;
      y++;
   } else {
      m++;
   }
   var obj = new EbyMonthExt(m,y);
   obj.days = this.days;
   obj.fCellPresentJS = this.fCellPresentJS;
   obj.fHeaderPresentJS = this.fHeaderPresentJS;
   obj.months = this.months;
   obj.strCellPresentation = this.strCellPresentation;
   obj.strHeaderPresentation = this.strHeaderPresentation;
   obj.strTableAttr = this.strTableAttr;
   obj.strTdAttr = this.strTdAttr;
   obj.strThAttr = this.strThAttr;
   obj.strTrAttr = this.strTrAttr;
   obj.fInclThTag = this.fInclThTag
   obj.fInclTdTag = this.fInclTdTag
   return obj;
}

EbyMonthExt.prototype.setCellPresentation = function( strHtml, fJavascript , fInclTdTag ) {
   this.strCellPresentation = strHtml;
   this.fCellPresentJS = ( fJavascript==true );
   this.fInclTdTag = ( fInclTdTag==true );
   }

EbyMonthExt.prototype.setHeaderPresentation = function( strHtml, fJavascript, fInclThTag ) {
   this.strHeaderPresentation = strHtml;
   this.fHeaderPresentJS = (fJavascript==true);
   this.fInclThTag = (fInclThTag==true);
}

EbyMonthExt.prototype.setMonths = function( a,b,c,d,e,f,g,h,i,j,k,l ) {
        this.months = new Array( a,b,c,d,e,f,g,h,i,j,k,l,a );
}

EbyMonthExt.prototype.setTableAttr = function( strTableAttr, strTrAttr,
                                               strThAttr, strTdAttr ) {
        this.strTableAttr = strTableAttr;
        this.strTrAttr = strTrAttr;
        this.strThAttr = strThAttr;
        this.strTdAttr = strTdAttr;
};

EbyMonthExt.prototype.setWeekdays = function( a,b,c,d,e,f,g ) {
        this.days = new Array( a,b,c,d,e,f,g );
}

EbyMonthExt.prototype.setFirstDayOfWeek = function(name) {
        this.strFirstDayOfWeek = name;
}

EbyMonthExt.prototype.setIntDaySelected = function(intDay) {
        this.intDaySelected = intDay;
}

EbyMonthExt.prototype.show = function() {
        document.writeln( this.getHtml() );
};

//-------------- P R I V A T E  - S E C T I O N -----------------------

EbyMonthExt.prototype.getCell = function( nRow, nColumn, nDay ) {
        var strHtml = this.getCellContent(nRow, nColumn, nDay);

		var my_date = new Date();
		
		if(my_date.getDate() == nDay && my_date.getMonth() == (this.nMonth-1) && my_date.getFullYear() == this.nYear)
			strClassAct = " aktuell";
		else
			strClassAct = "";
		
		if(this.intDaySelected == nDay)
			strClassSelected = " gewaehlt";
		else
			strClassSelected = "";

        if ( !this.fInclTdTag ) {
        	// Wochende ?
        	if((nColumn+1) == 6 || (nColumn+1) == 7)
        		strHtml = "<td " + this.strTdAttr + " class=\"ftag" + strClassAct + strClassSelected+"\">" + strHtml + "</td>";
        	else
        		strHtml = "<td " + this.strTdAttr + " class=\"tag" + strClassAct + strClassSelected+"\">" + strHtml + "</td>";
        }
        if ( nColumn == 6 ) {
                strHtml += "</tr>\n<tr " + this.strTrAttr + " class=\"woche\">";
        }
        return strHtml;
};

EbyMonthExt.prototype.getCellContent = function( nRow, nColumn, nDay ) {
        var str = this.strCellPresentation;
        str = str.replace(/{d}/g, "" + nDay);
        str = str.replace(/{m}/g, "" + this.nMonth );
        str = str.replace(/{y}/g, "" + this.nYear );
        str = str.replace(/{r}/g, "" + nRow);
        str = str.replace(/{c}/g, "" + nColumn);
        str = str.replace(/{}/g, "'");

        if ( this.fCellPresentJS ) {
         str = eval( str );
        }
        return str;
}

EbyMonthExt.prototype.getEnd = function( nDayOfWeek ) {
        var strHtml = "";
        for( i=nDayOfWeek; i<6; i++) {
                strHtml += "<td class=\"tag_leer\" " + this.strTdAttr
                        + ">&nbsp;</td>";
        }
        return strHtml + "</tr></table>\n\n";
};

EbyMonthExt.prototype.getStart = function( nDayOfWeek ) {
        var strHtml = "<table "
                    + this.strTableAttr +"><tr " + this.strTrAttr + " class=\"titel\">";
        for (i=0; i<7; i++) {
            var str = this.strHeaderPresentation;
            
            if(this.strFirstDayOfWeek == "Mo")
            	str = str.replace(/{d}/g, "" + this.days[ (i == 6) ? 0 : i + 1 ]);
            else
            	str = str.replace(/{d}/g, "" + this.days[i]);
            
			str = str.replace(/{m}/g, "" + this.nMonth );
            str = str.replace(/{y}/g, "" + this.nYear );
            str = str.replace(/{c}/g, "" + i);
            str = str.replace(/{}/g, "'");
            if ( this.fHeaderPresentJS ) {
               str = eval( str );
            }
            if ( this.fInclThTag ) {
               strHtml += this.strThAttr;
            } else {
               strHtml += "<th " + this.strThAttr + " class=\"tag_head\">"
                        + str + "</th>";
            }
        }
        strHtml += "</tr>\n<tr " + this.strTrAttr + " class=\"woche\">";
        for( i=0; i<nDayOfWeek; i++) {
                strHtml += "<td class=\"tag_leer\" " + this.strTdAttr
                         + ">&nbsp;</td>";
        }
        return strHtml;
};
