document.write('<LINK href="XWebDesignor.css" type=text/css rel=stylesheet>');

///////////////////////////////////////////////////////////////////////////////
// XWD Object
///////////////////////////////////////////////////////////////////////////////

XWD = function()
{
  this.modalWinU=null;
  this.modalWin=null;
}

XWD.prototype.isLocalMode = function(showMessage)
{
  var localMode=(location.href.substr(0,4)=="file");
  if (localMode && showMessage)
    this.alert("Accès impossible en prévisualisation.<br>Vous devez publier votre site pour utiliser cette fonctionnalité.");
  return localMode;
}

XWD.prototype.isGoodValue = function(type,value,showMessage) // type:email,numeric
{
  var emailC= /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/ ;
  var ok=false;
  if (type=="email")
  {
    var ok=emailC.test(value);
    if (!ok)
    {
      if (showMessage)
        this.alert("Adresse Email incorrecte","Erreur");
      return false;
    }
  }
  return true;
}

XWD.prototype.getURLTimeStamp = function ()
{
  var date = new Date();
  var time = date.getHours()+'/'+date.getMinutes()+'/'+date.getSeconds();
  return "&time="+escape(time);
}

XWD.prototype.getDataFromWeb = function (url)
{
  var xhr_object = null;

  if(window.XMLHttpRequest) // Firefox
    xhr_object = new XMLHttpRequest();
  else if(window.ActiveXObject) // Internet Explorer
    xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  else
  { // XMLHttpRequest non supporté par le navigateur
     alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
     return "";
  }

  xhr_object.open("GET", url, false);
  try
  {
    xhr_object.send(null);
  }
  catch(e){}
  if(xhr_object.readyState == 4)
    return xhr_object.responseText;
  else
    return "";
}

XWD.prototype.open = function(url,title,width,height)
{
  if (this.modalWinU == null)
    this.modalWinU = new DialogBox(true);
  if (typeof(title)=="undefined")
    title=url;
  if (typeof(width)!="undefined")
    this.modalWinU.setWidth(width);
  this.modalWinU.setTitle(title);
  if (typeof(height)!="undefined")
    this.modalWinU.setUrl(url,height);
  else
    this.modalWinU.setUrl(url);
  this.modalWinU.show();
  this.modalWinU.moveTo(-1,-1);
}

XWD.prototype.openForm = function(formId,title)
{
  var frm=document.getElementById(formId);
  if (frm==null)
    return;
  if (this.modalWinU == null)
    this.modalWinU = new DialogBox(true);
  if (typeof(title)=="undefined")
    title="Information";
  this.modalWinU.setTitle(title);
  var url=frm.action+"?";
  for (i=0;i<frm.length;i++)
    url+=frm.elements[i].name+"="+escape(frm.elements[i].value)+"&";
  this.modalWinU.setUrl(url);
  this.modalWinU.show();
  this.modalWinU.moveTo(-1,-1);
}

XWD.prototype.alert = function(text,title)
{
  if (this.modalWin == null)
    this.modalWin = new DialogBox(true);
  if (typeof(title)=="undefined")
    title="Information";
  this.modalWin.setTitle(title);
  this.modalWin.setContent(text);
  this.modalWin.show();
  this.modalWin.moveTo(-1,-1);
}

var xwd=new XWD();

///////////////////////////////////////////////////////////////////////////////
// Drag Object
///////////////////////////////////////////////////////////////////////////////

