var separator = '-';

function equalizeGenres(genreID, mainElement)
{
	elementCount = document.forma.elements.length;
	document.forma.elements['genre_' + genreID].value
		= (state = document.forma.elements[mainElement].checked) ? 1 : 0;

	prefix = 'g_' + genreID;
	prefixLen = prefix.length;

	for (var i = 0; i < elementCount; i++)
		if (document.forma.elements[i].name.substr(0,prefixLen) == prefix)
			if (document.forma.elements[i].name != mainElement)
				document.forma.elements[i].checked = state;
}

function enableRubrics()
{
	var mode = document.forma.all_rubrics.checked;
	var elementsCount = document.forma.elements.length;
	for (i = 0; i < elementsCount; i++)
		if (document.forma.elements[i].name.search(/(^r_)|(^t_)|(^genre_)|(^g_)/) >= 0)
			document.forma.elements[i].disabled = mode;
}

function checkData()
{
	if (DATA_SENT) return false;

	if (!checkActiveDate('from'))  return false;
	if (!checkActiveDate('to'))  return false;

	elementsCount = document.forma.elements.length;

	// check genre list
	genreCount = themeCount = topicCount = selGenres = selThemes = selTopics = 0;
	genreList = themeList = topicList = '';

	//if (!document.forma.all_rubrics.checked)
	for (i = 0; i < elementsCount; i++)
	// calculate selected items then disable it
	{
		if (document.forma.elements[i].name.search(/^genre_\d+/) >= 0)
		{
			genreCount++;
			if (document.forma.elements[i].value == 1)
			{
				selGenres++;
				num = document.forma.elements[i].name.match(/\d+/);
				genreList += (genreList ? separator : '') + num[0];
			}
		}
		else if (document.forma.elements[i].name.search(/^t_\d+/) >= 0)
		{
			themeCount++;
			if (document.forma.elements[i].checked)
			{
				selThemes++;
				num = document.forma.elements[i].name.match(/\d+/);
				themeList += (themeList ? separator : '') + num[0];
			}
		}
		else if (document.forma.elements[i].name.search(/^r_\d+/) >= 0)
		{
			topicCount++;
			if (document.forma.elements[i].checked)
			{
				selTopics++;
				num = document.forma.elements[i].name.match(/\d+/);
				topicList += (topicList ? separator : '') + num[0];
			}
		}
	}

	/*if (!document.forma.all_rubrics.checked)
	{
		// check user selection
		if (!(selGenres + selThemes + selTopics))
			if (confirm(noGenresThemesTopics)) document.forma.all_rubrics.checked = true; else return false;*/

		// disable selection if everithing is checked
		if ((selGenres == genreCount) || (selThemes == themeCount) || (selTopics == topicCount))
			genreList = themeList = topicList = '';
	//}

	with (document.forma.genres) if (genreList) value = genreList; else disabled = true;
	with (document.forma.themes) if (themeList) value = themeList; else disabled = true;
	with (document.forma.topics) if (topicList) value = topicList; else disabled = true;

	// construct date_from
	if (document.forma.date_filter_from.checked)
		document.forma.date_from.value
			= 	document.forma.year_from.value
			+ '-' + document.forma.month_from.selectedIndex
			+ '-' + document.forma.day_from.value;
	else	document.forma.date_from.disabled = true;

	// construct date_to
	if (document.forma.date_filter_to.checked)
		document.forma.date_to.value
			= 	document.forma.year_to.value
			+ '-' + document.forma.month_to.selectedIndex
			+ '-' + document.forma.day_to.value;
	else	document.forma.date_to.disabled = true;

	//document.forma.all_rubrics.disabled = true;

	// disable all rubrication elements
	for (i = 0; i < elementsCount; i++)
		if (document.forma.elements[i].name.search(/(^genre_)|(^t_)|(^r_)|(^g_)|(^date_filter_)|(^year_)|(^month_)|(^day_)/) >= 0)
			document.forma.elements[i].disabled = true;

	return DATA_SENT = true;
}