/*
NAME: calendar.js
PROJECT: podcihalkou
AUTHOR(S): Vítězslav Žák
EMAIL: vz@lotofidea.com
LANGUAGE: PHP
DESCRIPTION: 
 Functions for open and close calendar window
DEPENDENCY: 
 calendar.php
*/

//global variable - used for closing open calendar
var calendarWindow = null;

/*
Closing claendar window

Should be called form the <body> tag usign event onFocus. If this function will be not called,
calendar will be still working but calendar window will be not closed on click out of calendar window.

exmaple: <body onFocus="closeCalendar()">
*/
function closeCalendar() 
{
    if (calendarWindow != null) 
    {
        if (!calendarWindow.closed) 
        {
            calendarWindow.close();
        }
        calendarWindow = null;
    }
}

/*
Open calendar window
Parameters:
  formname - formular name with date fields
  field_day - formular item in which choosen day will by putted
  field_month - formular item in which choosen month will by putted
  field_year - formular item in which choosen year will by putted
  current_month_field - formular item from which default month will be taken
  current_year_field - formular item from which default year will be taken

  Variable calendar_file_name have to be set to url of the file calendar.php.

example: 
	<a href="javascript:openCalendar('formCalendar', 'zacd', 'zacm', 'zacr', '', '');">
or
  <a href="javascript:openCalendar('formCalendar', 'kond', 'konm', 'konr', 'zacm', 'zacr');">
*/
function openCalendar(formname, field_day, field_month, field_year, current_month_field, current_year_field, current_language)
{
	//set to right url
	calendar_file_name = './calendar.php';
	//compute default time
	time = 0
	
	if (current_month_field && current_year_field)
	{
	  
		if (document[formname][current_month_field].value && document[formname][current_year_field].value)
		{
			time = Date.UTC(document[formname][current_year_field].value, document[formname][current_month_field].value-1, 1, 1, 1, 1, 1)/1000;
		}
	}
	
	
	//build parameters
	parameters = '?form=' + formname + '&field_day=' + field_day + '&field_month=' + field_month + '&field_year=' + field_year + '&lang=' + current_language;
	
	if (time != 0)
	{
		parameters += '&time=' + time;
	}
	
	//open window and set focus
	calendarWindow = window.open(calendar_file_name + parameters, 'calendar', 'width=200,height=208,left=300,top=300,resizable=yes,toolbar=no,status=no,scrollbars=no,menubar=no,directories=no,location=no,dependant=yes'); 
	calendarWindow.focus()
}

function updateCalendarForm(form, field_day, day, field_month, month, field_year, year)
{	

	//alert('UKLADANIE DATUMU!');
	eval("document."+form+"."+field_day+".value=" + "\""+day+"\"");
	eval("document."+form+"."+field_month+".value=" + "\""+month+"\"");
	eval("document."+form+"."+field_year+".value=" + "\""+year+"\"");	

	closeCalendar();
}

