function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}

function validateEmpty(valfield)
{
	valfield2=valfield.value;
	if (trim(valfield2)=='')
	{
		valfield.style.border="1px solid red";
		return false;
	}
	return true;
}

function fillZero(value)
{
	if(value < 10)
		return '0'+value;
	else
		return value;
}

function validateOnSubmitSiriusAir() 
{
	var errs=0;

	if (!validateEmpty (document.getElementById("origin"))) errs += 1;
	if (!validateEmpty (document.getElementById("destination"))) errs += 1;
	if (errs>1)  alert('Wymagane pola nie zostaly wypelnione.');
	if (errs==1) alert('Wymagane pole nie zostalo wypelnione.');

	if (errs==0)
	{

		Now=new Date();

		wyloty_dzien=document.getElementById("ar_day").value;
		wyloty_miesiac=document.getElementById("ar_month").value;
		wyloty_rok=document.getElementById("ar_year").value;

		if((wyloty_miesiac == 2 || wyloty_miesiac == 4 || wyloty_miesiac == 6 || wyloty_miesiac == 9 || wyloty_miesiac == 11 ) && wyloty_dzien == 31)
		{
			alert('Błędna data.');
			errs += 1;
		}

		DataOdbior=new Date(wyloty_rok,wyloty_miesiac,wyloty_dzien);
		DataOdbior.setFullYear(wyloty_rok,wyloty_miesiac,wyloty_dzien);

		if (DataOdbior < Now && errs==0) 
		{
			alert('Data wylotu jest w przeszlosci.');
			errs += 1;
		}
		
		if(document.getElementById("oneway").checked)
		{
			powroty_dzien=document.getElementById("dep_day").value;
			powroty_miesiac=document.getElementById("dep_month").value;
			powroty_rok=document.getElementById("dep_year").value;

			if((powroty_miesiac == 2 || powroty_miesiac == 4 || powroty_miesiac == 6 || powroty_miesiac == 9 || powroty_miesiac == 11 ) && powroty_dzien == 31 && errs==0)
			{
				alert('Błędna data.');
				errs += 1;
			}

			DataZwrot=new Date();
			DataZwrot.setFullYear(powroty_rok,powroty_miesiac,powroty_dzien);

			if (DataZwrot < Now && errs==0) 
			{
				alert('Data przylotu jest w przeszlosci.');
				errs += 1;
			};

			if (DataZwrot < DataOdbior && errs==0)
			{
				alert('Data przylotu nie moze byc wczesniejsza od daty wylotu.');
				errs +=1;
			}
		}
		else
		{
			DataZwrot = DataOdbior;
		}

		origin_long=document.getElementById("origin").value;
		destination_long=document.getElementById("destination").value;
		
		origin_long=origin_long.toUpperCase();
		destination_long=destination_long.toUpperCase();
				
		if (document.getElementById("oneway"))
		{
			if (document.getElementById("oneway").checked)
			{
			flight_type='return';
			}
			else
			{
			flight_type='single';
			}
		}
		else
		{
			flight_type=document.getElementById("oneway").value;
		}
		
		paxADT=document.getElementById("adt").value;
		paxCHD=document.getElementById("chd").value;
		paxINF=document.getElementById("inf").value;
		cabinClass=document.getElementById("cabinClass").value;

		if (paxINF + paxCHD > paxADT && errs==0)
		{
			alert('Ilość dzieci i niemowląt nie może przekroczyć liczby dorosłych.');
			errs +=1;
		}

		if (errs==0)
		{
			var monthStart = DataOdbior.getMonth() + 1;
			var monthEnd = DataZwrot.getMonth() + 1;

			location.href='http://sirius.amadeus.com.pl/s3air/skytourspl/airstart.aspx?origin='+origin_long+'&depDate='+fillZero(DataOdbior.getDate().toString())+'.'+fillZero(monthStart)+'.'+DataOdbior.getFullYear()+'&destination='+destination_long+'&retDate='+fillZero(DataZwrot.getDate().toString())+'.'+fillZero(monthEnd)+'.'+DataZwrot.getFullYear()+'&cabinClass='+cabinClass+'&flightType='+flight_type+'&currency=PLN&adt='+paxADT+'&chd='+paxCHD+'&inf='+paxINF+'&offerType=Standard&_as=PowerPrice';	    
		}
	}
}