

// CORE ***************************************************************************************************


// isNull :)
function isNull(a){
  return (typeof(a)=="object" && !a);
}



// isUndefined :) 
function isUndefined(a){
  return typeof(a)=="undefined";
} 



// isFunction :)
function isFunction(a){
  return typeof(a)=="function";
}



// clones src object
function cloneObject(src){
  for(pr in src)if(typeof(src[pr])=="object")this[pr]=new cloneObject(src[pr]);else this[pr]=src[pr];
}



// lists enumerable properties of an object 
Object.prototype.listProperties=function(){
  var lst;
  for(var prop in this)lst+=prop+": "+this[prop]+"\n";
  return lst;
};



// php trim() analogue
function trim(inpstr){
  return inpstr.replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1");
}



// php trim() analogue
String.prototype.trim=function(){
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1");
}; 



// isMail :)
String.prototype.isMail=function(){
  var mailRE=new RegExp("^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$");
  return mailRE.test(this);
}; 



// Function.apply where not implemented
if(!isFunction(Function.apply)){
  Function.apply=function(o,a){
    var r;
    var x="____apply";
    if(!isObject(o))o={};
    o[x]=this;
    switch((a && a.length) || 0){
      case 0: r=o[x]();
              break;
      case 1: r=o[x](a[0]);
              break;
      case 2: r=o[x](a[0],a[1]);
              break;
      case 3: r=o[x](a[0],a[1],a[2]);
              break;
      case 4: r=o[x](a[0],a[1],a[2],a[3]);
              break;
      case 5: r=o[x](a[0],a[1],a[2],a[3],a[4]);
              break;
      case 6: r=o[x](a[0],a[1],a[2],a[3],a[4],a[5]);
              break;
      default: alert("Too many arguments to apply.");
    }
    delete o[x];
    return r;
  };
} 



// Array.pop where not implemented
if(!isFunction(Array.prototype.pop)){
  Array.prototype.pop=function(){
    return this.splice(this.length-1,1)[0];
  };
}



// Array.push where not implemented
if(!isFunction(Array.prototype.push)){
  Array.prototype.push=function(){
    this.splice.apply(this,[this.length,0].concat(Array.prototype.slice.apply(arguments)));
    return this.length;
  };
}



// Array.shift where not implemented
if(!isFunction(Array.prototype.shift)){
  Array.prototype.shift=function(){
    return this.splice(0,1)[0];
  };
}



// Array.splice where not implemented
if(!isFunction(Array.prototype.splice)){
  Array.prototype.splice=function(s,d){
    var max=Math.max;
    var min=Math.min;
    var a=[];
    var e;
    var i=max(arguments.length-2,0);
    var k=0;
    var l=this.length;
    var n;
    var v;
    var x;
    s=(s || 0);
    if(s<0)s+=l;
    s=max(min(s,l),0);
    d=max(min(isNumber(d)?d:l,l-s),0);
    v=i-d;
    n=l+v;
    while(k<d){
      e=this[s+k];
      if(!isUndefined(e))a[k]=e;
      k+=1;
    }
    x=l-s-d;
    if(v<0){
      k=s+i;
      while(x){
        this[k]=this[k-v];
        k+=1;
        x-=1;
      }
      this.length=n;
    }else if(v>0){
      k=1;
      while(x){
        this[n-k]=this[l-k];
        k+=1;
        x-=1;
      }
    }
    for(k=0;k<i;++k)this[s+k]=arguments[k+2];
    return a;
  };
}



// Array unshift where not implemented
if(!isFunction(Array.prototype.unshift)){
  Array.prototype.unshift=function(){
    this.splice.apply(this,[0,0].concat(Array.prototype.slice.apply(arguments)));
    return this.length;
  };
} 



// in_array analogue
Array.prototype.contains=function(what){
  for(var i=0;i<this.length;i++)if(this[i]==what)return true;
  return false;
}




// CLIENT SIDE ********************************************************************************************



// browser object
var browser=new Object();
browser.isIE=(navigator.appName=="Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera")<0);
browser.isOpera=navigator.userAgent.indexOf("Opera")>0;
browser.isMozilla=navigator.product=="Gecko";



// returns element
function getEl(elid){
  return document.getElementById(elid);
}



// returns viewport height
function winH(){
  if(window.innerHeight)return parseInt(window.innerHeight);
  else if(document.documentElement && document.documentElement.clientHeight)return parseInt(document.documentElement.clientHeight);
  else if(document.body && document.body.clientHeight)return parseInt(document.body.clientHeight);
}



// returns viewport width
function winW(){
  if(window.innerWidth)return parseInt(window.innerWidth);
  else if(document.documentElement && document.documentElement.clientWidth)return parseInt(document.documentElement.clientWidth);
  else if(document.body && document.body.clientWidth)return parseInt(document.body.clientWidth);
}



// returns computed height of el with id elid
function getH(elid){
  if(document.all)pupik=document.all.item(elid).offsetHeight; else{
    obj=document.getElementById(elid);
    pupik=document.defaultView.getComputedStyle(obj,"").getPropertyValue("height");
  }
  return parseInt(pupik);
}



// returns computed width of el with id elid
function getW(elid){
  if(document.all)pupik=document.all.item(elid).offsetWidth; else{
    obj=document.getElementById(elid);
    pupik=document.defaultView.getComputedStyle(obj,"").getPropertyValue("width");
  }
  return parseInt(pupik);
}



// returns computed height of el given as object
function getHObj(obj){
  if(!browser.isIE && obj.nodeType==3)obj=obj.parentNode;
  if(document.all)pupik=obj.offsetHeight; else pupik=document.defaultView.getComputedStyle(obj,"").getPropertyValue("height");
  return parseInt(pupik);
}



// returns computed width of el with id elid
function getW(elid){
  if(document.all)pupik=document.all.item(elid).offsetWidth; else{
    obj=document.getElementById(elid);
    pupik=document.defaultView.getComputedStyle(obj,"").getPropertyValue("width");
  }
  return parseInt(pupik);
}



// returns computed width of el given as object
function getWObj(obj){
  if(document.all)pupik=obj.offsetWidth; else pupik=document.defaultView.getComputedStyle(obj,"").getPropertyValue("width");
  return parseInt(pupik);
}



// finds x position of an element with id elid
function findPosX(elid){
  var obj=getEl(elid);
  var curleft=0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      curleft+=obj.offsetLeft;
      obj=obj.offsetParent;
    }
  }else if(obj.x)curleft+=obj.x;
  return parseInt(curleft);
}



// finds y position of an element with id elid
function findPosY(elid){
  var obj=getEl(elid);
  var curtop=0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      curtop+=obj.offsetTop
      obj=obj.offsetParent;
    }
  }else if(obj.y)curtop+=obj.y;
  return parseInt(curtop);
}




// finds x position of an element given as object
function findPosXObj(obj){
  var curleft=0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      curleft+=obj.offsetLeft;
      obj=obj.offsetParent;
    }
  }else if(obj.x)curleft+=obj.x;
  return parseInt(curleft);
}



// finds y position of an element given as object
function findPosYObj(obj){
  var curtop=0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      curtop+=obj.offsetTop
      obj=obj.offsetParent;
    }
  }else if(obj.y)curtop+=obj.y;
  return parseInt(curtop);
}



// returns array of elements with class name clsname inherent in element with id parel
function getElementsByClassName(parel,clsname){
  var outp=new Array();
  var els=getEl(parel).getElementsByTagName("*");
  for(var i=0;i<els.length;i++)if(els[i].className==clsname)outp.push(els[i]);
  return outp;
}



// clips an element with id elid (left, top, right, bottom)
function clipEl(elid,l,t,r,b){
  getEl(elid).style.clip="rect("+t+"px,"+(getW(elid)-r)+"px,"+(getH(elid)-b)+"px,"+l+"px)";
}



// sets display none for an element with id elid 
function setDN(elid){
  getEl(elid).style.display="none";
}



// sets display block for an element with id elid
function setDB(elid){
  getEl(elid).style.display="block";
}



// sets display inline for an element with id elid
function setDI(elid){
  getEl(elid).style.display="inline";
}



// sets visibility hidden for an element with id elid
function setVH(elid){
  getEl(elid).style.visibility="hidden";
}



// sets visibility visible for an element with id elid
function setVV(elid){
  getEl(elid).style.visibility="visible";
}



// clips an element given as object 
function clipElObj(el,l,t,r,b){
  el.style.clip="rect("+t+"px,"+(getW(elid)-r)+"px,"+(getH(elid)-b)+"px,"+l+"px)";
}



// sets display none for an element el
function setDNObj(el){
  el.style.display="none";
}



// sets display block for an element el
function setDBObj(el){
  el.style.display="block";
}



// sets display inline for an element el
function setDIObj(el){
  el.style.display="inline";
}



// sets visiblity hidden for an element el
function setVHObj(el){
  el.style.visibility="hidden";
}



// sets visiblity visible for an element el
function setVVObj(el){
  el.style.visibility="visible";
}



// swaps display property of an element with elid - none->block / block->none
function swapDisplay(elid){
  el=getEl(elid);
  el.style.display=el.style.display=="none"?"block":"none";
}



// swaps display property of an element el - none->block / block->none
function swapDisplayObj(el){
  el.style.display=el.style.display=="none"?"block":"none";
}



// opens debug window with content
function debugWindow(content){
  var win=window.open("","debug","width=760,height=450,resizable=yes,scrollbars=yes");
  win.document.open();
  win.document.write('<html><head><title>debug</title></head><body><pre>'+content+'</pre></body></html>');
  win.document.close();
}



// kills teckos
function killTeckos(){
  window.focus();
}



// writes cookie
function setCookie(name,value,days){
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires="; expires="+date.toGMTString();
 	document.cookie=name+"="+escape(value)+expires;
}



// reads cookie
function getCookie(name){
  var strCookie=document.cookie.split("; ");
  for (var i=0;i<strCookie.length;i++){
    var strPar=strCookie[i].split("=");
    if (name==strPar[0])return unescape(strPar[1]);
  }
  return "";
}










