function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
} 

    function SetCSS(title) {
       var i, a, main;
       for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
           if((a.getAttribute("rel").indexOf("style") != -1)&&(a.getAttribute("title"))){
               a.disabled = true;//vsechno implicitne vypinam
               if(a.getAttribute("title") == title)//pozadovany styl?
                    a.disabled = false; //opet zapnu
           }
       }
       newCookie('cssfile',title,24*7);
    }

    function newCookie(name,val,hours) {
       var date = new Date();
       date.setTime(date.getTime()+(hours*60*60*1000));
       var expires = "; expires="+date.toGMTString();
       document.cookie = name+"="+val+expires+"; path=/";       
    }
    
    function readCookie(name) {
       var nameEQ = name + "=";
       var ca = document.cookie.split(';');
       for(var i=0;i < ca.length;i++) {
           var c = ca[i];
           while (c.charAt(0)==' ') c = c.substring(1,c.length);
           if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
       }
       return null;
    }
    
    /********************/
        
    /*vratit pole z cookiny aname*/
    function CookieArrayGet(aname){
      var out = new Array();
      var s=readCookie(aname);
      if (s!=null){
		var x=s.split('-'); 
		for(var i=0; i<x.length; i++){
			y=x[i].split('_');
			out[y[0]]=y[1];
		}
	  }
      return out;
    }
    
    function CookieArraySet(aname,a){
      var s = '';
      for(var id in a){
        if (s!='') s=s+'-';
        s=s+id+'_'+a[id];
      }
      newCookie(aname,s,24*7*12);
    }
    
    /* cookiena aname obsahuje pole, ve ktere nastavit [x]=y */
    function CookieArrayStore(aname,Aid,Aval){
      if (Aid==null) return;
      var a=CookieArrayGet(aname);
	  a[Aid]=Aval;
	  CookieArraySet(aname,a);
    }
    
    /* z cookie pole precist hodnotu x a vratit*/
    function CookieArrayRead(aname,Aid){
      var a=CookieArrayGet(aname);
      for(var id in a)
		if (id==Aid)
		  return a[id];
    }


function parseURL(url){
  var i, pos, argname, value, queryString, pairs, out;
  queryString = url.search.substring(1);
  pairs = queryString.split("&");
  out=new Object();
  for (i = 0; i < pairs.length; i++){
   pos = pairs[i].indexOf('=');
   if (pos == -1)
     continue;
   argname = pairs[i].substring(0,pos);
   value = pairs[i].substring(pos+1);
   out[argname]=value;
  }
  return out;
}

//decode pro RFC1738
function decodeURL(url){
  return unescape(url).replace(/&amp;/g,'&');
}

function getId(obj){
  return document.getElementById(obj);
}

function reloadnow(){
  var scrTop = document.body.scrollTop;//pozice scrollu
  var a=window.location.href;//
  var ind=a.lastIndexOf('&scrollto');//hledam, jestli uz parametr existuje
  if (ind>0)//jestli jo
    a=a.slice(0,ind);//tak ho vyseknu
  window.location.href=a+'&scrollto='+scrTop;
}

function pageLoaded(){
  var a=window.location.href;//pozice
  var ind=a.lastIndexOf('&scrollto');//najdu parametr
  var scrTop=a.slice(ind+10);//zjistim hodnotu (tenhle je vzdy posledni)
  if ( scrTop > 40)
    window.scrollTo(0,scrTop);
}

function add2basket(pid){
  p=document.getElementById('hm_'+pid);
  window.location.href='?page=basket&buypid='+pid+'&count='+p.value;
}

function SetVisibility(objid, boolstate){
  if (boolstate){
    s='visible';
  }else{
    s='hidden';
  }
  if (obj=getId(objid)) obj.style.visibility=s;
}

function SetDisplaying(objid, boolstate){
  if (boolstate){
    s='';
  }else{
    s='none';
  }
  if (isObject(objid))
	objid.style.display=s;
  else
    if (obj=getId(objid)) obj.style.display=s;
}

function GetDisplaying(objid){
  if (!isObject(objid))
		objid=getId(objid);
  if (objid)
    s=objid.style.display;
  else
    return false;
  if (s=='none')
      return false;
  return true;
}

function ToogleDisplaying(objid){
  SetDisplaying(objid,!GetDisplaying(objid));
}

function SetClass(objid,cl){
	if (isString(objid))
		objid=getId(objid);
  if (objid)
    objid.className=cl;
}



var actionsrc="";//vystavuji globalne, aby si ji selektory mohli snadno precist

function popupaction(actsrc){
  if (actsrc.lastIndexOf('javascript:')==0){//je to javascript
      eval(actsrc.slice(11));
  }else
    window.location.href="../index.php"+actsrc;
}

