$(document).ready(function()
		{
			$('#formularz').ajaxForm({						
					target: 		'#list2',
					beforeSubmit: 	validateForm,
					
					success: schowaj
			});	
			
			
			$('#formularz_inwestycje').ajaxForm({						
					target: 		'#list3',
					beforeSubmit: 	validateForm_inwestycje,
					
					success: schowaj
			});	
			
			
			
			//hide error containers
			$("#nazwisko_error").hide();
			
		//	$("#telefon_error").hide();
			$("#mail_error").hide();
			

		});
		
		
		function schowaj() {
			$(".wyslij").hide();
		}
		
		
		function validateForm()
		{
			$("#nazwisko_error").empty().hide();
			
			//$("#telefon_error").empty().hide();
			$("#mail_error").empty().hide();
			
			
			var nazwisko 		= $("#imie_nazwisko").val();
			//var telefon = $("#telefon").val();
			var mail = $("#mail").val();
			
			var errors 				= 0;
			

			
			
			if (nazwisko == null || nazwisko == '' ||   nazwisko.length  <= '2')   
			{
				$("#nazwisko_error").show().append("Proszę podać nazwisko");
				errors++;
			}			
			
	
			if (mail == null || mail == '' ||   mail  <= '2')   
			{
				$("#mail_error").show().append("Proszę podać adres email.");
				errors++;
			}
			

			
			if(!(isValidEmailAddress(mail)))
				{ errors++;}

			
			if (errors > 0)
			{
				alert ("Znaleziono błędy w formularzu!");
				return false;
			}

		}		
		
		function isNumeric(form_value)
		{
			if (form_value.match(/^\d+$/) == null)
				return false;
			else
				return true;
		}
		
	// inwestycje	
		function validateForm_inwestycje()
		{
			$("#wlasciciel_error").empty().hide();
			
			//$("#telefon_error").empty().hide();
			$("#mail_error").empty().hide();
			
			
			var wlasciciel 		= $("#wlasciciel").val();
			var telefon = $("#telefon").val();
			var mail = $("#mail").val();
			
			var lokalizacja 		= $("#lokalizacja").val();
			var powierzchnia 		= $("#powierzchnia").val();
			var cena		= $("#cena").val();
			
			
			var errors 				= 0;
			

			
			
			if (wlasciciel == null || wlasciciel == '' ||   wlasciciel.length  <= '2')   
			{
				$("#wlasciciel_error").show().append("Proszę podać nazwisko");
				errors++;
			}			
			
	
			if (mail == null || mail == '' ||   mail  <= '2')   
			{
				$("#mail_error").show().append("Proszę podać adres email.");
				errors++;
			}
			
			if (telefon == null || lokalizacja  == '' ||   telefon.length  <= '2')   
			{
				$("#telefon_error").show().append("Proszę podać numer telefonu");
				errors++;
			}

			if (lokalizacja  == null || lokalizacja  == '' ||   lokalizacja.length  <= '2')   
			{
				$("#lokalizacja_error").show().append("Proszę podać lokalizację");
				errors++;
			}	
			
			if (powierzchnia  == null || powierzchnia  == '' ||   powierzchnia.length  <= '2')   
			{
				$("#powierzchnia_error").show().append("Proszę podać powierzchnię");
				errors++;
			}	
			
			if (cena  == null || cena  == '' )   
			{
				$("#cena_error").show().append("Proszę podać proponowaną cenę");
				errors++;
			}
			
			
			if(!(isValidEmailAddress(mail)))
				{ errors++;}

			
			if (errors > 0)
			{
				alert ("Znaleziono błędy w formularzu!");
				return false;
			}

		}		
		
		
		
		
			function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
