// ---------------------------------------------------------
// Calendar 1.0
// ---------------------------------------------------------

function openCalendar (fieldDate, form, fieldName)
{
	formName = form;
	formFieldName = fieldName;
	formDate = fieldDate;

	init();

	if (formDate.length>0)
	{
		var formDateArray = formDate.split(".");
		if (formDateArray[0].indexOf("0")==0) 
		{
			dispMonth = parseInt(formDateArray[0].substring(1,2));
	    } else
		{
			dispMonth = parseInt(formDateArray[0]);
		}
		dispDay = parseInt(formDateArray[1]);
		dispYear = parseInt(formDateArray[2]);
	}

	windowOptions = "toolbar=no,location=no,status=no,menubar=no,resizable=no,copyhistory=yes,width=180,height=260,left=250,top=250,screenX=350,screenY=350";
	calendarWindow = this.open("","calendarWindow",windowOptions);
	calendarWindow.callingForm = this;

	redraw();
}

//
// global declarations and initialization
//
function init ()
{
	theDate = new Date();
	todayDate = new Date();

	dispDay = todayDate.getDate();
	dispMonth = todayDate.getMonth()+1;
	dispYear = todayDate.getYear();

	var rawYear = dispYear;
	dispYear = adjustYear(rawYear);

	msPerHour	= 3600000;			// 60*60*1000 = 3,600,000 -> msPerHour
	msPerDay = 86400000;			// 24*60*60*1000 = 86,400,000 -> msPerDay
}

//
// repaint the calendar
//
function redraw()
{
	firstOfMonth = new Date(dispYear,dispMonth-1,1);
	calendarWindow.document.open();
//	calTitle = "<title>Calendar</title>";
	drawCalendar(firstOfMonth);

	calendarWindow.document.write(htmlBuffer);
	calendarWindow.document.close();
	calendarWindow.callingForm = this;
	calendarWindow.focus();
}

function removeDate()
{
	tempFieldName = eval("document." + formName + "." + formFieldName);
	tempFieldName.value = "";
	calendarWindow.close();
}

function fillDate(filler)
{
	myDay = filler;
	myMonth = dispMonth;
	myYear = adjustYear(dispYear);
	timeNow = new Date();

	if (myDay<10) myDay = "0" + myDay;
	if (myMonth<10) myMonth = "0" + myMonth;

	calendarWindow.close();
	document.location.href = "./normal@meteo.aspx?t=mapa_archiv&c="+myDay+"."+myMonth+"."+myYear+" 12;00";
}

function setMonth(numMonth)
{
	dispMonth = numMonth;
	redraw();
}

function setYear(numYear)
{
	dispYear = numYear;
	if(dispYear == 2006)
		dispMonth = 5;
	else
		dispMonth = 1;
	redraw();
}

