
//This function hides and shows DOM nodes depending if they have the "types" attribute set and they have the selected type
//in that attribute as part of a comma seperated list.
// sel         - either a select box object or a string containing the selected value
// parent_name - a dom object that is a parent to all the nodes you want to check
// maxdepth    - how many nodes deep the function will traverse, it defaults to 20.
function customisePage(sel,parent_name) {
  var maxdepth=(arguments.length>2) ? arguments[2] : 20;
  var selected=(typeof sel == "object") ? sel.options[sel.selectedIndex].value : sel ;
  var n=document.getElementById(parent_name);
  _customisePage(n,selected,maxdepth);
}

//This is a recursive function to traverse all nodes under the first supplied DOM element.
//For each node it will check if there is an attribute called 'types' and if so it
//will use that as a comma seperated list. If the string selected is in that list the element will be
//made visable, otherwise it will be hidden.
//Now has support for inverted sense in 'typesnot', which takes precedence over types
function _customisePage(n,selected,ttl) {
  var types=(n.getAttribute &&  n.getAttribute('types')) ? n.getAttribute('types').split(',') : false ;
  var typesnot=(n.getAttribute && n.getAttribute('typesnot')) ? n.getAttribute('typesnot').split(',') : Array() ;
  
  if(js_in_array(selected,typesnot)) {
    node_vis(n,false);
  }
  else {
    if(types==false) {
      //node_vis(n, true);
    }
    else {
      node_vis(n, (js_in_array(selected,types)));
    }
  }

  if(ttl<1) return;
  ttl--;
  for(var z=0; z<n.childNodes.length; z++) {
    //Skip text nodes
    if(n.childNodes[z].nodeType==3) continue;
    _customisePage(n.childNodes[z],selected,ttl);
  }
}

//hide or show a dom node.
//Only tested on TR elements so far but should work on others
function node_vis(element,visable)
{
  if(!element.style)
    alert(element);
  if(visable) {
	  element.style.display='';
  }
  else {
    element.style.display='none';
  }
}

//As the php in_array function.
function js_in_array(str,arr) {
  for(var i=0; i<arr.length; i++) {
    if(arr[i]==str || (arr[i]=='*' && str!='')) return true;
  }
  return false;
}
