/*****************************************************************************
 * $Header$
 * $Author$
 * $Revision$
 * $Date$
 *
 * controls.js
 *
 * Copyright: Neolane 2001-2007
 *****************************************************************************/

//[of]:DebugConsole
/** DebugConsole
  *
  * Widget used to display the context when __debug=1 is avaiable on the URL. */
function DebugConsole(controller)
{
  this.controller = controller
  controller.registerObserver("/", this, this.onNodeChange, "DebugConsole", controller.OBSERVE_CHILDREN)
}

/** One node has changed in the context */
DebugConsole.prototype.onNodeChange = function()
{
  if ( this.htmlElement == null )
  {
    this.htmlElement = document.getElementById("DebugConsole")
    if ( this.htmlElement == null )
    {
      this.htmlElement = document.body.appendChild(document.createElement("pre"));
      this.htmlElement.id = "DebugConsole"
    }
  }

  var strXMLContext = toXMLString(this.controller.ctx)
  this.htmlElement.innerHTML = "" /* reset the content */
  this.htmlElement.appendChild(document.createTextNode(strXMLContext))
}
//[cf]
//[of]:RichTextArea
/** RichTextArea
  *
  * Widget used to add an Html Text Area*/
  
  
  /** Constructor
  *
  * @controller       Main controller
  * @id               id of the td i want to be in
  * @xpath            the xpath of the richtextarea
  * @pos              position of the tool bar */ 
function richTextArea(controller, id, xpath, pos)
{
  this.controller = controller;
  this.htmlParent = document.getElementById(id);
  this.id = id + "_richTextArea";
  this.xpath = xpath;
  this.toolbarPosition = pos;
  this.countTrigger = 0;
  this.countCurrent = 0;
  this.isTimer = false;
  this.tableIframe = 0;
  this.divTextArea = 0;
  this.iframe = 0;
  this.divToolBar = 0;
  this.arrayFct = new Array();
  this.init();
  controller.registerObserver(xpath, this, this.onNodeChange, this.id, controller.OBSERVE_XPATH)
}

  
  /** Fct called on notify
  *
  * @xpath              the xpath of the change 
  * @value              value to set in */ 
richTextArea.prototype.onNodeChange = function (xpath, value)
{
  this.setContent(value);
}

/** Fonction called when the form is submit
  *
  *                             */

richTextArea.prototype.onSubmit = function()
{
  this.controller.setValue(this.xpath, this.getContent(), this.id);
  return true;
}


/** Initialisation fonction
  *
  *                             */ 
richTextArea.prototype.init = function ()
{
  this.initFunction();
  richTextArea.prototype.getMyArea(this.id, this) // register this richTextArea
  this.initTextArea();

  if (document.designMode && !isOpera()) 
  {
    this.divTextArea.style['display'] = "none";
    this.initBuildTable()
    this.iframe = document.getElementById(this.id);
    this.initAfterLoad();
  }
}


/** Add the function to build element to the arrayFCT
  *
  *                             */ 
richTextArea.prototype.initFunction = function ()
{
  this.arrayFct['bold'] = this.makeButton;
  this.arrayFct['italic'] = this.makeButton;
  this.arrayFct['underline'] = this.makeButton;
  this.arrayFct['justifyleft'] = this.makeButton;
  this.arrayFct['justifyfull'] = this.makeButton;
  this.arrayFct['justifycenter'] = this.makeButton;
  this.arrayFct['justifyright'] = this.makeButton;
  this.arrayFct['source'] = this.makeSource;
}

/** Make the textArea
  *
  *                             */ 
richTextArea.prototype.initTextArea = function ()
{
  this.divTextArea = document.createElement("div");
  this.htmlParent.appendChild(this.divTextArea);
  this.textArea = document.createElement("textarea");
  this.textArea.id = this.id + '_textarea';
  this.textArea.onblur = this.makeEvent(this.id, this.refresh);
  this.textArea.className = "textarea";
  this.divTextArea.appendChild(this.textArea);
}


/** Make the Table for the Iframe and the Iframe
  *
  *                             */ 
