/**
*  Fill form with data encoded in JSON
*
* @param string $data form data encoded in JSON
*/
function fillForm(data)
{
	for (key in data)
	{
		$('#field_'+key).val( data[key] );
	}
}

// START FUNCTIONS FOR CALENDAR
var cur_date = new Date();
cur_date = cur_date.toDateString();
function isDisabled(_date)
{
	var tmp = _date.getFullYear().toString();
	tmp += (_date.getMonth()+1) < 10 ? ('0'+(_date.getMonth()+1)) : (_date.getMonth()+1);
	tmp += _date.getDate() < 10 ? ('0' + _date.getDate()) : _date.getDate();
	for (i = 1; i < active_dates.length; i++)
	{
		if (active_dates[i] == tmp || _date.toDateString() == cur_date)
		{
			return false;
		}
	}
	return true;
}
function dateChanged(calendar)
{
	if (calendar.dateClicked)
	{
		var y = calendar.date.getFullYear();
		var m = calendar.date.getMonth() + 1;
		var d = calendar.date.getDate();
		window.location = url + 'date/' + y + '/' + m + '/' + d + '/index.html';
	}
};

$(function() {
	if ('undefined' != typeof(calendar))
	{
		if ('undefined' == typeof(active_dates))
		{
			try 
			{
				active_dates = eval('(' + top.name + ')');
			}
			catch(e)
			{
				$.ajax({
					async: false,
					url: url,
					data: {action: 'getActiveDates'},
					success: function(data) {
						top.name = data;
						active_dates = eval('(' + top.name + ')');
						}
				});				
			}
		}
		// If 'active_dates' is array, create a calendar
		if ('object' == typeof(active_dates))
		{
			Calendar.setup(
			{
				flat: 'calendar',
//				date: '{$sel_date}',
//				range: [2007,{$timestamp|date_format:"%Y"}],
				disableFunc: isDisabled,
				flatCallback: dateChanged
			});
		}
	}
});
// END FUNCTIONS FOR CALENDAR


