function readMore(source, target)
{
	$('#' + source).toggle(
		function() {
	  		$('#' + target).show("slow");
		},
		function() {
	  		$('#' + target).hide("slow");
		}
	);
}

function initAds()
{
	$.ajax({
		url: 'ads.php',
		type: 'POST',
		dataType: 'xml',
		timeout: 1000,
		error: function(e)
		{
			//alert(e.responseText);
			//alert('Wystąpił błąd podczas przesyłania danych');
			return;
		},
		success: function(xml)
		{
			rotateAds(
				$(xml).find('line0').text(),
				$(xml).find('line1').text(),
				$(xml).find('line2').text()
			);
		}
	});
}

function rotateAds(line0, line1, line2)
{
	var fade0 = {
		init: function() {
			$("#adline0").fadeIn('slow');
		}
	};
	var fade1 = {
		init: function() {
			$("#adline1").fadeIn('slow');
		}
	};
	var fade2 = {
		init: function() {
			$("#adline2").fadeIn('slow');
		}
	};

	/* zainicjowanie wartości */
	$('#adline0').html(line0);
	$('#adline1').html(line1);
	if (line2 != 'none')
	{
		$('#adline2').html(line2);
	}

	var interval0 = null;
	var interval1 = null;
	var interval2 = null;

	interval0 = setTimeout(
		function() {
			clearTimeout(interval0);
			fade0.init();

			interval1 = setTimeout(
				function() {
					clearTimeout(interval1);
					fade1.init();

					if (line2 != null)
					{
						interval2 = setTimeout(
							function() {
								clearTimeout(interval2);
								fade2.init();
							},
							3000
						);
					}
				},
				3000
			);
		},
		1000
	);
}

function showFields(field, product)
{
	var eyecrm = document.getElementById('eyecrm');
	var eyepos = document.getElementById('eyepos');
	var eyecontact = document.getElementById('eyecontact');

	var nrOfUsers = eyecrm != null && eyecrm.checked;
	nrOfUsers = nrOfUsers || (eyepos != null && eyepos.checked);
	nrOfUsers = nrOfUsers || (eyecontact != null && eyecontact.checked);

	var nrOfRooms = document.getElementById('eyehotel').checked;

	/* combo z liczbą użytkowników */
	// document.getElementById('users').selectedIndex = 0;
	document.getElementById('users').className = 'noerror';
	document.getElementById('usersTR').style.display = nrOfUsers ? '' : 'none';

	/* liczba pokoi */
	// document.getElementById('rooms').selectedIndex = 0;
	document.getElementById('rooms').className = 'noerror';
	document.getElementById('roomsTR').style.display = nrOfRooms ? '' : 'none';
}

function validateDemoLogin(form)
{
	var ok = validateNotEmpty('login') && validateNotEmpty('password');

	if (!ok)
	{
		if (LANG == 'pl')
		{
			alert('Proszę poprawić wartości w polach oznaczonych czerwoną ramką.');
		}
		else if (LANG == 'en')
		{
			alert('Correct values in fields marked with red border, please.');
		}
		return false;
	}
	return true;
}

function entranceMessages()
{
	var url = window.location.href;

	/* email */
	if (url.indexOf('emailsent=yes') > -1)
	{
		if (LANG == 'pl')
		{
			alert('Państwa zgłoszenie kontaktowe zostało przyjęte. Postaramy odezwać się najszybciej jak to możliwe.\nDziękujemy.');
		}
		else if (LANG == 'en')
		{
			alert('Your request has been submited. We\'ll do our best to contact you as quick as possible.\nThank you.');
		}
	}
}

function validateOrder(form)
{
	try
	{
		/* sprawdzenie, czy wypełniono wszystkie wymagane pola */
		var ok = true;

		var eyecrm = document.getElementById('eyecrm');
		var eyepos = document.getElementById('eyepos');
		var eyecontact = document.getElementById('eyecontact');

		var nrOfUsers = eyecrm != null && eyecrm.checked;
		nrOfUsers = nrOfUsers || (eyepos != null && eyepos.checked);
		nrOfUsers = nrOfUsers || (eyecontact != null && eyecontact.checked);

		var nrOfRooms = document.getElementById('eyehotel').checked;

		var okPrograms = validatePrograms();
		if (!okPrograms)
		{
			if (LANG == 'pl')
			{
				alert('Proszę wybrać produkt(y).');
			}
			else if (LANG == 'en')
			{
				alert('Pick product(s) please.');
			}
			return false;
		}

		var okCompany = validateNotEmpty('company');
		var okStreet = validateNotEmpty('street');
		var okCity = validateNotEmpty('city');
		var okPostcode = validateNotEmpty('postcode');
		var okNIP = validateNotEmpty('nip');
		var okPerson = validateNotEmpty('person');
		var okPhone = validateNotEmpty('phone');
		var okEmail1 = validateNotEmpty('email');
		var okEmail2 = validateEmail('email');
		var okUsers = true;
		var okRooms = true;

		if (nrOfUsers)
		{
			okUsers = validateNotEmptyNumber('users');
		}
		if (nrOfRooms)
		{
			okRooms = validateSelectNotEmpty('rooms');
		}

		ok = okCompany && okStreet && okCity && okPostcode && okNIP && okPerson && okPhone && okEmail1 && okEmail2 && okUsers && okRooms && okPrograms;
		if (!ok)
		{
			if (LANG == 'pl')
			{
				alert('Proszę poprawić wartości w wyróżnionych polach.');
			}
			else if (LANG == 'en')
			{
				alert('Correct values in highlited fields please.');
			}
			return false;
		}
	}
	catch (e)
	{
		alert("Wystąpił błąd podczas próby przesłania formularza.\n\nProsimy o kontakt pod adresem: biuro@atante.pl.\n\nPrzepraszamy za utrudnienia.");
		return false;
	}
	return false;
}

function validateContact(form)
{
	try
	{
		/* sprawdzenie, czy wypełniono wszystkie wymagane pola */
		var ok = true;

		var eyecrm = document.getElementById('eyecrm');
		var eyepos = document.getElementById('eyepos');
		var eyecontact = document.getElementById('eyecontact');

		var nrOfUsers = eyecrm != null && eyecrm.checked;
		nrOfUsers = nrOfUsers || (eyepos != null && eyepos.checked);
		nrOfUsers = nrOfUsers || (eyecontact != null && eyecontact.checked);

		var nrOfRooms = document.getElementById('eyehotel').checked;

		var okPrograms = validatePrograms();
		if (!okPrograms)
		{
			if (LANG == 'pl')
			{
				alert('Proszę wybrać produkt(y).');
			}
			else if (LANG == 'en')
			{
				alert('Pick product(s) please.');
			}
			return false;
		}

		var okCompany = validateNotEmpty('company');
		var okPerson = validateNotEmpty('person');
		var okPhone = validateNotEmpty('phone');
		var okEmail1 = validateNotEmpty('email');
		var okEmail2 = validateEmail('email')
		var okUsers = true;
		var okRooms = true;

		if (nrOfUsers)
		{
			okUsers = validateSelectNotEmpty('users');
		}
		if (nrOfRooms)
		{
			okRooms = validateSelectNotEmpty('rooms');
		}

		ok = okCompany && okPerson && okPhone && okEmail1 && okEmail2 && okUsers && okRooms && okPrograms;

		if (!ok)
		{
			if (LANG == 'pl')
			{
				alert('Proszę poprawić wartości w wyróżnionych polach.');
			}
			else if (LANG == 'en')
			{
				alert('Correct values in highlited fields, please.');
			}
			return false;
		}
	}
	catch (e)
	{
		alert("Wystąpił błąd podczas próby przesłania formularza.\n\nProsimy o kontakt pod adresem: biuro@atante.pl.\n\nPrzepraszamy za utrudnienia.");
		return false;
	}
	return true;
}

function validatePrograms()
{
	try
	{
		var products = new Array(
			"eyehotel",
			"eyepos",
			"eyecrm",
			"eyestore",
			"eyeevent",
			"eyecontact",
			"eyecontrol"
		);
		var atLeastOne = false;

		document.getElementById('productstd').className = '';

		for (var i = 0; i < products.length; i++)
		{
			var product = document.getElementById(products[i]);
			if (product == 'undefined' || product == null)
			{
				continue;
			}

			if (product.checked)
			{
				atLeastOne = true;
				break;
			}
		}

		if (!atLeastOne)
		{
			document.getElementById('productstd').className = 'error';
		}
	}
	catch (e)
	{
		alert(e);
	}

	return atLeastOne;
}

function validateSelectNotEmpty(id)
{
	var select = document.getElementById(id);
	if (select.style.display == 'none')
	{
		return true;
	}

	var value = select.options[select.selectedIndex];
	select.className = 'noerror';

	if (value == 'undefined' || value.value == 0)
	{
		select.className = 'error';
		return false;
	}
	return true;
}

function validateNotEmptyNumber(id)
{
	var f = document.getElementById(id);
	f.className = 'noerror';

	// sprawdzenie czy w ogóle coś wpisano
	if (f.value == 'undefined' || f.value == '')
	{
		f.className = 'error';
		return false;
	}

	// zapewnienie, że jest to liczba
	var regex = /^[0-9]+$/ig;
	if (!f.value.match(regex))
	{
		f.className = 'error';
		return false;
	}
	return true;
}

function validateNotEmpty(id)
{
	var f = document.getElementById(id);
	f.className = 'noerror';

	if (f.value == 'undefined' || f.value == '')
	{
		f.className = 'error';
		return false;
	}
	return true;
}

function validateEmail(id)
{
	var f = document.getElementById(id);
	var email = /^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/ig;

	if (!f.value.match(email))
	{
		f.className = 'error';
		return false;
	}
	return true;
}

function findParentTR(element)
{
	if (element.tagName == 'TR')
	{
		return element;
	}
	else
	{
		return findParentTR(element.parentNode);
	}
}