function isEmail(email) {
	email = email.replace(/[ ]/g, '');
	var regEmail = /^([a-zA-Z0-9._-]{1,})@([a-zA-Z0-9._-]{1,})\.([a-zA-Z]{2,4})$/;
	if (!regEmail.test(email)) return false;
	else return true;
}





var ilosc = 0;
var pp=0;
var cc=1;
var nn=2;
$(document).ready(function() {
	$('.bann').each (function() {
		ilosc++;
	});
	//pp=ilosc;

	if (ilosc>=1) {
		$('div#bann'+cc).fadeIn(300);
	}

	$('#next').click(function() {
		//alert(pp+'-'+cc+'-'+nn);
		clearInterval(id);
		id = setInterval("wywInt();", 5000);
		wywInt();
	});
	
	$('#prev').click(function() {
		//alert(pp+'-'+cc+'-'+nn);
		clearInterval(id);
		wywInt2();
	});

	id = setInterval("wywInt();", 5000);
});


function wywInt() {
	cc++;
	if (cc>ilosc) { cc=1; }
	nn = cc+1;
	if (nn>ilosc) { nn=1; }
	pp = cc-1;
	if (pp<1) { pp = ilosc; }
	fades(cc);
}

function wywInt2() {
	cc--;
	if (cc<1) { cc=ilosc; }
	nn = cc+1;
	if (nn>ilosc) { nn=1; }
	pp = cc-1;
	if (pp<1) { pp = ilosc; }
	fades(cc);
}


function fades(i) {
	for (var x=1; x<=ilosc; x++) {
		if (i==x) {
			$('div#bann'+x).fadeIn(300);
			//$('#krop'+x).attr("src","/css/images/on.png");
		}
		else {
			$('div#bann'+x).fadeOut(300);
			//$('#krop'+x).attr("src","/css/images/off.png");
		}
	}
}












function send_mail(obj) {
	var id = $(obj).attr('id');
	if (validacja_formy(id)) {
		$(obj).hide(500);
		$('#wysylanie').show(500);
		var imie = $(obj).find('input[name="imie"]').val();
		var mail = $(obj).find('input[name="mail"]').val();
		var tresc = $(obj).find('textarea[name="tresc"]').val();
		imie = encodeURIComponent(imie);
		mail = encodeURIComponent(mail);
		tresc = encodeURIComponent(tresc);
		
		$.ajax({
			type: "GET",
			url: "/ajax.php?mail="+mail+"&imie="+imie+"&tresc="+tresc,
			cache: true, // to prevent doubles ;)
			success: function(info) {
				if (info) {
					$('#loader').hide(500);
					if (info=="OK") {
						$('#wysylanie').hide(500);
						$('#sukces').show(500);
					}
					else {
						//alert(info);
						alert("Nieznany błąd. Spróbuj ponownie. Jeżeli błąd będzie się powtarzał - proszę skorzystaj z innej formy kontaktu.");
						$(obj).show(500);
						$('#wysylanie').hide(500);
					}
				}
				else {
					alert("Żądanie przekroczyło dozwolony czas oczekiwania. Sprawdź połączenie z internetem, odśwież stronę i spróbuj ponownie.");
					$(obj).show(500);
					$('#wysylanie').hide(500);
				}
	         } // success
		});// ajax
	}
	return false;
}


/**
 * Waliduje formularze
 * @param id - [atrybut html] id formularza 
 * @return bool - jezeli wszystko ok to true
 * TODO przepisac
 */
function validacja_formy(id, or_ok) {
	var blad='';
	$('#'+id+' .req_text').each (function () {
		var def = $(this)[0].defaultValue;
		var cur = $(this).val();
		if (cur=='' || cur==def) {
			blad += "- " + $(this).attr('title') + ".\n";
		}
	});
	$('#'+id+' .req_mail').each (function () {
		if (!isEmail($(this).attr('value'))) {
			blad += "- " + $(this).attr('title') + "\n";
		}
	});
	$('#'+id+' .type_mail').each (function () {
		if (!isEmail($(this).attr('value')) && ($(this).attr('value')!='')) {
			blad += "- Niepoprawny " + $(this).attr('title') + "\n";
		}
	});
	$('#'+id+' .req_checkbox').each (function () {
		if (!$(this).attr('checked')) {
			blad += "- " + $(this).attr('title') + "\n";
		}
	});
	
	if (or_ok != undefined) {
		or_ok = false;
		dodaj_txt = "Podaj "
		$('#'+id+' .req_or').each (function () {
			dodaj_txt += $(this).attr('title')+" lub ";
			if ($(this).attr('value')!='') {
				or_ok=true;
			}
		});
		dodaj_length=dodaj_txt.length;
		dodaj_txt=dodaj_txt.substring(0, dodaj_length-5)+'.';
		
		if (or_ok==false) {
			blad += "- "+dodaj_txt+"\n";
		}
	}
	

	if (blad != '') {
		alert(blad);
		return false;
	}
	else {
		return true;
	}
}






