	$(document).ready(
		function() {
			$('#form-nomination').validate(
				{
					'errorElement': 'div',
					'errorClass': 'form-error'
				}
			);
			$('#form-voting').validate(
				{
					'errorElement': 'div',
					'errorClass': 'form-error'
				}
			);

			function prepareVotingForm()
			{
				var checked = $('#soutezit').attr('checked');
				if (checked) 
				{
					$('#tip').addClass('required');
					$('#tip').attr('min', 1);
					$('.tipquestion').show();
				}
				else 
				{
					$('#tip').removeClass('required');
					$('#tip').removeAttr('min');
					$('.tipquestion').hide();
				}
			}
			$('#form-voting #soutezit').click(function () {
				prepareVotingForm();
			});
			$('#form-voting').ready(function () {
				prepareVotingForm();
			});
		}
	);

	var productUrl = '';

	function setProductURL()
	{
		var elmURL = document.getElementById('urlproduktu');
		if (elmURL) 
		{
			elmURL.setAttribute("href", productUrl);
			elmURL.style.display = (productUrl != '' ? '' : 'none');
		}
	}
	
	function unselectProduct(elm)
	{
		selectProduct(0, 0);
	}
	
	function eraseOwnProduct()
	{
		var elmProv = document.getElementById('own_provider_name');
		var elmProd = document.getElementById('own_product_name');
		
		if (elmProv) elmProv.value = '';
		if (elmProd) elmProd.value = '';
	}
	
	function setProduct(elm)
	{
		productUrl = '';
		var elmP = elm.options[elm.selectedIndex];
		var val = elmP.value;
		productUrl = elmP.getAttribute('url');
		setProductURL();
	}

	function setProducts(elm)
	{
		var val = elm.options[elm.selectedIndex].value;
		var product = products[val];
		
		productUrl = '';
		setProductURL();
		var selProd = document.getElementById('products');
		if (selProd)
		{
			clearSelect (selProd);
			for (i in product)
				createOption(selProd, product[i]['id'], product[i]['name'], product[i]['url']);
			if (selProd.options == null || selProd.options.length == 0)
				createOption(selProd, 0, '--- vybrat produkt ---', '');
			else	
				setProduct(selProd);
		}
	}
	
	function selectProduct(prov, prod)
	{
		var elmProvider = document.getElementById('poskytovatel');
		var elmProduct = document.getElementById('products');
		if (elmProvider)
		{
			for (var i = 0; i < elmProvider.options.length; i++)
			{
				if (elmProvider.options[i].value == prov)
				{
					elmProvider.selectedIndex = i;
					setProducts(elmProvider);
					if (prod > 0 && elmProduct)
					{
						for (var j = 0; j < elmProduct.options.length; j++)
						{
							if (elmProduct.options[j].value == prod)
							{
								elmProduct.selectedIndex = j;
								setProduct(elmProduct);
								continue;
							}
						}						
					}
				}
			}
		}
	}
	
	function clearSelect(elm){
		while(elm.options.length > 0){elm.remove(0);}
	}

	function createOption(elm, newValue, newText, newURL){
		var objOption = document.createElement("option");
		objOption.text = newText
		objOption.value = newValue
		objOption.setAttribute('url', newURL);
		if (document.all)
			elm.add(objOption);
		else
			elm.add(objOption, null);
	}
	
	function showProduct(elm)
	{
		//if (productUrl != '')
//			window.open(productUrl);
		//return false;
	}
