//	<script language="JavaScript" src="../include/datepicker.js"></script>
//	<input type="button" name="x" value="+" onclick="javascript:show_calendar(control.form.name + '.control');">
//      <script language="JavaScript">print_calendar('<? echo midhref();?>','<? echo $calendar;?>');</script>
//      <script language="JavaScript">print_calendar('<? echo midhref();?>','<? echo $calendar;?>','-1-12-23-25-');</script>
//       onblur="DateChk(control);"
var weekend      = [5,6];

/*
var css_global   = 
"body {background-color: #CCCCBB;font-family: verdana,arial,helvetica;color: #000000;font-size: 8pt;}" +
"a:link {font-size : 12px; color : #000000; text-decoration : none; font-weight: bold;}" +
"a:visited {font-size : 12px; color : #000000; text-decoration : none; font-weight: bold;}"+
"a:hover {font-size : 12px; color : #FF0000; text-decoration : none; font-weight: bold;}"+
"TABLE {BACKGROUND: #D5D5C6;}"+
"SELECT{BACKGROUND: #CCCCBB;BORDER-BOTTOM: 0pt;BORDER-LEFT: 0pt;BORDER-RIGHT: 0pt;BORDER-TOP: 0pt;FONT-SIZE: 8pt;}"+
"TD{BACKGROUND: #CCCCBB;text-align:center;font-size : 10px;}";
var css_private = 
"TD.weekname{BACKGROUND: #000000;text-align:center;color : #FFFFFF;font-size : 10px;}"+
"TD.workday{BACKGROUND: #B9B9A2;text-align:center;display: table-cell;}"+
"TD.weekend{BACKGROUND: #ACAC8E;text-align:center;}"+ 
"TD.selectday{BACKGROUND: #9FDF9F;text-align:center;}"+
"TD.eventday{BACKGROUND: #CC99FF;text-align:center;}"+
".selectnow{color: #FF0000; background: #B9B9A2;}";
*/

var gNow = new Date();
var dtCh = "-";
var ggWinCal;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE  = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

Calendar.Months = ["január", "február", "március", "április", "május", "június","július", "augusztus", "szeptember", "október", "november", "december"];

// nem szökőév
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// szökőév
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(p_item, p_WinCal, p_month, p_year) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;
	
	this.gMonthName = Calendar.get_month(p_month);
	this.gMonth = new Number(p_month);

	this.gYear       = p_year;
	this.gReturnItem = p_item;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
Calendar.print = Calendar_print;

function Calendar_get_month(monthNo) {
	return Calendar.Months[monthNo];
}