var Drag =
{
  obj: null,
  init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
  {
    o.onmousedown = Drag.start;

    o.hmode = bSwapHorzRef ? false : true;
    o.vmode = bSwapVertRef ? false : true;

    o.root = oRoot && oRoot != null ? oRoot : o;

    if (o.hmode && isNaN(parseInt(o.root.style.left)))
      o.root.style.left = "0px";

    if (o.vmode && isNaN(parseInt(o.root.style.top)))
      o.root.style.top = "0px";

    if (!o.hmode && isNaN(parseInt(o.root.style.right)))
      o.root.style.right = "0px";

    if (!o.vmode && isNaN(parseInt(o.root.style.bottom)))
      o.root.style.bottom = "0px";

    o.minX = typeof minX != 'undefined' ? minX : null;
    o.minY = typeof minY != 'undefined' ? minY : null;
    o.maxX = typeof maxX != 'undefined' ? maxX : null;
    o.maxY = typeof maxY != 'undefined' ? maxY : null;

    o.xMapper = fXMapper ? fXMapper : null;
    o.yMapper = fYMapper ? fYMapper : null;

    o.root.onDragStart = new Function();
    o.root.onDragEnd = new Function();
    o.root.onDrag = new Function();
  },
  start: function(e)
  {
    var o = Drag.obj = this;
    e = Drag.fixE(e);
    var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
    var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
    o.root.onDragStart(x, y);

    o.lastMouseX = e.clientX;
    o.lastMouseY = e.clientY;

    if (o.hmode)
    {
      if (o.minX != null)
        o.minMouseX = e.clientX - x + o.minX;

      if (o.maxX != null)
        o.maxMouseX = o.minMouseX + o.maxX - o.minX;
    }
    else
    {
      if (o.minX != null)
        o.maxMouseX = -o.minX + e.clientX + x;

      if (o.maxX != null)
        o.minMouseX = -o.maxX + e.clientX + x;
    }

    if (o.vmode)
    {
      if (o.minY != null)
        o.minMouseY = e.clientY - y + o.minY;

      if (o.maxY != null)
        o.maxMouseY = o.minMouseY + o.maxY - o.minY;
    }
    else
    {
      if (o.minY != null)
        o.maxMouseY = -o.minY + e.clientY + y;

      if (o.maxY != null)
        o.minMouseY = -o.maxY + e.clientY + y;
    }

    document.onmousemove = Drag.drag;
    document.onmouseup = Drag.end;

    return false;
  },
  drag: function(e)
  {
    e = Drag.fixE(e);
    var o = Drag.obj;

    var ey = e.clientY;
    var ex = e.clientX;
    var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
    var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
    var nx, ny;

    if (o.minX != null)
      ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);

    if (o.maxX != null)
      ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);

    if (o.minY != null)
      ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);

    if (o.maxY != null)
      ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

    nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
    ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

    if (o.xMapper)
      nx = o.xMapper(y)
    else if (o.yMapper)
      ny = o.yMapper(x)

    Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
    Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
    Drag.obj.lastMouseX = ex;
    Drag.obj.lastMouseY = ey;

    Drag.obj.root.onDrag(nx, ny);
    return false;
  },
  end: function()
  {
    document.onmousemove = null;
    document.onmouseup = null;
    Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
        parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
    Drag.obj = null;
  },
  fixE: function(e)
  {
    if (typeof e == 'undefined')
      e = window.event;

    if (typeof e.layerX == 'undefined')
      e.layerX = e.offsetX;

    if (typeof e.layerY == 'undefined')
      e.layerY = e.offsetY;
    return e;
  }
};

///////////////////////////////////////////////////////////////////////////////
// DialogBox Object
///////////////////////////////////////////////////////////////////////////////

DialogBox = function(isModal)
{
  if (arguments.length == 0)
    return;

  this.xdiv = document.getElementById("backDiv");

  if (this.xdiv == null)
  {
    DialogBox.createBlackDiv();
    this.xdiv = document.getElementById("backDiv");
  }
  this.isModal = isModal;

  this.container = document.createElement('div');
  this.container.className = DialogBox.className;
  this.container.style.zIndex=999999;
  this.container.dialogBox = this;

  var mainTable = document.createElement('table');
  mainTable.setAttribute('cellSpacing', '0');
  mainTable.setAttribute('cellPadding', '0');
  mainTable.setAttribute('border', '0');

  var tBodyM = document.createElement('tbody');
  var rowM = document.createElement('tr');
  var cellM = document.createElement('td');

  //*********** BEGIN Title TABLE ***********
  var titleTable = document.createElement('table');
  titleTable.setAttribute('cellSpacing', '0');
  titleTable.setAttribute('cellPadding', '0');
  titleTable.setAttribute('border', '0');
  titleTable.setAttribute('width', '100%');

  var tBodyT = document.createElement('tbody');
  var rowT = document.createElement('tr');
  var cellT = document.createElement('td');
  cellT.className = "tbLeft";
  rowT.appendChild(cellT);

  this.titleCell = document.createElement('td');
  this.titleCell.className = "Title";
  rowT.appendChild(this.titleCell);

  cellT = document.createElement('td');
  cellT.className = "tbRight";

  DialogBox.initCloseIcon();
  var closeIcon = document.createElement('img');
  closeIcon.src = DialogBox.closeIcon.src;
  closeIcon.alt = "Fermer";
  closeIcon.setAttribute('border', '0');
  closeIcon.dialogBox = this;

  var aLink = document.createElement('A');
  aLink.setAttribute('href', '#');
  aLink.appendChild(closeIcon);
  aLink.onclick = DialogBox.closeBox;

  cellT.appendChild(aLink);
  rowT.appendChild(cellT);

  tBodyT.appendChild(rowT);
  titleTable.appendChild(tBodyT);
  //*********** END Title TABLE ***********

  cellM.appendChild(titleTable);
  rowM.appendChild(cellM);
  tBodyM.appendChild(rowM);

  rowM = document.createElement('tr');
  cellM = document.createElement('td');
  cellM.className = "MainPanel";

  this.contentArea = document.createElement('div');
  this.contentArea.className = "ContentArea";
  cellM.appendChild(this.contentArea);

  rowM.appendChild(cellM);
  tBodyM.appendChild(rowM);
  mainTable.appendChild(tBodyM);
  this.container.appendChild(mainTable);
  document.body.appendChild(this.container);
  Drag.init(this.titleCell, this.container, 0, null, 0);
}

