var licznik_typ=0;
var xmlhttp1;var xmlhttp2;var xmlhttp3;var xmlhttp4;var xmlhttp5;

function szukaj_nazwa(str,str2,str3)
{

xmlhttp1=GetXmlHttpObject();
if (xmlhttp1==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="../include/szukaj_nazwy_strony.php";
url=url+"?q="+str+"&s="+str2+"&p="+str3;
url=url+"&sid="+Math.random();
xmlhttp1.onreadystatechange=stateChanged1;
xmlhttp1.open("GET",url,true);
xmlhttp1.send(null);



function stateChanged1()
{
if (xmlhttp1.readyState==4)
{
    if(document.getElementById("nadgmina") && xmlhttp1.responseText!=''){
document.getElementById("nadgmina").innerHTML = xmlhttp1.responseText;

            przetwarzaj_dane2();
}
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
}

function szukaj_nazwa2(str,str2)
{

xmlhttp3=GetXmlHttpObject();
if (xmlhttp3==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="../include/szukaj_nazwy_stronyg.php";
url=url+"?q="+str+"&s="+str2;
url=url+"&sid="+Math.random();
xmlhttp3.onreadystatechange=stateChanged3;
xmlhttp3.open("GET",url,true);
xmlhttp3.send(null);



function stateChanged3()
{
if (xmlhttp3.readyState==4)
{
document.getElementById("nadszkola").innerHTML=xmlhttp3.responseText;
przetwarzaj_dane3();

}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
}

function szukaj_nazwa3(str)
{

xmlhttp4=GetXmlHttpObject();
if (xmlhttp4==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="../include/szukaj_nazwy_stronys.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp4.onreadystatechange=stateChanged4;
xmlhttp4.open("GET",url,true);
xmlhttp4.send(null);



function stateChanged4()
{
if (xmlhttp4.readyState==4)
{
if(document.getElementById("nadtypy")){
document.getElementById("nadtypy").innerHTML='<select class="ctr_lista_nazwa" name="typy" size="1" id="typy"><option value="">[-wybierz-]</option></select>';//typy
string = xmlhttp4.responseText;
if(string!=' '){
tablica = string.split("!@#$%^&*()");
document.getElementById("nadtypy").innerHTML=tablica[0];//typy
document.getElementById("miejscowosc").innerHTML=tablica[1];//miasto
document.getElementById("kod").innerHTML=tablica[2];//kod
document.getElementById("adres").innerHTML=tablica[3];//adres
document.getElementById("tel").innerHTML=tablica[4];//telefon
document.getElementById("email").innerHTML=tablica[5];//email
document.getElementById("szk_id").value=tablica[6];//szk_id
}else{
document.getElementById("nadtypy").innerHTML='<select class="ctr_lista_nazwa" name="typy" size="1" id="typy"><option value="">[-wybierz-]</option></select>';//typy
document.getElementById("miejscowosc").innerHTML='';//miasto
document.getElementById("kod").innerHTML='';//kod
document.getElementById("adres").innerHTML='';//adres
document.getElementById("tel").innerHTML='';//telefon
document.getElementById("email").innerHTML='';//email
document.getElementById("szk_id").value='';//szk_id
}
}
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
}

function szukaj_nazwa4(str,str2)
{

xmlhttp5=GetXmlHttpObject();
if (xmlhttp5==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="../include/szukaj_nazwy_stronyp.php";
url=url+"?q="+str+"&p="+str2;
url=url+"&sid="+Math.random();
xmlhttp5.onreadystatechange=stateChanged5;
xmlhttp5.open("GET",url,true);
xmlhttp5.send(null);



function stateChanged5()
{
if (xmlhttp5.readyState==4)
{
if(document.getElementById("nadprz")){
document.getElementById("nadprz").innerHTML=xmlhttp5.responseText;
}
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
}


function usun_typ_ajax(id,nazwa)
{

xmlhttp2=GetXmlHttpObject();
if (xmlhttp2==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="../include/usun_typ.php";
url=url+"?q="+id+"&s="+nazwa;
url=url+"&sid="+Math.random();
xmlhttp2.onreadystatechange=stateChanged2;
xmlhttp2.open("GET",url,true);
xmlhttp2.send(null);



function stateChanged2()
{
if (xmlhttp2.readyState==4)
{
document.getElementById("gmina").innerHTML=xmlhttp1.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
}


function przetwarzaj_dane(id_szk,id_powiat)
{
    var powiat;
    if(document.getElementById('powiat')){
    powiat=document.getElementById('powiat').value;
    document.getElementById('powiat').style.backgroundColor  = "";
    }else{
    powiat=0;
    }
szukaj_nazwa(powiat,id_szk,id_powiat);
}

function przetwarzaj_dane2()
{document.getElementById('gmina').style.backgroundColor  = "";
if(document.getElementById('gmina') && document.getElementById('szk_id')){
szukaj_nazwa2(document.getElementById('gmina').value,document.getElementById('szk_id').value);
}
}
function przetwarzaj_dane3()
{
    document.getElementById('szkola').style.backgroundColor  = "";
szukaj_nazwa3(document.getElementById('szkola').value);
}
function przetwarzaj_dane4(id_przedmiot)
{
document.getElementById('kategoria').style.backgroundColor  = "";
szukaj_nazwa4(document.getElementById('kategoria').value,id_przedmiot);
}

function dodaj_typ(id){
   if(document.getElementById('typy').value!=''){
       
       string="<br>&nbsp;-&nbsp;&nbsp;" + document.getElementById('typy').value +"&nbsp;<span style=\"position: relative; top: 4px; cursor: pointer;\" onclick=\"usun_typ("+id+",'"+document.getElementById('typy').value+"');\"><img alt=\"usun szkole\" src=\"img/b_drop.png\"></span>";
   if(document.getElementById("dodane_typy").innerHTML.indexOf(string, 0)==-1){
//   if(document.getElementById("dodane_typy").innerHTML==""){
//       document.getElementById("dodane_typy").innerHTML="<br /><b>WYBRANE TYPY:</b>"
//   }
document.getElementById('typy').style.background  = "";
   licznik_typ=licznik_typ+1;
   document.getElementById("dodane_typy_input").innerHTML=document.getElementById("dodane_typy_input").innerHTML+"<input type='hidden' value='"+document.getElementById('typy').value+"' name='i["+licznik_typ+"]' />";
   document.getElementById("dodane_typy").innerHTML= document.getElementById("dodane_typy").innerHTML + string;
    }}
}

function usun_typ(id,nazwa){
if(confirm("Czy na pewno chcesz usunąć "+nazwa+" ?")){
string="<br>&nbsp;-&nbsp;&nbsp;" + nazwa +"&nbsp;<span style=\"position: relative; top: 4px; cursor: pointer;\" onclick=\"usun_typ("+id+",'"+nazwa+"');\"><img alt=\"usun szkole\" src=\"img/b_drop.png\"></span>";
od=document.getElementById("dodane_typy").innerHTML.indexOf(string, 0);
dlugosc=string.length;
calosc = document.getElementById("dodane_typy").innerHTML;
s1= calosc.substr(0,od);
s2= calosc.substr(od+dlugosc);
document.getElementById("dodane_typy").innerHTML=s1+s2;


string=nazwa;
od=document.getElementById("dodane_typy_input").innerHTML.indexOf(string, 0);
dlugosc=string.length;
calosc = document.getElementById("dodane_typy_input").innerHTML;
s1= calosc.substr(0,od);
s2= calosc.substr(od+dlugosc);
document.getElementById("dodane_typy_input").innerHTML=s1+s2;


usun_typ_ajax(id,nazwa);
}
}


function waznosc_ogloszenia(){
var d = new Date();
if(document.getElementById("waznosc")){
    d.setDate(d.getDate()+(document.getElementById("waznosc").value*7));
    document.getElementById("waznosc_odp").innerHTML=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear();
}

}

function administrator(){
    var formularz=document.forms['form2'];
    for (var i=0; i < document.form2.admin.length; i++)
   {
   if (document.form2.admin[i].checked)
      {
      var val = document.form2.admin[i].value;
      }
   }

   if (val==0 || val==2){
    document.getElementById("szkola").disabled=true;
    document.getElementById("powiat").disabled=true;
    document.getElementById("gmina").disabled=true;
  }else{
    document.getElementById("szkola").disabled=false;
    document.getElementById("powiat").disabled=false;
    document.getElementById("gmina").disabled=false;
  }
}


function sprawdz_uzytkownicy(){
    var brak_danych=false;
    var formularz=document.forms['form2'];
        if (formularz.login.value == "" ){
    document.getElementById('login').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('login').style.background  = "";
    }
        if (formularz.haslo1.value == ""){
    document.getElementById('haslo1').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('haslo1').style.background  = "";
    }
        if (formularz.haslo2.value == ""){
    document.getElementById('haslo2').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('haslo2').style.background  = "";
    }
        if (formularz.haslo1.value != formularz.haslo2.value){
    document.getElementById('haslo2').style.background  = "#ff6f6f";
    document.getElementById('haslo1').style.background  = "#ff6f6f";
    brak_danych=true;
    }

//            if (formularz.imie.value == ""){
//    document.getElementById('imie').style.background  = "#ff6f6f";
//    brak_danych=true;
//    }else{
//    document.getElementById('imie').style.background  = "";
//    }
//            if (formularz.nazwisko.value == ""){
//    document.getElementById('nazwisko').style.background  = "#ff6f6f";
//    brak_danych=true;
//    }else{
//    document.getElementById('nazwisko').style.background  = "";
//    }
            if (formularz.tel.value == ""){
    document.getElementById('tel').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('tel').style.background  = "";
    }
            if ((formularz.email.value == "") || (formularz.email.value.indexOf("@") == -1) || (formularz.email.value.indexOf(".") == -1)){
    document.getElementById('email').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('email').style.background  = "";
    }
    for (var i=0; i < document.form2.admin.length; i++)
   {
   if (document.form2.admin[i].checked)
      {
      var val = document.form2.admin[i].value;
      }
   }

    if(val==1){
        if (formularz.powiat.value == ""){
    document.getElementById('powiat').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('powiat').style.background  = "";
    }
        if (formularz.gmina.value == ""){
    document.getElementById('gmina').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('gmina').style.background  = "";
    }
        if (formularz.szkola.value == ""){
    document.getElementById('szkola').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('szkola').style.background  = "";
    }
    }else{
    document.getElementById('szkola').style.background  = "";
    document.getElementById('gmina').style.background  = "";
    document.getElementById('powiat').style.background  = "";
    }
    if(brak_danych==false){
    formularz.submit();
    }
}

function sprawdz_ogloszenia(nazwa,admin,pub){
    var brak_danych=false;
    var formularz=document.forms['formularz_ogloszenia'];
   
     if(admin==1 && pub==0 && nazwa == 'Edytowanie') {
     }else{
        if (formularz.wakaty.value == "" || IsNumeric(formularz.wakaty.value)){
    document.getElementById('wakaty').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('wakaty').style.background  = "";
    }
        if (formularz.godziny.value == "" || IsNumeric(formularz.godziny.value)){
    document.getElementById('godziny').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('godziny').style.background  = "";
    }
//        if (formularz.info.value == ""){
//    document.getElementById('info').style.background  = "#ff6f6f";
//    brak_danych=true;
//    }else{
//    document.getElementById('info').style.background  = "";
//    }
        if (formularz.waznosc.value == ""){
    document.getElementById('waznosc').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('waznosc').style.background  = "";
    }

        if (formularz.kategoria.value == ""){
    document.getElementById('kategoria').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('kategoria').style.background  = "";
    }
    
            if (formularz.przedmiott.value == ""){
   document.getElementById('przedmiott').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('przedmiott').style.background  = "";
    }

        if (formularz.typy.value == ""){
    document.getElementById('typy').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('typy').style.background  = "";
    }
     }
    if(nazwa=="Dodawanie" && admin==0){
        if (formularz.szkola.value == ""){
    document.getElementById('szkola').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('szkola').style.background  = "";
    }
        if (formularz.gmina.value == ""){
    document.getElementById('gmina').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('gmina').style.background  = "";
    }
        if (formularz.powiat.value == ""){
    document.getElementById('powiat').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('powiat').style.background  = "";
    }
    }
    if(brak_danych==false){
    formularz.submit();
    }
}

function sprawdz_szkola(){
    var brak_danych=false;
    var formularz=document.forms['formularz_szkola'];
        if (formularz.nazwa.value == ""){
    document.getElementById('nazwa').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('nazwa').style.background  = "";
    }
            if (formularz.gmina.value == ""){
    document.getElementById('gmina').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('gmina').style.background  = "";
    }
        if (formularz.powiat.value == ""){
    document.getElementById('powiat').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('powiat').style.background  = "";
    }
        if (formularz.miejscowosc.value == ""){
    document.getElementById('miejscowosc').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('miejscowosc').style.background  = "";
    }
    
        if (formularz.kod.value == ""){
    document.getElementById('kod').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('kod').style.background  = "";
    }
        if (formularz.adres.value == ""){
    document.getElementById('adres').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('adres').style.background  = "";
    }
        if (formularz.tel.value == ""){
    document.getElementById('tel').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('tel').style.background  = "";
    }
    if ((formularz.email.value == "") || (formularz.email.value.indexOf("@") == -1) || (formularz.email.value.indexOf(".") == -1)){
    document.getElementById('email').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('email').style.background  = "";
    }
        if (document.getElementById('dodane_typy').innerHTML == "" ){
    document.getElementById('typy').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('typy').style.background  = "";
    }

    if(brak_danych==false){
    formularz.submit();
    }
}


function sprawdz_typy(){
    var brak_danych=false;
    var formularz=document.forms['form2'];
        if (formularz.nazwa.value == ""){
    document.getElementById('nazwa').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('nazwa').style.background  = "";
    }

    if(brak_danych==false){
    formularz.submit();
    }
}

function sprawdz_gmina(){
    var brak_danych=false;
    var formularz=document.forms['form2'];
        if (formularz.nazwa.value == ""){
    document.getElementById('nazwa').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('nazwa').style.background  = "";
    }
        if (formularz.powiat.value == ""){
    document.getElementById('powiat').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('powiat').style.background  = "";
    }

    if(brak_danych==false){
    formularz.submit();
    }
}

function sprawdz_przedmiot(){
    var brak_danych=false;
    var formularz=document.forms['form2'];
        if (formularz.nazwa.value == ""){
    document.getElementById('nazwa').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('nazwa').style.background  = "";
    }
        if (formularz.kategoria.value == ""){
    document.getElementById('kategoria').style.background  = "#ff6f6f";
    brak_danych=true;
    }else{
    document.getElementById('kategoria').style.background  = "";
    }

    if(brak_danych==false){
    formularz.submit();
    }
}

function IsNumeric(inputVal) {
     if (isNaN(parseFloat(inputVal))) {
          return true;
     }
     return false
}

function aktywnosc_szkola(){
    document.getElementById('aktywnosc_szkola').innerHTML="<span style=\"color:red\">Jeżeli szkoła będzie nieaktywna wszyscy jej użytkownicy staną się nieaktywni.</span>";
}

function czyszczenie_ogloszenia(){
var formularz=document.forms['form1'];
formularz.data1.value = "";
formularz.data2.value = "";
formularz.slowo.value = "";
document.form1.publikacja[0].selected = "1";
document.form1.aktywnosc[0].selected = "1"

}