function CheckAll(checked){
    var col = document.getElementsByTagName("INPUT");
    for (var i=0;i<col.length;i++) {
  col[i].checked= checked;
    }
}
/*

Ways of calling the function now are:
To get all a elements in the document with a “info-links” class. 
<CODE>getElementsByClassName(document, "a", "info-links");</CODE> 
To get all div elements within the element named “container”, with a “col” and a “left” class. 
<CODE>getElementsByClassName(document.getElementById("container"), "div",["col", "left"]); </CODE>
*/

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
} 

function getElementsByClassName(oElm, strTagName, oClassNames){
    if (oElm==null) oElm=document.body;

    var arrElements = (strTagName == "*" )? getAllChildren(oElm) : 
					  oElm.getElementsByTagName(strTagName);

    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
            oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
        oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll)
            arrReturnElements.push(oElement);
		
    }
    return (arrReturnElements)
}

function getElementByClassName(oElm, strTagName, oClassNames){
  arr=getElementsByClassName(oElm, strTagName, oClassNames);
  
  return arr.length==0 ? null : arr[0];
}

function parseClasses(oClassNames){
    return oClassNames.split(' ');
}

function parseClassesContain(oClassNames,testedclass){
	var a = parseClasses(oClassNames);
	for(var i=0; i<a.length; i++){
		if (a[i]==testedclass)
			return true;
	}
	return false;
}

function Smaller(a,b){
  return a<b?a:b;
}
function Bigger(a,b){
  return a>b?a:b;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function str2int(s){
  return parseInt(s);
}


function countLineBreaks (string) {
  var re = /\r\n|\r|\n/g;
  var n = 0;
  while (re.exec(string))
    n++;
  return n;
}

function DynTextArea_SetAreas(parent){
  elements=getElementsByClassName(parent,'TEXTAREA','');
  for(i=0; i < elements.length;i++){
    elements[i].onkeyup=new Function('e',"DynTextArea_SetMe(this);");
    DynTextArea_SetMe(elements[i]);
  }
}

function DynTextArea_SetMe(obj) {
  var Min=2, Max=15, LineAproxHeight=20;
  if (document.all) {//IE simple reseni

    while (((obj.scrollHeight > obj.clientHeight)||(obj.rows<Min))&&(obj.rows<Max))//natahovat
      obj.rows++;
    while (((obj.scrollHeight+LineAproxHeight < obj.clientHeight)||(obj.rows>Max))&&(obj.rows>Min))//a i zkracovat
      obj.rows--;
    //obj.scrollTop = 0;
  }
  else if (obj.rows) {
    obj.rows=7;
    /* tohle je trapny reseni, takze v mozile natvrdo
    var lineBreaks = countLineBreaks(textarea.value);
    var rows = parseInt(textarea.rows);
    var wrap = textarea.getAttribute('wrap');
    if (lineBreaks > rows)
      textarea.rows = ++rows;
    else if (wrap.toLowerCase() == 'soft' || wrap.toLowerCase() == 'hard') {
      while (textarea.rows * textarea.cols <= textarea.value.length) {
        textarea.rows = ++rows;
      }
    }
    */
  }
}

function GetCellRelative(tdobj,offsetrow,offsetcol){
  tdindex=MyOrderNum(tdobj);
  //activetr=activetd.parentElement;
  activetr=activetd.parentNode;
  trindex=MyOrderNum(activetr);
  if (desttr=activetr.parentNode.childNodes[trindex+offsetrow*(document.all?1:2)]){
	if (desttd=desttr.childNodes[tdindex+offsetcol]){
	  return desttd;
    }
  }
  return null;
}

/*vraci kolikaty children sveho rodice jsem*/
function MyOrderNum(obj){
  par=obj.parentNode;
  for(var i=0; i<par.childNodes.length; i++)
    if (par.childNodes[i]==obj)
      return i;
  return -1;
}


function DisableSelecting(){
	document.onselectstart=new Function ("return false");
	document.onmousedown=new Function ("return false");
}

function EnableSelecting(){
	document.onselectstart=null;
	document.onmousedown=null;
}


function explode(inputstring, separators, includeEmpties) {
	inputstring = new String(inputstring);
	separators = new String(separators);

	if(separators == "undefined") { 
		separators = " :;";
	}

	fixedExplode = new Array(1);
	currentElement = "";
	count = 0;

	for(x=0; x < inputstring.length; x++) {
		ch = inputstring.charAt(x);
		if(separators.indexOf(ch) != -1) {
			if ( ( (includeEmpties <= 0) || 
				   (includeEmpties == false) ) 
				  && (currentElement == "")) { 
			} else {
				fixedExplode[count] = currentElement;
				count++;
				currentElement = ""; 
			} 
		}else { 
			currentElement += ch; 
		}
	}

	if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
		fixedExplode[count] = currentElement; 
	}
	return fixedExplode;
}
