
$(document).ready(function(){
	$('td input[type=text]').each(function() {
		$(this).width('98%');
		$(this).parent().css('text-align', 'center');
	});
	$('textarea').each(function() {
		$(this).width('98%');
		$(this).parent().css('text-align', 'center');
	});
	$('form#TransferTips select[name=company_id] option').click(function() {
		$('span#firma_id').html($(this).val());
		alert('aaaaa');
	});
	if ($('select[name=country_id]').length)
	{
		$('form').prepend('<input type="hidden" name="temp_country_id" value="'+$('select[name=country_id]').val()+'" />');
		$('form').prepend('<input type="hidden" name="temp_province_id" value="'+$('select[name=province_id]').val()+'" />');
		$('form').prepend('<input type="hidden" name="temp_city_id" value="'+$('select[name=city_id]').val()+'" />');
		$('select[name=country_id] option').remove();
		$.get(site_url+'get_select.php?get_country=1','', function (result){
			$('select[name=country_id]').append(result);
			$('select[name=country_id] option').each(function() {
				if (parseInt($(this).val())==parseInt($('input[name=temp_country_id]').val()))
				{
					$(this).attr('selected', 'selected');
				}
			});
			if ($('input[name=temp_country_id]').val())
			{
				$('select[name=province_id] option').remove();
				$.get(site_url+'get_select.php?country_id='+$('select[name=country_id]').val(),'', function (result){
					$('select[name=province_id]').append(result);
					$('select[name=province_id] option').each(function() {
						if (parseInt($(this).val())==parseInt($('input[name=temp_province_id]').val()))
						{
							$(this).attr('selected', 'selected');
						}
					});
					
					if ($('input[name=temp_province_id]').val())
					{
						$('select[name=city_id] option').remove();
						$.get(site_url+'get_select.php?province_id='+$('select[name=province_id]').val(),'', function (result){
							$('select[name=city_id]').append(result);
							$('select[name=city_id] option').each(function() {
								if (parseInt($(this).val())==parseInt($('input[name=temp_city_id]').val()))
								{
									$(this).attr('selected', 'selected');
								}
							});
						});
						$('select[name=city_id]').append('<option value="0">--- wszystkie ---</option>');
					} else {
						$('select[name=city_id] option').remove();
						$('select[name=city_id]').append('<option value="0">--- wybierz region ---</option>');
					}
				});
				$('select[name=province_id]').append('<option value="0">--- wszystkie ---</option>');
			} else {
				$('select[name=province_id] option').remove();
				$('select[name=province_id]').append('<option value="0">--- wybierz kraj ---</option>');
			}

		});
		$('select[name=country_id]').append('<option value="0">--- wybierz kraj ---</option>');
	}


	if ($('select[name=load_country_id]').length)
	{
		$('form').prepend('<input type="hidden" name="temp_load_country_id" value="'+$('select[name=load_country_id]').val()+'" />');
		$('form').prepend('<input type="hidden" name="temp_load_province_id" value="'+$('select[name=load_province_id]').val()+'" />');
		$('form').prepend('<input type="hidden" name="temp_load_city_id" value="'+$('select[name=load_city_id]').val()+'" />');
		$('select[name=load_country_id] option').remove();
		$.get(site_url+'get_select.php?get_country=1','', function (result){
			$('select[name=load_country_id]').append(result);
			$('select[name=load_country_id] option').each(function() {
				if (parseInt($(this).val())==parseInt($('input[name=temp_load_country_id]').val()))
				{
					$(this).attr('selected', 'selected');
				}
			});
			if ($('input[name=temp_load_country_id]').val())
			{
				$('select[name=load_province_id] option').remove();
				$.get(site_url+'get_select.php?country_id='+$('select[name=load_country_id]').val(),'', function (result){
					$('select[name=load_province_id]').append(result);
					$('select[name=load_province_id] option').each(function() {
						if (parseInt($(this).val())==parseInt($('input[name=temp_load_province_id]').val()))
						{
							$(this).attr('selected', 'selected');
						}
					});
					
					if ($('input[name=temp_load_province_id]').val())
					{
						$('select[name=load_city_id] option').remove();
						$.get(site_url+'get_select.php?province_id='+$('select[name=load_province_id]').val(),'', function (result){
							$('select[name=load_city_id]').append(result);
							$('select[name=load_city_id] option').each(function() {
								if (parseInt($(this).val())==parseInt($('input[name=temp_load_city_id]').val()))
								{
									$(this).attr('selected', 'selected');
								}
							});
						});
						$('select[name=load_city_id]').append('<option value="0">--- wszystkie ---</option>');
					} else {
						$('select[name=load_city_id] option').remove();
						$('select[name=load_city_id]').append('<option value="0">--- wybierz region ---</option>');
					}
				});
				$('select[name=load_province_id]').append('<option value="0">--- wszystkie ---</option>');
			} else {
				$('select[name=load_province_id] option').remove();
				$('select[name=load_province_id]').append('<option value="0">--- wybierz kraj ---</option>');
			}

		});
		$('select[name=load_country_id]').append('<option value="0">--- wybierz kraj ---</option>');
	}

	
	if ($('select[name=land_country_id]').length)
	{
		$('form').prepend('<input type="hidden" name="temp_land_country_id" value="'+$('select[name=land_country_id]').val()+'" />');
		$('form').prepend('<input type="hidden" name="temp_land_province_id" value="'+$('select[name=land_province_id]').val()+'" />');
		$('form').prepend('<input type="hidden" name="temp_land_city_id" value="'+$('select[name=land_city_id]').val()+'" />');
		$('select[name=land_country_id] option').remove();
		$.get(site_url+'get_select.php?get_country=1','', function (result){
			$('select[name=land_country_id]').append(result);
			$('select[name=land_country_id] option').each(function() {
				if (parseInt($(this).val())==parseInt($('input[name=temp_land_country_id]').val()))
				{
					$(this).attr('selected', 'selected');
				}
			});
			if ($('input[name=temp_land_country_id]').val())
			{
				$('select[name=land_province_id] option').remove();
				$.get(site_url+'get_select.php?country_id='+$('select[name=land_country_id]').val(),'', function (result){
					$('select[name=land_province_id]').append(result);
					$('select[name=land_province_id] option').each(function() {
						if (parseInt($(this).val())==parseInt($('input[name=temp_land_province_id]').val()))
						{
							$(this).attr('selected', 'selected');
						}
					});
					
					if ($('input[name=temp_land_province_id]').val())
					{
						$('select[name=land_city_id] option').remove();
						$.get(site_url+'get_select.php?province_id='+$('select[name=land_province_id]').val(),'', function (result){
							$('select[name=land_city_id]').append(result);
							$('select[name=land_city_id] option').each(function() {
								if (parseInt($(this).val())==parseInt($('input[name=temp_land_city_id]').val()))
								{
									$(this).attr('selected', 'selected');
								}
							});
						});
						$('select[name=land_city_id]').append('<option value="0">--- wszystkie ---</option>');
					} else {
						$('select[name=land_city_id] option').remove();
						$('select[name=land_city_id]').append('<option value="0">--- wybierz region ---</option>');
					}
				});
				$('select[name=land_province_id]').append('<option value="0">--- wszystkie ---</option>');
			} else {
				$('select[name=land_province_id] option').remove();
				$('select[name=land_province_id]').append('<option value="0">--- wybierz kraj ---</option>');
			}

		});
		$('select[name=land_country_id]').append('<option value="0">--- wybierz kraj ---</option>');
	}


	$('select[name=country_id]').change(function () {
		$('select[name=province_id] option').remove();
		$.get(site_url+'get_select.php?country_id='+$('select[name=country_id]').val(),'', function (result){
			$('select[name=province_id]').append(result);
		});
		$('select[name=province_id]').append('<option value="0">--- wszystkie ---</option>');
		$('select[name=city_id] option').remove();
		$('select[name=city_id]').append('<option value="0">--- wszystkie ---</option>');
	});
	
	$('select[name=province_id]').change(function () {
		$('select[name=city_id] option').remove();
		$.get(site_url+'get_select.php?province_id='+$('select[name=province_id]').val(),'', function (result){
			$('select[name=city_id]').append(result);
		});
		$('select[name=city_id]').append('<option value="0">--- wszystkie ---</option>');
	});








	$('select[name=load_country_id]').change(function () {
		$('select[name=load_province_id] option').remove();
		$.get(site_url+'get_select.php?country_id='+$('select[name=load_country_id]').val(),'', function (result){
			$('select[name=load_province_id]').append(result);
		});
		$('select[name=load_province_id]').append('<option value="0">--- wszystkie ---</option>');
		$('select[name=load_city_id] option').remove();
		$('select[name=load_city_id]').append('<option value="0">--- wszystkie ---</option>');
	});
	
	$('select[name=load_province_id]').change(function () {
		$('select[name=load_city_id] option').remove();
		$.get(site_url+'get_select.php?province_id='+$('select[name=load_province_id]').val(),'', function (result){
			$('select[name=load_city_id]').append(result);
		});
		$('select[name=load_city_id]').append('<option value="0">--- wszystkie ---</option>');
	});

	$('select[name=land_country_id]').change(function () {
		$('select[name=land_province_id] option').remove();
		$.get(site_url+'get_select.php?country_id='+$('select[name=land_country_id]').val(),'', function (result){
			$('select[name=land_province_id]').append(result);
		});
		$('select[name=land_province_id]').append('<option value="0">--- wszystkie ---</option>');
		$('select[name=land_city_id] option').remove();
		$('select[name=land_city_id]').append('<option value="0">--- wszystkie ---</option>');
	});
	
	$('select[name=land_province_id]').change(function () {
		$('select[name=land_city_id] option').remove();
		$.get(site_url+'get_select.php?province_id='+$('select[name=land_province_id]').val(),'', function (result){
			$('select[name=land_city_id]').append(result);
		});
		$('select[name=land_city_id]').append('<option value="0">--- wszystkie ---</option>');
	});


});

