/*d-direction przelaczania 0-forw*/
/*n-idpar+no*/
/*co-count na stronie*/
/*idsp-id spana ktry zawiera numerki*/
/*no-ilosc wsich elementow id*/
var rdyld = false;
var s = ',/;\'[]<>:"{}\\';
var d = '.';
var a = '@';

function so() {
  var c, t, i, l, k, m, n, o, p, r, z, j;

  t = document.getElementById("ife").value;
  if (t != null) {
    l = t.length;

    if (l > 0) {
      if (l > 4) {
        z = s.length;
        i = 0;
        p = k = m = n = o = 0;
        r = ' ';
        while (i < l) {
          c = t.charAt(i);
          if (c == a) {
            if (r == d) i = l + 1;
            else m++;
          }
          else if (c == d) {
            if (r == a) i = l + 1;
            else if (m > 0) {
              if (r == d) i = l + 1;
              else p = 1;
            }
          }
          else {
            if (m == 0) k++;
            else if (p == 1) o++;
            else n++;
          }
          for (j = 0; j < z; j++) if (c == s.charAt(j)) i = l + 1;
          if (m > 1) i = l + 1;
          else {
            i++;
            r = c;
          }
        }
        if ((i == l) && (r != d)) {
          if ((k > 0) && (n > 0) && (o > 1)) {
            document.getElementById("ifb").style.visibility = 'hidden';
            document.getElementById("iff").submit();
            document.getElementById("ife").disabled = true;
            return;
          }
        }
      }
      alert('niepoprawny email!');
    }
  }
}

function sw(d, n, co, idsp, no) {
  var x, y, z; // numer pierwszego, ilosc, numer ostatniego
  var i, j, pn, mp; // i, j, act page number, all pages
  var nmsp, skp; // span object

  if (!rdyld) return;

  i = 1;
  x = -1;
  z = 0;
  y = 0;
  while (i <= no) {
    if ((e = (document.getElementById((n+i)))) != null) {
      if (x < 0) x = i;
      if (z < i) z = i;
      y++;
    }
    i++;
  }

  if ((nmsp = (document.getElementById(idsp))) != null) {
    if (x < 0) {
      nmsp.innerHTML = '0/0';
      return;
    }

    mp = (''+(Math.ceil((y / co))));
    pn = xtrv(nmsp.innerHTML);
    if (d == 0) pn++;
    else if (d == 1) pn--;
    else pn = 1;
    if (pn > mp) pn = 1;
    else if (pn < 1) pn = mp;

    skp = (co * (pn-1));
    i = x;
    while (i <= z) {
      if ((e = (document.getElementById((n+i)))) != null) {
        if ((skp > 0) || (skp <= (-(co)))) {
          e.className = 'd0';
        }
        else {
          e.className = 'd1';
        }
        skp--;
      }
      i++;
    }
    nmsp.innerHTML = (pn+'/'+mp);
  }
}

// external links
function extL() {
  var anchors;
  var anchor;
  var tgt;
  var i, l;

  if (typeof document != 'undefined') {
    if (typeof document.getElementsByTagName != 'undefined') {
      anchors = document.getElementsByTagName('a');
      if (anchors) {
        if (typeof anchors.length != 'undefined') {
          l = anchors.length;
          for (i=0; i<l; i++) {
            anchor = anchors[i];
            if (typeof anchor.getAttribute != 'undefined') {
              if (anchor.getAttribute('href')) {
                if (anchor.getAttribute('rel')) {
                  tgt = anchor.getAttribute('rel');
                  if (tgt == '_blank') anchor.target = tgt;
                  else if (tgt == 'external') anchor.target = '_blank';
                  else if (tgt == '_self') anchor.target = tgt;
                  else if (tgt == '_top') anchor.target = tgt;
                }
              }
            }
          }
        }
      }
    }
  }
}

// extract value
function xtrv(v) {
  var t, c0, c9;

  c0 = (('0').charCodeAt(0));
  c9 = (('9').charCodeAt(0));
  if (v.indexOf('/') >= 0) t = v.substr(0, v.indexOf('/'));
  while ((t.charCodeAt(0) < c0) || (t.charCodeAt(0) > c9)) t = t.substr(1, (t.length - 1));
  while ((t.charCodeAt((t.length - 1)) < c0) || (t.charCodeAt((t.length - 1)) > c9)) t = t.substr(0, (t.length - 1));
  return parseInt(t);
}

