var Email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var Ekod  = /^([0-9]{2})+\-([0-9]{3})+$/;

function nowy_temat_opinia() {
   error = '';
   mejlik = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})/;
   if (document.getElementById('nowy_temat').value=='') error += "Podaj temat...\n";
   if (document.getElementById('nowa_tresc').value.length<10) error += "Treść wiadomości jest zbyt krótka.\n";
   if (document.getElementById('nowy_podpis').value=='') error += "Brak podpisu...\n";
   if (document.getElementById('nowy_temat').value.search(mejlik)>=0 ||
       document.getElementById('nowa_tresc').value.search(mejlik)>=0 ||
       document.getElementById('nowy_podpis').value.search(mejlik)>=0)
      error+= "W Twojej opinii wpisano adres e-mail, prosimy o usunięcie wszelkich danych kontaktowych.\n";

   if (error!='')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function odpowiedz_opinia() {
   error = '';
   mejlik = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})/;
   
   if (document.getElementById('odp_tresc').value.length<10) error += "Treść wiadomości jest zbyt krótka.\n";
   if (document.getElementById('odp_podpis').value=='') error += "Brak podpisu...\n";
   if (document.getElementById('odp_tresc').value.search(mejlik)>=0 ||
       document.getElementById('odp_podpis').value.search(mejlik)>=0)
      error+= "W Twojej opinii wpisano adres e-mail, prosimy o usunięcie wszelkich danych kontaktowych.\n";
   
   if (error!='')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function opinie_fotos(cz) {
   document.getElementById('opf1').style.display = 'none';
   document.getElementById('opf2').style.display = '';
}

function budABC(lit) {
   advAJAX.get({
      url : "ajax/bud_abc.php",
      parameters : { "litera" : lit },
      onInitialization : function(obj) { document.getElementById("bud_abc").innerHTML = 'Ładowanie danych, proszę czekać...'; },
      onSuccess : function(obj) { document.getElementById("bud_abc").innerHTML = obj.responseText; }
   });
}

function przeliczNotes() {
   advAJAX.post({
      url : "ajax/notes_count.php",
      onSuccess : function(obj) { document.getElementById("liczNotes").innerHTML = obj.responseText; }
   })
}

function pokazNotes() {
   advAJAX.post({
      url : "ajax/notes_wysw.php",
      onInitialization : function(obj) { document.getElementById("advNotesInner").innerHTML = 'Ładowanie danych, proszę czekać...'; },
      onSuccess : function(obj) { document.getElementById("advNotesInner").innerHTML = obj.responseText; }
   });
}

function doNotesu(nnr) {

   advAJAX.post({
      url : "ajax/notes_edit.php",
      parameters : { "n_plus" : nnr },
      onInitialization : function(obj) {
         document.getElementById("notes_index").innerHTML = 'Czekaj...';
         document.getElementById('advNotes').style.display = "block";
      },
      onSuccess : function(obj) {
         document.getElementById("notes_index").innerHTML = 'w notesie';
         
         if (document.getElementById("liczNotes").innerHTML=='')
            document.getElementById('advNotesInner').style.display = 'block';
            
         przeliczNotes();
         pokazNotes();
      }
   });
}

function zNotesu(nnr) {
   advAJAX.post({
      url : "ajax/notes_edit.php",
      parameters : { "n_minus" : nnr },
      onSuccess : function(obj) {
         przeliczNotes();
         pokazNotes();
      }
   });
}

function NotesZero()
{
   advAJAX.post({
      url : "ajax/notes_edit.php",
      parameters : { "n_zero" : "1" },
      onSuccess : function(obj) {
         przeliczNotes();
         pokazNotes();
      }
   });
}

function advNotesPok()
{
   nts = document.getElementById('advNotesInner');
   if (nts.style.display == "block")
      nts.style.display = "none";
   else
   {
      nts.style.display = "block";
      pokazNotes()
   }
}

function zmienProjekt(nr)
{
   if (nr != 0) document.getElementById('lista_projektow').submit();
}
function zamowProjekt(nr)
{
   if (nr != 0) document.getElementById('zamow_projekt').submit();
}

function zmienFotke(licz)
{
   licznik = fotos.length-1;
   nr_widoku = nr_widoku+licz;

   if (nr_widoku == 0) nr_widoku = licznik;
   if (nr_widoku > licznik) nr_widoku = 1;
   
   popup_foto = fotos[nr_widoku][1];
   
   document.getElementById('nr_widoku').innerHTML = nr_widoku;
   document.getElementById('obrazek').src = 'gfx/loading.gif';
   document.getElementById('obrazek').src = 'foto/widok/'+fotos[nr_widoku][0];
}

function okienko(lnk, w, h)
{
   userX = Math.floor((screen.width-w)/2);
   userY = Math.floor((screen.height-h)/2);
   
   window.open(lnk,'PProjekt','height='+h+',width='+w+',resizable=no,scrollbars=no,menubar=no,personalbar=no,toolbar=no,status=no,left='+userX+',top='+userY);
}

function okienko_suwak(lnk, w, h)
{
   userX = Math.floor((screen.width-w)/2);
   userY = Math.floor((screen.height-h)/2);

   window.open(lnk,'PProjekt','height='+h+',width='+w+',resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no,left='+userX+',top='+userY);
}

