// zamienia sekwencje \n => \r\n. Do mozilli glownie bo ona w javascriptowych length z textarea zwraca pojedyncze \n a wysyla \r\n!
function fixN2RN(str)
{
  return str.replace(/\r?\n/g,"\r\n");
}

function newwindow(url)
{
  window.open(url,'','scrollbars=yes, location=no, status=no, width=630, height=500, resizable');
}


function newhint(url)
{
  window.open(url,'','scrollbars=yes, location=no, status=no, width=450, height=300, resizable,menubar=no');
}


function radios_enable(formname, name, enable)
{
  radios=window.document.getElementById(formname).elements[name];
  if(radios)
    for(i=0; i<radios.length; i++)
    {
      radios[i].disabled = !enable;
      if(!enable)
        radios[i].checked = false;
    };
}

function radios_get_value(form, name)
{
  radios=form.elements[name];
  if(radios)
    for(i=0; i<radios.length; i++)
    {
      if(radios[i].checked)
        return radios[i].value;
    };
  return null;
}

// dla HTMLCollection (radios, checkboxy) zwraca liczbe zaznaczonych elementow
function getnumcheck(coll)
{
  var len=coll.length;
  var ncheck=0;
  for(var i=0; i<len; i++)
    if(coll.item(i).checked)
      ncheck++;
  return ncheck;
}

// coll - kolekcja, robimy nadajac wszystkim checkboxom ta samo name, np. "idp[]"
//        a potem pobieramy przez document.getElementsByName('idp[]')
function set_check(coll, checked)
{
  var len=coll.length;
  for(var i=0; i<len; i++)
    coll.item(i).checked=checked;
}

function check_pesel(pesel)
{
  wagi = new Array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);

  if(!pesel.match(new RegExp('^[0-9]{11}$')))
    return 0;
  var n=0;
  for(var i=0; i<10; i++)
    n+=pesel.substr(i, 1) * wagi[i];
  n=10-(n%10);
  if(n==10) n=0;
  if(pesel.substr(10, 1) != n)
    return 0;
  return 1;
}

function checkdate_rmd(ddd)
{
  var m=ddd.match(/^([0-9]{4})[\-\.\/]([0-9]{2})[\-\.\/]([0-9]{2})$/);
  if(m)
    return m[2]>0 && m[2]<13 && m[1]>0 && m[1]<10000 && m[3]>0 && m[3]<=(new Date(m[1], m[2], 0)).getDate();
  else
    return false;
}

function checkdate_dmr(ddd)
{
  var m=ddd.match(/^([0-9]{2})[\-\.\/]([0-9]{2})[\-\.\/]([0-9]{4})$/);
  if(m)
    return m[2]>0 && m[2]<13 && m[3]>0 && m[3]<10000 && m[1]>0 && m[1]<=(new Date(m[3], m[2], 0)).getDate();
  else
    return false;
}

function checkemail(s)
{
  return s.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
}

function xround(v)
{
  ZVAL=0.005001;

  if(v>=0)
    return Math.floor(100.0*(v+ZVAL))/100;
  else
    return Math.ceil(100.0*(v-ZVAL))/100;
}

function waluta(v)
{
  return v.toFixed(2).replace(/\./g, ',');
}

function unify_liczba(v)
{
  return parseFloat(v.replace(/,/g, '.'));
}

function check_liczba(v)
{
  return v.match(new RegExp('^\-{0,1}[0-9]+([\.,][0-9]*){0,1}$'));
}

// te wersje pod IE wymagaja zeby w dokumencie bylo <body>!
function getWindowSize()
{
  var width=0, height=0;
  if(typeof(window.innerWidth)=='number')
  {
    //Non-IE
    width=window.innerWidth;
    height=window.innerHeight;
  }else
  if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    //IE 6+ in 'standards compliant mode'
    width=document.documentElement.clientWidth;
    height=document.documentElement.clientHeight;
  }else
  if(document.body && ( document.body.clientWidth || document.body.clientHeight))
  {
    //IE 4 compatible
    width=document.body.clientWidth;
    height=document.body.clientHeight;
  };
  return new function() { this.width=width; this.height=height; }
}

