window.onload = Laduj;

function Laduj()
{
   var i;
   var inputy = document.getElementsByTagName("input");
   var selecty = document.getElementsByTagName("select");
   
   document.forms['formularz'].onsubmit = Sprawdz_wypelnienie;
   
   for(i=0; i<inputy.length; i++)
      if(inputy[i].getAttribute("type") == "text")
         if(inputy[i].getAttribute("name") == "pyt[1]")
            inputy[i].onblur = Sprawdz_pyt1;
         else
            inputy[i].onblur = Sprawdz_pyt8;
            
   for(i=0; i<selecty.length; i++)
      selecty[i].onblur = Sprawdz_selecty;
}

function is_int(testowana)
{
   var poprawne = "0123456789";
 
   for(i=0; i<testowana.length; i++)
      if(poprawne.indexOf(testowana.charAt(i)) == -1) 
         return false;

   return true;
}

function Sprawdz_pyt1()
{
   if (window.event && window.event.srcElement)
      var el = window.event.srcElement;
   else
      var el = this;

   if(el.value.length == 0 || !is_int(el.value) || el.value < 6 || el.value > 120)
   {
      el.style.backgroundColor = "#ff7a7a";
      alert("Wiek powinien być liczbą całkowitą zawierającą się w przedziale od 6, do 120.");
   }
   else
      el.style.backgroundColor = "#ffffff";
}

function Sprawdz_pyt8()
{
   if (window.event && window.event.srcElement)
      var el = window.event.srcElement;
   else
      var el = this;

   if(el.value.length == 0 || !is_int(el.value))
   {
      el.style.backgroundColor = "#ff7a7a";
      alert("Odpowiedź na to pytanie powinna być dodatnią liczbą całkowitą.");
   }
   else
      el.style.backgroundColor = "#ffffff";
}

function Sprawdz_selecty()
{
   if (window.event && window.event.srcElement)
      var el = window.event.srcElement;
   else
      var el = this;

   if(!el.value)
   {
      el.style.backgroundColor = "#ff7a7a";
      alert("Nie można zaznaczyć tej opcji.");
   }
   else
      el.style.backgroundColor = "#ffffff";
}

function Sprawdz_wypelnienie()
{
   var i;
   var inputy = document.getElementsByTagName("input");
   var selecty = document.getElementsByTagName("select");
   var error = false;
   
   for(i=0; i<inputy.length; i++)
      if(inputy[i].getAttribute("type") == "text" && !inputy[i].value)
      {
         inputy[i].style.backgroundColor = "#ff7a7a";
         error = true;
      }
   
   for(i=0; i<selecty.length; i++)
      if(!selecty[i].value)
      {
         selecty[i].style.backgroundColor = "#ff7a7a";
         error = true;
      }
      
   if(error)
   {
      alert("Nie wypełniono całej ankiety!");
      return false;
   }
}

