//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function calendar(YEAR, MONTH, CALLBACK){
	
	var calendarElementId  = this.getCalendarElementId();
	var startWeekDay 	   = this.getStartWeekDay();
	var imgPath      	   = this.getImgPath();
	var	YearsStart		   = this.getYearsStart();
	var onClickStart 	   = this.getOnClickStart();
	var onClickEnd   	   = this.getOnClickEnd();
	var	YearsEnd		   = this.getYearsEnd();
	var UnclickableMsg 	   = this.getUnclickableMsg();
	var thisYear     	   = parseInt(YEAR);
	var thisMonth    	   = parseInt(MONTH); 
	
	var firstWeekDay = new Date(thisYear, thisMonth, 1).getDay();
	var daysOfMonth  = new Date(thisYear, thisMonth+1, 0).getDate();
	
	var table       = buildCalendar();
	var set         = setCalendar();	
	
	// Kalender zuweisen 
	function setCalendar(){
		
		document.getElementById(calendarElementId).innerHTML = table;
		
	} // end function setCalendar
	
	
	////////////////////////////////////////////////////////////////
	function buildCalendar(){
		
		var table = '';
			table+= '<table class="calendar" border="0" cellpadding="2" cellspacing="0">';
			table+=		getOperations();
			table+=    	getCurrentDate();
			table+=     getNavigation();
			table+=     getColumns();
			table+= 	buildMonth();
			table+= '</table>';
	
		return(table);
			
	} // end function build Calendar
	
	////////////////////////////////////////////////////////////////
	function buildMonth(){
		
		var daysback = (firstWeekDay >= startWeekDay) 
				     ? (firstWeekDay - startWeekDay) 
				     : (7- (startWeekDay - firstWeekDay));	
			
		var month = new Array();
		var day   = 1-daysback;
		
		
		while(month.length < 42){
		
			with(new Date(thisYear, thisMonth, day)){
				 
				 month.push(buildDay(getFullYear(), getMonth(), getDate()));			
				
			} // end with
			day++
		
		}// end while
		
		return(buildWeeks(month));
	
	} // end function buildCalendar

	////////////////////////////////////////////////////////////////
	function buildDay(YEAR, MONTH, DAY){
		
		var utc        = Date.UTC(YEAR, MONTH, DAY); 
		var styleclass = new Array("normal", "soft", "normal_linethrough", "soft_linethrough");
		
		with(new Date(YEAR, MONTH, DAY)){
		
			var soft 		= (getFullYear() == thisYear && getMonth() == thisMonth) ? false  : true;
			var linethrough = (onClickStart <= utc && utc <= onClickEnd)             ? false  : true;
			var onclick 	= CALLBACK + '(new Date(' + YEAR + ',' + MONTH + ',' + DAY + '));';
			var title		= "";
			
			// Der Normalfall
			if(!soft && !linethrough){
				styleclass["input"] =  "normal";
			} // end if
			else{   // Die Ausnahmen 
					var onclick    = 'alert(\'Auswahl nicht möglich\')';
					// Blass und Durchgestrichen
					if(soft && linethrough){				
						styleclass["input"] =  "soft_linethrough";
						title = UnclickableMsg;
					} // end if
			
					// Normal und Durchgestrichen
					if(!soft && linethrough){				
						styleclass["input"] =  "normal_linethrough";
						title = UnclickableMsg;
					} // end if
			
					// Blass und nicht Durchgestrichen
					if(soft && !linethrough){				
						styleclass["input"] =  "soft";
					} // end if
			
			} // end else
						
			var day = '<td class="' + styleclass["td"] + '">';
				day+=	'<input type="text" onClick="' + onclick + '" value="' + DAY + '" size="3" title="' + title + '" class="' + styleclass["input"] + '"  readonly />';
				day+= '</td>';
				
			return(day);
		
		}// end with
		
		return(DAY);
		
	} // end function buils Day	
			
	////////////////////////////////////////////////////////////////
	function buildWeeks(MONTH){
	

		var rows = '';
		for (var i = 0; i < MONTH.length; i+=7){
  		
			rows+= '<tr class="week">';
			rows+= 		MONTH.slice(i,i+7).join('');
			rows+= '</tr>';
  		
		} // end for
		
		return(rows);
		
	} // end function formMonth
	
	////////////////////////////////////////////////////////////////
	function getOperations(){
	
		var calendarClose = '<th colspan="7" align="right">';
			calendarClose+=		'<img alt="Kalender schließen" title="Kalender schließen" src="' + imgPath + 'stop.gif" onClick="document.getElementById(\'' + calendarElementId + '\').innerHTML=\'\'" />';
			calendarClose+= '</th>';
	
		return('<tr>' + calendarClose + '</tr>');
	
	} // end funtion get Operations
	
	////////////////////////////////////////////////////////////////
	function getNavigation(){
		
		var goVeryFastBack = '<span class="navigation">';
		    goVeryFastBack+=	'<img alt="10 Jahre zurück" title="10 Jahre zurück" src="' + imgPath + 'veryfastback.gif" onClick="' + calendarBackward(120) +'"/>';	
		    goVeryFastBack+= '</span>';
		    		  
		var goFastBack = '<span class="navigation">';
		    goFastBack+=	'<img alt="1 Jahr zurück" title="1 Jahr zurück" src="' + imgPath + 'fastback.gif" onClick="' + calendarBackward(12) +'"/>';	
		    goFastBack+= '</span>';
		    

		var goBack = '<span class="navigation">';
		    goBack+=	'<img alt="1 Monat zurück" title="1 Monat zurück" src="' + imgPath + 'back.gif" onClick="' + calendarBackward(1) +'"/>';	
		    goBack+= '</span>';
		
	
		var goAhead = '<span class="navigation" >';
			goAhead+= 	'<img alt="1 Monat vor" title="1 Monat vor" src="' + imgPath + 'ahead.gif" onClick="' + calendarForward(1) +'" />';
			goAhead+= '</span>';
		
		var goFastAhead = '<span  class="navigation">';
		    goFastAhead+=	'<img alt="1 Jahr vor" title="1 Jahr vor" src="' + imgPath + 'fastahead.gif" onClick="' + calendarForward(12) +'"/>';		   
		    goFastAhead+= '</span>';
			
		var goVeryFastAhead = '<span class="navigation">';
		    goVeryFastAhead+=	'<img alt="10 Jahre vor" title="10 Jahre vor" src="' + imgPath + 'veryfastahead.gif" onClick="' + calendarForward(120) +'"/>';		   
		    goVeryFastAhead+= '</span>';
			
		
		  
		return('<tr><th colspan="7">' + goVeryFastBack + goFastBack + goBack  + goAhead + goFastAhead + goVeryFastAhead + '</th></tr>');
	
	
	} // end function getNavigation
	
	
	////////////////////////////////////////////////////////////////
	function getCurrentDate(){
		
		
		var	currentDate = 	'<th colspan="1" height="45" valign="middle">';
			currentDate+= 	'</th>';
		    currentDate+= 	'<th colspan="5" valign="middle">';
		    currentDate+=		'<select title="Wählen Sie einen Monat aus" title="" onChange = "new calendar(' + thisYear + ', this.value,\'' + CALLBACK + '\');">'; 
		    currentDate+=			getMonths();
		    currentDate+=		'</select>';
			currentDate+=			'&nbsp';
			currentDate+=		'<select title="Wählen Sie ein Jahr aus" onChange = "new calendar(this.value ,' + thisMonth + ',\'' + CALLBACK + '\');">'; 
		    currentDate+=			getYears();
		    currentDate+=		'</select>';
			currentDate+= 	'</th>';
			currentDate+= 	'<th colspan="1" valign="middle">';
			currentDate+= 	'</th>';
		
		
		return('<tr>' + currentDate + '</tr>');
		
	} // end function getTop

	
	////////////////////////////////////////////////////////////////
	function getColumns(){
	
		// startWeekDay existiert Klassenweit
		var start = parseInt(startWeekDay);
		var end   = start + 7;
		
		
		
		var sunday    = '<th class = "sunday"  >So</th>';
		var monday    = '<th class = "monday"  >Mo</th>';
		var tuesday   = '<th class = "tuesday" >Di</th>';
		var wednesday = '<th class = "wendsday">Mi</th>';
		var thursday  = '<th class = "thursday">Do</th>';
		var friday    = '<th class = "friday"  >Fr</th>';
		var saturday  = '<th class = "saturday">Sa</th>';
	
		var columns = new Array(sunday, monday, tuesday, wednesday, thursday, friday, saturday,
			                    sunday, monday, tuesday, wednesday, thursday, friday, saturday);
	   
		
		return('<tr class="columns">' + columns.slice(start, end).join('') + '</tr>');
	
	}// end private function getWeekDays
	
	////////////////////////////////////////////////////////////////
	function getYears(){
		
		var options = new Array();
			
			//options.push('<option value="' + (thisYear + 10)  + '">10 Jahre vor</option>');
			//options.push('<option value="' + (thisYear - 10)  + '">10 Jahre zurück</option>');
			//options.push('<option value="' +  thisYear  + '"  selected>' + thisYear + '</option>');
		
			
			for (var i = Math.max(YearsEnd, thisYear); i >= Math.min(YearsStart, thisYear); i--){
				
				if(thisYear != i){
					options.push('<option value="' +  i  + '">' + i + '</option>');
				}// end if
				else{	
					options.push('<option value="' +  i  + '" selected>' + i + '</option>');
				}// end else
			
			} // end for
			
			return(options.join(""));
	
	} // end function 
	
	////////////////////////////////////////////////////////////////
	function getMonths (){
	
		var months =  new Array();
			months.push('Januar', 'Februar','März','April','Mai', 'Juni');
			months.push('Juli', 'August','September', 'Oktober','November', 'Dezember');					
								
		var options  = new Array();
		var selected = "";
		
		
		for (var i = 0; i < months.length; i++){
		
			selected = (i == thisMonth) ? "selected" : "";
			options.push('<option value="'+ i +'" ' + selected + '>' + months[i] + '</option>')
			
		}
		
		
		return(options.join(""));
	
	} // end public function getMonths
	
	
	////////////////////////////////////////////////////////////////
	function calendarForward(STEP){
		
		with(new Date(thisYear, thisMonth + STEP)){
			
			return('new calendar('+ getFullYear() +',' + getMonth() + ',\'' + CALLBACK + '\');');
			
		} // end with
		
	} // end function calendarForward
	
	
	////////////////////////////////////////////////////////////////
	function calendarBackward(STEP){
		
		with(new Date(thisYear, thisMonth - STEP)){
			
			return('new calendar('+ getFullYear() +',' + getMonth() + ',\'' + CALLBACK + '\');');
			
		} // end with
		
	} // end function calendarForward
	
	
} // end function calendar

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////