//
// generate the calendar document
//
function drawCalendar(theDate)
{
	previousYear = dispYear-1;
	nextYear = dispYear+1;
	dispMonth = dispMonth;

	htmlBuffer  = "<html>";
	htmlBuffer += "<head>";
	htmlBuffer += "<style>";
	htmlBuffer += "BODY {font-size: 12px;font-family: Arial, Tahoma, Verdana, sans-serif;background-color: #ffffff;}";
	htmlBuffer += "TD {font-size: 11px;}";
	htmlBuffer += ".CSelected{font-weight: bold;color: #ffffff;background-color: #86ad36;cursor: pointer;}";
	htmlBuffer += ".CDisable{font-weight: bold;color: #ffffff;background-color: #aaaaaa;cursor: default;}";
	htmlBuffer += ".CDay{font-weight: bold;font-size: 11px;cursor: pointer;color: #ffffff;background-color: #1C62BA;}";
	htmlBuffer += ".CMonth{font-weight: bold;font-size: 12px;color: #000000;}";
	htmlBuffer += ".CYear{font-size: 17px;}";
	htmlBuffer += ".Frame{border-width: 0px;}";
	htmlBuffer += "a.Arr:link{color: #86ad36;font-weight: bold;text-decoration: none;}";
	htmlBuffer += "a.Arr:hover{color: #86ad36;font-weight: bold;text-decoration: underline;}";
	htmlBuffer += "a.Arr:visited{color: #86ad36;font-weight: bold;text-decoration: none;}";
	htmlBuffer += "</style>";
	htmlBuffer += "<META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=windows-1250\">";
	htmlBuffer += "<title>Kalendar</title></head>";
	htmlBuffer += "<body onblur=\"self.focus();\" rightmargin=\"3\" leftmargin=\"3\" topmargin=\"3\" bottommargin=\"3\" bgcolor=\"#ffffff\">";
	htmlBuffer += "<table width=\"166px\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\">";
// Year Navigation
    htmlBuffer += "<form>";
	htmlBuffer += "<tr valign=\"middle\">";
	htmlBuffer += "<td align=\"center\">";
	htmlBuffer += "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"Frame\">";
	htmlBuffer += "<tr>";
	if(dispYear > 2006)
		htmlBuffer += "<td align=\"center\" class=\"CYear\"><a href=\"javascript:callingForm.setYear(" + previousYear + ")\" class=\"Arr\">&nbsp;<<<&nbsp;</a> ";
	else
		htmlBuffer += "<td align=\"center\" class=\"CYear\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	htmlBuffer += dispYear;

	if(nextYear <= todayDate.getYear())
		htmlBuffer += "<a href=\"javascript:callingForm.setYear(" + nextYear + ")\" class=\"Arr\">&nbsp;>>>&nbsp;</a></td>";
	else
		htmlBuffer += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";

	htmlBuffer += "</tr>";
	htmlBuffer += "</table>";
	htmlBuffer += "</td></tr>";
    htmlBuffer += "<tr>";
    htmlBuffer += "<td>";
	
  // Month Navigation
	htmlBuffer += "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\">";  
	htmlBuffer += "<tr class=\"CDay\">";
	if(dispYear == 2006)
	{
		htmlBuffer += "<td  align=\"center\" class=\"CDisable\">I</td>";
		htmlBuffer += "<td  align=\"center\" class=\"CDisable\">II</td>";
		htmlBuffer += "<td  align=\"center\" class=\"CDisable\">III</td>";
		htmlBuffer += "<td  align=\"center\" class=\"CDisable\">IV</td>";
	} else
	{
		htmlBuffer += (1 <= todayDate.getMonth()+1)?(dispMonth == 1)?"<td  align=\"center\" class=\"CSelected\">I</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(1)\">I</td>":"<td  align=\"center\" class=\"CDisable\">I</td>";
		htmlBuffer += (2 <= todayDate.getMonth()+1)?(dispMonth == 2)?"<td  align=\"center\" class=\"CSelected\">II</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(2)\">II</td>":"<td  align=\"center\" class=\"CDisable\">II</td>";
		htmlBuffer += (3 <= todayDate.getMonth()+1)?(dispMonth == 3)?"<td  align=\"center\" class=\"CSelected\">III</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(3)\">III</td>":"<td  align=\"center\" class=\"CDisable\">III</td>";
		htmlBuffer += (4 <= todayDate.getMonth()+1)?(dispMonth == 4)?"<td  align=\"center\" class=\"CSelected\">IV</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(4)\">IV</td>":"<td  align=\"center\" class=\"CDisable\">IV</td>";
	}
	htmlBuffer += (5 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 5)?"<td  align=\"center\" class=\"CSelected\">V</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(5)\">V</td>":"<td  align=\"center\" class=\"CDisable\">V</td>";
	htmlBuffer += (6 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 6)?"<td  align=\"center\" class=\"CSelected\">VI</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(6)\">VI</td>":"<td  align=\"center\" class=\"CDisable\">VI</td>";
	htmlBuffer += "</tr>";
	htmlBuffer += "<tr class=\"CDay\">";
	htmlBuffer += (7 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 7)?"<td  align=\"center\" class=\"CSelected\">VII</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(7)\">VII</td>":"<td  align=\"center\" class=\"CDisable\">VII</td>";
	htmlBuffer += (8 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 8)?"<td  align=\"center\" class=\"CSelected\">VIII</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(8)\">VIII</td>":"<td  align=\"center\" class=\"CDisable\">VIII</td>";
	htmlBuffer += (9 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 9)?"<td  align=\"center\" class=\"CSelected\">IX</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(9)\">IX</td>":"<td  align=\"center\" class=\"CDisable\">IX</td>";
	htmlBuffer += (10 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 10)?"<td  align=\"center\" class=\"CSelected\">X</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(10)\">X</td>":"<td  align=\"center\" class=\"CDisable\">X</td>";
	htmlBuffer += (11 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 11)?"<td  align=\"center\" class=\"CSelected\">XI</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(11)\">XI</td>":"<td  align=\"center\" class=\"CDisable\">XI</td>";
	htmlBuffer += (12 <= todayDate.getMonth()+1 || dispYear < todayDate.getFullYear())?(dispMonth == 12)?"<td  align=\"center\" class=\"CSelected\">XII</td>":"<td align=\"center\" class=\"CDay\" onClick=\"callingForm.setMonth(12)\">XII</td>":"<td  align=\"center\" class=\"CDisable\">XII</td>";
 	htmlBuffer += "</tr>";
	htmlBuffer += "</table></td></tr>";

  // Day Navigation
	htmlBuffer += "<tr>";
	htmlBuffer += "<td>";
	htmlBuffer += "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";
	htmlBuffer += "<tr class=\"CMonth\">";
	htmlBuffer += "	<td align=\"center\"><b>PO</b></td>";
	htmlBuffer += "	<td align=\"center\"><b>UT</b></td>";
	htmlBuffer += "	<td align=\"center\"><b>ST</b></td>";
	htmlBuffer += "	<td align=\"center\"><b>ČT</b></td>";
	htmlBuffer += "	<td align=\"center\"><b>PÁ</b></td>";
	htmlBuffer += "	<td align=\"center\"><b>SO</b></td>";
	htmlBuffer += "	<td align=\"center\"><b>NE</b></td>";
	htmlBuffer += "</tr>";

	drawBody(theDate);

	htmlBuffer += "</table>";
	htmlBuffer += "</td></tr>";
 	htmlBuffer += "</form>";
	htmlBuffer += "</table>";
	htmlBuffer += "</body></html>";
}

