function Calendar(m, y, urlParamMode, targetId, calendarName, targetFunctionName, futureSelectable){
	this.myName = calendarName;
	this.month = m;
	this.year = y;
	this.target = targetId;
	this.mode = urlParamMode;
	this.futureSelectable = futureSelectable;
	//this.dayStr = "MTWTFSS";
	this.dayStr = "MDMDFSS";
	this.hourStr="hour ";
	this.minStr = "minute ";
	this.secondStr = "second ";
	this.millisStr = "millisecond ";
	//this.months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	this.months = ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
	this.inputFields = new Array();
	this.myTargetFunction = targetFunctionName;
	if(!this.myTargetFunction){
		this.myTargetFunction = "";
	}
	this.showHour=false;
	this.showSeconds=false;
}

function CalendarSetInputFields(inputTotalMillis, inputYear, inputMonth, inputDay, inputHour, inputMinute, inputSecond, inputDDMMYYYY){
	this.inputFields = new Array();
	this.inputFields['total'] = inputTotalMillis;
	this.inputFields['year'] = inputYear;
	this.inputFields['month'] = inputMonth;
	this.inputFields['day'] = inputDay;
	this.inputFields['hour'] = inputHour;
	this.inputFields['minute'] = inputMinute;
	this.inputFields['second'] = inputSecond;
	this.inputFields['DDMMYYYY'] = inputDDMMYYYY;
}

function CalendarWrite(){
	if(document.getElementById(this.target)){
		document.getElementById(this.target).innerHTML = this.build();
	}
}

function CalendarPrevious(){
	if(this.month > 1){
		this.month--;
	}
	else{
		this.month = 12;
		this.year--;
	}
	this.write();
}

function CalendarNext(){
	if(this.month < 12){
		this.month++;
	}
	else{
		this.month = 1;
		this.year++;
	}
	this.write();
}


function CalendarSetDate(day, month, year, hour, minute, seconds, millis){
	var date;
	
	if ( !this.showHour ) {
		hour = 1;
		minute = 1;
	}
	
	if ( !hour ) {
		hour = document.forms['time'].hour.value;
	}
	if ( !minute ) {
		minute = document.forms['time'].minute.value;
	}
	
	
	if ( seconds ) {
		date = new Date(year, month-1, day, hour, minute, seconds);
	}
	else {
		if ( this.mode == 'from' ) {
			date = 	new Date(year, month-1, day, hour, minute, 0);
		}
		else if ( this.mode == 'to' ) {
			date = new Date(year, month-1, day, hour, minute, 59);
		}
		if ( this.showSeconds ) {
			seconds = document.forms['time'].second.value;
			date.setSeconds(seconds);
		}
	}
	if ( millis ) {
		date.setMilliseconds(millis);
	}
	if( ! date ) {
		this.close();
		return;
	}
	if(this.inputFields['year']){
		this.inputFields['year'].value=date.getFullYear();
	}
	if(this.inputFields['month']){
		this.inputFields['month'].value=date.getMonth()+1;
	}
	if(this.inputFields['day']){
		this.inputFields['day'].value=date.getDate();
	}
	if(this.inputFields['hour']){
		this.inputFields['hour'].value=date.getHours();
	}
	if(this.inputFields['minute']){
		this.inputFields['minute'].value=date.getMinutes();
	}
	if(this.inputFields['second']){
		this.inputFields['second'].value=date.getSeconds();
	}
	if(this.inputFields['total']){
		this.inputFields['total'].value=date.getTime();
	}
	if(this.inputFields['DDMMYYYY']){
		var dayPlaceholder = "";
		if ( day < 10 ) {
			dayPlaceholder = "0";
		}
		var monthPlaceholder = "";
		if ( month < 10 ) {
			monthPlaceholder = "0";
		}
		this.inputFields['DDMMYYYY'].value= dayPlaceholder+day+"."+monthPlaceholder+month+"."+year;
	}
	this.close();
}

function CalendarClose(){
	document.getElementById(this.target).style.visibility="hidden";
	if(window.opera){
		document.getElementById(this.target).style.display="none";
	}
}

