/**
 *
 * @param {Object} name
 * @param {Object} mainDivId
 * @param {Object} loadRequestMethod
 * @param {Object} firstLoadRequestMethod
 * @param {Object} saveRequestMethod
 */
Activsoft.Manager.register("activsoft.ajax.arbo");

activsoft.ajax.arbo.CacheArbo = function(name, mainDivId, loadRequestMethod, firstLoadRequestMethod, saveRequestMethod){
  if (!activsoft.ajax.arbo.ArboCacheManager) {
  	activsoft.ajax.arbo.ArboCacheManager = new activsoft.ajax.cache.XMLCacheManager();
  }
  activsoft.ajax.arbo.ArboCacheManager.initCache("arbo_" + name);
  this._myCache = activsoft.ajax.arbo.ArboCacheManager.getCache("arbo_" + name);
  this._busy = false;
  this._name = name;
  this._currentLoadedArbo = "";
  this._currentSavedArbo = "";
  this._currentSavedNode = null;
  this._mainDivRapport = document.getElementById(mainDivId);
  this._currentArbo = "";
  this._arboToSwitch = "";
  this._removeEmptyLevelOne = false;
  this._onSearch = false;
  this._nbTries = 3;
  this._listOfArboToSave = new Array();
  this._autoCommit = true;
  this._isDragDropable = false;  
  this._dragDropManager = null;
  this._lastDragArbo = null;
  this._onReload = false;
  
  // paramètre : arbo
  this.arboOpened = new YAHOO.util.CustomEvent("arboOpened" + this._name, this);
  // paramètre : arbo
  this.arboClosed = new YAHOO.util.CustomEvent("arboClosed" + this._name, this);
  // cache Updated : le cache a été chargé
  this.cacheUpdated = new YAHOO.util.CustomEvent("cacheUpdated" + this._name, this);
  // cache saved : le cache a été sauvegardé
  this.cacheSaved = new YAHOO.util.CustomEvent("cacheSaved" + this._name, this);
  // loadRequest
  
  this._loadRequestMethod = loadRequestMethod;
  
  this._firstLoadRequestMethod = firstLoadRequestMethod;
  
  this._saveRequestMethod = saveRequestMethod;
  
  this._createDragElement = null;
  
  this._isDropAuthorized = null;
  
  this._getOpenedArbo = new Array();
  
  this._arboSize = 4;
  
  if (!this._mainDivRapport) {
    alert("L'arborescence ne peut être fabriqué, l'élément de base n'existe pas!");
  }
  else {
    var divFin = document.createElement("div");
    divFin.setAttribute("id", "endDivArbo_" + this._name);
    divFin.setAttribute("style", "display:none");
    this._mainDivRapport.appendChild(divFin);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.setDragDropable = function(val, createDragElement, isDropAuthorized, dragManager) {
  this._isDragDropable = true;
  this._createDragElement = createDragElement;
  this._isDropAuthorized = isDropAuthorized;
  this._dragDropManager = dragManager;
}

activsoft.ajax.arbo.CacheArbo.prototype.removeNode = function(arbo) {
  var noeud = this.getElementByArbo(arbo);
  noeud.setAttribute("deleted","1");
  if (this._autoCommit) {
    this.saveFromArbo(noeud.parentNode.getAttribute("arbo"));
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.setAutoCommit = function(b) {
  this._autoCommit = b;
}

activsoft.ajax.arbo.CacheArbo.prototype.setArboSize = function(s){
  this._arboSize = s;
}

activsoft.ajax.arbo.CacheArbo.prototype.defaultHttpRequestErrorMethod = function(){
  alert("L'appel au serveur n'a pas fonctionné, prière de contacter l'administrateur technique.");
}

activsoft.ajax.arbo.CacheArbo.prototype.setNbTries = function(nb){
  this._nbTries = nb;
}

activsoft.ajax.arbo.CacheArbo.prototype.setEmptyLevelOne = function(empty){
  this._emptyLevelOne = empty;
}

activsoft.ajax.arbo.CacheArbo.prototype.reloadArbo = function(arbo) {
  var noeud = this.getElementByArbo(arbo);
  if (noeud.getAttribute("opened")=="1") {
    this._getOpenedArbo.push(noeud.getAttribute("arbo"));
  }
  // suppression des éléments graphiques
  this.destroyGraphicElement(noeud);
  // suppression des éléments finaux
  var tab = noeud.getElementsByTagName("ELEMENT");
  for (var i=0;i<tab.length;i++) {
    var no = tab[i];
    if (no.getAttribute("opened")=="1" && no.getAttribute("visible")=="1") {
      this._getOpenedArbo.push(no.getAttribute("arbo"));
    }
  }

  tab = activsoft.ajax.util.xpathNodeList("ELEMENT", noeud, this._myCache);
  for (var i=0;i<tab.length;i++) {
    var no = tab[i];
    noeud.removeChild(no);
  }
  
  if (arbo=="") {
    noeud.parentNode.removeChild(noeud);
    this._mainDivRapport.removeChild(document.getElementById("divArbo_" + this._name + "_"));
    this._mainDivRapport.removeChild(document.getElementById("lastArboDiv_" + this._name + "_"));
  }
  this._getOpenedArbo.sort();
  this._getOpenedArbo.reverse();
  this.loadArbo(arbo);  
}

activsoft.ajax.arbo.CacheArbo.prototype.showUnderArbo = function(myNode){
  var tableau = myNode.getElementsByTagName("ELEMENT");
  for (var i = 0; i < tableau.length; i++) {
    var noeud = tableau[i];
    if (noeud.parentNode == myNode) {
      noeud.setAttribute("visible", "1");
      this.getGraphicElement(noeud.getAttribute("arbo")).style.display = "";
    }
    else {
      if (noeud.getAttribute("visible") == "1") {
        this.getGraphicElement(noeud.getAttribute("arbo")).style.display = "";
      }
    }
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.destroyGraphicElement = function(element, himself) {
  var tab = element.getElementsByTagName("ELEMENT");
  for (var i=0;i<tab.length;i++) {
    var chaine = tab[i].getAttribute("arbo");
    this._mainDivRapport.removeChild(document.getElementById("divArbo_" + this._name + "_" + chaine));
    this._mainDivRapport.removeChild(document.getElementById("lastArboDiv_" + this._name + "_" + chaine));
  }  
  if (himself) {
    this._mainDivRapport.removeChild(document.getElementById("divArbo_" + this._name + "_" + element.getAttribute("arbo")));
    this._mainDivRapport.removeChild(document.getElementById("lastArboDiv_" + this._name + "_" + element.getAttribute("arbo")));  	
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.getElementFromGraphics = function(element) {
  if (element==document.body) {
    return null;
  }
  if (!element.getAttribute) {
    return null;
  }
  if (element.getAttribute("id")) {
    if (element.getAttribute("id")!="") {
      if (element.getAttribute("id").substring(0, ("divArbo_" + this._name + "_").length)=="divArbo_" + this._name + "_") {
        return this.getElementByArbo(element.getAttribute("id").substr(("divArbo_" + this._name + "_").length));
      }
    }
  }
  return this.getElementFromGraphics(element.parentNode);
}

activsoft.ajax.arbo.CacheArbo.prototype.createGraphicElement = function(element){
  var arbo = element.getAttribute("arbo");
  var divArbo = document.createElement("div");
  divArbo.setAttribute("id", "divArbo_" + this._name + "_" + element.getAttribute("arbo"));
  if (element.getAttribute("visible") == "0") {
    divArbo.style.display = "none";
  }
  this._mainDivRapport.insertBefore(divArbo, document.getElementById("lastArboDiv_" + this._name + "_" + element.getAttribute("arbo").substring(0, element.getAttribute("arbo").length - this._arboSize)));

  var lastSousDivArbo = document.createElement("div");
  lastSousDivArbo.setAttribute("id", "lastArboDiv_" + this._name + "_" + element.getAttribute("arbo"));
  lastSousDivArbo.style.display = "none";
  this._mainDivRapport.insertBefore(lastSousDivArbo, document.getElementById("lastArboDiv_" + this._name + "_" + element.getAttribute("arbo").substring(0, element.getAttribute("arbo").length - this._arboSize)));
  
  if (this._isDragDropable) {
    var monDiv = this;
    var cElement = new activsoft.dragdrop.DragDropElement(divArbo, element, this._createDragElement, function(objDest) {return monDiv.isAuthorized(objDest, this);});
    cElement.elementDropped.subscribe(function(type, args, me) {monDiv.returnDropped(type, args, me);}, element);
    cElement.elementMoved.subscribe(function(type, args, me) {monDiv.returnMoved(type, args, me);}, element);
    dgManager.addElement(cElement);
  }
  return divArbo;
}

activsoft.ajax.arbo.CacheArbo.prototype.returnDropped = function(type, args, me) {
  var el = this.getElementFromGraphics(args[0]);
  if (el) {
    this.moveElement(me, el);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.returnMoved = function(type, args, me) {
  var el = this.getElementFromGraphics(args[0]);  
  var moi = this;
  if (el) {
    this._lastDraggedElement = this._dragDropManager.getDraggedElement();
    this._lastDragArbo = el.getAttribute("arbo");
    setTimeout(function() {
      moi.verifyOpen(el.getAttribute("arbo"));
    }, 500);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.verifyOpen = function(ar) {
  if (this._dragDropManager.isDragging()) {
    if (this._dragDropManager.getDraggedElement()) {
      if (this._lastDraggedElement == this._dragDropManager.getDraggedElement()) {
        if (this._lastDragArbo == ar) {
          this.openArbo(ar);
        }
      }
    }
  }
  return false;
}

activsoft.ajax.arbo.CacheArbo.prototype.endMove = function(type, args, me) {
  var newParent = me.getElementByArbo(args[0]);
  var ar = "0000" + newParent.getAttribute("nfils");
  ar = ar.substr(ar.length - 4, 4);
  newParent.setAttribute("nfils", parseFloat(newParent.getAttribute("nfils")) + 1);
  newParent.appendChild(me._currentMoved);
  
  me.arboOpened.unsubscribe(me.endMove, me);
  me.changeNodeArbo(me._currentMoved,  args[0] + ar);  
}

activsoft.ajax.arbo.CacheArbo.prototype.moveElement = function(element, newParent) {
  element.parentNode.setAttribute("nfils", parseFloat(element.parentNode.getAttribute("nfils")) - 1);
  element.parentNode.removeChild(element);
  this._currentMoved = element;
  this.arboOpened.subscribe(this.endMove, this);
  
  this.openArbo(newParent.getAttribute("arbo"));
}

activsoft.ajax.arbo.CacheArbo.prototype.isAuthorized = function(objDest, dElement) {
  var element = this.getElementFromGraphics(objDest);
  if (element) {
    return this._isDropAuthorized(element, dElement);
  }
  return false;
}

activsoft.ajax.arbo.CacheArbo.prototype.getGraphicElement = function(arbo){
  return document.getElementById("divArbo_" + this._name + "_" + arbo);
}

activsoft.ajax.arbo.CacheArbo.prototype.getNoeudNFils = function(noeud) {
  var retour = 0;
  for (var i=0;i<noeud.childNodes.length;i++) {
    if (noeud.childNodes[i].nodeName=="ELEMENT") {
      retour++;
    }
  }
  return retour;
}


activsoft.ajax.arbo.CacheArbo.prototype.openArbo = function(arbo){
  if (this._busy) {
    return;
  }

  var myNode = this.getElementByArbo(arbo);
  var nfils = parseInt(myNode.getAttribute("nfils"));
  myNode.setAttribute("opened", "1");
  if (nfils <= this.getNoeudNFils(myNode)) {
    this.showUnderArbo(myNode);
    this.arboOpened.fire(arbo);
  }
  else {
    this.cacheUpdated.subscribe(this.endOpenArbo, this);
    this.loadArbo(arbo);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.endOpenArbo = function(type, args, me) {
  if (args[0]==this._currentLoadedArbo) {
    me.cacheUpdated.unsubscribe(me.endOpenArbo, me);
    me.arboOpened.fire(me._currentLoadedArbo);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.loadArbo = function(arbo){
  var arboRequest = new activsoft.ajax.RequestToSend();
  this._currentLoadedArbo = arbo;
  this._busy = true;
  
  var currentArboCache = this;
  arboRequest.nbTries = this._nbTries;
  arboRequest.afterLoadCallBack = function(){
    currentArboCache.returnFromArbo(this);
  }
  arboRequest.httpErrorCallBack = this.defaultHttpRequestErrorMethod;
  if (arbo == "") {
	this._firstLoadRequestMethod(arboRequest, arbo);
  }
  else {
  	this._loadRequestMethod(arboRequest, arbo);
  }
  arboRequest.send();
}

activsoft.ajax.arbo.CacheArbo.prototype.returnFromArbo = function(arboRequest){
  var parentNode;
  if (this._currentLoadedArbo == "") {
    parentNode = this._myCache.documentElement;
  }
  else {
    parentNode = this.getElementByArbo(this._currentLoadedArbo);
  }
  for (var i = 0; i < arboRequest.xmlDocumentLoaded.documentElement.childNodes.length; i++) {
    var newNode = arboRequest.xmlDocumentLoaded.documentElement.childNodes[i].cloneNode(true);
    parentNode.appendChild(newNode);
    this.paintFromCache(newNode);
  }
  this._busy = false;
  if (this._getOpenedArbo.length>0 && !this._onReload) {
    this._onReload = true;
    var el = this._getOpenedArbo.pop();
    
    while (!this.getElementByArbo(el) && this._getOpenedArbo.length>0) {
      el = this._getOpenedArbo.pop();
    }
    if (this.getElementByArbo(el)) {
      this.arboOpened.subscribe(this.nextArbo, this);
      this.openArbo(el);
    }
    else {
      this._onReload = false;    
    }
  }
  this.cacheUpdated.fire(this._currentLoadedArbo);
}

activsoft.ajax.arbo.CacheArbo.prototype.nextArbo = function(type, args, me) {
  if (me._getOpenedArbo.length>0) {
    me.arboOpened.unsubscribe(me.nextArbo, me);
    this._onReload = false;
  }
  var el = me._getOpenedArbo.pop();
  
  while (!me.getElementByArbo(el) && me._getOpenedArbo.length>0) {
    el = me._getOpenedArbo.pop();
  }
  if (me.getElementByArbo(el)) {
    me.openArbo(el);
  }
  else {
    me.arboOpened.unsubscribe(me.nextArbo, me);
    this._onReload = false;
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.paintFromCache = function(element){
  var divArbo = this.createGraphicElement(element);  
  this._paintCallBack(element, divArbo);
  /**
   Peinture des enfants
   */
  for (var i = 0; i < element.childNodes.length; i++) {
    if (element.childNodes[i].nodeName) {
      this.paintFromCache(element.childNodes[i]);
    }
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.closeArbo = function(arbo){
  if (this._busy) {
    return;
  }
  var myNode = this.getElementByArbo(arbo);
  
  var tableau = myNode.getElementsByTagName("ELEMENT");
  for (var i = 0; i < tableau.length; i++) {
    if (tableau[i].getAttribute("visible") == "1") {
      this.getGraphicElement(tableau[i].getAttribute("arbo")).style.display = "none";
    }
    if (tableau[i].parentNode == myNode) {
      tableau[i].setAttribute("visible", "0");
    }
  }
  myNode.setAttribute("opened", "0");
  this.arboClosed.fire(arbo);
}

activsoft.ajax.arbo.CacheArbo.prototype.addNode = function(arboParent, titre, id, params){
  var parentNode = this.getElementByArbo(arboParent);
  var pos = parseFloat(parentNode.getAttribute("nfils"));
  var newNode = this._myCache.createElement("ELEMENT");
  newNode.setAttribute("nfils","0");
  parentNode.setAttribute("nfils", parseFloat(parentNode.getAttribute("nfils")) + 1);
  newNode.setAttribute("title",titre);
  newNode.setAttribute("id", id);
  newNode.setAttribute("added","1");
  newNode.setAttribute("opened","0");
  newNode.setAttribute("modified", "0");
  newNode.setAttribute("deleted", "0");
  newNode.setAttribute("arboParent", arboParent);
  
  for (var n in params) {
    newNode.setAttribute(n, params[n]);
  }
  var newArbo = "0000" + pos;
  newArbo = arboParent + newArbo.substr(newArbo.length - 4, 4);
  newNode.setAttribute("arbo", newArbo);
  parentNode.appendChild(newNode);
  var monDiv = this.createGraphicElement(newNode);
  this._paintCallBack(newNode, monDiv);
  return parentNode;
}

activsoft.ajax.arbo.CacheArbo.prototype.repaintNode = function(arbo) {
  // on effacde tout et on recommence!!!
  var monDiv = this.getGraphicElement(arbo);
  while (monDiv.childNodes.length>0) {
    monDiv.removeChild(monDiv.firstChild);
  }
  this._paintCallBack(this.getElementByArbo(arbo), monDiv);
}

activsoft.ajax.arbo.CacheArbo.prototype.changeModified = function(node, value) {
  node.setAttribute("modified",value);
  if (value=="0") {
    var tab = activsoft.ajax.util.xpathNodeList("OLD_ELEMENT", node, this._myCache);
    if (tab.length > 0) {
      node.removeChild(tab[0]);
    }
  }
  else {
    var tab = activsoft.ajax.util.xpathNodeList("OLD_ELEMENT", node, this._myCache);
    if (tab.length > 0) {
      return;
    }    
    var old = this._myCache.createElement("OLD_ELEMENT");
    for (var i=0;i<node.attributes.length;i++) {
      noeud = node.attributes[i];
      old.setAttribute(noeud.nodeName, noeud.nodeValue);
    }
    for (var i=0;i<node.childNodes.length;i++) {
      noeud = node.childNodes[i];
      if (noeud.nodeName!="ELEMENT") {
        old.appendChild(noeud.cloneNode(true));
      }
    }
    node.appendChild(old);
  }
}

/**
 * TODO:implémentation
 * 
 * @param {Object} node
 * @param {Object} newArbo
 */
activsoft.ajax.arbo.CacheArbo.prototype.changeNodeArbo = function(node, newArbo, norepaint) {
  var rep = false;
  var oldArbo = node.getAttribute("arbo");
  if (norepaint) {
    rep = true;
  }
  this.changeModified(node, "1");
  
  // implémentation graphique : on supprime l'arbo d'avant, et on repeint l'arbo correspondante.
  if (!rep) {
    this.destroyGraphicElement(node, true);
  }
  
  node.setAttribute("arbo", newArbo);
  node.setAttribute("arboParent", node.parentNode.getAttribute("arbo"));
  if (!rep) {
    node.setAttribute("visible", parseFloat(node.parentNode.getAttribute("opened")) * parseFloat(node.parentNode.getAttribute("visible")));
  }
  var el = this.getGraphicElement(newArbo);
  if (!el) {
    el = this.createGraphicElement(node);
  }
  else {
    if (node.getAttribute("visible")=="1") {
      el.style.display = "";
    }
    else {
      el.style.display = "none";
    }
  }
  this.repaintNode(node.getAttribute("arbo"));
  var tab = node.getElementsByTagName("ELEMENT");
  for (var i=0;i<tab.length;i++) {
    var newArbo2 = tab[i].getAttribute("arbo");
    newArbo2 = newArbo + newArbo2.substr(oldArbo.length);
    this.changeNodeArbo(tab[i], newArbo2, true);
  }
  if (!rep && this._autoCommit) { 
    var i = oldArbo.length;
    if (newArbo.length<oldArbo.length) {
      i = newArbo.length;
    }
    while (i>0 && oldArbo.substr(0,i)!=newArbo.substr(0,i)) {
      i = i - 4;
    }
    var arboCommun = "";
    if (i>0) {
      arboCommun = newArbo.substr(0,i);
    }
    this.saveFromArbo(arboCommun);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.switchNode = function(arbo1, arbo2) {
  var noeud1 = this.getElementByArbo(arbo1);
  var noeud2 = this.getElementByArbo(arbo2);
  if (!noeud1 || !noeud2) {
    return;
  }
  var parentNode = noeud1.parentNode;
  if (parentNode!=noeud2.parentNode) {
    return;
  }
  else {
    var suivant1 = noeud1.nextSibling;
    parentNode.removeChild(noeud1);
    parentNode.insertBefore(noeud1, noeud2);
    parentNode.removeChild(noeud2);
    if (suivant1) {
      parentNode.insertBefore(noeud2, suivant1);
    }
    else {
      parentNode.appendChild(noeud2);
    }
  }
  this.rebuildArbo(parentNode);
}

activsoft.ajax.arbo.CacheArbo.prototype.upNode = function(arbo) {
  var element = parseFloat(arbo.substr(arbo.length - 4, 4)) - 1;
  if (element<0) {
    return;
  }
  else {
    var arboBefore = "0000" + element.toString();
    arboBefore = arbo.substr(0, arbo.length - 4) + arboBefore.substr(arboBefore.length - 4, 4);
    this.switchNode(arboBefore, arbo);
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.downNode = function(arbo) {
  var element = parseFloat(arbo.substr(arbo.length - 4, 4)) + 1;
  var arboAfter = "0000" + element.toString();
  arboAfter = arbo.substr(0, arbo.length - 4) + arboAfter.substr(arboAfter.length - 4, 4);
  this.switchNode(arbo, arboAfter);
}

activsoft.ajax.arbo.CacheArbo.prototype.rebuildArbo = function(myNode) {
  var arboParent = myNode.getAttribute("arbo");
  var pos = 0;
  for (var i=0;i<myNode.childNodes.length;i++) {
    var enfant = myNode.childNodes[i];
    if (enfant.nodeName == "ELEMENT") {
      var ajoutArbo = "0000" + pos;
      ajoutArbo = ajoutArbo.substr(ajoutArbo.length - 4, 4);
      this.changeNodeArbo(enfant, arboParent + ajoutArbo);
      pos++;
      this.rebuildArbo(enfant);
    }
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.setAutoSave = function(autoSave){
  this._autoSave = autoSave;
}

activsoft.ajax.arbo.CacheArbo.prototype.save = function(){
  this.saveFromArbo("");
  
}

activsoft.ajax.arbo.CacheArbo.prototype.createToSaveList = function(parentNode) {
  if (parentNode.getAttribute("added") == "1" || parentNode.getAttribute("modified") == "1" || parentNode.getAttribute("deleted") == "1") {
    this._listOfArboToSave.push(parentNode);
  }
  else {
    for (var i = 0; i < parentNode.childNodes.length; i++) {
      this.createToSaveList(parentNode.childNodes[i]);
    }
  }  
}

activsoft.ajax.arbo.CacheArbo.prototype.saveFromNode = function(parentNode){
  this._currentSavedArbo = parentNode.getAttribute("arbo");
  this._listOfArboToSave = new Array();
  this.createToSaveList(parentNode);
  if (this._listOfArboToSave.length > 0) {
	this.launchSave();
  }
  else {
  	this.cacheSaved.fire(this._currentSavedArbo, true);
  }
}

/**
 * Sauve les éléments marqué added="1" ou modified="1" dans l'ordre de l'arbo
 * @param {Object} arboParent
 */
activsoft.ajax.arbo.CacheArbo.prototype.saveFromArbo = function(arboParent){
  this._busy = true;
  this._currentSavedArbo = arboParent;
  
  var parentNode = null;
  if (arboParent == "") {
    parentNode = this._myCache.documentElement.firstChild;
  }
  else {
    parentNode = this.getElementByArbo(arboParent);
  }
  this.saveFromNode(parentNode);
}

activsoft.ajax.arbo.CacheArbo.prototype.launchSave = function(){
  var arboRequest = new activsoft.ajax.RequestToSend();
  this._currentSavedNode = this._listOfArboToSave.shift();
  this._busy = true;
  
  var currentArboCache = this;
  arboRequest.nbTries = this._nbTries;
  arboRequest.afterLoadCallBack = function(){
    currentArboCache.returnFromSave(this);
  }
  arboRequest.httpErrorCallBack = this.defaultHttpRequestErrorMethod;
  this._saveRequestMethod(arboRequest, this._currentSavedNode.getAttribute("arbo"));
  if (arboRequest.request) {
    arboRequest.send();
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.getOldElement = function(node) {
  var tab = activsoft.ajax.util.xpathNodeList("OLD_ELEMENT", node, this._myCache);
  if (tab.length>0) {
    return tab[0];
  } 
  return null;
}

activsoft.ajax.arbo.CacheArbo.prototype.returnFromSave = function(arboRequest) {
  if (arboRequest.xmlDocumentLoaded.documentElement.nodeName == "OK") {
    this.changeModified(this._currentSavedNode, "0");
    this._currentSavedNode.setAttribute("added","0");
    if (this._currentSavedNode.getAttribute("deleted")=="1") {
      this.destroyGraphicElement(this._currentSavedNode, true);
      this._currentSavedNode.parentNode.setAttribute("nfils", parseFloat(this._currentSavedNode.parentNode.getAttribute("nfils"))-1);
      this._currentSavedNode.parentNode.removeChild(this._currentSavedNode);
    }

    if (this._listOfArboToSave.length>0)  {
      launchSave();
    }
    else {
      this.cacheSaved.fire(this._currentSavedArbo, true);
      this._busy = false;
    }
  }
  else {
    arboRequest.httpErrorCallBack(arboRequest.xmlDocumentLoaded.documentElement.getAttribute("message"));
  }
}

activsoft.ajax.arbo.CacheArbo.prototype.setPaintCallBack = function(callBack){
  this._paintCallBack = callBack;
}

activsoft.ajax.arbo.CacheArbo.prototype.setLoadRequestMethod = function(method){
  this._loadRequestMethod = method;
}

activsoft.ajax.arbo.CacheArbo.prototype.setSaveRequestMethod = function(method){
  this._saveRequestMethod = method;
}

activsoft.ajax.arbo.CacheArbo.prototype.setFirstLoadRequestMethod = function(method){
  this._firstLoadRequestMethod = method;
}

activsoft.ajax.arbo.CacheArbo.prototype.getElementByArbo = function(arbo){
  var tab = activsoft.ajax.util.xpathNodeList("/BODY//ELEMENT[@arbo='" + arbo + "']", this._myCache.documentElement, this._myCache);
  if (tab.length > 0) {
    return tab[0];
  }
  return null;
}