function Calendar_get_daysofmonth(monthNo, p_year) {
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];
	
		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	var ret_arr = new Array();
	
	if (incr == -1) {
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

function Calendar_print() {
	ggWinCal.print();
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	var ret_arr = new Array();
	
	if (incr == -1) {
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	vCode = vCode + "<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0 width='100%' class='workday'>";
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	vCode = vCode + "</TABLE>";
	return vCode;
}

Calendar.prototype.show = function() {
var vCode = "";
var s     = "";
if(this.gReturnItem != "null")
	{
	this.gWinCal.document.open();
	this.wwrite("<html>");
	this.wwrite("<STYLE>" + css_global + css_private + "</STYLE>");

	this.wwrite("<head><title>Kalendárium</title>");
	this.wwrite("</head>");

	this.wwrite("<body>");
        }
else
	{
	this.wwrite("<STYLE>" + css_private + "</STYLE>");
	}

	
if(this.gReturnItem == "null")
  {
  s = "<select ID='chooseYear' NAME='chooseYear' onchange=\"setUrl('" + p_href + "',document.getElementById('chooseYear').value,document.getElementById('chooseMonth').value);\">";
  }
else
  {
  s = "<select ID='chooseYear' NAME='chooseYear' onchange=\"window.opener.Build('" + this.gReturnItem + "', '" + this.gMonth + "', document.getElementById('chooseYear').value);\">";
  }

for (i = 1920; i < 2020; i++)
   {
   s = s + "<option value='" + i + "'";
   if(i == d_year)
      s = s + " class=selectnow ";
   if(i == this.gYear)
      s = s + " selected ";
   s = s + ">" + i + "</option>";
   }

s = s + "</select>";

if(this.gReturnItem == "null")
  {
  s = s + "<select ID='chooseMonth' NAME='chooseMonth' onchange=\"setUrl('" + p_href + "',document.getElementById('chooseYear').value,document.getElementById('chooseMonth').value);\">";
  }
else
  {
  s = s + "<select ID='chooseMonth' NAME='chooseMonth' onchange=\"window.opener.Build('" + this.gReturnItem + "', chooseMonth.value, '" + this.gYear + "');\">";
  }
for (i = 0; i < 12; i++)
   {
   s = s + "<option value='" + i + "'";
   
   if(i == d_month)
      s = s + " class=selectnow ";
   if(i == this.gMonth)
      s = s + " selected ";
   s = s + ">" + Calendar.Months[i] + "</option>";
   }

s = s + "</select>";
this.wwrite(s);
	
	vCode = this.getMonthlyCalendarCode();
	this.wwrite(vCode);

	this.wwrite("</font></body></html>");
	this.gWinCal.document.close();
}


Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext) {
	this.gWinCal.document.write(wtext);
}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	
	vCode = vCode + "<TR>";
	vCode = vCode + "<TD WIDTH='14%' class='weekname'><B>H</B></TD>";
	vCode = vCode + "<TD WIDTH='14%' class='weekname'><B>K</B></TD>";
	vCode = vCode + "<TD WIDTH='14%' class='weekname'><B>Sz</B></TD>";
	vCode = vCode + "<TD WIDTH='14%' class='weekname'><B>Cs</B></TD>";
	vCode = vCode + "<TD WIDTH='14%' class='weekname'><B>P</B></TD>";
	vCode = vCode + "<TD WIDTH='16%' class='weekname'><B>Sz</B></TD>";
	vCode = vCode + "<TD WIDTH='14%' class='weekname'><B>V</B></TD>";
	vCode = vCode + "</TR>";
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	if(vFirstDay)
	  vFirstDay -= 1;
	else
	  vFirstDay += 6;
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";

 
	vCode = vCode + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD WIDTH='14%' " + this.write_weekend_string(i,-1) + "></TD>";
	}

	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "<TD WIDTH='14%' " + this.write_weekend_string(j,vDay) + ">";
		if(this.gReturnItem == "null")
		  vCode = vCode + "<A HREF='" + p_href + "&calendar="+ this.format_data(vDay) + "'>" + this.format_day(vDay) + "</A>";
		else
		  vCode = vCode + "<A HREF='#' " + "onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 	this.format_data(vDay) + "';window.close();\">" + this.format_day(vDay) + "</A>";
		vCode = vCode + "</TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
			vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j,vDay) + ">";
			if(this.gReturnItem == "null")
			  vCode = vCode + "<A HREF='" + p_href + "&calendar="+ this.format_data(vDay) + "'>" + this.format_day(vDay) + "</A>";
			else
			  vCode = vCode + "<A HREF='#' " + "onClick=\"self.opener.document." + this.gReturnItem + ".value='" + this.format_data(vDay) + "';window.close();\">" + this.format_day(vDay) + "</A>";
			vCode = vCode + "</TD>";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	for (m=1; m<(7-j); m++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(j+m,vDay) + 
		">" + m + "</TD>";
	}
	
	return vCode;
}

Calendar.prototype.format_day = function(vday) {
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();

	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		return ("<FONT COLOR=\"RED\"><B>" + vday + "</B></FONT>");
	else
		return (vday);
}

