// main.js : główny skrypt JS (VEGA Studio by Tomasz Cynacewicz)

function browser() {
  var v = navigator.userAgent.toLowerCase();
  
  if (v.indexOf('firefox')!= -1)
    return 'ff';
  else if (v.indexOf('opera')!= -1)
    return 'op';
  else if (v.indexOf('msie')!= -1)
    return 'ie';
  else if (v.indexOf('chrome')!= -1)
    return 'ch';
  else if (v.indexOf('safari')!= -1)
    return 'sf';
  else
    return 'ff';
}

function change_opacity(id, oo) {
  var elem = document.getElementById(id);
  elem.style.color=oo?"#A3050A":"#ffffff";
  
  return;
  
  if (browser() == "ie")
      elem.style.filter=oo?"alpha(opacity=85)":"alpha(opacity=75)";
  else
    elem.style.opacity=oo?"0.85":"0.75";
}

function validate_contact_form(el) {
  if (el.imie.value.length < 5 || el.imie.value.length > 100) {
    alert("Imię lub nazwisko powinno zawierać od 5 do 100 znaków.");
    return false;
  } else if (el.com.value.length < 3 || el.com.value.length > 100) {
    alert("Nazwa firmy powinna zawierać od 3 do 100 znaków.");
    return false;
  } else if (el.mail.value.length < 7 || el.mail.value.length > 50) {
    alert("Adres email powinien zawierać od 7 do 50 znaków.");
    return false;
  } else if (el.tel.value.length < 7 || el.tel.value.length > 15) {
    alert("Nr telefonu powinien zawierać od 7 do 15 znaków.");
    return false;
  } else if (el.content.value.length < 20 || el.content.value.length > 500) {
    alert("Treść wiadomości powinna zawierać od 20 do 500 znaków.");
    return false;
  } else if (el.pcode.value.length != 6 || isNaN(el.pcode.value)) {
    alert("Przepisz prawidłowo kod z obrazka.");
    return false;
  } else if (!el.acc.checked) {
    alert("Zgoda na przetwarzanie danych osobowych jest niezbędna.");
    return false;
  }

  return true;
}

function validate_login_form(el) {
  if (el.login.value == null || el.login.value == "" || el.login.value.length < 3) {
    alert("Wpisz poprawnie login.");
    return false;
  } else if (el.password.value == null || el.password.value == "" || el.password.value.length < 3) {
    alert("Wpisz poprawnie hasło.");
    return false;
  }

  return true;
}

function validate_email_form(el) {
  if (!check_email(el.email.value)) {
    alert("Wpisz poprawnie adres email administratora.");
    return false;
  }

  return true;
}
 
function check_email(str) {
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (!filter.test(str))
    return false;

  return true;
}

function validate_addnews_form(el) {
  if (el.title.value.length < 5 || el.title.value.length > 100) {
    alert("Tytuł newsa powinien zawierać od 10 do 100 znaków.");
    return false;
  } else if (el.content.value.length < 3 || el.content.value.length > 10000) {
    alert("Treść newsa powinna zawierać od 10 do 10000 znaków.");
    return false;
  }
  return true;
}

function validate_adddelnews_form(el) {
  if (el.all.checked)
    if (confirm("Uwaga! Napewno chcesz usunąć wszystkie newsy?") == false)
      return;
      
    for(var i = 0; i < el.length; i++) {
      var e = el[i];
      if (e.name == "id[]" && e.checked == true) {
        el.submit();
        return;
      }
    }

  alert("Nie zaznaczono żadnego newsa.");
}

function validate_rm_form(el) {
  if (el.all.checked && el.mailing.value == 0)
    if (confirm("Uwaga! Napewno chcesz usunąć wszystkich klientów?") == false)
      return;

  for(var i = 0; i< el.length; i++) {
    var e = el[i];
    if (e.name == "id[]" && e.checked == true) {
      el.submit();
      return;
    }
  }

  alert("Nie zaznaczono żadnego klienta.");
}

function check_all(el) {
  for(var i = 0; i< el.length; i++) {
    var e = el[i];
    if (e.name == "id[]")
      e.checked = el.all.checked;
  }
}

