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