richTextArea.prototype.initBuildTable = function()
{
  this.tableIframe = document.createElement('table');
  this.tableIframe.appendChild(document.createElement('tbody'));
  this.tableIframe.lastChild.appendChild(document.createElement("tr"));
  if (this.toolbarPosition == 0)
  {
    this.divBar = this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td"));
    this.tableIframe.lastChild.appendChild(document.createElement("tr"));
    this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td")).appendChild(this.makeIframe("type"));
  }
  else if (this.toolbarPosition == 1)
  {
    this.divBar = this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td"));
    this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td")).appendChild(this.makeIframe("type"));
  }
  else if (this.toolbarPosition == 2)
  {
    this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td")).appendChild(this.makeIframe("type"));
    this.tableIframe.lastChild.appendChild(document.createElement("tr"));
    this.divBar = this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td"));
  }
  else if (this.toolbarPosition == 3)
  {
    this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td")).appendChild(this.makeIframe("type"));
    this.divBar = this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td"));
  }
  else
  {
    this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td")).appendChild(this.makeIframe("type"));
    this.divBar = this.tableIframe.lastChild.lastChild.appendChild(document.createElement("td"));
    this.divBar.style['display'] = 'none';
  }
  
  this.divBar.className = "richAreaToolbar"
  this.htmlParent.appendChild(this.tableIframe);
}

/** STATIC fct that let the browser build the iframe document
  * @id               id of the iframe
  *                             */ 
richTextArea.prototype.initAfterLoad = function ()
{
  var idoc;
  var w3c = true;
  if (this.iframe.contentDocument !== undefined)
    idoc = this.iframe.contentDocument;
  else if (this.iframe.contentWindow !== undefined)
  {
    idoc = this.iframe.contentWindow;
    w3c = false;
  }

  if (idoc == null || idoc === undefined)
  {
    window.setTimeout(this.makeEvent(this.id, this.initAfterLoad) , 250);
  }
  else 
  {
    if (w3c == false)
    {
      idoc = idoc.document;
      if (window[this.id].document.body == null || window[this.id].document.body == undefined)
        {
          window.setTimeout(this.makeEvent(this.id, this.initAfterLoad) , 500);
          return ;
        }
    }

    idoc.open();
    idoc.close();
    idoc.designMode = w3c ? "on" : "On";
    this.setContent(this.controller.getValue(this.xpath));
    this.addEvents();
    this.initIframeStyle(idoc);
  }
}


/** Make Set the style of the Iframe Body and get it from the .richTextArea_Body in the document.styleSheets
  *
  *                             */ 
richTextArea.prototype.initIframeStyle = function (idoc)
{
  var rule;
  for (var i = 0; i < document.styleSheets.length; i++)
  {
    var rules;
    if (isIE())
      rules = document.styleSheets[i].rules;
    else
      rules = document.styleSheets[i].cssRules;
    for (var j = 0; j < rules.length; j++)
      if (rules[j].selectorText == ".richTextArea_Body")
        rule = rules[j];
  }
  var  style_node = idoc.createElement("style");
  style_node.setAttribute("type", "text/css");
  style_node.setAttribute("media", "screen");
  if (isSafari())
  {
    var head = idoc.createElement("head");
    idoc.firstChild.insertBefore(head, idoc.firstChild.firstChild);
  }
  if (idoc.getElementsByTagName("head").length > 0)
  {
    idoc.getElementsByTagName("head")[0].appendChild(style_node);
    if (isIE() && idoc.styleSheets && idoc.styleSheets.length > 0)
    {
      var last_style_node = idoc.styleSheets[idoc.styleSheets.length - 1];
      if (typeof(last_style_node.addRule) == "object")
      {
        last_style_node.addRule(".richTextArea_Body", rule.style.cssText);
        last_style_node.addRule("P", "margin: 0;");
      }
    }
    else
      style_node.appendChild(document.createTextNode(rule.cssText));
  }
  if (idoc.body)
  {
    idoc.body.className = "richTextArea_Body";
  }
}

/** Make a button
  * @type      type of the element you want to add
  *                             */