function validate_message_form(el) {
  if (el.subject.value.length < 10 || el.subject.value.length > 100) {
    alert("Temat wiadomości powinien zawierać od 10 do 100 znaków.");
    return false;
  } else if (el.message.value.length < 10 || el.message.value.length > 500) {
    alert("Treść wiadomości powinna zawierać od 10 do 500 znaków.");
    return false;
  }
  
  return true;
}

function validate_chpass_form(el) {
  var regex1 = /\w{5,30}/g;
  var regex2 = /\w{5,30}/g;
  
  if (el.old_password.value == "" || el.old_password.value == null || el.old_password.value.length < 5 || el.old_password.value.length > 30) {
    alert("Wpisz poprawnie stare hasło. Od 5 do 30 znaków, bez polskich liter.");
    return false;
  } else if (el.new_password.value == "" || el.new_password.value == null || el.new_password.value.length < 5 || el.new_password.value.length > 30) {
    alert("Wpisz poprawnie nowe hasło. Od 5 do 30 znaków, bez polskich liter.");
    return false;
  } else if (el.re_password.value == "" || el.re_password.value == null || el.re_password.value.length < 5 || el.re_password.value.length > 30) {
    alert("Powtórz poprawnie nowe hasło.");
    return false;
  } else if (el.new_password.value != el.re_password.value) {
    alert("Hasła nie są takie same.");
    return false;
  } else if (!regex1.test(el.new_password.value)) {
    alert("Wpisz poprawnie nowe hasło. Od 5 do 30 znaków, bez polskich liter.");
    return false;
  } else if (!regex2.test(el.re_password.value)) {
    alert("Wpisz poprawnie nowe hasło. Od 5 do 30 znaków, bez polskich liter.");
    return false;
  }

  return true;
}

function pre_load() {
  var img200 = new Image();
  var img201 = new Image();
  var img202 = new Image();
  var img203 = new Image();
  var img204 = new Image();
  var img205 = new Image();
  img200.src = "img/stickers/ostatnie_hov.png";
  img201.src = "img/stickers/wydawnictwo_hov.png";
  img202.src = "img/stickers/reklama_hov.png";
  img203.src = "img/stickers/poligrafia_hov.png";
  img204.src = "img/stickers/internet_hov.png";
  img205.src = "img/stickers/inne_hov.png";
}

function myFlash() {
  var img100 = new Image();
  var img101 = new Image();
  var img102 = new Image();
  var img103 = new Image();
  var img104 = new Image();
  var img105 = new Image();
  var img106 = new Image();
  var img107 = new Image();
  var img108 = new Image();
  var img109 = new Image();
  var img110 = new Image();
  img100.src = "img/baner.jpg";
  img101.src = "img/hair1.jpg";
  img102.src = "img/hair2.jpg";
  img103.src = "img/i_b.png";
  img104.src = "img/o_b.png";
  img105.src = "img/p_b.png";
  img106.src = "img/a_b.png";
  img107.src = "img/c_b.png";
  img108.src = "img/n_b.png";
  img109.src = "img/opacity_w.png";
  img110.src = "img/cl_b.png";

  var ax;
  var regex = /.*(\s11\.){1}.*/g;
  var EI = "flash/expressInstall.swf";
  var isNew = false;
  
  try {
    ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
    isFlash = true;
  }
  catch (e) {
    isFlash = false;
  }

  if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin != null)
    isFlash = true;

  /*
  if (isFlash) {
    delete ax;
    try {
      ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
    }
    catch (e) {
      isNew = false;
    }
    if (ax)
      isNew = true;
      
    if (browser() != "ie" && regex.test(navigator.plugins["Shockwave Flash"].description))
      isNew = true;
  }
  */
  
  isNew = true; // wykrywanie wersji
  
  /*  
  if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) { 
    document.writeln('<script language="VBscript">\n Dim result = false \n on error resume next \n result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.1"))</scr' + 'ipt>');
      if (result)
        isFlash = true;
  }
  */
  if (isFlash && !Baner) {
      document.writeln('<object type="application/x-shockwave-flash" data="'+(isNew?superMegaProdukcjaArtura:EI)+'" width="980" height="283" id="header" align="top">');
      //document.writeln('<param name="allowScriptAccess" value="sameDomain" />');
      //document.writeln('<param name="allowFullScreen" value="false" />');
      //document.writeln('<param name="menu" value="false" />');
      //document.writeln('<param name="salign" value="lt" />');
      //document.writeln('<embed src="'+(isNew?superMegaProdukcjaArtura:"flash/expressInstall.swf")+'" loop="false" menu="false" quality="high" salign="lt" wmode="gpu" bgcolor="#000000" width="980" height="283" name="intro 8 b szersze" align="top" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />');  
      document.writeln('<param name="movie" value="'+(isNew?superMegaProdukcjaArtura:EI)+'" />');
      document.writeln('<param name="loop" value="false" />');
      document.writeln('<param name="quality" value="high" />');
      document.writeln('<param name="wmode" value="gpu" />');
      document.writeln('<param name="bgcolor" value="#000000" />');
      document.writeln('<param name="scale" value="exactfit" />');
      document.writeln('<embed src="'+(isNew?superMegaProdukcjaArtura:EI)+'" loop="false" menu="false" quality="high" scale="exactfit" bgcolor="#000000" width="980" height="283" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="https://www.adobe.com/go/getflashplayer" />');
      document.writeln('</object>');
  } else {
    document.writeln('<div class="baner_main" style="visibility: visible;"></div>');
  }
}

