//typ pozycji obiektu
function ObjectPosition(x, y) {
  this.x=x;
  this.y=y;
}

//dodawanie funcji do wywoałania przy body onload
function addOnLoad(f) {
var currentEvent = window.onload==undefined?undefined:window.onload.toString();
  if(currentEvent == undefined) {
    window.onload = new Function("event", f+"();\n");
  } else {
    if(currentEvent.lastIndexOf("}") != -1)
      window.onload = new Function("event", currentEvent.substring(currentEvent.indexOf("{")+1, currentEvent.lastIndexOf("}")-1)+"\n"+f+"();\n");
    else
      window.onload = new Function("event", f+"();\n");
  }
}

//parent tag "tag" obiektu o
function getOuterTag(o, tag) {
  if(o == null || o.parentNode == undefined) return null;
  o = o.parentNode;
  while(o.parentNode != undefined && o.nodeName.toUpperCase() != tag.toUpperCase())
    o = o.parentNode;
  return (o.nodeName != undefined && o.nodeName.toUpperCase() == tag.toUpperCase())?o:null;
}

//array children tagów "tag" obiektu o (bezpośrednie)
function getInnerTags(o, tag) {
var r = new Array();
  if(o == null) return r;
  for(var i = 0; i < o.childNodes.length; i++)
    if(o.childNodes[i].nodeName.toUpperCase() == tag.toUpperCase())
      r.push(o.childNodes[i]);
  return r;
}

//next sibling tag "tag" dla obiektu o
function getNextTag(o, tag) {
  if(o == null || o.nextSibling == undefined) return null;
  o = o.nextSibling;
  while(o.nextSibling != undefined && o.nodeName.toUpperCase() != tag.toUpperCase())
    o = o.nextSibling;
  return (o.nodeName != undefined && o.nodeName.toUpperCase() == tag.toUpperCase())?o:null;
}

//previous sibling tag "tag" dla obiektu o
function getPreviousTag(o, tag) {
  if(o == null || o.previousSibling == undefined) return null;
  o = o.previousSibling;
  while(o.previousSibling != undefined && o.nodeName.toUpperCase() != tag.toUpperCase())
    o = o.previousSibling;
  return (o.nodeName != undefined && o.nodeName.toUpperCase() == tag.toUpperCase())?o:null;
}

//zwraca pozycję obiektu od id i
function getObjectPositionById(i) {
  try {
    return getObjectPosition(document.getElementById(i));
  } catch(ex) {
    return null;
  }
}

//zwraca pozycję obiektu o
function getObjectPosition(o) {
var p = new ObjectPosition(o.offsetLeft, o.offsetTop);
  while(o.offsetParent) {
    o = o.offsetParent;
    p.x += o.offsetLeft;
    p.y += o.offsetTop;
  }
  return p;
}

//naprawiacz daty dla cookies
function fixDate(date) {
  base = new Date(0);
  skew = base.getTime();
  if(skew > 0) date.setTime(date.getTime() - skew);
}

//zapisanie cookie
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

//odczytanie nazw cookies
function getCookiesNames(prefix) {
var dc = document.cookie;
var cookieName;
var stack = new Array();
var offset = 0;
  while(dc.indexOf(prefix, offset) != -1) {
    cookieName = prefix;
    offset = dc.indexOf(prefix, offset)+prefix.length;
    while(dc.charAt(offset)>='0' && dc.charAt(offset)<='9')
      cookieName += dc.charAt(offset++);
    if(dc.charAt(offset)=='=')
      stack.push(cookieName);
  }
  return stack.length!=0?stack:null;
}

//odczytanie cookie
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

//usunięcie cookie
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//otwarcie popupa z obrazkiem
function popImage(l) {
var popImage=window.open('',"popImage", (document.all?"width=150,height=150,":"")+"scrollbars=no,resizable=yes,toolbar=no,userbar=no,location=no,status=no,menubar=no,screenX=100,screenY=100");
  popImage.document.writeln('<html><head><title>Obrazek</title></head>');
  popImage.document.writeln('<body style="background-color: #ffffff; margin: 0px;" onLoad="autoSize();">');
  popImage.document.writeln('<img style="border: 0px; cursor: pointer;" id="image" src="'+l+'" onClick="window.close();">');
  popImage.document.writeln('<script language="JavaScript" type="text/javascript" ><!--');
  popImage.document.writeln('function autoSize(o,l) {');
  if(document.all) {
    popImage.document.writeln('window.resizeBy(document.getElementById("image").offsetWidth-document.body.clientWidth, document.getElementById("image").offsetHeight-document.body.clientHeight);');
  } else {
    popImage.document.writeln('window.resizeTo(2000, 150);');
    popImage.document.writeln('window.resizeBy(document.getElementById("image").offsetWidth-window.innerWidth, document.getElementById("image").offsetHeight-window.innerHeight);');
  }
  popImage.document.writeln('window.focus();');
  popImage.document.writeln('}');
  popImage.document.writeln('/'+'/-'+'-></script>');
  popImage.document.writeln('</body></html>');
  popImage.document.close();
}

//otwarcie popupa
function popUp(l,w,h,s) {
var popUp = window.open(l,"popUp", "width="+w+",height="+h+",scrollbars="+(s==1?"yes":"no")+",resizable=yes,toolbar=no,userbar=no,location=no,status=no,menubar=no");
  popUp.document.close();
  popUp.focus();
}

//przejście do urla l z możliwością ustawienia targetu t
function linkGo(l,t) {
  if(t == undefined) {
    window.location = l;
  } else if(t == "_blank") {
    window.open(l);
  } else {
    window.open(l, t);
  }
}

//objekt request
function Request() {}

//wyciąganie parametrów z urla
function extractReqParams(s) {
  try {
    var anch = document.createElement('a');
    anch.href = s;
    if(anch.search != '') {
      var retArr = anch.search.replace('?', '').split('&');
      var req = new Request();
      for(var i = retArr.length - 1; i >= 0 ; i--)
        if(retArr[i].indexOf('=') != -1) {
          var attr = retArr[i].split('=', 2);
          eval('Request.prototype.'+attr[0]+'=null;');
          eval('req.'+attr[0]+'="'+attr[1]+'";');
        } else {
          retArr.splice(i, 1);
        }
      if(retArr.length > 0)
        return req;
      return null;
    } else {
      return null;
    }
  } catch(ex) {
    return null;
  }
}