richTextArea.prototype.addElement = function (type)
{
  if (document.designMode /*&& !isOpera()*/) 
  {
    if (this.arrayFct[type] !== undefined)
    {
      this.divBar.appendChild(this.arrayFct[type].call(this, type));
      if (this.toolbarPosition & 1)
        this.divBar.appendChild(document.createElement("br"));
    }
  }
}

/** Change the width of the RichtextArea
  * @value                      value you want to set
  *                             */
richTextArea.prototype.setWidth = function(value)
{
  this.textArea.style["width"] = value;
  if (document.designMode && !isOpera())
    this.iframe.style["width"] = value;
}

/** Change the height of the RichtextArea
  * @value                      value you want to set
  *                             */
richTextArea.prototype.setHeight = function(value)
{
  this.textArea.style["height"] = value;
  if (document.designMode && !isOpera())
    this.iframe.style["height"] = value;
}

/** Change the align of the RichtextArea
  * @value                      value you want to set
  *                             */
richTextArea.prototype.setAlign = function(value)
{
  this.htmlParent.style["align"] = value;
  this.divTextArea.style['align'] = value;
  if (document.designMode && !isOpera())
  {
    this.tableIframe.lastChild.firstChild.firstChild.style["align"] = value;
    this.tableIframe.lastChild.lastChild.firstChild.style["align"] = value;
    this.tableIframe.lastChild.lastChild.lastChild.style["align"] = value;
    this.tableIframe.lastChild.firstChild.lastChild.style["align"] = value;
  }
}

/** Change the valign of the RichtextArea
  * @value                      value you want to set
  *                             */
richTextArea.prototype.setVAlign = function(value)
{
  this.htmlParent.style["valign"] = value;
  this.divTextArea.style['valign'] = value;
  if (document.designMode && !isOpera())
  {
    this.tableIframe.lastChild.firstChild.firstChild.style["valign"] = value;
    this.tableIframe.lastChild.lastChild.firstChild.style["valign"] = value;
    this.tableIframe.lastChild.lastChild.lastChild.style["valign"] = value;
    this.tableIframe.lastChild.firstChild.lastChild.style["valign"] = value;
  }
}

/** Make a button
  * @type      type of the button (!!!used to execCommand!!!)
  *                             */ 
richTextArea.prototype.makeButton = function (type) 
{
  var input = document.createElement("img");
  input.src = "/xtk/img/form/"+ type + ".png";
  input.className = type;
  input.id = this.id + "_" + type;
  input.onmousedown = this.makeEvent(this.id, this.buttonOnMouseDown);
  input.onclick = this.makeEvent(this.id, this.buttonOnClick);

  var link = document.createElement("div");

  link.className = "button"
  link.appendChild(input)
  return (link);
}

/** Make a button
  * @type      nearly unused
  *                             */ 
richTextArea.prototype.makeSource = function (type) 
{
  var input = document.createElement("img");
  input.className = type;
  input.src = "/xtk/img/form/"+ type + ".png";
  input.id = this.id + "_" + type;
  input.onmousedown = this.makeEvent(this.id, this.buttonOnMouseDown);
  input.onclick = this.makeEvent(this.id, this.SourceOnClick);
  var input2 = input.cloneNode(true);
  this.divTextArea.appendChild(input2); 
  input2.onmousedown = this.makeEvent(this.id, this.buttonOnMouseDown);
  input2.onclick = this.makeEvent(this.id, this.SourceOnClick);
  var link = document.createElement("div");
  link.className = "button"
  link.appendChild(input)  
  return (link);
}


/** Make the iframe
  * @type              UNUSED
  *                             */ 
richTextArea.prototype.makeIframe = function (type)
{
  this.iframe = document.createElement("iframe");
  this.iframe.id = this.id;
  this.iframe.className = "richTextArea_Frame";
  return (this.iframe);
}

/** Set the EventsHandler to the iframe
  *
  *                             */ 