function expressInstallCallback() {
  change_baner();
}

var swfobject = new Object();
swfobject.expressInstallCallback = expressInstallCallback;

function change_baner() {
  var div = document.createElement('div');
  div.className="baner_main";
  div.style.visibility="visible";
  var elem = document.getElementById("header");
  var parent = elem.parentNode;
  elem.parentNode.removeChild(elem);
  parent.appendChild(div);
  var hair1 = document.getElementById('her_hair1');
  var hair2 = document.getElementById('her_hair2');
  hair1.style.visibility="visible";
  hair2.style.visibility="visible";
}

function about(w, h) {
  var ab=window.open("", "", "HEIGHT="+h+",WIDTH="+w+",menubar=no,status=no,toolbar=no,locationbar=no,personalbar=no,scrollbar=no,resizable=no");
  var content = "<html><head><title>O nas</title></head><body style=\"background-color: #eaeaea; border: 1px solid #5e5e5e; margin: 0; padding: 3px;\">";
  content += "<img src=\"pic/about/fm_big.jpg\" style=\"margin: 0; padding: 0; cursor: pointer;\" onclick=\"window.close()\" /></body></html>";
  ab.document.write(content);
  ab.document.close();
}

function show_big(el) {
  var imgB = "pic/big/"+unescape(el.name.replace(/\-/g, '%'));
  var pr = window.open(imgB, "obrazek", "status=0,toolbar=0, width=920, height=400");
}

function reload(dir, el) {
    
  var big = document.getElementById("big_img");
  var img = new Image();

  img.src = unescape(el.name.replace(/\_s\./g, '_b.'));
  img.onload = function() { big.parentNode.style.borderColor="#a0a0a0"; big.parentNode.style.backgroundColor="#eaeaea"; big.src = this.src; }
  big.style.visibility="hidden";
  big.parentNode.style.backgroundColor="white";
  big.parentNode.style.borderColor="white";  
  big.src = "img/loading.gif";
  big.style.visibility="visible";
  delete img;
  
  for (var i = 0; i < imgCounter; i++) {
    Images[i]["sel"]=false;
  }
  
  Images[parseInt(el.className)]["sel"]=true;
  preserve_border_color();
}

function oo(el, ov) {
  if (ov) {
    el.parentNode.style.borderColor="#D22517"
  } else {
    if (!Images[parseInt(el.className)].sel)
      el.parentNode.style.borderColor="#a0a0a0";
  }
}

function load_img(el) {
  el.parentNode.style.width = el.width + 2+'px';
  el.parentNode.style.height = el.height + 2+'px';
}

// opacity miniaturek ...a może tak w klasę ??
var speed = 10;
var timer = 10;
var j_b = timer;
var j_f = 0;
var tm_b = null;
var tm_f = null;
var cntFB = 0;
var locked = false;