function CalendarBuild(){
	var y = this.year;
	var m = this.month;
	var urlParamMode = this.mode;
	var cM="main";
	var cH="month";
	var cDW="daysofweek";
	var cD="days";
	var brdr=1;
	var mn=this.months;
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
	
	var aDate = new Date(y, m-1, 1);
	aDate.firstWeekDay=aDate.getDay();
	var todaydate=new Date();
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0;
	
	dim[1]=(((aDate.getFullYear()%100!=0)&&(aDate.getFullYear()%4==0))||(aDate.getFullYear()%400==0))?29:28;
	
	if ( m > 12 ) {
		m=1;
		y=y+=1;
	}
	
	if ( m < 1 ) {
		m=12;
		y=y-=1;
	}
	
	var lastMonth=m-1;
	var nextMonth=m+1;
	var t='<form name="time">';
	t+='<div class="'+cM+'"><table class="'+cM+'" cols="9" cellpadding="0" border="'+brdr+'" cellspacing="0"><tr align="center">';
	
	//t+='<td class="'+cH+'">&nbsp;</td>';
	t+='<td class="'+cH+'"><a class="'+cH+'" href="javascript:void(0);" onclick="calendar.previous();">&#60;</a>';
	// month, year
	t+='</td><td colspan="5" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td>';
	
	t+='<td class="'+cH+'">';
	if ( y == todaydate.getFullYear() && m <= todaydate.getMonth() ) {
	  t+= '<a class="'+cH+'" href="javascript:void(0);" onclick="calendar.next();">&#62;</a>';
	}
	else {
		if ( y < todaydate.getFullYear() || this.futureSelectable && y <= todaydate.getFullYear()+1 ) {
		  t+= '<a class="'+cH+'" href="javascript:void(0);" onclick="calendar.next();">&#62;</a>';
		}
		else {
		  t+= '&nbsp;';
		}
	}
	t+= '</td><td class="'+cD+'" rowspan="9" valign="top"><a href="javascript:void(0);" onclick="calendar.close()"><img src="/iptv/assets/global/images/icons/cancel_16x16.gif" border="0" /></a></div>';
	t+= '</td></tr><tr align="center">';

	var week = 1;	
	// day name column
	//t+='<td class="'+cDW+'">&nbsp;</td>';
	for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+this.dayStr.substr(s,1)+'</td>';
	t+='</tr><tr align="center">';
	//t+='<td class="'+cD+'"><a class="link" href="javascript:setWeek('+y+','+m+','+(week++)+');">&#62;</a></td>';

	// dates
	var isLaterThanToday = false;
	//if firstWeekDay = 0 it is sunday!
	if(aDate.firstWeekDay == 0){
		aDate.firstWeekDay = 7;
	}
	for(var i=1;i<=42;i++){
		
	  var x=((i-aDate.firstWeekDay>=0)&&(i-aDate.firstWeekDay<dim[m-1]))? i-aDate.firstWeekDay+1 : '&nbsp;';
	  var day = x;
	  if (x==scanfortoday) {
		  x='<span id="today">'+x+'</span>';
	  }
	  else if ( isLaterThanToday && !this.futureSelectable ) {
		  x='<span id="invalid">'+x+'</span>';
	  }
	  t+='<td class="'+cD+'">';
	  if ( (!isLaterThanToday || this.futureSelectable) && day != '&nbsp;' ) t+= '<a class="link" href="javascript:void(0);" onclick="' + this.myName + '.setDate('+day+','+m+','+y+');'+this.myTargetFunction+'">';
	  t+=x;
	  if ( (!isLaterThanToday || this.futureSelectable) && day != '&nbsp;' ) t+= '</a>';
	  t+='</td>';
	  if((i)%7==0 && (i<36)) {
	  	t+='</tr><tr align="center">';
	  }
	  /*if((i)%7==0 && (i<36)) {
	  	t+='</tr><tr align="center"><td class="'+cD+'">';
	  	t+= (day != '&nbsp;' && day != dim[m-1]) ? '<a class="link" href="javascript:setWeek('+y+','+m+','+(week++)+');">&#62;</a>' : '&nbsp;';
	  	t+='</td>';
	  }*/
	  
	  if (day==scanfortoday) {
	  	  isLaterThanToday = true;
	  }
	}
	t+='</tr>';
	
	if ( this.showHour ) {
		t+='<tr align="center">';
		
		t+='<td colspan="4" align="center" valign="middle" class="'+cD+'">'+ this.hourStr;
		t+='<select name="hour">';
		var optionToSelect = (urlParamMode=='from' ? 0 : 23 );
		for(hour=0;hour<24;hour++){
			t+='<option value="'+hour+'"'+ (hour==optionToSelect ? " selected" : "") +'>'+hour+'</option>';
		}
		t+='</select>';
		t+='</td>';
		t+='<td colspan="4" align="center" valign="middle" class="'+cD+'">'+ this.minStr;
		t+='<select name="minute">';
		optionToSelect = (urlParamMode=='from' ? 0 : 59 );
		for(minutes=0;minutes<60;minutes++){
			t+='<option value="'+minutes+'"'+ (minutes==optionToSelect ? " selected" : "") +'>'+minutes+'</option>';
		}
		t+='</select>';
		t+='</td>';
		t+='</tr>';
	}
	
	if ( this.showSeconds ) {
		t+='<tr align="center">';
		t+='<td colspan="4" align="center" valign="middle" class="'+cD+'">'+ this.secondStr;
		t+='<select name="second">';
		var optionToSelect = ( urlParamMode=='from' ? 0 : 59 );
		for(second=0;second<60;second++){
			t+='<option value="'+second+'"'+ (second==optionToSelect ? " selected" : "") +'>'+second+'</option>';
		}
		t+='</select>';
		t+='</td><td colspan="4" class="'+cD+'"></td></tr>';
	}
	
	t+='</table></div>';
	t+='</form>';
	return t;
}

Calendar.prototype.build = CalendarBuild;
Calendar.prototype.write = CalendarWrite;
Calendar.prototype.previous = CalendarPrevious;
Calendar.prototype.next = CalendarNext;
Calendar.prototype.setDate = CalendarSetDate;
Calendar.prototype.setInputFields = CalendarSetInputFields;
Calendar.prototype.close = CalendarClose;

function showCalendar(e, monthsArray, day, hour, minute , mode, targetDiv, calendarName, curY, curM, targetFunctionName, futureSelectable) {
	var todaydate=new Date();
	var curmonth=todaydate.getMonth()+1; //get current month (1-12)
	if(curM && curM > 0 && curM < 13 ) {
		curmonth = curM;
	}
	var curyear=todaydate.getFullYear(); //get current year
	if(curY && curY > 0 && curY <= todaydate.getFullYear() ) {
		curyear = curY;
	}
	calendar = new Calendar(curmonth, curyear, mode, targetDiv, calendarName, targetFunctionName, futureSelectable);
	calendar.dayStr = day;
	calendar.hourStr = hour;
	calendar.minStr = minute;
	
	calendar.months = monthsArray;
	calendar.write();
	if(e){
		if(window.gecko){
			document.getElementById(targetDiv).style.left= e.pageX + "px";
		}
		else{
			document.getElementById(targetDiv).style.left= e.x + "px";
		} 
	}
	document.getElementById(targetDiv).style.visibility="visible";
	if(window.opera){
		document.getElementById(targetDiv).style.display="block";
	}			
}