  function VisibilitySwitch(IDName)
  {
    //Get the element from the document with the specified ID
    var Element = document.getElementById(IDName);
    
    //Check if an element is found
    if(Element)
    {
     //Switch the display state
     if(Element.style.display=="none")
       Element.style.display="inline";
     else
       Element.style.display="none";
       
     //Element was found
     return true;
    }
    else
     //Element was not found
     return false;
  };
  
  function VisibilitySwitchWithFocus(IDName, FocusName)
  {
    //Get the element from the document with the specified ID
    var Element = document.getElementById(IDName);
    
    //Check if an element is found
    if(Element)
    {
     //Switch the display state
     if(Element.style.display=="none")
     {
       Element.style.display="inline";
       var FocusElement = document.getElementById(FocusName);
       if(FocusElement != null)
       {
            FocusElement.focus();
       }
     }
     else
       Element.style.display="none";
       
     //Element was found
     return true;
    }
    else
     //Element was not found
     return false;
  };
  
   function VisibilitySwitchCookie(IDName)
  {
    //Get the element from the document with the specified ID
    var Element = document.getElementById(IDName);
    
   // opened = new Array(); // opened folders;
    opened = loadState();
    //Check if an element is found
    if(Element)
    {
     //Switch the display state
     if(Element.style.display=="none")
     {   
       Element.style.display="inline-block";
       showBlockState(opened, IDName);
     }
     else
     {
       Element.style.display="none";
       saveState(opened, IDName);
     }  
     //Element was found
     return true;
    }
    else
     //Element was not found
     return false;
  };
  
  function SaveVisibilitySwitchCookie(IDName, state)
  {
     // opened = new Array(); // opened folders;
    opened = loadState();
    //Check if an element is found
     //Switch the display state
     if(state=="open")
     {   
       showBlockState(opened, IDName);
     }
     else
     {
       saveState(opened, IDName);
     }  
     return true; 
  };
  
  
  function SetVisibility(IDName, DisplayMode)
  {
    //Get the element from the document with the specified ID
    var Element = document.getElementById(IDName);
    
    //Check if an element is found then set the displaymode
    if(Element)
    {
     Element.style.display=DisplayMode;
     return true;
    }
    else
     //Element was not found
     return false;
  };
  
  function GetVisibilityState(IDName)
  {
    //Get the element from the document with the specified ID
    var Element = document.getElementById(IDName);
    
    //Check if an element is found and return the displaymode
    if(Element)
    {
     return Element.style.display;
    }
    else
     //Element was not found return false
     return false;
  };
  
  
  //Status uit cookie halen
   function loadState() {
        var opened = getCookie("opened");
        if (opened) {
            opened = opened.split("|");
        }
        else
        {
            opened = new Array();
        }
        return opened;
    };
        
    function saveState(opened, ID) {
        if (!opened.contains(ID))
        {
            opened.push(ID);
        }
        
        if (opened.length) {
            setCookie("opened", opened.join("|"), 3600*24*30, "", "");
        } else {
            clearState();
        }
    };
    
    function showBlockState(opened, ID) {
        opened.removeByValue(ID); 
        if (opened.length) {
            setCookie("opened", opened.join("|"), 3600*24*30, "", "");
        } else {
            clearState();
        }       
    };
    
    function getCookie(name) {
       
            var cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; ++i) {
                var a = cookies[i].split("=");
                if (a.length == 2) {
                    a[0] = a[0].trim();
                    a[1] = a[1].trim();
                    if (a[0] == name) {
                        return unescape(a[1]);
                    }
                }
            }
            return "";
        };
        
     function setCookie(name, value, seconds, path, domain, secure) {
            var cookie = (name + "=" + escape(value));
            if (seconds) {
                var date = new Date(new Date().getTime()+seconds*1000);
                cookie += ("; expires="+date.toGMTString());
            }
            cookie += (path    ? "; path="+path : "");
            cookie += (domain  ? "; domain="+domain : "");
            cookie += (secure  ? "; secure" : "");
            document.cookie = cookie;
        };
    
    function delCookie(name) {
            document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
        };
    
    
    function clearState() {
        delCookie("opened");
    };
    
    function getStatus(ID) {
        opened = loadState();
        if (opened.contains(ID))
        {
            return true;
        }
        return false;
    };
    
    function setStatus()
    {
        opened = loadState();
        for (var i = 0; i < opened.length; ++i) {
            VisibilitySwitch(opened[i]);
        }
    };
    
    function setStatus(ID)
    {
        opened = loadState();
        for (var i = 0; i < opened.length; ++i) {
            if (opened[i].match(ID) != null)
            {
                VisibilitySwitch(opened[i]);
            }
        }
    };
    
  //  window.onload = function() {
  //      setStatus();
  //  };
    
    /* Check whether array contains given string */
if (!Array.prototype.contains) {
    Array.prototype.contains = function(s) {
        for (var i = 0; i < this.length; ++i) {
            if (this[i] === s) { return true; }
        }
        return false;
    };
}

    /* Remove elements with such value (mutates) */
if (!Array.prototype.removeByValue) {
    Array.prototype.removeByValue = function(value) {
        var i, indexes = [];
        for (i = 0; i < this.length; ++i) {
            if (this[i] === value) { indexes.push(i); }
        }
        for (i = indexes.length - 1; i >= 0; --i) {
            this.splice(indexes[i], 1);
        }
    };
}

