    /**
     *  This function will update an elements style display attribute toggling
     *  it between hidden and (by default) block, although this can be overwritten 
     *  by passing inline or another string as the showState variable
     *
     *  @author Dave Folan
     *  @param (String) OR (Object) targetId Id or HtmlElement that needs toggling.
     *  @param (String) the showState to use (defaults to block) 
     **/
    function toggleHidden(targetId, showState)
    {
        if (showState == undefined) { showState='block'; }
        
        if (typeof targetId != 'object') 
        { 
            var targetElement  = document.getElementById(targetId);
            if (targetElement == null) { return false; }
        } 
        else
        {
            var targetElement = targetId;
        }
        
        if (targetElement.style.display == 'none')
        {
            targetElement.style.display = showState;
        }
        else 
        {
            targetElement.style.display = 'none';
        }
    }

    /**
     *  This function toggles an elements className between open and closed 
     *
     *  @author Dave Folan
     *  @param (String) targetId Id of the element to toggle.
     **/
     
    function toggleOpenClose(targetId)
    {
        var targetElement  = document.getElementById(targetId);
        if (targetElement == null) { return false; }
        
        if (targetElement.className.search(/open/i) > 0)
        {
            targetElement.className = targetElement.className.replace(/open/i, "closed");
        } 
        else 
        {
            targetElement.className = targetElement.className.replace(/closed/i, "open");
        }
    }
    
    /**
     *  Function to open and close facets. When facets are in this stucture:
     *   <headerElement id="ID" onclick="toggleFacetOpenClose(ID)">Header</headerElement>
     *   <listContainer id="childof-ID">
     *       <listItem>1</listItem>
     *       <listItem>2</listItem>
     *       <listItem>3</listItem>
     *   </listContainer>
     *
     *  @TODO: create two functions from this: toggleOpenClose and toggleHidden(targetId, showState = 'block')
     *  @author Dave Folan
     *  @param (String) headerId Id of the facet to close.
     **/
    function toggleFacet( facetId )
    {
        toggleOpenClose( facetId );
        toggleHidden( 'childof-' + facetId );
    }
    
    /**
     *  This function is to override the function above when trying to click the link of the parent element,
     *  as clicking on the link will trigger the li's event.
     *  So by using this the toggleOpenClose function wont work!
     *
     *  @author Dave Folan
     *  @param (String) headerId Id of the element to override onclick functionality of.
     **/
    function overrideToggleOpenClose(headerId) {
        var target = document.getElementById(headerId);
        target.id = 'empty';
        var listElement  = document.getElementById('childof-' + headerId);
        listElement.id = 'childof-empty';
    }
        
    /**
     *  Function to hide the click element and display the hidden element (i.e. Clicking "Search again button"
     *  will set the button to hidden and will show the hidden search form.
     *   <click_element id="click_element-ID" onclick="displayHidden(ID)">Click text</click_element>
     *   <hidden_element id="hidden_element-ID">
     *      Element Content
     *   </listContainer>
     *
     *  @TODO: change to use toggleHidden fucntion
     *  @author Dave Folan
     *  @param (String) clickId Id of the element to hide.
     *  @param (String) showId Id of the element to display.
     **/
    function clickToDisplay(clickId, displayId)
    {
        var clickElement  = document.getElementById(clickId);
        clickElement.style.display = 'none';
        
        var displayElement  = document.getElementById(displayId);
        displayElement.style.display = 'block';
    }
    
    /**
     *  This is the function to show and hide the extra facets using the more/less links.
     *  
     *  this will get the click Object and change it from more to less and vice versa
     *  it will then change the displayId's hidden elements with class togglable to blank/hidden
     *
     *  @author Dave Folan
     *  @param (String) clickId Id of the more/less link.
     *  @param (String) displayId Id of the element containing the hidden facets.
     **/
     
     function toggleShowMore(clickId, displayId)
     {
        var clickElement  = document.getElementById(clickId);
        if (clickElement.innerHTML.search(/more/) > 0)
        {
            clickElement.innerHTML = clickElement.innerHTML.replace(/more/g, "less");
            clickElement.innerHTML = clickElement.innerHTML.replace(/més/g, "menys");
        } 
        else 
        {
            clickElement.innerHTML = clickElement.innerHTML.replace(/less/g, "more");
            clickElement.innerHTML = clickElement.innerHTML.replace(/menys/g, "més");
        }
        
        /** Search through all li children of displayId and toggle any with class togglable **/
        var displayChildren = document.getElementById(displayId);
        
        try
        {
            var liElements = displayChildren.getElements('li.togglable');
            
            for(i=0; i<liElements.length ;i++)
            {
                var liItem = liElements[i];               
                toggleHidden(liItem);
            }
        }        
        catch (err)
        {
            var liElements = displayChildren.getElementsByTagName('li');
            
            for(i=0; i<liElements.length ;i++)
            {
                var liItem = liElements[i];

                if (liItem.className.search(/togglable/) > 0)
                {
                    toggleHidden(liItem);
                }
            }
        }
     }

    /**
     *  When called this will set a cookie using the data given 
     *  Leaving days empty will mean the cookie expires when browser is closed.
     *  Setting days to -1 will erase cookie @see eraseCookie
     *
     *  @see http://www.quirksmode.org/js/cookies.html
     *  @author Dave Folan (editted from quirksmode.org)
     *  @param (String) name name of the cookie
     *  @param (String) value value to give the cookie
     *  @param (Int)    days The number of days before the cookie expires
     **/
    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }
    
    /**
     *  Will return the value of the cookie or null
     *
     *  @see http://www.quirksmode.org/js/cookies.html
     *  @author Dave Folan (editted from quirksmode.org)
     *  @param (String) name name of the cookie to look up
     **/
    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;
    }
    
    /**
     *  Will delete a cookie (by setting it to a previous date)
     *
     *  @see http://www.quirksmode.org/js/cookies.html
     *  @author Dave Folan (editted from quirksmode.org)
     *  @param (String) name name of the cookie to delete
     **/
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }

