var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
var selectedClass = 'selected';
var stDateElementId = 'startDateCal';
var endDateElementId = 'endDateCal';
var timeTypeElementId = 'timeType';
var classDay = 'days';
var hideAtStop = false;
var themonths=['Jan','Feb','Mar','Apr','Mai','Jun',
'Jul','Aug','Sep','Okt','Nov','Dez'];
var todaydate=new Date();
var curmonth=todaydate.getMonth()+1; //get current month (1-12)
var curyear=todaydate.getFullYear();
var selectedDate;
var place = 'calendarDiv';
var field;

function selectMonth(m,y){
/*	setVal('monthly',timeTypeElementId);
	setDate(1,m,y,stDateElementId);
	setDate(dim[m-1],m,y,endDateElementId);
	submitForm(1,stDateElementId,endDateElementId,timeTypeElementId);
	*/
//setVal(\'monthly\',\''+timeTypeElementId+'\');setDate('+1+','+m+','+y+',\''+stDateElementId+'\');';
//t+='setDate('+dim[m-1]+','+m+','+y+',\''+endDateElementId+'\');highlight(1,'+31+',\''+cD+'\',\''+selectedClass+'\');submitForm(0,\''+stDateElementId+'\',\''+endDateElementId+'\',\''+timeTypeElementId+'\');
}
function setDate(day,month,year,elementId){
	field.value = day + '.' + month + '.' +year;
	hideMe();
}

function setVal(val,elementId){
	if(document.getElementById(elementId))
		document.getElementById(elementId).value=val;
}
function hideMe(){
	if(hideAtStop){
		document.getElementById('calendarDiv').style.display = 'none';
	}
}

function delMe(elementId) {
	if(document.getElementById(elementId)) {
		document.getElementById(elementId).value='';
	}
	submitForm(1);
	hideMe();
	
}
function showCal(el){
 //get current year
 	field = el;
 	
	stDateElementId = el.id;
	
	if(el.value != ''){
		var sDate = el.value.split('.');
		if(sDate && sDate.length == 3){
			var day = sDate[0];
			var month = sDate[1];
			var year = sDate[2];
			//alert(year+ "," + month +","+day);
			selectedDate = new Date();//year,month-1,day
			selectedDate.setDate(day);
			selectedDate.setMonth(month-1);
			selectedDate.setFullYear(year);
			curmonth = month;
		}
	}else{ 
	}
	//alert(selectedDate.getMonth()+"/"+selectedDate.getYear());
	if(selectedDate != null)
		document.getElementById('calendarDiv').innerHtml= buildCal(selectedDate.getMonth() +1, selectedDate.getFullYear(), "main", "month", "daysofweek", "days", 0,'startDate','endDate','timeType');
	else
		document.getElementById('calendarDiv').innerHtml= buildCal(curmonth, curyear, "main", "month", "daysofweek", "days", 0,'startDate','endDate','timeType');
	
	//document.getElementById('monthPicker').selectedIndex = curmonth-1;
	hideAtStop = true;
	
}

function highlight(start,end,classDay,selectedClass){
/*	var el;
	for(var i = 0;(el= document.getElementsByName("tdDay")[i]);i++)
		if(el && el.className && el.className == classDay)
			el.className=classDay;
	for(var i = start; i < end+1; i++){
		if(document.getElementById('tdDay'+i)){
			document.getElementById('tdDay'+i).className = selectedClass;
		}
	}*/
}

function submitForm(f){
	//if(document.forms[f] && document.getElementById(stDateElementId)){
		//else alert(' could not be assigned to '+timeTypeElementId);
		//document.getElementById('calendarForm').submit();
	//	document.forms['calendarForm'].fakeSubmitButton.click();
		//document.forms[f].submit();	
//	}
	//else alert('');
}
function selectYear(y){
/*	setVal('yearly',timeTypeElementId);
	setDate(1,1,y,stDateElementId);
	setDate(31,12,y,endDateElementId);
	highlight(1,31,classDay,selectedClass);
	submitForm(1);
	*/
}

function updateCalendar(isMonth,period){
	if(period == 'hide'){
		if (document.getElementById)
			document.getElementById(place).innerHTML='';
		return;
	}
	var calendarstr='';
	if(isMonth){
		//curmonth=parseInt(theselection[theselection.selectedIndex].value)+1;
		curmonth=parseInt(period)+1;
		calendarstr=buildCal(curmonth, curyear, "main", "month", "daysofweek", "days",0);
	}else{
		//curyear = parseInt(theselection[theselection.selectedIndex].value);
		curyear = parseInt(period);
		calendarstr=buildCal(curmonth, curyear, "main", "month", "daysofweek", "days",0);
	}
	if (document.getElementById)
		document.getElementById(place).innerHTML=calendarstr;
	//only for debug:
	//document.getElementById("user_eingabe").value=calendarstr;
}

function showCalendar(el){
	this.field = el;

	showCal(el);
	if(selectedDate != null)
		document.getElementById('calendarDiv').innerHTML = buildCal(selectedDate.getMonth() +1, selectedDate.getFullYear(), "main", "month", "daysofweek", "days", 0,'startDate','endDate','timeType');
	else
		document.getElementById('calendarDiv').innerHTML = buildCal(curmonth, curyear, "main", "month", "daysofweek", "days", 0,'startDate','endDate','timeType');
	
	//document.getElementById('monthPicker').selectedIndex = curmonth-1;
	document.getElementById('calendarDiv').style.display = 'block';
}
	