function flt(f, n, co, idsp, no) {
  var v;
  var pel, i, j, chld, chi, ind;

  if (!rdyld) return;

  //alert('f: '+f+', n: '+n+', co: '+co+', idsp: '+idsp+', no: '+no);
  v = parseInt(f);

  if ((pel = document.getElementById('parent0')) != null) {
    if ((chld = pel.childNodes) != null) {
      j = chld.length;
      for (i = 0; i < j; i++) {
        chi = chld[i];
        ind = chi.id.indexOf('akt');
        if (ind < 0) ind = chi.id.indexOf('act');
        if (ind >= 0) {
          if ((chi.name) == 'undefined') return;
          if ((v == 0) || ((v > 0) && ((chi.name) == (''+v))) || ((v < 0) && ((chi.name) == (''+v)))) {
            //alert(chi.name);
            chi.id = ('akt' + chi.id.substr((ind+3), (chi.id.length - (ind+3))));
            chi.className = 'd1';
          }
          else {
            chi.id = ('act' + chi.id.substr((ind+3), (chi.id.length - (ind+3))));
            chi.className = 'd0';
          }
        }
      }
    }
  }


  sw(2, n, co, idsp, no);
}

function trim(s) {
  var rtval;

  rtval = s;
  if (s) {
    if ((s.length) && (rtval.length)) {
      if (rtval.length > 0) {
        while ((rtval.length > 0) && (rtval.charAt(0) == ' ')) rtval = rtval.substring(1, rtval.length);
        while ((rtval.length > 0) && (rtval.charAt((rtval.length-1)) == ' ')) rtval = rtval.substring(0, (rtval.length-1));
      }
    }
  }
  return rtval;
}

//init
function st() {
  var i, j, pel, chld, ind, fl, e, c;

  extL();
  if ((pel = document.getElementById('post_form')) != null) pel.action = window.location;
  if ((pel = document.getElementById('parent0')) != null) {
    if ((chld = pel.childNodes) != null) {
      j = chld.length;
      for (i = 0; i < j; i++) {
        c = chld[i];
        if ((ind = c.id.indexOf('akt')) == 0) {
          if ((fl = c.id.indexOf('_')) > 0) {
            if ((e = (document.getElementById(c.id))) != null) {
              e.name = (c.id.substr((fl+1), (c.id.length - fl - 1)));
              e.id = (c.id.substr(0, fl));
            }
          }
        }
      }
    }
  }
  rdyld = true;
}

/*------------------------------------------------------------------------------------------------*/
/*sekcja WWW :P*/

//menu item mouse over
//n - obiekt
function mh(n) {
  n.src = n.src.substring(0, n.src.lastIndexOf('.')) + 'v' + n.src.substring(n.src.lastIndexOf('.'), n.src.length);
}

//menu item mouse out
//n - obiekt
function mho(n) {
  n.src = n.src.substring(0, (n.src.lastIndexOf('.') - 1)) + n.src.substring(n.src.lastIndexOf('.'), n.src.length);
}

function check_kom() {
  var name = document.getElementById('postname'),
      kom = document.getElementById('postcnt'),
      pst_form, el;

  if ((name != null) && (kom != null)) {
    name.value = trim(name.value);
    kom.value = trim(kom.value);
    if ((name.value.length < 2) && (kom.value.length < 3)) alert('Treść komentarza nie może być krótsza niż 3 znaki, oraz pseudonim musi składać się z co najmniej 2 znaków.');    
    else if (kom.value.length < 3) alert('Treść komentarza nie może być krótsza, niż 3 znaki.');    
    else if (name.value.length < 2) alert('Pseudonim musi składać się z co najmniej 2 znaków."');    
    else {
      if ((pst_form = document.getElementById('post_form')) != null) {
        pst_form.onclick = "return false;";
        if ((el = document.getElementById('postcnt')) != null) {
          if (el.value.length > 10000) el.value = el.value.substr(0, 10000);
          if ((el = document.getElementById('postname')) != null) {
            if (el.value.length > 64) el.value = el.value.substr(0, 64);
            pst_form.submit();
          }
        }
      }
    }
  }
}