richTextArea.prototype.addEvents = function ()
{
  if (this.iframe.contentDocument && this.iframe.contentDocument.addEventListener)
  {
    this.iframe.contentDocument.addEventListener("keyup", this.makeEvent(this.id, this.askRefresh), false);
    this.iframe.contentDocument.addEventListener("mouseup", this.makeEvent(this.id, this.askRefresh), false);
  }
  else if(this.iframe.contentWindow && this.iframe.contentWindow.document &&
           this.iframe.contentWindow.document.attachEvent)
  {
    this.iframe.contentWindow.document.attachEvent("onkeyup", this.makeEvent(this.id, this.askRefresh))
    this.iframe.contentWindow.document.attachEvent("onmouseup", this.makeEvent(this.id, this.askRefresh))
  }
}

/** Handler of mousedown event on button. Do nothing but avoid some default problem with IE
  *
  *                             */ 
richTextArea.prototype.buttonOnMouseDown = function(e)
{
  var evt = e ? e : window.event; 
  if (evt.returnValue)
    evt.returnValue = false;
  else if (evt.preventDefault)
    evt.preventDefault( );
  return false;
}

/** Handler of click event on Style buttons.
  *
  *                             */ 
richTextArea.prototype.buttonOnClick = function (e) 
{
  var target;
  var event = e || window.event;
  if(event.target)
    target = event.target;
  else
    target = event.srcElement;

  if (window[this.id])
    window[this.id].document.execCommand(target.className, false, null);
  else
    document.getElementById(this.id).contentWindow.document.execCommand(target.className, false, null); 
  this.askRefresh(e);
  return false;
}

/** Handler of click event on Source button.
  *
  *                             */ 
richTextArea.prototype.SourceOnClick = function (e) 
{
  var target;
  var event = e || window.event;
  if(event.target)
    target = event.target;
  else
    target = event.srcElement;

  
  var str = this.getContent();
  var tmp = this.tableIframe.style['display'];
  this.tableIframe.style['display'] = this.divTextArea.style['display'];
  this.divTextArea.style['display'] = tmp;
  this.setContent(str);
  this.askRefresh(e);
  return false;
}

/** return the content of the RichTextArea.
  *
  *                             */ 
richTextArea.prototype.getContent = function() 
{
  if (document.designMode && !isOpera() && this.divTextArea.style['display'] == 'none') 
  {
    var str;
    if (window[this.id])
      str = window[this.id].document.body.innerHTML;
    else
      str = document.getElementById(this.id).contentWindow.document.body.innerHTML;
    if (isIE() && (str == '<P>&nbsp;</P>' || str == '<DIV>&nbsp;</DIV>'))
      str = "";
    else if (isGecko() && str == '<br>')
      str = "";
    else if (isSafari() && (str == '<div><br></div>' || str == '<br>'))
      str = "";
    return (str);
  } 
  else
  {
    return document.getElementById(this.id + "_textarea").value;
  }
}

/** Set the value of the RTA.
  *
  *                             */
richTextArea.prototype.setContent = function(value) 
{
  if (document.designMode && !isOpera() && this.divTextArea.style['display'] == 'none') 
  {
    if (this.iframe.contentDocument && this.iframe.contentDocument.body)
      this.iframe.contentDocument.body.innerHTML = value;
    else if(this.iframe.contentWindow && this.iframe.contentWindow.document
            && this.iframe.contentWindow.document.body)
      this.iframe.contentWindow.document.body.innerHTML = value;
    else
      this.initAfterLoad(this.id);
  }
  else
    document.getElementById(this.id + "_textarea").value = value;
  return (true);
}

/** STATIC FONCTION, retrieve or set a richtextarea associated to an id
  * @id              The RTA id that we want to retrieve
  * @val             if undefined the fct will return the RTA associated to the param id.
  *                  else it will store val and associated it to val.id
  *                             */
richTextArea.prototype.getMyArea = function (id, val)
{
  if (this.hashRichTextArea === undefined)
    this.hashRichTextArea = new Array();
  if (val !== undefined)
  {
    this.hashRichTextArea[val.id] = val;
  }
  else
    return this.hashRichTextArea[id];
  return null;
}

/** Fct to notifiate the controler from changes
  *
  *                             */