var email = /^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/;

var v = new RegExp();
v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 


function ZleceniaAdd() {
	if ($('input[name=name]').val().length < 6)
	{
		alert("Podaj prawidłowe imię i nazwisko");
	}
	else if ($('input[name=phone]').val().length < 6)
	{
		alert("Podaj prawidłowy numer telefonu");
	} 
	else if ($('select[name=load_country_id]').val() == "0")
	{
		alert("Wybierz kraj załadunku");
	}
	else if (($('select[name=load_country_id]').val() > "0") && (($('select[name=load_province_id] option').length > 1) && ($('select[name=load_province_id]').val() == "0")))
	{
		alert("Wybierz region załadunku");
	}
	else if ((($('select[name=load_province_id]').val() > "0") && ($('select[name=load_city_id] option').length > 1) && ($('select[name=load_city_id]').val() == "0")) || ($('input[name=load_city_name]').val().length < 4))
	{
		alert("Wybierz miejscowość załadunku");
	} 
	else if ($('select[name=land_country_id]').val() == "0")
	{
		alert("Wybierz kraj wyładunku");
	}
	else if (($('select[name=land_country_id]').val() > "0") && (($('select[name=land_province_id] option').length > 1) && ($('select[name=land_province_id]').val() == "0")))
	{
		alert("Wybierz region wyładunku");
	}
	else if ((($('select[name=land_province_id]').val() > "0") && ($('select[name=land_city_id] option').length > 1) && ($('select[name=land_city_id]').val() == "0")) || ($('input[name=land_city_name]').val().length < 4))
	{
		alert("Wybierz lub wpisz miejscowość wyładunku");
	} else {
		return true;
	}
	return false;
}

