$(function() {
	$("#oferta, #historia, #zapytanie, #kontakt").livequery('click', function(event) {
		event.preventDefault();
		var element = $(this).attr("id");
		$("#content_inner_right_tresc").css("display", "none");
		$.ajax({
			type: "POST",
			url: "php/funkcje_wyswietl.php",
			data: "data="+element,
			dataType: "html",
			success: function(data) {
				$("#content_inner_right_tresc").fadeIn();
				$("#content_inner_right_tresc").html(data);
			}
		});
		
	var i; //zmienna pomocnicza
	var flags_zapytanie = new Array();
	for(i=0; i<=2; i++) { flags_zapytanie[i] = 0; } //zerowanie flag, ilosc flag = 3
	var wartosc_name;
	var wartosc_mail;
	var wartosc_text;
		
		$("#send_form_pl").livequery('click', function(event) {
			var user_name = $("input#user_name_pl").val();
    	var mail = $("input#mail_pl").val();
    	var tresc = $("textarea#tresc_pl").val();
			var dataString = 'user_name=' + user_name + '&mail=' + mail + '&tresc=' + tresc;
			//alert("aaa");
			$('#contact_form').html("<img id='wait' src='images/wait.gif' />");
			//$('#contact_form').html("<h2>Formularz zgłoszeniowy tymczasowo nie działa, proszę spróbować później...</h2><img id='wait' src='images/wait.gif' />");
			$.ajax({
				type: "POST",  
				url: "php/mail.php",  
				data: dataString,
				dataType: "html",
				success: function(data) {
					//$('#contact_form').html(data);
					$('#contact_form').html("<div id='message'></div>");
					$('#message').html("<h2>Dziekujemy za wypełnienie formularza!</h2>")
					.append("<p>Twoja wiadomość została wysłana do naszej firmy.</p>")
					.hide()
					.fadeIn(1000, function() {
						$('#message').append("<img id='checkmark' src='images/check.png' />");  
					});
				}
			});
			return false;
		});	
		$("#user_name_pl").livequery('keyup', function(event) {
			var wartosc_name = $(this).val();
			if(czyPuste(wartosc_name) == false) {
				$("#user_name_info_pl").addClass("error");
				$("#send_form_pl_info").addClass("error");
				$("#send_form_pl_info").html("Prosze poprawnie wypełnić pola formularza!");
				$("#send_form_pl").attr("disabled","disabled");
				$("#user_name_info_pl").html("To pole nie może być puste!");
				flags_zapytanie[0] = 0;
			}
			else {
				$("#user_name_info_pl").removeClass("error");
				$("#user_name_info_pl").html("ok");
				flags_zapytanie[0] = 1;
				if(sprawdzFlagi(flags_zapytanie, 3)) {
					$("#send_form_pl_info").removeClass("error");
					$("#send_form_pl_info").html("Możesz teraz wysłać zapytanie.");
					$("#send_form_pl").removeAttr("disabled");
				}
			}
			return false;
		});
		$("#mail_pl").livequery('keyup', function(event) {
			var wartosc_mail = $(this).val();
			if(czyPuste(wartosc_mail) == false) {
				$("#mail_info_pl").addClass("error");
				$("#send_form_pl_info").addClass("error");
				$("#send_form_pl_info").html("Prosze poprawnie wypełnić pola formularza!");
				$("#send_form_pl").attr("disabled","disabled");
				$("#mail_info_pl").html("To pole nie może być puste!");
				flags_zapytanie[1] = 0;
			}
			else if(czyEmail(wartosc_mail) == false) {
				$("#mail_info_pl").addClass("error");
				$("#send_form_pl_info").addClass("error");
				$("#send_form_pl_info").html("Prosze poprawnie wypełnić pola formularza!");
				$("#send_form_pl").attr("disabled","disabled");
				$("#mail_info_pl").html("Wpisz prawidłowy adres email!");
				flags_zapytanie[1] = 0;
			}
			else {
				$("#mail_info_pl").removeClass("error");
				//$("#send_form_pl").removeAttr("disabled");
				$("#mail_info_pl").html("ok");
				flags_zapytanie[1] = 1;
				if(sprawdzFlagi(flags_zapytanie, 3)) {
					$("#send_form_pl_info").removeClass("error");
					$("#send_form_pl_info").html("Możesz teraz wysłać zapytanie.");
					$("#send_form_pl").removeAttr("disabled");
				}
			}
			return false;
		});
		$("#tresc_pl").livequery('keyup', function(event) {
			var wartosc_text = $(this).val();
			if(czyPuste(wartosc_text) == false) {
				$("#tresc_info_pl").addClass("error");
				$("#send_form_pl_info").addClass("error");
				$("#send_form_pl_info").html("Prosze poprawnie wypełnić pola formularza!");
				$("#send_form_pl").attr("disabled","disabled");
				$("#tresc_info_pl").html("To pole nie może być puste!");
				flags_zapytanie[2] = 0;
			}
			else {
				$("#tresc_info_pl").removeClass("error");
				$("#tresc_info_pl").html("ok");
				flags_zapytanie[2] = 1;
				if(sprawdzFlagi(flags_zapytanie, 3)) {
					$("#send_form_pl_info").removeClass("error");
					$("#send_form_pl_info").html("Możesz teraz wysłać zapytanie.");
					$("#send_form_pl").removeAttr("disabled");
				}
			}
			return false;
		});
	});
	return false;
});

function sprawdzFlagi(flaga, ile) {
	var i;
	for(i=0; i<ile; i++) {	if(flaga[i] === 0) { return false; } }
	return true;
}
function czyPuste(element) {
	if (element.length == 0) return false;
	else return true;
}
function czyEmail(element) {
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (!element.match(re)) return false;
	else return true;
}

//startuje sesje, wyswietla strone logowania
function start() {
	$("body").removeAttr("onload");
	$("#main").css("display", "none");
	$("#main").html("<img id='wait' src='images/wait1.gif' />");
	$.ajax({
		type: "POST",
		url: "php/funkcje_wyswietl.php",
		data: "data=start",
		dataType: "html",
		success: function(data) {
			$("#main").fadeIn();
			$("#main").html(data);
		}
	});
}

