var MILLIDAY=86400000;
function scrollCalendarLeft(c){
	if(c.currentMonth>c.leftMonths){
		display = document.getElementById('month_'+c.id+"_"+(c.currentMonth+c.rightMonths)).style.display;
		document.getElementById('month_'+c.id+"_"+(c.currentMonth+c.rightMonths)).style.display = "none";
		c.currentMonth--;
		document.getElementById('month_'+c.id+"_"+(c.currentMonth-c.leftMonths)).style.display = display;
		if(c.currentMonth<=c.leftMonths){
			document.getElementById('scrollLeft_'+c.id).style.display = "none";
		}
		document.getElementById('scrollRight_'+c.id).style.display = "block";
	}
}
function scrollCalendarRight(c){
	if(c.currentMonth<c.maxMonth-c.rightMonths){
		display = document.getElementById('month_'+c.id+"_"+(c.currentMonth-c.rightMonths)).style.display;
		document.getElementById('month_'+c.id+"_"+(c.currentMonth-c.rightMonths)).style.display = "none";
		c.currentMonth++;
		document.getElementById('month_'+c.id+"_"+(c.currentMonth+c.leftMonths)).style.display = display;
		if(c.currentMonth>=c.maxMonth-c.rightMonths){
			document.getElementById('scrollRight_'+c.id).style.display = "none";
		}
		document.getElementById('scrollLeft_'+c.id).style.display = "block";
	}
}
function StringToDate(date){
	ar = date.split(".");
	dt=Date.UTC(ar[2],ar[1]-1,ar[0])
	return dt;
}
function DateToString(sd){
	return sd.getUTCDate()+"."+(sd.getUTCMonth()+1)+"."+sd.getUTCFullYear();
}
function selectDates(c, fdate, ldate){
	fd = StringToDate(fdate);
	ld = StringToDate(ldate);
	for (i=fd; i<=ld; i+=MILLIDAY) {
		day=new Date(i);
		if(document.getElementById("day_"+c.id+"_"+DateToString(day))){
			document.getElementById("day_"+c.id+"_"+DateToString(day)).className += "Selected";
		}
	}
}
function deselectDates(c, fdate, ldate){
	fd = StringToDate(fdate);
	ld = StringToDate(ldate);
	for (i=fd; i<=ld; i+=MILLIDAY) {
		day=new Date(i);
		if(document.getElementById("day_"+c.id+"_"+DateToString(day))){
			document.getElementById("day_"+c.id+"_"+DateToString(day)).className = document.getElementById("day_"+c.id+"_"+DateToString(day)).className.replace( "Selected", "" );
		}
	}
}
function hoverDates(c, fdate, ldate){
	fd = StringToDate(fdate);
	ld = StringToDate(ldate);
	if(fd>ld){
		td = fd;
		fd = ld;
		ld = td;
	}
	for (i=fd; i<=ld; i+=MILLIDAY) {
		day=new Date(i);
		if(document.getElementById("day_"+c.id+"_"+DateToString(day))){
			document.getElementById("day_"+c.id+"_"+DateToString(day)).className += "Hover";
		}
	}
}
function changeHoverDates(c, fdate1, ldate1, fdate2, ldate2){
	fd1 = StringToDate(fdate1);
	ld1 = StringToDate(ldate1);
	fd2 = StringToDate(fdate2);
	ld2 = StringToDate(ldate2);
	if(fd1>ld1){
		td = fd;
		fd1 = ld1;
		ld1 = td;
	}
	if(fd2>ld2){
		td = fd;
		fd2 = ld2;
		ld2 = td;
	}
	for (i=fd1; i<=ld1; i+=MILLIDAY) {
		day=new Date(i);
		if(day<fd2 || day>ld2){
			if(document.getElementById("day_"+c.id+"_"+DateToString(day))){
				document.getElementById("day_"+c.id+"_"+DateToString(day)).className += "Hover";
			}
		}
	}
	for (i=fd2; i<=ld2; i+=MILLIDAY) {
		day=new Date(i);
		if(day<fd1 || day>ld1){
			if(document.getElementById("day_"+c.id+"_"+DateToString(day))){
				document.getElementById("day_"+c.id+"_"+DateToString(day)).className = document.getElementById("day_"+c.id+"_"+DateToString(day)).className.replace( "Hover", "" );
			}
		}
	}
}
function dehoverDates(c, fdate, ldate){
	fd = StringToDate(fdate);
	ld = StringToDate(ldate);
	if(fd>ld){
		td = fd;
		fd = ld;
		ld = td;
	}
	for (i=fd; i<=ld; i+=MILLIDAY) {
		day=new Date(i);
		if(document.getElementById("day_"+c.id+"_"+DateToString(day))){
			document.getElementById("day_"+c.id+"_"+DateToString(day)).className = document.getElementById("day_"+c.id+"_"+DateToString(day)).className.replace( "Hover", "" );
		}
	}
}
function leaveCalendar(c){
	if(c.firstDate){
		dehoverDates(c, c.firstDate, c.hoverDate);
		c.firstDate = 0;
	}
}
function clickDate(c, date){
	if(c.firstDate){
		if(StringToDate(c.firstDate)<=StringToDate(date)){
			c.selectedFirstDate = c.firstDate;
			c.selectedLastDate = date;
		}
		else{
			c.selectedFirstDate = date;
			c.selectedLastDate = c.firstDate;
		}
		dehoverDates(c, c.firstDate, c.hoverDate);
		selectDates(c, c.selectedFirstDate, c.selectedLastDate);
		c.firstDate = 0;
		//c.search();

		ClickSelected(c.selectedFirstDate, c.selectedLastDate);
	}
	else
	{
		if(c.selectedFirstDate){
			deselectDates(c, c.selectedFirstDate, c.selectedLastDate);
		}
		c.firstDate = date;
		c.hoverDate = c.firstDate;
		hoverDates(c, c.firstDate, c.hoverDate);
	}
}
function hoverDate(c, date){
	if(c.firstDate){
		changeHoverDates(c, c.firstDate, date, c.firstDate, c.hoverDate);
		c.hoverDate = date;		
	}
}
function ClickSelected(first, last) {
    var a = $('#kalendar');
    a.css('display', 'none');

    $(".hiddenElements > input[type=hidden]").val(first + "-" + last);
    setTimeout(function() {
        $(".hiddenElements > input[type=submit]").click();
        a.css('display', 'block');
    }, 100);
}
//function OpenRadWindow(url, title, windowName, width, height) {
//    var radWindow = window.top.radopen(url, windowName);
//    radWindow.setSize(width, height);
//    radWindow.SetTitle(title);
//    radWindow.Center();
//    radWindow.SetModal(true);
//    return false;
//}
//function GetRadWindow() {
//    var oWindow = null;
//    if (window.radWindow) oWindow = window.radWindow;
//    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
//    return oWindow;
//}
//function CloseRadWindow() {
//    var radWindow = GetRadWindow();
//    radWindow.close();
//}