function FirmaAddEdit() {
	if (!email.test($('input[name=email]').val()))
	{
		alert("Podaj prawidłowy login / adres e-mail");
	}
	else if ($('input[name=name]').val().length < 3)
	{
		alert("Podaj prawidłową nazwę firmy");
	}
	else if ($('input[name=phone]').val().length < 7)
	{
		alert("Podaj prawidłowy numer telefonu");
	}
	/*else if ($('input[name=nip]').val().length < 10)
	{
		alert("Wpisz NIP");
	}*/
	else if ($('select[name=country_id]').val() == "0")
	{
		alert("Wybierz kraj");
	}
	else if (($('select[name=country_id]').val() == "1") && ($('select[name=province_id]').val() == "0"))
	{
		alert("Wybierz region");
	}
	/*else if ($('input[name=city_name]').val().length < 3)
	{
		alert("Wpisz miejscowość");
	}*/
	/*else if ($('input[name=street]').val().length < 3)
	{
		alert("Wpisz adres");
	}*/
	/*else if ($('input[name=postal_code]').val().length < 4)
	{
		alert("Wpisz kod pocztowy");
	}*/
	else if ($('input[name=pass]').val().length < 6)
	{
		alert("Hasło musi być dłuższe niż 6 znaków");
	} 
	else if ($('select[name=account_id]').val() == "0")
	{
		alert("Wybierz accounta");
	} else {
		return true;
	}
	return false;
}

function FirmaLocationAddEdit() {
	if ($('select[name=country_id]').val() == "0")
	{
		alert("Musisz wybierz przynajmniej kraj");
	} else {
		return true;
	}
	return false;
}


function AccountsAddEdit() {
	if ($('input[name=name]').val().length < 3)
	{
		alert("Podaj prawidłowe imię");
	}
	else if ($('input[name=surname]').val().length < 3)
	{
		alert("Podaj prawidłowe nazwisko");
	}
	else if ($('input[name=commission]').val() < 0)
	{
		alert("Podaj prawidłową wartość prowizji");
	}
	else if (email.test($('input[name=email]').val())==false)
	{
		alert("Podaj prawidłowy login / adres e-mail");
	}
	else if ($('input[name=pass]').val().length < 6)
	{
		alert("Hasło musi być dłuższe niż 6 znaków");
	} else {
		return true;
	}
	return false;
}