//
// generate the calendar body
//
function drawBody(theDate)
{
	thisYear = theDate.getYear();
	thisMonth = theDate.getMonth()+1;
	thisDate  = theDate.getDate();

	firstSunday(theDate);

	var dayStr = "";
	for (w=0; w<6; w++)
	{
		htmlBuffer += "<tr>";
		for (d=0; d<7; d++)
		{
			dayStr = "";
			date = theDate.getDate();
			
			if (theDate.getMonth()+1 != thisMonth)
			{
				htmlBuffer  += "<td>";
				htmlBuffer += "&nbsp;";
			}
			else
			{
			    if ((date <= todayDate.getDate()) || (dispMonth != todayDate.getMonth() + 1) || (dispYear != todayDate.getYear()))
				{
					htmlBuffer  += (dispDay==date && dispMonth==todayDate.getMonth()+1 && dispYear==todayDate.getYear())?"<td class=\"CSelected\" align=\"center\"":"<td class=\"CDay\" align=\"center\"";
					dayStr += date;
					htmlBuffer += " onClick=\"callingForm.fillDate(";
					htmlBuffer += dayStr;
					htmlBuffer += ")\">";
					htmlBuffer += dayStr;
				} else
				{
					htmlBuffer  += "<td class=\"CDisable\" align=\"center\">";
					dayStr += date;
					htmlBuffer += dayStr;
				}				
				//htmlBuffer += "</a>";				
			}
			htmlBuffer += "</td>";
			// increment the date
			timezoneoffset = theDate.getTimezoneOffset();
			if ( timezoneoffset == 0)
			{
				theDate.setTime(newTime);
			}
			newTime = theDate.getTime() + msPerDay;
			theDate.setTime(newTime);

			// check for DST
			if (theDate.getHours() != 23)
			{
				theDate.setTime(newTime + msPerHour);
			}
		}
		htmlBuffer += "</tr>";
	}
	//htmlBuffer += "</form>";
}

//
// reset the startdate to get the
// previous sunday before the current date
// so that the drawing can begin from a sunday
//
function firstSunday (fromDate)
{
//	alert("From date: "+fromDate);

	while (fromDate.getDay() != 1)
	{
		newTime = fromDate.getTime() - msPerDay;
		fromDate.setTime(newTime);
	}

	// Set the date to Sunday before current date
	theDate = fromDate;
}

function validDayMonth(dayNum,monthNum,yearNum,properties)
{
	if((dayNum > daysInMonth[monthNum]) || (dayNum == 0 || monthNum == 0) ||
	(monthNum == 2) && (dayNum > daysInFeb(yearNum)))
	{
		alertMessage += properties.boxLabel + " has an invalid date\n";
		return true;
	}
	else return false;
}

function daysInFeb(year)
{
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29:28);
}

function adjustYear(rawYear)
{
	var tempYr = rawYear < 2000 ? (rawYear + 1900) : rawYear;
	return tempYr;
}
