function $(element)
{
	return document.getElementById(element);
}

function Focus(control) {
  control.focus();
  control.select();
}

function IsEmailCorrect(email) {
  return email.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/)!=null;
}

function IsHasloCorrect(haslo) {
  return haslo.match(/^([a-zA-Z0-9-_]{5,20})$/)!=null;
}

function IsNickCorrect(nick) {
  return nick.match(/^([a-zA-Z0-9-_]{4,20})$/)!=null;
}


function IsEmpty(pole) {
  if (pole=='') 
  	return true
	else
	return false
}

function ekspert_sprawdz()
{
if (IsEmpty(document.getElementById('imie').value)) {
    alert('Proszę podać imię.');
    Focus(document.getElementById('imie'));
    return false;
  }
if (!IsEmailCorrect(document.getElementById('email').value))	{
    alert('Proszę podać poprawny adres e-mail.');
    Focus(document.getElementById('email'));
	return false;
	}
if (IsEmpty(document.getElementById('tresc').value)) {
    alert('Proszę podać pytanie.');
    Focus(document.getElementById('tresc'));
    return false;
  }
return true;
}

function sprawdz_lekarze()
{
if (document.getElementById('wojewodztwo').selectedIndex<1 && document.getElementById('specjalizacja').selectedIndex<1)
	{
		alert('Proszę wybrać województwo lub specjalizację');
		document.getElementById('wojewodztwo').focus();
		return false;
	}
return true;
}

function sprawdz_lekarze_dodaj()
{
if (IsEmpty(document.getElementById('nazwa').value)) {
    alert('Proszę podać imię i nazwisko lub nazwę.');
    Focus(document.getElementById('nazwa'));
    return false;
	}
if (IsEmpty(document.getElementById('adres').value)) {
    alert('Proszę podać adres.');
    Focus(document.getElementById('adres'));
    return false;
	}
if (IsEmpty(document.getElementById('miasto').value)) {
    alert('Proszę podać miasto.');
    Focus(document.getElementById('miasto'));
    return false;
	}
if (IsEmpty(document.getElementById('miasto').value)) {
    alert('Proszę podać miasto.');
    Focus(document.getElementById('miasto'));
    return false;
	}
if (IsEmpty(document.getElementById('kod_pocztowy').value)) {
    alert('Proszę podać kod pocztowy.');
    Focus(document.getElementById('kod_pocztowy'));
    return false;
	}
if (IsEmpty(document.getElementById('telefon').value)) {
    alert('Proszę podać numer telefonu.');
    Focus(document.getElementById('telefon'));
    return false;
	}
if (document.getElementById('wojewodztwo').selectedIndex<1)
	{
		alert('Proszę wybrać województwo');
		document.getElementById('wojewodztwo').focus();
		return false;
	}
if (document.getElementById('specjalizacja1').selectedIndex<1 && document.getElementById('specjalizacja2').selectedIndex<1 && document.getElementById('specjalizacja3').selectedIndex<1 && document.getElementById('specjalizacja4').selectedIndex<1)
	{
		alert('Proszę wybrać przynajmniej jedną specjalizację');
		document.getElementById('specjalizacja1').focus();
		return false;
	}
return true;
}