function CennikAddEdit() {
	if ($('input[name=name]').val().length < 3)
	{
		alert("Wpisz nazwę pakietu");
	}
	else if ($('input[name=number]').val().length == 0)
	{
		alert("Wpisz ilość zleceń");
	}
	else if ($('input[name=access]').val().length < 3)
	{
		alert("Podaj opis dostępu");
	} 
	else if ($('input[name=regions]').val().length == 0)
	{
		alert("Podaj ilość regionów do wyboru");
	} 
	else if ($('input[name=price_netto]').val().length == 0)
	{
		alert("Podaj cenę netto");
	} 
	else if ($('input[name=price_brutto]').val().length == 0)
	{
		alert("Podaj cenę brutto");
	} 
	else if ($('input[name=sum_save]').val().length == 0)
	{
		alert("Podaj kwotę, jaką oszczędzasz");
	} else {
		return true;
	}
	return false;
}


function DoPobraniaAddEdit() {
	if ($('input[name=description]').val().length < 3)
	{
		alert("Wpisz opis pliku");
	}
	else if (($('input[name=file_id]').val().length == 0) && ($('input[name=file_src_f]').val().length == 0))
	{
		alert("Wybierz plik");
	}
	else if (($('input[name=file_src]:checked').val() == "n") && ($('input[name=file_src_f]').val().length == 0))
	{
		alert("Wybierz plik");
	} else {
		return true;
	}
	return false;
}


function ZamowieniaAddEdit() {
	if ($('select[name=company_id]').val() == "0")
	{
		alert("Wybierz firmę");
	}
	else if ($('select[name=package_id]').val() == "0")
	{
		alert("Wybierz pakiet zleceń dla tej firmy");
	}
	else if ($('select[name=iuser_id]').val() == "0")
	{
		alert("Przypisz accounta do firmy");
	} else {
		return true;
	}
	return false;
}


function FakturyAddEdit() {
	if ($('select[name=company_id]').val() == "0")
	{
		alert("Wybierz firmę");
	}
	else if ($('select[name=iuser_id]').val() == "0")
	{
		alert("Przypisz fakturę do accounta");
	} 
	else if ($('select[name=package_id]').val() == "0")
	{
		alert("Wybierz pakiet zleceń");
	}
	else if ($('input[name=cost]').val().length == 0)
	{
		alert("Podaj kwotę faktury");
	}
	else if ($('input[name=file_name]').val().length < 3)
	{
		alert("Wpisz nazwę pliku");
	}
	else if (($('input[name=invoice_id]').val().length == 0) && ($('input[name=file_src_f]').val().length == 0))
	{
		alert("Wybierz plik");
	}
	else if (($('input[name=file_src]:checked').val() == "n") && ($('input[name=file_src_f]').val().length == 0))
	{
		alert("Wybierz plik");
	} else {
		return true;
	}
	return false;
}

function CsvAdd() {
	if ($('input[name=csvFile]').val().length == 0)
	{
		alert("Wybierz plik");
	}
	else if ($('select[name=country_id]').val() == "0")
	{
		alert("Wybierz przynajmniej kraj");
	} else {
		return true;
	}
	return false;
}

function PomocSubmit() {
	if ($('input[name=title]').val().length < 3)
	{
		alert("Wpisz tytuł wiadomości");
	}
	else if ($('textarea[name=content]').val().length < 20)
	{
		alert("Treść wiadomości powinna zawierać minimum 20 znaków");
	} else {
		return true;
	}
	return false;
}

function MojeDaneUserEdit() {
	if ($('input[name=name]').val().length < 3)
	{
		alert("Wpisz swoje imię");
	}
	else if ($('input[name=surname]').val().length < 3)
	{
		alert("Wpisz swoje nazwisko");
	}
	else if ($('input[name=bank_account]').val().length < 26)
	{
		alert("Wpisz swój prawidłowy numer konta bankowego");
	}
	else if (email.test($('input[name=email]').val())==false)
	{
		alert("Podaj prawidłowy login / adres e-mail");
	}
	else if ($('input[name=pass]').val().length < 6)
	{
		alert("Hasło musi być dłuższe niż 6 znaków");
	} else {
		return true;
	}
	return false;
}

/*function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}
*/
function isSpace(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode == 32)
		return false;
	return true;
}