function makeSelectBoxes(){
	var res = '<select id="monthPicker" onChange="updateCalendar(true,this.options[this.selectedIndex].value)">\n';
	for (i=0; i<12; i++) //display option for 12 months of the year
		res +='<option value="'+i+'" ' + (i+1 == curmonth ?'selected':'')+'>'+themonths[i]+'</option>\n';
	res += '</select>\n';
	res += ' - '; 
	res += '<select id="yearPicker" onchange="updateCalendar(false,this.options[this.selectedIndex].value);">\n';
	for(j=todaydate.getFullYear()+1; j >= todaydate.getFullYear()-1; j--)
		res += '<option value=\"'+j+'\" '+(j == curyear?'selected':'')+'>'+j+'</option>\n';
	res += '</select>\n';
	return res;
}

function changeMonth(diff){
	var monthPicker = document.getElementById('monthPicker');
	var yearPicker = document.getElementById('yearPicker');
	if((monthPicker.selectedIndex > 0 && diff == -1) ||(monthPicker.selectedIndex < monthPicker.length - 1 &&diff==1)){
		monthPicker.selectedIndex += diff;
		updateCalendar(true,monthPicker.options[monthPicker.selectedIndex].value);
		selectMonth(parseInt(monthPicker.options[monthPicker.selectedIndex].value)+1,yearPicker.options[yearPicker.selectedIndex].value);
	}else{
		diff = -diff;
		if((yearPicker.selectedIndex > 0 && diff == -1)||(yearPicker.selectedIndex < yearPicker.length - 1 && diff == 1)){
			yearPicker.selectedIndex += diff;
			if(diff == -1){//+1
				monthPicker.selectedIndex = 0;
				curmonth = 1;
			}else{
				monthPicker.selectedIndex = monthPicker.length-1;
				curmonth = 12;
			}
			updateCalendar(false,yearPicker.options[yearPicker.selectedIndex].value);
			selectMonth(parseInt(monthPicker.options[monthPicker.selectedIndex].value)+1,yearPicker.options[yearPicker.selectedIndex].value);
		}
	}
}

function makeCalendarNavigation(){
	var res = '<div id="calendarNavigation">\n';
	res += '<a href="javascript:hideMe()">[ schliessen ]</a><a href="javascript:delMe(stDateElementId)">[ l&ouml;schen ]</a>';
	res += '</div>\n';
	return res;
}

function buildCal(m, y, cM, cH, cDW, cD, brdr){
classDay = cD;
var mn=['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
var oD = new Date(y, m-1, 0); //DD replaced line to fix date bug when current day is 31st, with 0 start on monday,1 start week on sunday
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st
var todaydate=new Date() //DD added
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

var t = '\n';
t+='<div class="'+cM+'">\n<table class="'+cM+'" cols="7">\n<tr>\n';
t+='<td colspan="7" class="'+cH+'">';
/*t+='<a href="javascript:selectMonth('+m+','+y+',\''+cD+'\');" class="'+cH+'">';
//function selectMonth(m,y,stDateElementId,endDateElementId,timeTypeElementId,classDay){
t+=mn[m-1];
t+='</a>';
t+=' - ';
t+='<a href="javascript:selectYear('+y+');" class="'+cH+'">';
t+=y + '</a>';
*/

t += '<a href="javascript:void(0)" onclick="changeMonth(-1)">&lt;&lt;</a>';
t +=makeSelectBoxes();
t += '<a href="javascript:void(0)" onclick="changeMonth(1)">&gt;&gt;</a>';

t+='</td></tr>\n<tr>';//<td class="'+cH+'">&nbsp;</td>
for(s=0;s<7;s++)t+='<td class="'+cDW+'" colspan="1">'+"MDMDFSS".substr(s,1)+'</td>';
t+='</tr>\n<tr>';
var firstDay = 1;
var lastDay = 2;
var makeSelLastWeek = false;
var d;
for(i=1;i<=42;i++){
	var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
	d = x;
	if (x==scanfortoday){ //DD added
		x='<span id="today">'+x+'</span>'; //DD added
	}
	if(d != '&nbsp;'){
		t+='<td id="tdDay'+d+'" name="tdDay" class="'+(i != 0 && (i%7==0 || (i+1) % 7 ==0)?'weekend':cD)+'">';
		makeSelLastWeek = true;
		t+='<a href="javascript:setVal(\'daily\',\''+timeTypeElementId+'\');setDate('+d+','+m+','+y+',\''+stDateElementId+'\');';
		t+='setVal(\'\',\''+endDateElementId+'\');highlight('+d+','+d+',\''+cD+'\');submitForm(1);">';
	}else t+='<td class="'+cD+'">';
	t+=x;
	if(d != '&nbsp;'){
		t+='</a>';
		lastDay = d;
	}
	t+='</td>';
	if((i%7==0)&&(i<36)){
		if(d != '&nbsp;')
			firstDay = d+1;
		t+='</tr>\n<tr>';
		makeSelLastWeek = false;
	}
}

 t+='</table>\n</div>\n' + makeCalendarNavigation();
return t;
}