Calendar.prototype.write_weekend_string = function(vday,pday) {
	var i;
	if(p_day==pday && this.gReturnItem == "null")
	  {
	  return (" class='selectday' ");
	  }
	else if((p_event.indexOf('-'+ pday + '-') > 0) && (pday > 0))
	  {
          return " class='eventday ' ";
	  }
	else
	  { 
	  for (i=0; i<weekend.length; i++) 
	    {
		if (vday == weekend[i])
			return (" class='weekend' ");
	    }
          return " class='workday ' ";
	  } 
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;
        
// YY   = vY2
// YYYY = vY4
// MM   = vMonth
// MON  = vMon
// MON  = vFMon
// DD   = vDD
        
        if(this.gReturnItem == "null")
          vData = vY4 + vMonth + vDD;
        else
          vData = vY4 + dtCh + vMonth + dtCh + vDD;

	return vData;
}

function setUrl(urlstr,urlyear,urlmonth) 
{
urlmonth ++;
urlmonth = (urlmonth.toString().length < 2) ? "0" + urlmonth : urlmonth;
urlstr = urlstr + '&calendar=' + urlyear + urlmonth + '01';
document.location.href = urlstr;
//document.URL = 'http://index.hu';//urlstr;
}



function Build(p_item, p_month, p_year) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year);
        gCal.show();
}

function show_calendar(p_item) {

	p_month = new String(gNow.getMonth());
	p_year = new String(gNow.getFullYear().toString());

        try
        {
        ggWinCal.close();
        }
        catch(err){  }

	vWinCal = window.open("", "Calendar", "width=250,height=160,scrollbars=no,status=no,resizable=no,top=200,left=200");
	vWinCal.opener = self;
	ggWinCal = vWinCal;
	Build(p_item, p_month, p_year);
}


var p_href  = "";
var p_day   = 0;
var p_event = "";
var d_year  = new String(gNow.getFullYear().toString());
var d_month = new String(gNow.getMonth());


function print_calendar(i_href,i_date,i_event) {

	p_href   = i_href;
	p_event  = "--" + i_event + "--";
	p_item   = "null";
	
	p_day = i_date.substr(6,2) * 1;

  	p_month = i_date.substr(4,2);
	if(!p_month)
	  p_month = new String(gNow.getMonth());
        else
          p_month--;

	p_year = i_date.substr(0,4);
	if(!p_year)
	  p_year = new String(gNow.getFullYear().toString());
        
        vWinCal = self;
	vWinCal.opener = self;
	ggWinCal = vWinCal;
	Build(p_item, p_month, p_year);
}



function isDate (year,month,day) 
{
var today = new Date();
year  = ((!year) ? today.getFullYear():year);
month = ((!month) ? today.getMonth():month - 1);
if (!day) 
  return '';
var test = new Date(year,month,day);
if ((year  == test.getFullYear()) &&
    (month == test.getMonth()) &&
    (day   == test.getDate()))
    {
    var gCal = new Calendar('', '', month + '', year + '');
    return gCal.format_data(day);
    }
else
    return '';
}


function DateChk(o)
{
var pos1;
var pos2;
var strYear;
var strMonth;
var strDay;
var dtStr = o.value;
if(dtStr != '')
  {
  pos1     = dtStr.indexOf(dtCh);
  pos2     = dtStr.indexOf(dtCh,pos1+1);
  if(pos2 > 0)
    {
    strYear  = dtStr.substring(0,pos1);
    strMonth = dtStr.substring(pos1+1,pos2);
    strDay   = dtStr.substring(pos2+1);
    }
  else if(pos1 > 0)
    {
    strYear   = 0;
    strMonth  = dtStr.substring(0,pos1);
    strDay    = dtStr.substring(pos1+1);
    }
  else
    {
    strYear  = 0;
    strMonth = 0;
    strDay   = dtStr;
    }
  dtStr = isDate(strYear,strMonth,strDay);
  if(dtStr)
    {
    o.value = dtStr;
    }
  else
    {
    alert('Rossz dátum!');
    o.focus();
    }
  }
}



