function przezuc_dane(z, dest)
{
	 document.getElementById(dest).value=document.getElementById(z).value;
}

function checkPOLEout(a)
{
         var y=document.getElementById(a).value;
         if(y=='')
         {
          document.getElementById(a).style.background='#f43333';
          return false;
         }
         else
	 {
          document.getElementById(a).style.background='#ffffff';
          return true;
         }
}

function checkPOLEradio( _prefix, tekst )
{
         var checked = false;
         var i = 1;
         var ctrl = document.getElementById( _prefix + i );
         while ( ctrl )
         {
               if ( ctrl.checked )
               {
                  checked = true;
                  break;
               }
               i++;
               ctrl = document.getElementById( _prefix + i );
         }
         if ( !checked )
         {
              alert( tekst );
              return false;
         }
         else return true;
}

function checkPOLEcheck(a, tekst)
{
					var y=document.getElementById(a).checked;
					if(y=='')
					{
						alert(tekst);
						return false;
					}
					else return true;
}

function sprawdz_wybor(a, nazwa)
{
         var wybor=document.getElementById(a).value;
	 if(wybor=="0")
	 {
		alert('Proszę wybrać odpowiednią opcję w polu ' + nazwa);
		return false;
         }
	 else
	 {
		return true;
         }
}

function ValidNIP(a)
{
	var input_data=document.getElementById(a).value;
	if ( !input_data.match( /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/ )
	 && !input_data.match( /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/ )
	 && !input_data.match( /^[0-9]{10}$/ ) )
	{
		alert('Proszę podać poprawny NIP.\nFormat:XXX-XX-XX-XXX');
		document.getElementById(a).style.color='#ff0000';
		return false;
	}
	var my_nums = input_data.replace(/-/g,'');
	var valid_nums = "657234567";
	var sum=0;
	for (var temp=8;temp>=0;temp--)
	sum += (parseInt(valid_nums.charAt(temp)) * parseInt(my_nums.charAt(temp)));
	if ( (sum % 11) == 10 ? false : ((sum % 11) == parseInt(my_nums.charAt(9))) )
	{
		document.getElementById(a).style.color='#000000';
		return true;
	}
	else
	{
		document.getElementById(a).style.color='#ff0000';
		return false;
	}
}

function checkKodPocztowy(a)
{
         var Kod=document.getElementById(a).value;
				
         if(Kod=="") return false;
						
	 regex=/^([0-9]{2}-[0-9]{3})$/;
	 if(!regex.test(Kod))
	 {
		alert('Proszę podać poprawny kod pocztowy.\n(np. 12-345)');
		document.getElementById(a).style.color='#ff0000';
		return false;
	 }
	 else
	 {
		document.getElementById(a).style.color='#000000';
		return true;
	 }
}

function checkTel(a)
{
	var input_data=document.getElementById(a).value;
	if ( !input_data.match( /^[0-9]{1,4}\s*[0-9]{3}\s*[0-9]{2,3}\s*[0-9]{2,3}$/ ) )
	{
		alert('Proszę podać poprawny numer telefonu.\nNp.: 012 234 56 78\nlub: 0 123 456 789');
		document.getElementById(a).style.color='#ff0000';
		return false;
	}
	
	document.getElementById(a).style.color='#000000';
	return true;
}
       function Sprawdz1()
       {
         var pustepola=0;

	 if(!checkPOLEout(document.getElementById("instytucja").id))
	 pustepola+=1;
	
	 if(!checkPOLEout(document.getElementById("nip").id))
	  pustepola+=1;
	 else
	     if(!ValidNIP(document.getElementById("nip").id))
	     return false;
	     
         if(!checkPOLEout(document.getElementById("kod").id))
	  pustepola+=1;
	 else
	     if(!checkKodPocztowy(document.getElementById("kod").id))
	     return false;
	     
         if(!checkPOLEout(document.getElementById("miejscowosc").id))
	 pustepola+=1;
	 
	 if(!checkPOLEout(document.getElementById("ulica").id))
	 pustepola+=1;
	 
	 if(!checkPOLEout(document.getElementById("numer").id))
	 pustepola+=1;
	 
         if(!checkPOLEout(document.getElementById("imie").id))
	 pustepola+=1;
	
	 if(!checkPOLEout(document.getElementById("nazwisko").id))
	 pustepola+=1;
	 
	 if(!checkPOLEradio( "plec", "Proszę wybrać odpowiednią opcję w polu Płeć" ))
	 return false;
	 
	 if(!checkPOLEout(document.getElementById("stanowisko").id))
	 pustepola+=1;
	
	 if(!checkPOLEout(document.getElementById("email").id))
	 pustepola+=1;
	 
	 if(!checkPOLEout(document.getElementById("telefon").id))
	  pustepola+=1;
	 else
	     if(!checkTel(document.getElementById("telefon").id))
	     return false;
	     
         if(document.getElementById("fax").value != "")
	     if(!checkTel(document.getElementById("fax").id))
	     return false;

	 if(!sprawdz_wybor(document.getElementById("szkolenie_lista_id").id, 'Wybrane szkolenie'))
	 return false;
	 
	 if(!checkPOLEcheck(document.getElementById("osw").id, "Oświadczenie o zapoznaniu się z regulaminem jest obowiązkowe do zaznaczenia"))
	 return false;
	
	 if(pustepola)
	 {
          alert("Proszę uzupełnić wszystkie wymagane pola.");
          return false;
         }
       }