function nowaOpinia()
{
   error = '';
   mejlik = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})/;
   if (document.getElementById('nick').value=='') error += "Podaj swój nick.\n";
   if (document.getElementById('tresc').value.length<10) error += "Treść wiadomości jest zbyt krótka.\n";
   if (document.getElementById('tresc').value.search(mejlik)>=0)
      error+= "W Twojej opinii wpisano adres e-mail, prosimy o usunięcie wszelkich danych kontaktowych.\n";
      
   if (error!='')
   {
      alert(error);
      return false;
   }
   else
      return true;
}

function number_format (a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

function zmienCene()
{
   netto = wcena[document.getElementById('wersja').value];
   cena = netto*vat;
   if (document.getElementById('dokumenty').value == 2) cena = cena*2;
   
   if (isNaN(cena))
      document.getElementById('koszta').innerHTML = 'najpierw wybierz wersje';
   else
   {
      if (document.getElementById('dokumenty').value == 2)
         document.getElementById('ccpr').value = netto*2;
      else
         document.getElementById('ccpr').value = netto;
         
      document.getElementById('koszta').innerHTML = number_format(cena,2,',',' ')+' zł brutto';
   }
}

function daneWysylka()
{
   if (document.getElementById('dane2').checked)
      document.getElementById('wysylka').style.display='none';
   else
      document.getElementById('wysylka').style.display='';
}

function sprawdzFormularz()
{
   error = '';
   kod = document.getElementById('z_kod1').value+document.getElementById('z_kod2').value+'-'+document.getElementById('z_kod3').value+document.getElementById('z_kod4').value+document.getElementById('z_kod5').value;
   
   if (document.getElementById('imie').value.length<2) error += "Podaj swoje imię...\n";
   if (document.getElementById('nazw').value.length<2) error += "Podaj nazwisko...\n";
   if (!Ekod.test(kod)) error += "Podaj prawidłowy kod pocztowy...\n";
   if (document.getElementById('miasto').value.length<2) error += "Podaj miejscowość...\n";
   if (document.getElementById('ulica').value.length<3) error += "Podaj ulicę i nr domu...\n";
   if (document.getElementById('wojewodztwo').value==0) error += "Wybierz województwo...\n";
   if (document.getElementById('tel').value.length<9) error += "Podaj nr telefonu wraz z nr kierunkowym i/lub komórkę...\n";

   if (document.getElementById('wersja').value==0)
   {
      error = "Nie wybrano wersji projektu...\n";
      document.getElementById('wersja').focus();
   }

   if (error!='')
   {
      alert(error);
      return false;
   }
   else
   {
      document.getElementById('btn').value = 'proszę czekać...';
      document.getElementById('btn').disabled = 'true';
      return true;
   }
}

function next_kp(nzw1,nzw2)
{
   if (document.getElementById(nzw1).value != parseInt(document.getElementById(nzw1).value))
   {
      document.getElementById(nzw1).value = '';
      nzw2 = '';
   }
   if (nzw2!='') document.getElementById(nzw2).focus();
}

function ozm(nr)
{
   var rozw = document.getElementById('f'+nr);
   var lnk = document.getElementById('af'+nr);
   
   if (rozw.style.display == 'block') rozw.style.display = 'none'; else rozw.style.display = 'block';

   return false;
}

function ozn(nr)
{
   var rozw = document.getElementById('s'+nr);
   if (rozw.style.display == 'block') rozw.style.display = 'none'; else rozw.style.display = 'block';

   return false;
}

function zapytaj()
{
   error = '';
   
   if (document.getElementById('tel').value.length<7 && !Email.test(document.getElementById('emajl').value))
      error = "Podaj telefon lub adres e-mail.\n";
   if (document.getElementById('pytanie').value.length<10)
      error += 'Prosimy zadać konkretne pytanie :)';
      
   if (error!='')
   {
      alert(error);
      return false;
   }
   else
   {
      document.getElementById('btn').value = 'przesyłanie formularza...';
      document.getElementById('btn').disabled = 'true';
      return true;
   }
}

function pokaz_form_odp()
{
   document.getElementById('odp_form').style.display='block';
}

function pok_reg_for()
{
   rgl = document.getElementById('regFor');
   if (rgl.style.display == "block")
      rgl.style.display = "none";
   else
   {
      rgl.style.display = "block";
   }
}
function pok_filtr_info()
{
   rgl = document.getElementById('filtr_opis');
   if (rgl.style.display == "block")
      rgl.style.display = "none";
   else
   {
      rgl.style.display = "block";
   }
}

function otworz_spacer(prefix) {
   var data=new Date;
   var sekundy=data.getTime();
   window.open("wirtualne/"+prefix+"/index_"+prefix+".html", "WirtualnySpacer", "height=530,width=1015,status=no,toolbar=no,menubar=no,location=no, resizable=no");
}

function wyprc(nur)
{
   advAJAX.post({
      url : "ajax/wypr.php",
      parameters : { "id" : nur },
      onSuccess : function(obj) { document.getElementById("wyprc").innerHTML = obj.responseText; }
   })
}