/************ BEGIN: Public Methods ************/

DialogBox.prototype.show = function()
{
  this.container.style.display = "block";
  this.xdiv.style.display = "block";
}

DialogBox.prototype.hide = function(ok)
{
  this.container.style.display = "none";
  this.xdiv.style.display = "none";
  if (ok)
  {
    if (this.callOK)
      if (this.returnData)
        this.callOK(this.returnData);
      else
        this.callOK();
  }
  else if (this.callCancel)
    this.callCancel();
}

DialogBox.prototype.moveTo = function(x, y)
{
  if (x == -1)
    x = Math.round((document.body.clientWidth - this.container.offsetWidth) / 2);

  if (y == -1)
    y = Math.round((document.body.clientHeight - this.container.offsetHeight) / 2) + document.body.scrollTop;
  this.container.style.left = x + "px";
  this.container.style.top = y + "px";
}

DialogBox.prototype.setTitle = function(title)
{
  this.titleCell.innerHTML = title;
}

DialogBox.prototype.setUrl = function(url, height)
{
  if (!this.iframe)
  {
    this.iframe = document.createElement('IFRAME');
    this.iframe.setAttribute('frameBorder', 'no');
    this.iframe.style.width = "100%";
    if (height)
      this.iframe.style.height = height;
    this.contentArea.parentNode.insertBefore(this.iframe, this.contentArea);
  }
  this.iframe.src = url;
  this.contentArea.style.height="0px";
}

DialogBox.prototype.getUrl = function()
{
  if (this.iframe)
  {
    var url = this.iframe.src;
    if (this.iframe.contentWindow)
    {
      try
      {
        url = this.iframe.contentWindow.location.href;
      }
      catch (e)
      {
      }
    }
    return url;
  }
}

DialogBox.prototype.setContent = function(htmlContent)
{
  this.contentArea.innerHTML = htmlContent;
}

DialogBox.prototype.setWidth = function(width)
{
  this.contentArea.style.width = (width-25) + "px";
}

DialogBox.prototype.setCallOK = function(callOK)
{
  this.callOK = callOK;
}

DialogBox.prototype.setCallCancel = function(callCancel)
{
  this.callCancel = callCancel;
}

DialogBox.prototype.getContentNode = function()
{
  return this.contentArea;
}
/************ END: Public Methods ************/


/************ BEGIN: Private Methods ************/
DialogBox.className = "DialogBox";
DialogBox.closeIcon = null;
DialogBox.maxDepth = 5;

DialogBox.initCloseIcon = function()
{
  if (DialogBox.closeIcon == null)
  {
    DialogBox.closeIcon = new Image();
    DialogBox.closeIcon.src = "close.gif";
  }
}

DialogBox.closeBox = function(e)
{
  if (!e)
    e = window.event;
  var node = e.target ? e.target : e.srcElement;
  var count = 0;

  while ((node != null) && (count < DialogBox.maxDepth))
  {
    if (node.dialogBox)
    {
      node.dialogBox.hide();
      return false;
    }
    node = node.parentNode;
    count++;
  }
  return false;
}

DialogBox.createBlackDiv = function(e)
{
  var xdiv = document.createElement("div");
  xdiv.id = "backDiv";
  xdiv.style.position = "absolute";
  xdiv.style.top = 0;
  xdiv.style.left = 0;
  xdiv.style.width = "100%";
  xdiv.style.height = "100%";
  xdiv.style.display = "none";
  xdiv.style.background = "black";
  var opacity = 20;
  xdiv.style.MozOpacity = (opacity / 100); //FireFox
  xdiv.style.opacity = (opacity / 100);    //Opera
  xdiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")"; //IE
  document.body.appendChild(xdiv);
  xdiv.style.zIndex = 99999;
}
