
Activsoft.Manager.register("activsoft.ajax.util");

var obj;

activsoft.ajax.util.xpathNodeList = function(expression,node,doc) {
  var isIE = false;
  var nodes = new Array();

  if(window.ActiveXObject) {
    isIE = true;
  }
  
  if (isIE) {
    var nodeList = node.selectNodes(expression);
    for(u=0;u<nodeList.length;u++) {
      nodes[u] = nodeList.item(u);
    }
  }
  else {
    /*var xpath = new XPathEvaluator();
    var xpathresult = xpath.evaluate(expression, node, null, 7, null);
    for(u=0;u<xpathresult.snapshotLength;u++) {
      nodes[u] = xpathresult.snapshotItem(u);
    }*/
    var xpathresult = doc.evaluate(expression, node, null, 6, null);
    
    for(u=0;u<xpathresult.snapshotLength;u++) {
      nodes[u] = xpathresult.snapshotItem(u);    
    }
  }
  
  return nodes;
}

activsoft.ajax.util.getChildData = function(parentEl) {
  if (parentEl == null) {
    return null;
  }
  var tempNode = parentEl.firstChild;
  var text="";
  
  while (tempNode != null) {
    switch (tempNode.nodeType) {
      case 3 /*TEXT_NODE*/ :
        text+=tempNode.nodeValue;
      break;

      case 4 /*CDATA_SECTION_NODE*/ :
        text+=tempNode.nodeValue;
      break;
      
      default :
      break;
    }
    tempNode = tempNode.nextSibling;
  }
  return text;
}

activsoft.ajax.util.WaitPanelObject = function() {
  if (!(document.getElementById('divAjaxWaitPanel'))) {
    var parentDiv = document.createElement("div");
    parentDiv.setAttribute("id", "divAjaxWaitPanel");
    parentDiv.style.display = "none";
    
    var imgDiv = document.createElement("div");
    imgDiv.setAttribute("id", "divAjaxWaitPanelStatePanel");
    imgDiv.className = "stateWait";
    parentDiv.appendChild(imgDiv);

    var imgDiv = document.createElement("div");
    imgDiv.setAttribute("id", "divAjaxWaitPanelContent");
    parentDiv.appendChild(imgDiv);
    
    document.body.appendChild(parentDiv);
  }
}

activsoft.ajax.util.getNextSiblingElement= function(element) {
  var el = element.nextSibling;
  while (el) {
  	if (el.nodeType==1) {
	  return el;
	}
  	el = el.nextSibling;
  }
  return null;  
}

activsoft.ajax.util.WaitPanelObject.prototype.showNextToMouse = function(state, message, event) {
  this.showInPosition(state, message, YAHOO.util.Event.getPageY(event), YAHOO.util.Event.getPageX(event));
}

activsoft.ajax.util.WaitPanelObject.prototype.showCenter = function(state, message) {
  var pan = document.getElementById('divAjaxWaitPanel');
  this.showInPosition(state, message, YAHOO.util.Dom.getDocumentHeight() / 2, (YAHOO.util.Dom.getClientWidth() / 2));  
}  

activsoft.ajax.util.WaitPanelObject.prototype.showCenterAtMouse = function(state, message, event) {
  var pan = document.getElementById('divAjaxWaitPanel');
  this.showInPosition(state, message, YAHOO.util.Event.getPageY(event), (YAHOO.util.Dom.getClientWidth() / 2));  
}  

activsoft.ajax.util.WaitPanelObject.prototype.showInPosition = function(state, message, top, left) {
  var pan = document.getElementById('divAjaxWaitPanel');

  pan.style.top = top + "px";
  pan.style.left = left + "px";

  if (state == "WAIT") {
    document.getElementById('divAjaxWaitPanelStatePanel').className = 'stateWait';
  }
  if (state == "FINISH") {
    document.getElementById('divAjaxWaitPanelStatePanel').className = 'stateFinish';
  }
  document.getElementById('divAjaxWaitPanelContent').innerHTML = message;
  pan.style.display = 'block';
  pan.style.left=pan.offsetLeft-(pan.offsetWidth/2)+'px';
}

activsoft.ajax.util.WaitPanelObject.prototype.updateMessage = function(state, message) {
  var pan = document.getElementById('divAjaxWaitPanel');
  if (state == "WAIT") {
    document.getElementById('divAjaxWaitPanelStatePanel').className = 'stateWait';
  }
  if (state == "FINISH") {
    document.getElementById('divAjaxWaitPanelStatePanel').className = 'stateFinish';
  }
  document.getElementById('divAjaxWaitPanelContent').innerHTML = message;
}

