/**************************************************************************
**
**	   Purpose: create tree javascript
**
**    Created By : Declercq Carl
**
**    Date : 22/01/2004
**************************************************************************/

// Arrays for nodes and images
var theImages		= new Array(4);
var filters;
var indicator_timer = null;
var loadingtime=500;
// Loads all images that are used in the tree
function preloadIcons() 
{
	theImages[0] = new Image();
	theImages[0].src = "images/plus.gif";
	theImages[1] = new Image();
	theImages[1].src = "images/minus.gif";
}

function CloseAll()
{
   var i;
   var j;
   var divs=document.getElementsByTagName("DIV");
   for(i=0;i<document.images.length;i++)
   {  
      if(document.images[i].src==theImages[2].src)
      {
         document.images[i].src=theImages[0].src;
         continue;
      }
      if(document.images[i].src==theImages[3].src)
      {
         document.images[i].src=theImages[1].src;
         continue;
      }
   }
 
   for(j=0;j<divs.length;j++)
   {
		divs[j].style.display = 'none';      
   }
//   theJoin.src = images[1].src;

}

function _onmouseover(nodeval)
{
   window.status = nodeval;
}

// Opens or closes a node
function oc(node, bottom) 
{
	var theDiv = document.getElementById("div" + node); //the div wher the childs going in.
	var theJoin	= document.getElementById("join" + node); //the image of the parent.
	var theIcon ;//= document.getElementById("icon" + node).
   
   if(!theDiv || theDiv.innerHTML=="")
   {
      //the div is empty we must connect to the server to get the next node(s)
      //init the div display;
      if(theDiv)
         theDiv.style.display='none';
      var path = MakePath(theDiv);
//      alert("oc: "+path);
      getNode(node,path);
   }

   //   alert(theDiv.style.display);
	if (theDiv.style.display == 'none') 
   {
		//enable the nodes
      /*if (bottom==1) theJoin.src = theImages[1].src;
		else theJoin.src = theImages[1].src;*/
      theJoin.src = theImages[1].src;
		theDiv.style.display = '';
//      alert('hier');
	} 
   else 
   {
		//disable the nodes
/*		if (bottom==1) theJoin.src = theImages[0].src;
		else theJoin.src = theImages[0].src;*/
      theJoin.src = theImages[0].src;      
		theDiv.style.display = 'none';
	}
}

function ChangeContent(params,string)
{
   var path=ChangePath(string);
   //alert('content.php?'+params+"&"+path);
   var url = 'content.php?'+params+"&"+path;
   if(parent.frames['content'].frameElement && parent.content.frameElement.clientWidth && parent.content.frameElement.clientHeight)
   {
      url += '&pageWidth='+(parent.content.frameElement.clientWidth-40)+'&pageHeight='+(parent.content.frameElement.clientHeight-35) ;
   }
   //alert(url);
   parent.frames['content'].location.href=url; //when we click on a node we load a the content section
}

function ChangePath(str)
{
   str='path='+str;
   parent.path.location.replace('path.php?'+str);
   return str;
}

function getNode(nodeId,path)
{
   var args=getNode.arguments;
   var argc=getNode.arguments.length;
   var theParent = document.getElementById('div'+nodeId);
   var link = null;
   var reg_and = new RegExp("&", "ig");
   var filter ="",tree_location = "";
   var reg_greater = new RegExp("[>]", "ig");      
   var reg_pipe = new RegExp("[,]", "ig");
   var reg = new Array;
   var res,i,j;
   var value;
   var path2 = path.replace(reg_and, "");
   path = path.replace(reg_and, "__vdwand__");
   if(argc<2)
      return false;

   //get sup args
   if(argc>2)
   {
      reg[0] = new RegExp("filtertree=", "ig");
      reg[1] = new RegExp("tree_location=","ig");
      for(i=2;i<argc;i++)
      {
         for(j=0;j<reg.length;j++)
         {
            if((res = args[i].search(reg[j])) == 0)
            {
               switch(j)
               {
                  case 0:
                     value = args[i].substring((args[i].lastIndexOf("=")+1));
                     filters = new Array();
                     filters = value.split(reg_pipe);
                     break;
                  case 1:
                     tree_location = args[i].substring((args[i].lastIndexOf("=")+1));
                     //alert(tree_location);
                     break;
                  default:
                     break;
               }
            }
         }
      }
   }
   
   //set filter
   /*CDCL
   if(filters && filters.length)
   {
      pos = path2.lastIndexOf(">",path2.length-2);
      if( pos>=0)
         path2 = path2.substring(pos+1,path2.length);
      for(i=0;i<filters.length;i++)
      {
         pos = filters[i].indexOf(path2);
         if(pos>=0) filters[i]= filters[i].substr(pos+path2.length,filters[i].length);//remove the not necessary part of the string
         else continue;//this filter string is not needed for this node
         pos = filters[i].indexOf(">");
         pos=(pos<0)?filters[i].length:pos;
         filter += filters[i].substring(0,pos)+"|";//get next node name
      }
      filter = (filter.length>=1)?filter.substring(0,(filter.length-1)):"";
   }*/

   //set tree location

   req = new ServerRequest();
   req.server = 'class/makeTree.php';
   req.add('dbtable',theDBtable);
   req.add('nodeId',nodeId);
   req.add('path',path);
   req.add('filter',filter);
   req.add('tree_location',tree_location); 
   if((link=theParent.getAttribute("link"))!=null)
   {
      req.add('link',link);
   }
   req.setIndicatorFunction(SetLoading,theParent.getAttribute("id"));
   req.setCallFunction(SetResult,"sId("+theParent.getAttribute("id")+") tree_location("+tree_location+")");
   if(!req.execute())
   {
      req.cancelRequest();
      if(!req.execute())
      {
         alert("Verifie if your browser support xmlHTTPRequest object");
      }
   }
   
//   theParent.innerHTML+=response;
//   req.SetFunctionParseResult(SetResult);
}

