var konkurencje = Array();

function openW(strona,width_,height_,sc){
         new_window = window.open(strona,'strona','width='+width_+',height='+height_+',toolbars=no,resizeble="no",scrollbars='+sc);
}

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,3})$/)!=null;
}

function IsEmpty(pole) {
  if (pole=='') 
  	return true
	else
	return false
}

function formularz_sprawdz(form)
{
if (form.zawody.selectedIndex==0) {
    alert('Proszę wybrać zawody');
    return false;
  } 
if (form.sekcja.selectedIndex==0) {
    alert('Proszę wybrać sekcję');
    return false;
  } 
if (IsEmpty(form.imie_nazwisko.value)) {
    alert('Proszę podać imię i nazwisko');
    Focus(form.imie_nazwisko);
    return false;
  }
if (form.plec.selectedIndex==0) {
    alert('Proszę wybrać płeć');
    return false;
  } 
if (form.wiek.selectedIndex==0) {
    alert('Proszę wybrać wiek');
    return false;
  } 
if (IsEmpty(form.data_urodzenia.value)) {
    alert('Proszę podać datę urodzenia');
    Focus(form.data_urodzenia);
    return false;
  }
if (form.stopien_niepelnosprawnosci.selectedIndex==0) {
    alert('Proszę wybrać stopień niepełnosprawności');
    return false;
  } 
if (form.stopien_niepelnosprawnosci.selectedIndex==3 && IsEmpty(form.sprzezenia.value)) {
    alert('Proszę podać sprzężenia');
    Focus(form.sprzezenia);
    return false;
  }
if (form.epi.selectedIndex==0) {
    alert('Proszę wybrać epi');
    return false;
  } 
if (IsEmpty(form.miejscowosc.value)) {
    alert('Proszę podać miejscowość');
    Focus(form.miejscowosc);
    return false;
  }
if (IsEmpty(form.miejscowosc.value)) {
    alert('Proszę podać miejscowość');
    Focus(form.miejscowosc);
    return false;
  }
if (IsEmpty(form.pesel.value)) {
    alert('Proszę podać pesel');
    Focus(form.pesel);
    return false;
  }
if (form.kategoria_wiekowa.selectedIndex==0) {
    alert('Proszę wybrać kategorię wiekową');
    return false;
  } 

var numer=document.getElementById("zawody").selectedIndex - 1;


if (document.getElementById("dyscyplina_" + numer).selectedIndex==0) {
    alert('Proszę wybrać dyscyplinę');
    return false;
  } 

if (document.getElementById("gry_zabawy_" + numer).selectedIndex==0) {
    alert('Proszę wybrać gry i zabawy');
    return false;
  } 


if (document.getElementById("iks_" + numer).value=='' && document.getElementById("gry_zabawy_" + numer).selectedIndex!=2) {
	if (document.getElementById("konkurencja1_" + numer).selectedIndex==0) {
    	alert('Proszę wybrać przynajmniej jedną konkurencję');
    	return false;
  	} 
	if (document.getElementById("konkurencja1_" + numer).selectedIndex!=0) {
		if (document.getElementById("czas1_" + numer).value=='') {
    		alert('Proszę podać czas/wynik pierwszej konkurencji');
    		return false;
  		} 
	}
	if (document.getElementById("konkurencja2_" + numer).selectedIndex!=0) {
		if (document.getElementById("czas2_" + numer).value=='') {
    		alert('Proszę podać czas/wynik drugiej konkurencji');
    		return false;
  		} 
	}
	if (document.getElementById("konkurencja3_" + numer).selectedIndex!=0) {
		if (document.getElementById("czas3_" + numer).value=='') {
    		alert('Proszę podać czas/wynik trzeciej konkurencji');
    		return false;
  		} 
	}
if (document.getElementById("konkurencja1_" + numer).selectedIndex==document.getElementById("konkurencja2_" + numer).selectedIndex || document.getElementById("konkurencja1_" + numer).selectedIndex==document.getElementById("konkurencja3_" + numer).selectedIndex || (document.getElementById("konkurencja3_" + numer).selectedIndex==document.getElementById("konkurencja2_" + numer).selectedIndex && document.getElementById("konkurencja2_" + numer).selectedIndex!=0)) {
	alert('Proszę wybrać różne konkurencję');
	return false;
}

konkurencja1=document.getElementById("konkurencja1_" + numer);
konkurencja2=document.getElementById("konkurencja2_" + numer);
konkurencja3=document.getElementById("konkurencja3_" + numer);

if ((konkurencja1.options[konkurencja1.selectedIndex].value.substring(0,4)=='Bieg' && konkurencja2.options[konkurencja2.selectedIndex].value.substring(0,4)=='Bieg') || (konkurencja1.options[konkurencja1.selectedIndex].value.substring(0,4)=='Bieg' && konkurencja3.options[konkurencja3.selectedIndex].value.substring(0,4)=='Bieg') || (konkurencja3.options[konkurencja3.selectedIndex].value.substring(0,4)=='Bieg' && konkurencja2.options[konkurencja2.selectedIndex].value.substring(0,4)=='Bieg'))
	{
	alert('Może zostać wybrana tylko jedna konkurencja biegowa.');
	return false;
	}

if ((konkurencja1.options[konkurencja1.selectedIndex].value.substring(0,8)=='Technika' && konkurencja2.options[konkurencja2.selectedIndex].value.substring(0,8)=='Technika') || (konkurencja1.options[konkurencja1.selectedIndex].value.substring(0,8)=='Technika' && konkurencja3.options[konkurencja3.selectedIndex].value.substring(0,8)=='Technika') || (konkurencja3.options[konkurencja3.selectedIndex].value.substring(0,8)=='Technika' && konkurencja2.options[konkurencja2.selectedIndex].value.substring(0,8)=='Technika'))
	{
	alert('Może zostać wybrana tylko jedna konkurencja techniczna.');
	return false;
	}

if ((konkurencja1.options[konkurencja1.selectedIndex].value.substring(0,8)=='Sztafeta' && konkurencja2.options[konkurencja2.selectedIndex].value.substring(0,8)=='Sztafeta') || (konkurencja1.options[konkurencja1.selectedIndex].value.substring(0,8)=='Sztafeta' && konkurencja3.options[konkurencja3.selectedIndex].value.substring(0,8)=='Sztafeta') || (konkurencja3.options[konkurencja3.selectedIndex].value.substring(0,8)=='Sztafeta' && konkurencja2.options[konkurencja2.selectedIndex].value.substring(0,8)=='Sztafeta'))
	{
	alert('Może zostać wybrana tylko jedna sztafeta.');
	return false;
	}
}

if (IsEmpty(form.opiekun.value)) {
    alert('Proszę podać imię i nazwisko opiekuna');
    Focus(form.opiekun);
    return false;
  }

return true;
}