richTextArea.prototype.refresh = function(e)
{
  if (this.countCurrent != this.countTrigger)
  {
    this.isTimer = true;
    this.countTrigger = this.countCurrent;
    window.setTimeout(this.makeEvent(this.id, this.refresh), 500);
  }
  else
  {
    this.isTimer = false;
    this.controller.setValue(this.xpath, this.getContent(), this.id);
  }
}

/** Fct to temporate the notification
  *
  *                             */
richTextArea.prototype.askRefresh = function(e)
{
  this.countCurrent++;

  if (this.isTimer == false)
  {
    this.isTimer = true;
    this.countTrigger = this.countCurrent;
    window.setTimeout(this.makeEvent(this.id, this.refresh), 500);
  }
  return false;
}

/** STATIC FONCTION.  it return a function that you should use for eventhandling setting.
  *                   It helps calling richTextArea methode with the RTA associated to the @id
  * @id               The RTA id that will instanciate the fct param
  * @fct              the fonction you want to be called with the rta with the param id
  *                             */
richTextArea.prototype.makeEvent = function(id, fct)
{
  return function(e)
  {
    fct.call(richTextArea.prototype.getMyArea(id), e);
    return false;
  };
}
//[cf]

/** Functions to set the width of a table's tds to the size of the biggest one
  *
  *                                 */
function AutoAdjustMatrixColumns(id)
{
  var table = document.getElementById(id);
  var ths = table.getElementsByTagName ('TH');
  var max = 0;
  for (var i = 1; i < ths.length; i++)
  {
    if (max < ths[i].offsetWidth)
      max = ths[i].offsetWidth;
  }
  for (var i = 1; i < ths.length; i++)
  {
    ths[i].width = max + "px";
  }
}

// ---------------------------------------------------------------
// Calls a function when the enter key is pressed
// ---------------------------------------------------------------
function onEnterPressed(e, func) 
{
  var evt = (e) ? e:(window.event) ? window.event : null;
  if( evt )
  {
    var cKey = (evt.charCode) ? evt.charCode : 
                ((evt.keyCode) ? evt.keyCode : 
                  ((evt.which) ? evt.which : 0));
    if( cKey == "13" )
      return func();
  }
  return true;
}


// ---------------------------------------------------------------
// Performs a click
// @option URL or object ID list or javascript code
// ---------------------------------------------------------------
function PerformClickAction(ctx, action, target, transition, option, param, event)
{
  // Use a form to pass URL parameters.
  // If the form already exists, reuse it.
  if( action == "url" )
  {
    /** Add a value as an hidden input in a form */
    function addInputValue(form, strName, strValue)
    {
      input = document.createElement("input")
      input.type = "hidden"
      input.name = strName
      input.value = strValue
      form.appendChild(input)
    }

    if( document.getElementById("URLForm") != null )
    {
      var ndForm = document.getElementById("URLForm")
      // Delete previous form inputs 
      var ndFormChild = ndForm.firstChild
      while( ndFormChild != null )
      {
        if( ndFormChild.tagName == "INPUT" )
          ndFormChild = ndForm.removeChild(ndFormChild)
        ndFormChild = ndFormChild.nextSibling
      }
    }
    
    var ndURLForm = document.createElement("form")
    ndURLForm.setAttribute("id", "URLForm")
    
    ndURLForm.method = "GET"
    ndURLForm.action = option
    ndURLForm.target = target
    
    for (var i=0; i < param.length; i++)
      addInputValue(ndURLForm, param[i].name, param[i].value)
    
    document.body.appendChild(ndURLForm)
    ndURLForm.submit()
  }
  else
  {
    for (var i=0; i < ctx.length; i++)
      document.controller.setValue(ctx[i].xpath, ctx[i].value)
    
    if( action == "refreshData" )
    {
      var jsString = ""
      for ( var i in option )
        jsString += "document.lstData"+ option[i] +".load(true);"
      return eval(jsString)
    }
    else if( action == "javascript" )
      return eval(option)
    else
      return document.controller.submit(action, target, transition)
  }
}