function SetLoading(bool,id)
{
   var theNode=document.getElementById(id);
   var dIndicator = document.getElementById("dIndicator");
   if(!theNode)
      return;
   if(!dIndicator)
   {
      dIndicator = document.createElement('DIV');
      dIndicator.setAttribute('id','dIndicator');
      dIndicator.className = 'tree_dIndicator';
      dIndicator.innerText='Loading';
      dIndicator.style.display = 'none';
      document.body.appendChild(dIndicator);
   }
   
   if(bool)
   {
      theNode.appendChild(dIndicator);
      dIndicator.style.display = '';
      dIndicator.innerText='Loading';      
      indicator_timer=setTimeout("LoadingAnimation(0,'Loading')",loadingtime);
   }
   else
   {
      dIndicator.style.display = 'none';
      clearTimeout(indicator_timer);
   }
}

function LoadingAnimation(nb,text)
{
   nb++;
   if(nb>3||nb<1)
      nb=1;
   clearTimeout(indicator_timer);
   
   var dIndicator = document.getElementById("dIndicator");
   if(!dIndicator)
      return;

   if(dIndicator.style.display=="none")
      return;

   newtext=text;
   for(i=0;i<nb;i++)
      newtext+=".";

   dIndicator.innerText=newtext;

   indicator_timer=setTimeout("LoadingAnimation("+nb+",'"+text+"')",loadingtime)
   
}

function SetResult(req,sArgs)
{
   if(!req)
      return false;

   var statements = GetStatements(sArgs);
   var nr_statements = statements["name"].length;
   var id,tree_location;

   for(var i=0;i<nr_statements;i++)
   {
      switch(statements["name"][i])
      {
         case "sId":
            id = statements["argument"][i];
            break;
         case "tree_location":
            tree_location = statements["argument"][i];
            break;
         default : 
            break;
      }
   }
   
   var oElem = null;

   if(document.getElementById(id))
   {
      oElem = document.getElementById(id);
   }

   if(oElem)
   {
      oElem.innerHTML+=req.getResponse();
      var nodename = tree_location.substring(tree_location.lastIndexOf("/")+1);
      var reg_underscore = new RegExp("_","g");
      nodename = nodename.replace(reg_underscore," ");
      if(nodename && nodename.length>0)
      {
         var typeNode = oElem.nodeType;
         while(oElem = NextNode(oElem,typeNode,0))
         {
            if(oElem.innerHTML == nodename) //not a parent node
            {
               if(oElem.click)
               {
                  oElem.click();
               }
               else
               {
                  eval(oElem.getAttribute("onclick"));                     
                  if(oElem.getAttribute("href").charAt(0)!='#')
                     eval(oElem.getAttribute("href"));
               }
               break;
            }
         }
      }
   }

   req.validateRequest();
   req.cleanRequest();
}

function MakePath(theDiv)
{
  var path = new Array()
  var element=theDiv;
  //alert(theDiv.parentNode.outerHTML);
  var link = "url";

  if(element.getAttribute(link))
  {
     path.push(element.getAttribute("name")+"|"+element.getAttribute(link));
  }
  else
  {
     path.push(element.getAttribute("name"));
  }
  
  while((element = element.parentNode)!=null)
  {
     if(element && element.tagName && element.tagName == "DIV")
     {
        if(element.getAttribute(link))
        {
           path.push(element.getAttribute("name")+"|"+element.getAttribute(link));
        }
        else
        {
           path.push(element.getAttribute("name")); 
        }
     }
  }
  
  path.reverse();
  //alert(path.join(">")+">");
  return path.join(">")+">";
}

function FindNodeByValue(name,obj)
{
   var elem;
   var theParent = obj;
   var fl_found = false;
   
   if(!obj)
   {
      theParent = document.getElementById('div1');
   }
   else
   {
      while(theParent.tagName != "DIV")
      {
         theParent =  theParent.parentNode;
      }
   }
   
   element = theParent;
   while((element=NextElement(element,0)) && !fl_found)
   {
     // alert(element.nodeValue+"<>"+name);
      if(element.nodeValue == name)
      {
//         alert("found : "+element.parentNode.innerHTML);
         fl_found = true;
         break;
      }
   }
   if(!fl_found)
      return false;
   else
      return element.parentNode;
}

function FindNodeByName(name,obj)
{
   var elem;
   var theParent = obj;
   var fl_found = false;
   
   if(!obj)
   {
      theParent = document.getElementById('div1');
   }
   else
   {
      while(theParent.tagName != "DIV")
      {
         theParent =  theParent.parentNode;
      }
   }
   
   element = theParent;
   while((element=NextElement(element,0)) && !fl_found)
   {
//      alert(element.nodeValue+"<>"+name);
      if(element.name == name)
      {
//         alert("found : "+element.innerHTML);
         fl_found = true;
         break;
      }
   }
   if(!fl_found)
      return false;
   else
      return element;
}
preloadIcons();

