
// Originals: Robert W. Husted & Eric Freed
// Modified: Fülep Dávid

topBackground   ="#fdeeaa";
bottomBackground="#fdeeaa";
tableBGColor    ="black";
cellColor       ="#ffff99";
headingCellColor="#6699cc";
headingTextColor="white";
dateColor       ="blue";
focusColor      ="#ff0000";
hoverColor      ="darkred";
fontStyle       ="8pt arial, helvetica";
headingFontStyle="8pt arial, helvetica";
bottomBorder    =true;
windowSize      ="width=185,height=175";
cal_opentwice	=true; //This is a workaround for IE to make the window come to top when it was already open

//End of customizable section

todayStr = "Ma";
weekdayList  = new Array('Vasárnap', 'Hétfő', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
weekdayArray = new Array('H','K','Se','Cs','P','So','V');
monthArray = new Array('Január', 'Február', 'Március', 'Április', 'Május', 'Június',
			'Július', 'Augusztus', 'Szeptember', 'Október', 'November', 'December');

var calCtrl, initDate, calDateFormat, windowTitle;
var cal  = "loaded";
var isNav= (navigator.appName == "Netscape");
var isIE = !isNav;

var weekdays = "<TR BGCOLOR='"+headingCellColor+"'>";

for (i in weekdayArray) 
	{
	 weekdays += "<TD class='heading' align=center>"+weekdayArray[i]+"</TD>";
	}

if (bottomBorder) weekdays += "<TD rowspan=7 bgcolor=black></TD>";

weekdays += "</TR>";

blankCell= "<TD align=center class='heading' bgcolor='"+cellColor+"'>&nbsp;</TD>";

var longSpace="";
for (i=0; i<50; i++) { longSpace+="&nbsp;" }

calendarBegin =
"<HTML><HEAD>" +
"<STYLE type='text/css'>" +
"<!--" +
"TD.heading { text-decoration: none; color:" + headingTextColor + "; font: " + headingFontStyle + "; }" +
"A.focusDay { color: " + focusColor + "; text-decoration: none; font: " + fontStyle + "; }" +
"A.weekDay  { color: " + dateColor  + "; text-decoration: none; font: " + fontStyle + "; }" +
"A.weekDay:hover { color: " + hoverColor + "; text-decoration: none; font: " + fontStyle + "; }" +
"-->" +
"</STYLE>" +
"</HEAD>" +
"<BODY BGCOLOR='" + bottomBackground + "'" +
"<CENTER>";
if (isNav) {
calendarBegin += 
"<TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0 ALIGN=CENTER BGCOLOR='" +tableBGColor+ "'><TR><TD>";
}
calendarBegin +=
"<TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0 ALIGN=CENTER BGCOLOR='" +tableBGColor+ "'>" +
weekdays +
"<TR>";

calendarEnd = "";
if (bottomBorder) 
	{
	  calendarEnd += "<TR><TD colspan=8></TD></TR>";
	}

if (isNav) 
	{
	  calendarEnd += "</TD></TR></TABLE>";
	}

calendarEnd += "</TABLE></CENTER></BODY></HTML>";

var delim, val, DayFormat, DayStr; //used to for next 2 functions to communicate

function makeTwoDigit(inValue) 
	{
		var numVal = parseInt(inValue, 10);
		if (numVal < 10) 
		{
		  return("0" + numVal);
		}
		else 
		{
		  return numVal;
		}
	}

function markerFound(marker) {
var l=marker.length;
if (DayFormat.indexOf(marker)==0 && DayStr != null) {
 delim = DayStr.indexOf(DayFormat.substring(l,l+1));
 if (delim==0) delim=DayStr.length;
 val =DayStr.substring(0,delim);
 DayStr=DayStr.substring(delim,DayStr.length);
 DayFormat=DayFormat.substring(l,DayFormat.length);
 if (marker.indexOf("mon")==1 || !isNaN(val)) return true;
}
return false;
}

function readComplexDate(DayStr) {
DayFormat = calDateFormat.toLowerCase();
DayStr=DayStr.toLowerCase();
for (i=0; DayFormat.length!=0; DayFormat=DayFormat.substring(1,DayFormat.length)) {
 if (markerFound("dd")) calDate.setDate(val);
 if (markerFound("mm")) calDate.setMonth(val-1);
 if (markerFound("yyyy")) calDate.setFullYear(val);
 if (markerFound("yy")) {
  var n="19"+val;
  if (n<1930) n="20"+val;
  calDate.setFullYear(n);
 }
 if (markerFound("month")) {
  for (i in monthArray) {
   if (monthArray[i].toLowerCase()==val) calDate.setMonth(i);
  }
 }
 if (markerFound("mon")) {
  for (i in monthArray) {
   if (monthArray[i].toLowerCase().substring(0,3)==val) calDate.setMonth(i);
  }
 }
 DayStr=DayStr.substring(1,DayStr.length);
}
}

function showCalendar(DF_ev,DF_ho,DF_nap,Title) {
if (parseInt(navigator.appVersion)<4) return false;
windowTitle = Title;

calDateFormat = "yyyyMMDD";

//set initial date
mezo_ev = DF_ev;
mezo_ho = DF_ho;
mezo_nap= DF_nap;

calDate = new Date(DF_ho.value + '/' + DF_nap.value + '/' + DF_ev.value);
if (isNaN(calDate)) 
	{
	 calDate = new Date();
	 readComplexDate(DF_ho.value + '/' + DF_nap.value + '/' + DF_ev.value);
	}

initDate = new Date(calDate);
calDate.setDate(1);

if (isNav) rows= "'62,*'";
else rows= "'50,*'";
calDocFrameset = 
"<HTML><HEAD><TITLE>"+ windowTitle + longSpace +"</TITLE></HEAD>\n" +
"<FRAMESET ROWS="+ rows +" BORDER='0'>\n" +
"  <FRAME NAME='topCalFrame'    SRC='javascript:parent.opener.calDocTop' SCROLLING='no'>\n" +
"  <FRAME NAME='bottomCalFrame' SRC='javascript:parent.opener.calDocBottom' SCROLLING='no'>\n" +
"</FRAMESET>\n";

calDocBottom = buildBottomCalFrame();

calDocTop =
"<HTML><HEAD>" +
"<style> .f {font-size: 9px; background-color: #6699cc; color:ffffff} </style>" +
"</HEAD>" +
"<BODY onLoad='calControl.today.focus();calControl.today.blur();' BGCOLOR='" +topBackground+ "'>" +
"<CENTER>" + 
"<FORM NAME='calControl' onSubmit='document.calControl.year.blur();return false;'>" +
honaplista() +
"<INPUT class=f NAME='year' VALUE='" +calDate.getFullYear()+ "' TYPE=TEXT SIZE=4 MAXLENGTH=4 onChange='parent.opener.setYear()'>" +
"<BR><NOBR><INPUT " +
"TYPE=BUTTON class=f NAME='previousYear' VALUE='<<' onClick='parent.opener.setPreviousYear()'><INPUT " +
"TYPE=BUTTON class=f NAME='previousMonth' VALUE=' < ' onClick='parent.opener.setPreviousMonth()'><INPUT " +
"TYPE=BUTTON class=f NAME='today' VALUE="+todayStr+" onClick='parent.opener.setToday()'><INPUT " +
"TYPE=BUTTON class=f NAME='nextMonth' VALUE=' > ' onClick='parent.opener.setNextMonth()'><INPUT " +
"TYPE=BUTTON class=f NAME='nextYear' VALUE='>>' onClick='parent.opener.setNextYear()'>" + 
"</NOBR><br>" +
"</FORM></CENTER>" +
"<SCRIPT language=JavaScript>" +
"parent.opener.calCtrl=document.calControl;" +
"</SCRIPT></BODY></HTML>";

if (isIE&&cal_opentwice) 
	{
 calWin = window.open('about:blank','calWin','dependent=yes,'+windowSize+',screenX=200,screenY=300,top=200,left=300,titlebar=no');
 calWin.document.write('<html><body BGCOLOR="' +topBackground+ '"><b>Várjon...')
 calWin.close();
	}
calWin = window.open('javascript:opener.calDocFrameset','calWin','dependent=yes,'+windowSize+',screenX=200,screenY=300,top=200,left=300,titlebar=yes');
if (isNav) calWin.focus();
}


function buildBottomCalFrame() {       
var calDoc = calendarBegin;
month=calDate.getMonth();
year =calDate.getFullYear();
day  =initDate.getDate();
var columnCount=0;
var days = honapszam();
if (day > days) day = days;
var firstOfMonth = new Date (year, month, 1);
var startingPos  = firstOfMonth.getDay()-1;  //amig nem volt -1, a vasárnappal kezdte a hetet!
days += startingPos;
for (i=0; i < startingPos; i++) {
 calDoc += blankCell;
 columnCount++;
}
var currentDay=0;
for (i=startingPos; i < days; i++) {
 currentDay = i-startingPos+1;

 if (currentDay==day && month==initDate.getMonth() && year==initDate.getFullYear())
  dayType = "focusDay";
 else
  dayType = "weekDay";

 calDoc +="<TD align=center bgcolor='" +cellColor+ "'>" +
          "<a class='" +dayType+ "' href='javascript:parent.opener.returnDate(" + 
          currentDay + ")'>&nbsp;" + currentDay + "&nbsp;</a></TD>";

 columnCount++;
 if (columnCount % 7 == 0) calDoc += "</TR><TR>";
}
for (i=days; i<42; i++)  {
 calDoc += blankCell;
 columnCount++;
 if (columnCount % 7 == 0) {
  calDoc += "</TR>";
  if (i<41) calDoc += "<TR>";
 }
}
calDoc += calendarEnd;
return calDoc;
}

function writeCalendar() {
calDocBottom = buildBottomCalFrame();
calWin.frames['bottomCalFrame'].document.open();
calWin.frames['bottomCalFrame'].document.write(calDocBottom);
calWin.frames['bottomCalFrame'].document.close();
}

function setToday() {
calDate = new Date();
returnDate(calDate.getDate());
}

function setYear() {
var year = calCtrl.year.value;
if (isFourDigitYear(year)) {
calDate.setFullYear(year);
writeCalendar();
}
}

function setCurrentMonth() {
var month = calCtrl.month.selectedIndex;
calDate.setMonth(month);
writeCalendar();
}

function setPreviousYear() 
	{
		calCtrl.year.value--;
		setYear();
	}

function setNextYear() 
	{
		calCtrl.year.value++;
		setYear();
	}

function setPreviousMonth() {
var year = calCtrl.year.value;
if (isFourDigitYear(year)) {
 var month = calCtrl.month.selectedIndex;
 if (month == 0) {
  month = 11;
  if (year > 1000) {
   year--;
   calDate.setFullYear(year);
   calCtrl.year.value = year;
  }
 } else {
  month--;
 }
 calDate.setMonth(month);
 calCtrl.month.selectedIndex = month;
 writeCalendar();
}
}

function setNextMonth() 
	{
	var year = calCtrl.year.value;
	if (isFourDigitYear(year)) 
		{
		 var month = calCtrl.month.selectedIndex;
		 if (month == 11) 
			{
			  month = 0;
			  year++;
			  calDate.setFullYear(year);
			  calCtrl.year.value = year;
			 } 
		else 
			{
			  month++;
			 }
		 calDate.setMonth(month);
		 calCtrl.month.selectedIndex = month;
		 writeCalendar();
		}
	}

function honapszam()  
	{
	var days=28;
	var month=calDate.getMonth()+1;
	var year =calDate.getFullYear();
	if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) 
		{
		 days=31;
		} 
	else if (month==4 || month==6 || month==9 || month==11) 
		{
		 days=30;
		} 
	else if (month==2 && ( (year%4)==0 && (year%100)!=0 || (year%400)==0 )) 
		{
		 days=29;
		}
	return (days);
	}

function isFourDigitYear(year) 
	{
	if (year.length != 4 || isNaN(year)) 
		{
		calCtrl.year.value = calDate.getFullYear();
		calCtrl.year.select();
		calCtrl.year.focus();
		return false;
		}
	return true;
	}

function honaplista() 
	{
	var activeMonth = calDate.getMonth();
	monthSelect = "<SELECT class=f NAME='month' onChange='parent.opener.setCurrentMonth()'>";
	for (i in monthArray) 
		{
		if (i == activeMonth) 
			{
			    monthSelect += "<OPTION SELECTED>" + monthArray[i] + "\n";
			}
			else 
			{
			    monthSelect += "<OPTION>" + monthArray[i] + "\n";
			}
		}
	monthSelect += "</SELECT>";
	return monthSelect;
	}

function returnDate(inDay) 
	{
	calDate.setDate(inDay);

	mezo_ev.value =calDate.getFullYear();
	mezo_ho.value =makeTwoDigit(calDate.getMonth()+1);
	mezo_nap.value=makeTwoDigit(calDate.getDate());

	mezo_ev.focus;
	calWin.close();
	}

