function DaneBilety(Id,IloscMiejsc,Cena,Nazwa,Grupa)
{
  this.Id=Id;
  this.IloscMiejsc=IloscMiejsc;
  this.Cena=Cena;
  this.Nazwa=Nazwa;
  this.Grupa=Grupa;
};
var Bilety=new Array();
function DaneOpcje(Nazwa,Id,Grupa)
{
  this.Nazwa=Nazwa;
  this.Id=Id;
  this.Grupa=Grupa;
};
var Opcje=new Array();
function DaneTerminy(Id,Grupa,Data)
{
  this.Id=Id;
  this.Grupa=Grupa;
  this.Data=Data;
};
var Terminy=new Array();
function RodzajePlatnosci(Nazwa,Czas)
{
  this.Nazwa=Nazwa;
  this.Czas=Czas;
};
var Platnosci=new Array();
var Bledy=new Array();
function ChangeRodzajBiletu()
{
  r_biletu=document.bilety.rodzaje_biletow;
  var id_rodzaju_biletu=r_biletu.options[r_biletu.selectedIndex].value;
  for (var i=0;i < Bilety.length; i++)
  {
    if (id_rodzaju_biletu==Bilety[i].Id)
    {
      document.bilety.ilosc.value=Bilety[i].IloscMiejsc;
      var cena=Bilety[i].Cena*document.bilety.ilosc.value/Bilety[i].IloscMiejsc;
      document.bilety.cena.value=cena.toFixed(2)+' PLN';
    }
  }
}
function ChangeTermin(Termin)
{
  r_biletow=document.bilety.rodzaje_biletow;
  r_opcji=document.bilety.opcje;
  var id_terminu=Termin.options[Termin.selectedIndex].value;
  var grupa=0;
  for (var i=0; i < Terminy.length; i++)
    if (Terminy[i].Id==id_terminu)
      grupa=Terminy[i].Grupa;
  for (var i = r_biletow.options.length; i > 0; i--)
    r_biletow.options[i - 1] = null;
  for (var i=0; i < Bilety.length; i++)
    if (Bilety[i].Grupa==grupa || Bilety[i].Grupa==-1)
      r_biletow.options[r_biletow.options.length] = new Option(Bilety[i].Nazwa,Bilety[i].Id)
  r_biletow.selectedIndex=0;
  if (r_opcji)
  {
    for (var i = r_opcji.options.length; i > 0; i--)
      r_opcji.options[i - 1] = null;
    for (var i=0; i < Opcje.length; i++)
      if (Opcje[i].Grupa==grupa || Opcje[i].Grupa==-1)
         r_opcji.options[r_opcji.options.length] = new Option(Opcje[i].Nazwa,Opcje[i].Id)
    r_opcji.selectedIndex=-1;
  }
  ChangeRodzajBiletu();
}
function ChangeLiczbaOsob()
{
  if(Math.round(document.bilety.ilosc.value/Bilety[document.bilety.rodzaje_biletow.selectedIndex].IloscMiejsc)!=document.bilety.ilosc.value/Bilety[document.bilety.rodzaje_biletow.selectedIndex].IloscMiejsc)
  {
    alert(Bledy[3]+' '+Bilety[document.bilety.rodzaje_biletow.selectedIndex].IloscMiejsc);
    document.bilety.ilosc.value=Bilety[document.bilety.rodzaje_biletow.selectedIndex].IloscMiejsc;
  }
  var cena=Bilety[document.bilety.rodzaje_biletow.selectedIndex].Cena*document.bilety.ilosc.value/Bilety[document.bilety.rodzaje_biletow.selectedIndex].IloscMiejsc;
  document.bilety.cena.value=cena.toFixed(2)+' PLN';
}
function SprawdzOpcjeBilety()
{
  r_opcji=document.bilety.opcje;
  if (r_opcji && r_opcji.selectedIndex<0)
  {
    alert(Bledy[7]);
    return false;
  }
  r_biletow=document.bilety.rodzaje_biletow;
  if (r_biletow && r_biletow.selectedIndex<0)
  {
    alert(Bledy[8]);
    return false;
  }
  return true;
}
function SprawdzDane()
{
  var TestEmail = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var Wynik = document.bilety.email.value.match(TestEmail);
  if (document.bilety.imie.value=='')
  {
    alert(Bledy[1]);
    return false;
  }
  if (document.bilety.nazwisko.value=='')
  {
    alert(Bledy[2]);
    return false;
  }
  if (Wynik == null)
  {
    alert(Bledy[6]);
    return false;
  }
  radio=document.bilety.kanal;
  var checked=false;
  for (i=0;i<radio.length;i++)
    if (radio[i].checked)
      checked=true;
  if (!checked)
  {
    alert(Bledy[10]);
    return false;
  }
  if (SprawdzOpcjeBilety())
    return SprawdzPlatnosc();
  else
    return false;
}
function SprawdzEmail(AdresEmail,Temat,Tresc)
{
var TestEmail = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
var TestWWW="http://";
var Wynik = AdresEmail.match(TestEmail);
var Wynik1=Tresc.match(TestEmail);
var Wynik2=Tresc.search(TestWWW);
if (Wynik == null)
{
  alert(Bledy[6]);
  return false;
}
if (Temat=='')
{
  alert(Bledy[4]);
  return false;
}
if (Tresc=='' || Wynik1!=null || Wynik2>=0)
{
  alert(Bledy[5]);
  return false;
}
  return true;
}
function SprawdzPlatnosc()
{
  var czas="";
  var id_terminu;
  for (var i=1;i<50;i++)
  {
    termin=document.getElementById("terminy"+i)
    if (termin)
    {
      id_terminu=termin[termin.selectedIndex].value;
      for (var k=0; k < Terminy.length; k++)
      {
        if (Terminy[k].Id==id_terminu && (czas=="" || czas>Terminy[k].Data))
          czas=Terminy[k].Data;
      }
    }
  }
  if (document.bilety)
  {
    radio=document.bilety.kanal;
    if (radio)
    {
      for (i=0;i<radio.length;i++)
      {
        if (radio[i].checked==true)
        {
          if (Platnosci[radio[i].value].Czas>czas)
          {
             alert(Platnosci[radio[i].value].Nazwa+" "+Bledy[9]+" "+Platnosci[radio[i].value].Czas);
             return false;
          }
          else
            return true;
        }
      }
    }
  }
}