function rejestracja()
{
if (IsEmpty(document.getElementById('imie').value)) {
    alert('Proszę podać imię i nazwisko.');
    Focus(document.getElementById('imie'));
    return false;
	}
if (!IsNickCorrect(document.getElementById('nick').value))	{
    alert('Proszę podać poprawny nick. \n Nick powinno mieć od 4 do 20 znaków i składać się z liter i cyfr lub znaków: "-_.".');
    Focus(document.getElementById('nick'));
	return false;
	}
if (!IsHasloCorrect(document.getElementById('haslo').value))	{
    alert('Proszę podać poprawne hasło. \n Hasło powinno mieć od 5 do 20 znaków i składać się z liter i cyfr lub znaków: "-_.".');
    Focus(document.getElementById('haslo'));
	return false;
	}
if (document.getElementById('haslo').value!=document.getElementById('haslo_p').value) {
    alert('Podane hasła nie są identyczne.');
    Focus(document.getElementById('haslo_p'));
    return false;
	}
if (!IsEmailCorrect(document.getElementById('email').value))	{
    alert('Proszę podać poprawny adres e-mail.');
    Focus(document.getElementById('email'));
	return false;
	}
if (document.getElementById('plec').selectedIndex<1)
	{
		alert('Proszę wybrać płeć.');
		document.getElementById('plec').focus();
		return false;
	}
if (IsEmpty(document.getElementById('wiek').value)) {
    alert('Proszę podać wiek.');
    Focus(document.getElementById('wiek'));
    return false;
	}
if (IsEmpty(document.getElementById('miasto').value)) {
    alert('Proszę podać miasto.');
    Focus(document.getElementById('miasto'));
    return false;
	}
if (document.getElementById('wojewodztwo').selectedIndex<1)
	{
		alert('Proszę wybrać województwo');
		document.getElementById('wojewodztwo').focus();
		return false;
	}
if (!document.getElementById('regulamin').checked)
	{
		alert('Proszę zapoznać się z regulaminem.');
		document.getElementById('regulamin').focus();
		return false;
	}
sprawdz_login_istnieje();
return false;
}

function user_change()
{
if (IsEmpty(document.getElementById('imie').value)) {
    alert('Proszę podać imię i nazwisko.');
    Focus(document.getElementById('imie'));
    return false;
	}
if (!IsHasloCorrect(document.getElementById('haslo').value) && document.getElementById('haslo').value!='')	{
    alert('Proszę podać poprawne hasło. \n Hasło powinno mieć od 5 do 20 znaków i składać się z liter i cyfr lub znaków: "-_.".');
    Focus(document.getElementById('haslo'));
	return false;
	}
if (document.getElementById('haslo').value!=document.getElementById('haslo_p').value) {
    alert('Podane hasła nie są identyczne.');
    Focus(document.getElementById('haslo_p'));
    return false;
	}
if (!IsEmailCorrect(document.getElementById('email').value))	{
    alert('Proszę podać poprawny adres e-mail.');
    Focus(document.getElementById('email'));
	return false;
	}
if (document.getElementById('plec').selectedIndex<1)
	{
		alert('Proszę wybrać płeć.');
		document.getElementById('plec').focus();
		return false;
	}
if (IsEmpty(document.getElementById('wiek').value)) {
    alert('Proszę podać wiek.');
    Focus(document.getElementById('wiek'));
    return false;
	}
if (IsEmpty(document.getElementById('miasto').value)) {
    alert('Proszę podać miasto.');
    Focus(document.getElementById('miasto'));
    return false;
	}
if (document.getElementById('wojewodztwo').selectedIndex<1)
	{
		alert('Proszę wybrać województwo');
		document.getElementById('wojewodztwo').focus();
		return false;
	}
return true;
}

function sprawdz_login_istnieje()
{
	var req = mint.Request();		
	req.OnSuccess =
	function()
    {		
		if (this.responseText=='jest')
			{
				alert('Podany nick istnieje w naszym systemie. Prosimy wybrać inny.');
			    Focus(document.getElementById('nick'));
				return false;
			}
		sprawdz_email_istnieje();
	}
	
	req.AddParam("rodzaj", 'sprawdz_login');
	req.AddParam("nick", document.getElementById('nick').value);
	req.Send("/ajax.php");
	return false;
}

function sprawdz_email_istnieje()
{
	var req = mint.Request();		
	req.OnSuccess =
	function()
    {		
		if (this.responseText=='jest')
			{
				alert('Podany adres email istnieje w naszym systemie. Prosimy wybrać inny.');
			    Focus(document.getElementById('email'));
				return false;
			}
		document.getElementById('register').submit();
	}
	
	req.AddParam("rodzaj", 'sprawdz_email');
	req.AddParam("email", document.getElementById('email').value);
	req.Send("/ajax.php");
	return false;
}

