// podmieniamy Object.toString zeby wypisywal nazwy pol i ich wartosci
Object.prototype.toString=function()
{
  var s="";
  for(var i in this)
    s+=i+': '+this[i]+"\n";
  return s;
};

// 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='';
}

function chk_dev_serial(s)
{
  var m=s.match(new RegExp('^[a-fA-F0-9]{8}[bB]0([0-2][0-9]{3})$'));
  if(!m)
    return false;
  var ss=m[1];
  var l=0;
  while(l<ss.length && ss[l]=='0')
    l++;
  ss=ss.substring(l, ss.length);
  var r=parseInt(ss);
  if(r<1 || r>2000)
    return false;
  return true;
}

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');
}