function mig1(dir) {
  if (imgCounter < 7 && imgCounter != 2 && imgCounter != 4 && imgCounter != 6)
    return;
    
  if (tm_b == null) {
    tm_b = setTimeout("mig1("+dir+")", speed);
    j_b = timer;
    locked = true;
  }

  if (j_b == 0) {
    // zmień miniaturki
    change_mini(dir);
    preserve_border_color();
    clearTimeout(tm_b);
    tm_b = null;
    j_f = 0;
    tm_f = setTimeout("mig2()", speed);
    return;
  } else {
    j_b--;
    tm_b = setTimeout("mig1("+dir+")", speed);
    for (var i = 0; i < limit; i++) {
      if (browser() == "ie")
        document.getElementById("mini_"+i).style.filter="alpha(opacity="+(j_b*timer)+")";
      else
        document.getElementById("mini_"+i).style.opacity=j_b/timer;
    }
  }
}

function mig2() {
  if (j_f == timer) {
    clearTimeout(tm_f);
    tm_f = null;
    j_b = timer;
    locked = false;
    return;
  } else {
    j_f++;
    tm_b = setTimeout("mig2()", speed);
    for (var i = 0; i < limit; i++) {
      if (browser() == "ie")
        document.getElementById("mini_"+i).style.filter="alpha(opacity="+(j_f*timer)+")";
      else
        document.getElementById("mini_"+i).style.opacity=j_f/timer;
    }
  }
}

function change_mini(dir) {
  var elem = null;
  cntFB=dir?'next':'prev';

  for (var i = 0; i < limit; i++) {
    elem = document.getElementById("mini_"+i);
    elem.src= 'mini?file='+Images[Images[parseInt(elem.className)][cntFB]]['filename'];
    elem.name=Images[Images[parseInt(elem.className)][cntFB]]['filename'];
    elem.className=Images[parseInt(elem.className)][cntFB];
  }
}

function preserve_border_color() {
  for (var i = 0; i < limit; i++) {
    el = document.getElementById("mini_"+i);
    if (Images[parseInt(el.className)]["sel"])
      el.parentNode.style.borderColor="#D22517";
    else
      el.parentNode.style.borderColor="#909090";
  }
}

function uncheck(form, el) {
  if(document.getElementById("all").checked) {
    document.getElementById("all").checked = false;
  } else {
    for(var i = 0; i < form.length; i++) {
      var e = form[i];
      if (e.name == "id[]" && e.checked == false)
        return;
    }
    document.getElementById("all").checked = true;
  }
}

function show_help() {
  var ab=window.open("", "", "HEIGHT=480,WIDTH=440,menubar=no,status=no,toolbar=no,locationbar=no,personalbar=no,scrollbar=no,resizable=no");
  var content = "<html><head><title>Pomoc</title></head><body style=\"background-color: #fff; border: 1px solid #5e5e5e; margin: 0; padding: 0; width: 100%; height: 100%;\"><div style=\"width: 100%; height: 100%; overflow: scroll;\">";
  content += "<h3 style=\"background-color: #000; color: #CF0101; padding:0; margin:0; line-height: 46px; border-bottom: 2px solid #5e5e5e;\">&nbsp;&nbsp;&nbsp;Pomoc</h3><ul><li>pogrubianie tekstu w newsach<p>aby pogrubić tekst należy umieścić go w znacznikach [b][/b]<br>przykład: to [b]jest[/b] text<br />wynik: to <strong>jest</strong> text</p></li>";
  content += "<li>podkreślenie tekstu w newsach<p>aby podkreślić fragment tekstu należy umieścić go w znacznikach [u][/u]<br>przykład: to [u]jest[/u] text<br />wynik: to <u>jest</u> text</p></li>";
  content += "<li>pochylony tekst w newsach<p>aby tekst był pochylony należy umieścić go w znacznikach [i][/i]<br>przykład: to [i]jest[/i] text<br />wynik: to <i>jest</i> text</p></li>";
  content += "</ul>";  
  content += "<p style=\"text-align: center;\"><button onclick=\"window.close()\">zamknij</button></p></div></body></html>";
  ab.document.write(content);
  ab.document.close();
}