function pokaz_rss(id)
{
	var req = mint.Request();		
	req.OnSuccess = function()
    {		
		document.getElementById('rss').innerHTML=this.responseText;
	}
	
	req.AddParam("rodzaj", 'pokaz_rss');
	req.AddParam("id_", id);
	req.Send("/ajax.php");
//	return false;
}
	
function galeria(numer)
{
if (IsEmpty(document.getElementById('tytul').value)) {
    alert('Proszę podać tytuł.');
    Focus(document.getElementById('tytul'));
    return false;
	}
if (IsEmpty(document.getElementById('opis').value)) {
    alert('Proszę podać opis.');
    Focus(document.getElementById('opis'));
    return false;
	}
if (IsEmpty(document.getElementById('zdjecie').value) && numer!=1) {
    alert('Proszę podać zdjecie.');
    Focus(document.getElementById('zdjecie'));
    return false;
	}
if (document.getElementById('wojewodztwo').selectedIndex<1)
	{
		alert('Proszę wybrać województwo');
		document.getElementById('wojewodztwo').focus();
		return false;
	}
return true;
}

function blog_sprawdz(numer)
{
if (IsEmpty(document.getElementById('tytul').value)) {
    alert('Proszę podać tytuł.');
    Focus(document.getElementById('tytul'));
    return false;
	}
if (IsEmpty(document.getElementById('zajawka').value)) {
    alert('Proszę podać zajawkę.');
    Focus(document.getElementById('zajawka'));
    return false;
	}
return true;
}

function kategorie_sprawdz(numer)
{
if (IsEmpty(document.getElementById('kategoria').value)) {
    alert('Proszę podać kategorię.');
    Focus(document.getElementById('kategoria'));
    return false;
	}
return true;
}

function usun(element)
{
	var confirmed = confirm("Czy na pewno chcesz usunąć wybrane zdjęcie?");
	if (confirmed)
		{
	 	element.href=element.href + 'usun/';		
		return true;
		}
	return false;
}

function usun_wpis(element)
{
	var confirmed = confirm("Czy na pewno chcesz usunąć wybrany wpis?");
	if (confirmed)
		{
	 	element.href=element.href + 'usun/';		
		return true;
		}
	return false;
}

function usun_kategorie(element)
{
	var confirmed = confirm("Czy na pewno chcesz usunąć wybraną kategorię?");
	if (confirmed)
		{
	 	element.href=element.href + 'usun/';		
		return true;
		}
	return false;
}

function openW(strona,width_,height_,sc){
         new_window = window.open(strona,'strona','width='+width_+',height='+height_+',toolbars=no,resizeble="no",scrollbars='+sc);
}

function newsletter()
{
if (!IsEmailCorrect(document.getElementById('mail').value))	{
    alert('Proszę podać poprawny adres e-mail.');
    Focus(document.getElementById('mail'));
	return false;
	}	
}

function forum_watek_sprawdz()
{
if (IsEmpty(document.getElementById('tytul').value)) {
    alert('Proszę podać tytuł.');
    Focus(document.getElementById('tytul'));
    return false;
	}
return true;		
}

function sprawdz_kontakt()
{
if (IsEmpty(document.getElementById('imie').value)) {
    alert('Proszę podać imię.');
    Focus(document.getElementById('imie'));
    return false;
	}
if (!IsEmailCorrect(document.getElementById('mail').value))	{
    alert('Proszę podać poprawny adres e-mail.');
    Focus(document.getElementById('mail'));
	return false;
	}
if (IsEmpty(document.getElementById('wiadomosc').value)) {
    alert('Proszę podać wiadomość.');
    Focus(document.getElementById('wiadomosc'));
    return false;
	}
return true;		
}

function sprawdz_wyszukiwarka()
{
alert('tak');
if (document.getElementById('slowo').value.length()<3) {
    alert('Prosimy podać wyszukiwane słowo. Słowo powinno mieć minimum 3 znaki.');
    Focus(document.getElementById('slowo'));
    return false;
	}
return true;		
}

function sprawdz_szukaj_zaawansowane()
{
if (document.getElementById('slowo').value.length()<3 || document.getElementById('miasto').value.length()<3 || document.getElementById('uzytkownik').value.length()<3) {
    alert('Prosimy podać wyszukiwane słowo. Słowo powinno mieć minimum 3 znaki.');
    Focus(document.getElementById('slowo'));
    return false;
	}
return true;		
}

