function dhtmlTree(ID, tree_refID, is_node, name, internal_name, short_description, value, order_num)
{
  this.ID = ID;
  this.tree_refID = tree_refID;
  this.is_node = is_node;
  this.name = name;
  this.internal_name = internal_name;
  this.short_description = short_description;
  this.value = value;
  this.order_num = order_num;

  this.toggle = toggleIt(ID);
  this.toggleRowsDown = toggleRowsDown(ID);
  this.imageChange = imageChange(ID);
}

function swapTree(section, rowName, show, inherit, cur_section)
{
  if(typeof cur_section == "undefined") cur_section = 0;
  if(typeof inherit == "undefined") inherit = false;

  if(cur_section < section)
  {
    if((this.is_node) && (this.sub))
    {
      for(var i in this.sub)
      {
        this.sub[i].swapTree(section, rowName, show, inherit, cur_section+1);
      }
    }

    return true;
  }

  if(cur_section > section && !inherit) return true;

  if(this.is_node)
  {
    this.toggle(this.ID, rowName, false, false, (show ? 1 : 0));

    if(inherit && this.sub)
    {
      for(var i in this.sub)
      {
        this.sub[i].swapTree(section, rowName, show, inherit, cur_section+1);
      }
    }
  }

  return true;
}

function swapTreeE(section, rowName, fieldName, cur_section)
{
  if(typeof cur_section == "undefined") cur_section = 0;

  if(cur_section < section)
  {
    if((this.is_node) && (this.sub))
    {
      for(var i in this.sub)
      {
        this.sub[i].swapTreeE(section, rowName, fieldName, cur_section+1);
      }
    }

    return false;
  }

  var rc = false, rc2 = false;
  var elem = document.getElementById(fieldName + "[" + this.ID + "]");

  if(elem) rc = (elem.checked == true);

  //if(rc) alert(fieldName + this.ID);

  if((this.is_node) && (this.sub))
  {
    for(var i in this.sub)
    {
      rc2 = this.sub[i].swapTreeE(section, rowName, fieldName, cur_section+1) || rc2;
    }

    if(!rc2) this.toggle(this.ID, rowName, false, false, 0);
  }

  return rc || rc2;
}


function toggleIt(ID, rowName, isFound, isFirst, newState)
{
  isFirst = (typeof isFirst == "undefined") ? false : isFirst;
  isFound = ((typeof isFound == "undefined") || (isFound === false)) ? (this.ID === ID) : isFound;
  //alert(this.ID);
  //alert(isFound);

  if(isFound)
  {
    //alert("isFound");
    var elem = null;
    // change elem!!!
    if(!isFirst)
    {
      isFirst = true;

      //newState = this.state = (this.state === 0) ? 1 : 0;  // only now to change the state!!!

      if(typeof newState == "undefined") newState = this.state = (this.state === 0) ? 1 : 0;  // only now to change the state!!!
      else
      {
        if(newState == this.state) return true;

        this.state = newState;
      }

      elem = document.getElementById("t_down" + this.ID);

      //alert("xxx");
      if(elem)
      {
        // + -> -, - -> +
        //alert("hier");
        //alert(elem.src);

        if(elem.src.indexOf("ominus.gif") >= 0)
        {
          elem.src = tree_images["plus"].src;
        }
        else if(elem.src.indexOf("ominusEnd.gif") >= 0)
        {
          elem.src = tree_images["plusEnd"].src;
        }
        else if(elem.src.indexOf("oplus.gif") >= 0)
        {
          elem.src = tree_images["minus"].src;
        }
        else if(elem.src.indexOf("oplusEnd.gif") >= 0)
        {
          elem.src = tree_images["minusEnd"].src;
        }
      }
    }
    else
    {
      elem = document.getElementById(rowName + this.ID); // "treeRow"

      if(elem)
      {
        // hide or show
        elem.style.display = (newState) ?  "" : "none";

        if(this.is_node && (newState == 1) && (this.state == 0))
        {
          newState = 0;
        }
      }
    }
  }

  if((this.is_node) && (this.sub))
  {
    for(var i in this.sub)
    {
      this.sub[i].toggle(ID, rowName, isFound, isFirst, newState);
    }
  }

  return true;
}

function dumpIt()
{
  alert("hello");
}

function toggleRowsDown(ID)
{

}

function imageChange(ID)
{

}

// --- normal function

function toggle_table(img, to_toggle, should_plus)
{
  var is_plus = null;
  var elem = null;
  var middle = /\/o([a-z]+)([A-Z][a-zA-Z]+)\.gif/;

  is_plus = ((typeof should_plus != "undefined") ? should_plus : (img.src.indexOf("/ominus") < 0));

  /*img.src = (is_plus) ?
    img.src.replace(/oplusEnd.gif/, "ominusEnd.gif") :
    img.src.replace(/ominusEnd.gif/, "oplusEnd.gif");*/

  if((img) && !middle.test(img.src)) return false;

  //alert(RegExp.$1);
  //alert(RegExp.$2);

  if(img) img.src = (is_plus) ? tree_images["minus" + RegExp.$2].src : tree_images["plus" + RegExp.$2].src;

  if(typeof to_toggle == "string")
  {
    elem = document.getElementById(to_toggle);

    if(elem) elem.style.display = (is_plus) ?  "" : "none";
  }
  else
  {
    for(var i in to_toggle)
    {
      elem = document.getElementById(to_toggle[i]);

      if(elem) elem.style.display = (is_plus) ?  "" : "none";
    }
  }
}