// zamienia '&amp;' -> '&' - to takie rozwiazanie problemu w3c validatora ktory wymaga &amp; a ktorych nie chce javascript
function unamp(s)
{
  return s.replace('&amp;', '\x26');
}

function clear_form(form)
{
  for(var i=0; i<form.elements.length; i++)
    if({"hidden":1, "submit":1, "button":1}[form.elements[i].type]!=1 && typeof(form.elements[i].value)!='undefined')
      form.elements[i].value='';
}

var zoom=null;

function zoom_image(el, center2window)
{
  unzoom_image(1);

  var render_url=el.id;
  $('#zoom_img').bind('load.zoom', function()
    {
      $(this).unbind('load.zoom');

//      alert($(window).scrollLeft()+', '+$(window).scrollTop()+', '+$(window).width()+', '+$(window).height());
      if(center2window)
      {
        $('#zoom').css('position', 'fixed');
        var pos={ left:0, top:0 };
        var elw=$(window).width();
        var elh=$(window).height();
      }else
      {
        $('#zoom').css('position', 'absolute');
        var pos=$(el).position();
        var elw=$(el).width();
        var elh=$(el).height();
      };

      var zw=$('#zoom').width();
      var zh=$('#zoom').height();
      var dw=(elw-zw)/2, dh=(elh-zh)/2;
      var zx=pos.left+dw, zy=pos.top+dh;

      // ponizej uwzgledniamy zeby nie wychodzilo za body. zeby dobrze dzialalo trzeba jeszcze zrobic:
      // $(function()
      //   {
      //     $('#zoom_img')
      //       .css('max-height', ($('body').height()-200)+'px')
      //       .css('max-width', ($('body').width()-200)+'px')
      //       .mouseout(function() { unzoom_image(1); });
      //   });
      var bw=$('body').width(), bh=$('body').height()
      if(zy<10) zy=10;
      if(zx<10) zx=10;
      if(zy+zh>bh) zy=bh-zh-10;
      if(zx+zw>bw) zx=bw-zw-10;

      $('#zzz').css('display', 'block');

      $('#zoom').css('left', zx).css('top', zy);

      zoom=new PopupElement('zoom', null, function()
        {
          $('#zzz').css('display', 'none');
          $('#zoom_img').attr('src', 'img/white.gif'); });
        }).attr('src', render_url);
}

function unzoom_image(instant)
{
  if(zoom)
    zoom.Hide(instant);
  zoom=null;
}

function popup_form_show(dest, maxs, param)
{
  if(typeof(param)!='undefined')
    dest=dest+"?param="+escape(param);
  $('#zzz').css('display', 'block');
  $('#popup_div_iframe').bind('load.src', function()
    {
      $(this).unbind('load.src');

      var pos={ left:0, top:0 };
      var elw=$(window).width();
      var elh=$(window).height();

      if(typeof(maxs)==='object')
      {
        maxh=maxs.maxh;
        maxw=maxs.maxw;
      }else
      {
        maxw=null;
        maxh=maxs;  // dla kompatybilnosci
      };
/*  z maxw to na razie nie dziala
      if(maxw===null || typeof(maxw)=='undefined')
        maxw=$(this).contents().find('html').width();
      else
        maxw=710;
        
   robimy jak nizej dla zachowania kompatybilnosci
*/
      if(maxw===null || typeof(maxw)=='undefined')
        maxw=710;
      $('#popup_div').width( elw-20<maxw ? elw-20 : maxw )
      var zw=$('#popup_div').width();
      
      if(maxh===null || typeof(maxh)=='undefined')
        maxh=$(this).contents().find('html').height();
      $('#popup_div').height( elh-20<maxh ? elh-20 : maxh )
      var zh=$('#popup_div').height();

      var dw=(elw-zw)/2, dh=(elh-zh)/2;
      var zx=pos.left+dw, zy=pos.top+dh;
      $('#popup_div').css('left', zx).css('top', zy);
      $('#popup_div').css('visibility', 'visible');
//      $('body').css('overflow', 'hidden');
    }).attr('src', dest);
}

function popup_form_hide()
{
  $('#popup_div').css('visibility', 'hidden');
  $('#zzz').css('display', 'none');
  $('#popup_div_iframe').attr('src', '');
//  $('body').css('overflow', 'auto');
}