function sprawdz_rejestracje(form)
{
if (IsEmpty(form.data_przyjazdu.value)) {
    alert('Proszę podać datę przyjazdu');
    Focus(form.data_przyjazdu);
    return false;
  }
if (IsEmpty(form.data_wyjazdu.value)) {
    alert('Proszę podać datę wyjazdu');
    Focus(form.data_wyjazdu);
    return false;
  }
if (IsEmpty(form.imie.value)) {
    alert('Proszę podać imię');
    Focus(form.imie);
    return false;
  }  
if (IsEmpty(form.nazwisko.value)) {
    alert('Proszę podać nazwisko');
    Focus(form.nazwisko);
    return false;
  } 
if (IsEmpty(form.telefon.value)) {
    alert('Proszę podać telefon');
    Focus(form.adres);
    return false;
  } 
if (!IsEmailCorrect(form.mail.value)) {
    alert('Proszę podać poprawny adres e-mail');
    Focus(form.mail);
    return false;
  }
return true;
}

function sprawdz_zadaj_pytanie(form)
{
if (IsEmpty(form.imie.value)) {
    alert('Proszę podać Imię i nazwisko / Firmę.');
    Focus(form.imie);
    return false;
  }
if (IsEmpty(form.adres.value)) {
    alert('Proszę podać adres.');
    Focus(form.adres);
    return false;
  }
if (IsEmpty(form.telefon.value)) {
    alert('Proszę podać telefon.');
    Focus(form.telefon);
    return false;
  }
if (IsEmpty(form.tresc.value)) {
    alert('Proszę podać treść.');
    Focus(form.tresc);
    return false;
  }
return true;
}

function zegar()
{
//pobranie aktualnej godziny, minuty i sekundy
var czas = new Date();
godzina = czas.getHours(); if (godzina <= 9) {godzina = "0"+godzina;}
minuta = czas.getMinutes(); if (minuta <= 9) {minuta = "0"+minuta;}
sekunda = czas.getSeconds(); if (sekunda <= 9) {sekunda = "0"+sekunda;}

 
//wyswietlenie godziny w polu formularza
//zapis przypisania do pola formularza wyglada tak:
document.getElementById("zegarek").value = godzina + ":" + minuta + ":" + sekunda;
}