activsoft.ajax.util.WaitPanelObject.prototype.updateMessageAndClose = function(state, message, timeToWait) {
  var pan = document.getElementById('divAjaxWaitPanel');
  if (state == "WAIT") {
    document.getElementById('divAjaxWaitPanelStatePanel').className = 'stateWait';
  }
  if (state == "FINISH") {
    document.getElementById('divAjaxWaitPanelStatePanel').className = 'stateFinish';
  }
  document.getElementById('divAjaxWaitPanelContent').innerHTML = message;
  
  obj=this;
  var t=setTimeout("obj.hide()",timeToWait)
}


activsoft.ajax.util.WaitPanelObject.prototype.hide = function() {
  var pan = document.getElementById('divAjaxWaitPanel');
  pan.style.display = 'none';
}

activsoft.ajax.util.PromptPanelObject = function() {
  if (!(document.getElementById('divAjaxPromptPanel'))) {
    var parentDiv = document.createElement("div");
    parentDiv.setAttribute("id", "divAjaxPromptPanel");
    parentDiv.style.display = "none";

    var field = document.createElement("fieldset");
    parentDiv.appendChild(field);    
    var enfant = document.createElement("label");
    enfant.setAttribute("for", "divAjaxPromptInput");
    field.appendChild(enfant);
    this._label = enfant;
    enfant = document.createElement("input");
    enfant.setAttribute("type", "text");
    enfant.setAttribute("name", "divAjavPromptInput");
    enfant.setAttribute("id", "divAjavPromptInput");
    enfant.setAttribute("value", "");
    this._input = enfant;
    field.appendChild(enfant);

    var act = document.createElement("div");
    act.className = "actionPanel";
    parentDiv.appendChild(act);
    enfant = document.createElement("div");
    enfant.className = "actionButton";
    enfant.setAttribute("id","divAjavPromptOk");
    enfant.appendChild(document.createTextNode("Ok"));
    act.appendChild(enfant);
    
    enfant = document.createElement("div");
    enfant.className = "actionButton";
    enfant.appendChild(document.createTextNode("Annuler"));
    enfant.setAttribute("id","divAjavPromptCancel");
    act.appendChild(enfant);

    YAHOO.util.Event.addListener("divAjavPromptOk", "click", function(event, me) {me.validation();}, this);
    YAHOO.util.Event.addListener("divAjavPromptCancel", "click", function(event, me) {me.cancel();}, this);

    document.body.appendChild(parentDiv);
  }
  if (!(document.getElementById('divAjaxPromptOverlayPanel'))) {
    var over = document.createElement("div");
    over.setAttribute("id", "divAjaxPromptOverlayPanel");
    var img = document.createElement("img");
    img.setAttribute("border","0");
    img.setAttribute("src", "img/0.gif");
    img.setAttribute("width", "100%");
    img.setAttribute("height", "100%");
    img.onclick = function() {return false;}
    img.style.display = "none";
    over.appendChild(img);
    document.body.appendChild(over);
  }
}

activsoft.ajax.util.PromptPanelObject.prototype.validation = function() {
  if (this.onValid) {
    this.onValid(this._input.value);
  }
  this.hide();
}

activsoft.ajax.util.PromptPanelObject.prototype.cancel = function() {
  if (this.onCancel) {
    this.onCancel();
  }
  this.hide();
}

activsoft.ajax.util.PromptPanelObject.prototype.hide = function() {
  var pan = document.getElementById('divAjaxPromptPanel');
  pan.style.display = 'none';
  document.getElementById('divAjaxPromptOverlayPanel').style.display = 'none';
}

activsoft.ajax.util.PromptPanelObject.prototype.showNextToMouse = function(label, defaultValue, onValid, onCancel) {
  this.showInPosition(label, defaultValue, onValid, onCancel, event.getY(), event.getX());
}

activsoft.ajax.util.PromptPanelObject.prototype.showCenter = function(label, defaultValue, onValid, onCancel) {
  if (document.body.clientHeight==0) {
    this.showInPosition(label, defaultValue, onValid, onCancel, document.body.clientHeight / 2, document.body.clientWidth / 2 - 300);
  }
  else {
    this.showInPosition(label, defaultValue, onValid, onCancel, 150, document.body.clientWidth / 2 - 300);
  }
}  

activsoft.ajax.util.PromptPanelObject.prototype.showInPosition = function(label, defaultValue, onValid, onCancel, top, left) {
  this.onValid = onValid;
  this.onCancel = onCancel;
  this._input.value = defaultValue;
  while (this._label.childNodes.length>0) {
    this._label.removeChild(this._label.firstChild);
  }
  this._label.appendChild(document.createTextNode(label));
  YAHOO.util.Event.addListener(this._input.getAttribute("id"), "selectstart", function() {alert("test");return true;});
  
  var pan = document.getElementById('divAjaxPromptPanel');

  pan.style.top = top + "px";
  pan.style.left = left + "px";

  document.getElementById('divAjaxPromptOverlayPanel').style.display = 'block';
  pan.style.display = 'block';
  
}