function getStyleDisplay(no_disp)
{
  return ((no_disp) ?  "none" : "");
}

function toggle_menu_table(img, to_toggle, should_plus)
{
  var is_plus = null;
  var elem = null;
  var to_test = /^(.+)_([0-9]+)_([0-9]+)$/;

  is_plus = ((typeof should_plus != "undefined") ? should_plus : (img.src.indexOf("/ominus") < 0));
  // ominusStartEnd.gif


  img.src = (is_plus) ?
    img.src.replace(/oplusStartEnd.gif/, "ominusStartEnd.gif") :
    img.src.replace(/ominusStartEnd.gif/, "oplusStartEnd.gif");

  //if(img) img.src = (is_plus) ? tree_images["minusEnd"].src : tree_images["plusEnd"].src;

  //admstart_0_10

  if(!to_test.test(to_toggle)) {alert("Error"); return false;}

  var i = 1 * RegExp.$2, end = 1 * RegExp.$3;
  var elem_name = RegExp.$1;

  if(end > 10000) return false;

  for(; i <= end; i++)
  {
    elem = document.getElementById(elem_name + "_" + i);

    if(!elem) continue;

    elem.style.display = (is_plus) ?  "" : "none";
  }

  return true;
}

function write2obj(obj_name, to_write, what, doc)
{
  if(!is_string(obj_name) || strlen(obj_name)===0 ||
     !is_string(to_write) || strlen(to_write)===0 ||
     !isset(what) || !(is_string(what) || is_number(what) || is_null(what))
    ) return false;

  if(!isset(doc)) doc = document;

  var obj = doc.getElementById(obj_name);

  if(!obj) return false;

  var delimiter = (is_number(what) ? '' : '"');

  //var to_eval = 'obj.' + to_write + ' = ' + delimiter + what + delimiter + ';';
  var to_eval = 'obj.' + to_write + ' = what;'

  try {
    eval(to_eval);
  } catch(not_eval)
  {
    alert('canīt write data to obj: ' + obj_name);
  }

  return true;
}

function setMouseCursor(new_cursor, obj, doc)
{
  if(!is_string(new_cursor) || strlen(new_cursor) === 0) new_cursor = '';

  if(!isset(obj))
  {
    if(!isset(doc)) doc = document;

    obj = doc.getElementsByTagName('body')[0];
  }

  if(is_object(obj)) obj.style.cursor = new_cursor;

  return true;
}

function setEvent_byObj(obj)
{
  if(!isset(obj)) return false;

  if(array_key_exists('onselectstart', obj) && isset(obj.onselectstart)) //IE route
	  obj.onselectstart=function(){return false}
  else if (array_key_exists('style', obj) && isset(obj.style.MozUserSelect)) //Firefox route
	  obj.style.MozUserSelect = 'none';
  else //All other route (ie: Opera)
	  obj.onmousedown=function(){return false}

  obj.style.cursor = 'default';


  return true;
}

function setEvent(obj_name, doc)
{
  if(is_string(obj_name) && strlen(obj_name)===0) return false;

  if(is_string(obj_name)) obj_name = [obj_name];

  if(!is_array(obj_name) || obj_name.length===0) return false;
  if(!isset(doc)) doc = document;

  var obj = null;

  for(var i in obj_name)
  {
    obj = doc.getElementById(obj_name[i]);

    if(!obj) return false;

    if(isset(obj.onselectstart)) //IE route
	    obj.onselectstart=function(){return false}
    else if (isset(obj.style.MozUserSelect)) //Firefox route
	    obj.style.MozUserSelect = 'none';
    else //All other route (ie: Opera)
	    obj.onmousedown=function(){return false}

    obj.style.cursor = 'default';
  }

  return true;
}

function toggle_disp_obj(obj_name, display, doc)
{
  if(is_string(obj_name) && strlen(obj_name)===0) return false;

  if(is_string(obj_name)) obj_name = [obj_name];

  if(!is_array(obj_name) || obj_name.length===0 || !is_bool(display)) return false;
  if(!isset(doc)) doc = document;

  var obj = null;

  for(var i in obj_name)
  {
    obj = doc.getElementById(obj_name[i]);

    if(!obj) return false;

    obj.style.display = (display) ? '' : 'none';
  }

  return true;
}

function toggle_disp_objs(obj_name, display, from, to, doc)
{
  if(is_string(obj_name) && strlen(obj_name)===0) return false;

  if(is_string(obj_name)) obj_name = [obj_name];

  if(!is_array(obj_name) || obj_name.length===0 || !is_bool(display) ||
     !is_number(from) || !is_number(to) ||
     from < 0 || to < from) return false;

  var i;

  for(; from <= to; from++)
  {
    for(i in obj_name) if(strlen(obj_name[i])!==0) toggle_disp_obj(obj_name[i] + from, display, doc);
  }

  return true;
}