function ustawzawody()
{
var zawody = document.getElementById('zawody');
for (i=0;i<zawody.options.length;i++)
	{
	if (i==zawody.selectedIndex && i!=0)
		{
		var j=i-1;
		document.getElementById('dyscypliny_' + j).style.display = 'block';		
		document.getElementById('dyscyplina_' + j).name = 'dyscyplina';
		document.getElementById('konkurencja1_' + j).name = 'konkurencja1';
		document.getElementById('konkurencja2_' + j).name = 'konkurencja2';
		document.getElementById('konkurencja3_' + j).name = 'konkurencja3';		
		document.getElementById('czas1_' + j).name = 'wyniki1';
		document.getElementById('czas2_' + j).name = 'wyniki2';
		document.getElementById('czas3_' + j).name = 'wyniki3';	
		document.getElementById('gry_zabawy_' + j).name = 'gry_zabawy';	
		}
		else if (i!=0)
		{
		var j=i-1;
		document.getElementById('dyscypliny_' + j).style.display = 'none';
		document.getElementById('dyscyplina_' + j).name = 'dyscyplina2';		
		document.getElementById('konkurencja1_' + j).name = 'konkurencja_';
		document.getElementById('konkurencja2_' + j).name = 'konkurencja_';
		document.getElementById('konkurencja3_' + j).name = 'konkurencja_';		
		document.getElementById('czas1_' + j).name = 'wyniki_';
		document.getElementById('czas2_' + j).name = 'wyniki_';
		document.getElementById('czas3_' + j).name = 'wyniki_';	
		document.getElementById('gry_zabawy_' + j).name = 'gry_zabawy_';
		}
	}
}

function ustawdyscypline(numer)
{
//pobranie dyscypliny

var dysc = document.getElementById('dyscyplina_' + numer);
var dysc_nazwa = dysc.options[dysc.selectedIndex].value;

var nazwa = "konkurencja1_" + numer;
var nazwa2 = "konkurencja2_" + numer;
var nazwa3 = "konkurencja3_" + numer;

wyczysc_select(nazwa);
wyczysc_select(nazwa2);
wyczysc_select(nazwa3);
 
wstaw(nazwa,'- wybierz -');
wstaw(nazwa2,'- wybierz -');
wstaw(nazwa3,'- wybierz -');
	
document.getElementById('diks_' + numer).style.display = "none";

if (dysc.selectedIndex!=0)
	{
	for (i=0;i<konkurencje[numer].length;i++)
		{
			if (konkurencje[numer][i].substring(0,dysc_nazwa.length)==dysc_nazwa)
				{
				if (konkurencje[numer][i].substring(dysc_nazwa.length + 3,konkurencje[numer][i].length)=="IKS")
					{
					document.getElementById('diks_' + numer).style.display = "block";
					}
					else
					{
					wstaw(nazwa,konkurencje[numer][i].substring(dysc_nazwa.length + 3,konkurencje[numer][i].length));
					wstaw(nazwa2,konkurencje[numer][i].substring(dysc_nazwa.length + 3,konkurencje[numer][i].length));
					wstaw(nazwa3,konkurencje[numer][i].substring(dysc_nazwa.length + 3,konkurencje[numer][i].length));
					}
				}
		}
	document.getElementById('d'+nazwa).style.display='block';
	document.getElementById('d'+nazwa2).style.display='none';
	document.getElementById('d'+nazwa3).style.display='none';
	}
	else
	{
	document.getElementById('d'+nazwa).style.display='none';
	}
}

function iks(numer)
{
	if (document.getElementById("iks_" + numer).value=='' && document.getElementById('gry_zabawy_' + numer).selectedIndex!=2)
		{
			document.getElementById("dgry_"+numer).style.display="block";
		}
		else
		{
			document.getElementById("dgry_"+numer).style.display="none";
		}
}

function gry(numer)
{
	var gry = document.getElementById("gry_zabawy_" + numer);
	var dgry = document.getElementById("dgry_" + numer);
	if (gry.selectedIndex==1 && document.getElementById("iks_" + numer).value=='')
		{
			dgry.style.display="block";
		}
		else if (gry.selectedIndex==2)
		{
			dgry.style.display="none";
		}
}

function konkurencja(numer,id)
{
	var nazwa='dkonkurencja' + numer + '_' + id;
	var pole=document.getElementById(nazwa);
	
	var numerpola=numer-1;
	
	if (document.getElementById("konkurencja" + numerpola + "_" + id).selectedIndex!=0) 
		{
		pole.style.display = 'block';
		document.getElementById("czas" + numer + "_" + id).value = '';
		}
		else
		{
		pole.style.display = 'none';
		document.getElementById("konkurencja" + numer + "_" + id).selectedIndex = 0;
		if (numer==2) 
			{
				document.getElementById("konkurencja3_" + id).selectedIndex = 0;
				document.getElementById("dkonkurencja3_" + id).style.display = 'none';								
			}
		}
}

function wyczysc_select(nazwa)
{
var elSel = document.getElementById(nazwa);
while (elSel.length > 0)
	{
		elSel.remove(elSel.length - 1);
	}	
}

function wstaw(nazwa,napis)
{

  var elOptNew = document.createElement('option');
  elOptNew.text = napis;
  elOptNew.value = napis;
  var elSel = document.getElementById(nazwa);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function sprawdzstopien(pole)
{
	if (document.getElementById('stopien_niepelnosprawnosci').selectedIndex==3)
		{
			document.getElementById('dsprzezenia').style.display='block';
		}
		else
		{
			document.getElementById('dsprzezenia').style.display='none';
		}
}