/******************************************************************************
 *
 *                   INDIGEN SOLUTIONS CODE PROPERTY
 *       The present javascript code is property of Indigen Solutions. This 
 *     code can only be used inside Internet/Intranet web sites located on 
 *  *web servers*, as the outcome of a licensed Indigen Solutions application 
 *  only. Any unauthorized use, reverse-engineering, alteration, transmission, 
 * transformation, facsimile, or copying of any means (electronic or not) is 
 *     strictly prohibited and will be prosecuted. Removal of the present 
 *              copyright notice is strictly prohibited
 *         Copyright (c) 2004 Indigen Solutions. All Rights Reserved.
 *
 * RCS Id                       $Id: buttons.js,v 1.9 2006/06/08 10:07:17 indigen Exp $
 * 
 ******************************************************************************/


/**
 * @todo
 *  Remove the "editor" ref when editing the CIB.
 *
 *
 */

var myEditorButtonViews = new Array();

/*
   ButtonsV Object.
*/

function ButtonsV() {
  this.base = CIB;
  this.base();
  // Attributes.
  this.buttons;
  this.sendData = null;
  this.back;

  // Methods.
  this.init = ButtonsV_init;
  this.write = ButtonsV_write;
  this.writeS = ButtonsV_writeS;
  this.manageButton = ButtonsV_manageButton;
}

// Init.

function ButtonsV_init(back) {
  this.back = back;
  for ( var key in myEditorButtons ) { 
    var button = myEditorButtons[key];
    if ( (back == "") && ((button.action == 'back') || (button.action == 'quit')) )
      continue;
    myEditorButtonViews.push(new ButtonsButton(button.index,
					       button.name,
					       button.action,
					       button.handler,
					       button.test,
					       this));
  }
  // Write.
  document.write(this.write());
}

// myButtons is global.
function ButtonsV_write() {
  var str = "";
  str += "<table class=\"oe_principalTable\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" >";
  str += "<tr><td class=\"oe_line\">&nbsp;</td></tr>";
  str += "<tr>"
  str += "<td align=\"center\" >"
  str += "<table  cellpadding=\"0\" cellspacing=\"0\" border=\"0\" >";
  str += "<tr>";
  if (myEditorButtonViews.length != 0)
    for (var i = 0; i < myEditorButtonViews.length; i++) {
      str += "<td style='text-align:center;padding:0px 4px;'>";
      str += myEditorButtonViews[i].write();
      str += "</td>";	
    }
  str += "</tr>";
  str += "</table>"
  str += "</td>";
  str += "</tr>";
  str += "</table>";
  return str;
}

function ButtonsV_writeS() {
    var str = "";
    str += "<td>";
    str += "<div class=\"oe_sep\"></div>";
    str += "</td>";
    return str;
}

function ButtonsV_manageButton(index) {
  var button = myEditorButtonViews[index];
  if ( button.action == "back" )
    document.location.href = this.back;
  else if ( button.action == "close" ) {
    window.close();
    window.parent.close();
  }
  eval(button.handler + "('" + button.action + "')");
  eval("var tmp = " + button.test);
  button.enable(index, tmp);
}

/*
  ButtonsButton Object.
*/

function ButtonsButton(index, name, action, handler, test, buttons) {
  this.index = index;
  this.name = name;
  this.action = action;
  this.handler = handler;
  this.test = test;
  this.buttons = buttons;
  this.enable = ButtonsButton_enable;
  this.write = ButtonsButton_write;
}

function ButtonsButton_write() {
  var str = "";
  var tmp = eval(this.test);
  str += "<div ";
  str += " class=\"oe_but\" ";
  str += "id="+ this.name +  "_button ";
  str += " onmouseover=\"this.className='oe_but oe_butH'\" ";
  str += "onmouseout=\"this.className='oe_but'\" ";
  if (!tmp)
    str += "disabled=\"true\"";
  else
    str += "onclick=\"" + this.buttons.getStrRef() + ".manageButton(" + this.index + ")\"  ";
  str += ">";
  str += this.name;
  str += "</div>";
  return str;
}

function ButtonsButton_enable(index, flag) {
  var obj = document.getElementById(this.name + "_button");
  if ( !obj )
    return;

  obj.disabled = !flag;
  var func = this.buttons.getStrRef() + ".manageButton(" + index + ")";
  if (flag)
    obj.onclick = function () {
      eval(func);
    }
  else
    obj.onclick = "";
}

/* 
   Global
*/


function reloadEditorButtons() {
  for (var i = 0; i < myEditorButtonViews.length; i++) {
    eval("var tmp = " + myEditorButtonViews[i].test);
    myEditorButtonViews[i].enable(i ,tmp);
  }
}