function ankietaZaznacz(element){
	jQuery("#"+element).val(["inne"]);
}

function ankietaOdznacz(input,element){
	if (IsEmpty(jQuery(input).val())){
		jQuery("#"+element).attr("checked","");
	}
}

function ankietaSchowaj(element,input,wartosci){
	for (iter in wartosci){
		if(jQuery(input).val()==wartosci[iter]){
			jQuery("."+element).hide();
			return;
		}
	}
	jQuery("."+element).show();
}

function sprawdz_ankieta(){
	error=0;
	// pola textowe
	teksty=new Array('wiek','best_zakladki','informacje','inne_portale','sugestie');
	for (iter in teksty){
		if(IsEmpty(jQuery("#"+teksty[iter]).val())){
			jQuery("#"+teksty[iter]).addClass('error');
			error=1;
		} else {
			jQuery("#"+teksty[iter]).removeClass('error');
		}
	}
	// email
	if(!IsEmailCorrect(jQuery("#mail").val())){
		jQuery("#mail").addClass('error');
		error=1;
	} else {
		jQuery("#mail").removeClass('error');
	}
	// radio buttony
	radio_check=new Array('zamieszkanie','wada_sluchu','odwiedziny','referer','akcje','budowa_menu','wartosc_tresci');
	for (iter in radio_check){
		
		if(jQuery("form [name='"+radio_check[iter]+"']:checked").length<1){
			error=1;
			jQuery("form [name='"+radio_check[iter]+"']").addClass('error');
		} else {
			jQuery("form [name='"+radio_check[iter]+"']").removeClass('error');
		}
	}
	// checkboxy
	check=new Array('czas_wolny','funkcjonalnosci');
	for (iter in check){
		if(jQuery("form [name='"+check[iter]+"[]']:checked").length<1){
			error=1;
			jQuery("form [name='"+check[iter]+"[]']").addClass('error');
		} else {
			jQuery("form [name='"+check[iter]+"[]']").removeClass('error');
		}
	}
	// pola textowe gdy zaznaczono inne
	warunki=new Array('referer','czas_wolny','funkcjonalnosci');
	for (iter in warunki){
		if((jQuery("#"+warunki[iter]+"_inne:checked").length>0) && (IsEmpty(jQuery("#"+warunki[iter]+"_inne_opis").val()))){
			error=1;
			jQuery("#"+warunki[iter]+"_inne_opis").addClass('error');
		} else {
			jQuery("#"+warunki[iter]+"_inne_opis").removeClass('error');
		}
	}
	// dodatkowe elementy zalezne od innych odpowiedzi
	sprawdz=new Array();
	ws=jQuery("form [name='wada_sluchu']:checked").val();
	if (!IsEmpty(ws) && ws!='slyszaca'){
		sprawdz.push('wada_sluchu_opis');
		sprawdz.push('wada_sluchu_bariera');
	}
	ak=jQuery("form [name='akcje']:checked").val();
	if (!IsEmpty(ak) && ak!='zadna'){
		sprawdz.push('akcje_uzasadnij');
	}
	bm=jQuery("form [name='budowa_menu']:checked").val();
	if (!IsEmpty(bm) && bm!='tak'){
		sprawdz.push('budowa_menu_uzasadnij');
	}
	wt=jQuery("form [name='wartosc_tresci']:checked").val();
	if (!IsEmpty(wt) && wt!='5' && wt!='4' && wt!='3'){
		sprawdz.push('wartosc_tresci_uzasadnij');
	}
	
	for (iter in sprawdz){
		if(IsEmpty(jQuery("#"+sprawdz[iter]).val())){
			jQuery("#"+sprawdz[iter]).addClass('error');
			error=1;
		} else {
			jQuery("#"+sprawdz[iter]).removeClass('error');
		}
	}
	if (error==1){
		alert ('Niewypełniono wszystkich pól');
		return false;
	}
	return true;
}