//TreeCB object functions
var Layer=200;
function CTreeCB(name, valueName)
{
  this.name=name;
  this.valueName=valueName;
  this.items=new Array();
  this.width=300;
  this.height=20;
  this.boxHeight=200;
  this.boxWidth=200;
  this.boxDown=true;
  this.color='black';
  this.backgroundColor='white';
  this.FontFamily='Ms Sans Serif, Arial, Helvetica';

  this.imgAlias='';
  this.imgOpenBtn='alias/dropdown_btn.bmp';
  this.imgLinesPlus='alias/lines_plus.gif';
  this.imgLinesPlusTop='alias/lines_plus_top.gif';
  this.imgLinesPlusBottom='alias/lines_plus_bottom.gif';
  this.imgLinesPlusNo='alias/lines_plus_no.gif';
  this.imgLinesMinus='alias/lines_minus.gif';
  this.imgLinesMinusTop='alias/lines_minus_top.gif';
  this.imgLinesMinusBottom='alias/lines_minus_bottom.gif';
  this.imgLinesMinusNo='alias/lines_minus_no.gif';
  this.imgLines='alias/lines.gif';
  this.imgLinesTop= 'alias/lines_top.gif';
  this.imgLinesBottom= 'alias/lines_bottom.gif';
  this.imgLinesNo='alias/lines_no.gif';
  this.imgLinesNoNo='alias/lines_no_no.gif';

  this.imgOpenedDef='alias/opened.gif';
  this.imgClosedDef='alias/closed.gif';
  this.imgLeafDef='alias/leaf.gif';

  this.Create=TreeCBCreate; 
  this.AddItem=TreeCBAddItem;
  this.ShowBox=TreeCBShowBox;
  this.HideBox=TreeCBHideBox;
  this.GetImgSrc=TreeCBGetImgSrc;

  return this;
}

function TreeCBAddItem(id, text)
{
  var item = new CTreeCBItem(this, 0, id, text);
  this.items[this.items.length] = item;
  return item;
}

function TreeCBGetImgSrc(img)
{
  if(img.substring(0,6)=='alias/')
    img=this.imgAlias+img.substring(6,img.length);
  return img;
}

//TreeCBItem object functions
function CTreeCBItem(TreeCB, Level, id, text)
{
  this.TreeCB=TreeCB;

  this.parent=Level;
  if(Level!=0)
    this.name=Level.name+'.items['+Level.items.length+']';
  else
    this.name=TreeCB.name+'.items['+TreeCB.items.length+']';
  this.items=new Array();

  this.id=id;
  this.text=text;
  this.imgFolderOpened='';
  this.imgFolderClosed='';
  this.imgLeaf='';
  
  this.AddItem=TreeCBItemAddItem;
  this.Expand=TreeCBItemExpand;
  this.GetLinesImg=TreeCBItemGetLinesImg;
  this.Collapse=TreeCBItemCollapse;
  this.Select=TreeCBItemSelect;
  this.GetImg=TreeCBItemGetImg;

  return this;
}

function TreeCBItemAddItem(id, text)
{
  var item = new CTreeCBItem(this.TreeCB, this, id, text);
  this.items[this.items.length] = item;
  return item;
}

function TreeCBItemGetLinesImg(back, opened)
{
  var parent=this.parent;
  if(parent==0)
    parent=this.TreeCB;

  if(back>0)
    back=0;

  var img;
  if(back==0)
  {
    if(this.items.length>0)
    {
      if(this==parent.items[0])
      {
          if(this.parent==0)
            if(this==parent.items[parent.items.length-1])
              img=opened==1?this.TreeCB.imgLinesPlusNo:this.TreeCB.imgLinesMinusNo;
            else
              img=opened==1?this.TreeCB.imgLinesPlusTop:this.TreeCB.imgLinesMinusTop;
          else
            img=opened==1?this.TreeCB.imgLinesPlus:this.TreeCB.imgLinesMinus;
      }
      else 
      {
        if(this!=parent.items[parent.items.length-1])
          img=opened==1?this.TreeCB.imgLinesPlus:this.TreeCB.imgLinesMinus;
        else
          img=opened==1?this.TreeCB.imgLinesPlusBottom:this.TreeCB.imgLinesMinusBottom;
      }                                      
    }
    else
    {
      if(this==parent.items[0])
      {
        if(this.parent==0)
          if(this==parent.items[parent.items.length-1])
            img=this.TreeCB.imgLinesNoNo;
          else
            img=this.TreeCB.imgLinesTop;
        else
          if(this==parent.items[parent.items.length-1])
            img=this.TreeCB.imgLinesBottom;
          else
            img=this.TreeCB.imgLines;
      }
      else if(this==parent.items[parent.items.length-1])
        img=this.TreeCB.imgLinesBottom;
      else
        img=this.TreeCB.imgLines;
    }
  }
  else
  {
    var item=this;
    while(back<0 && item.parent!=0)
    {
      item=item.parent;
      back++;
    }
    parent=item.parent;
    if(parent==0)
      parent=this.TreeCB;
    if(item==parent.items[parent.items.length-1])
      img="";
    else
      img=this.TreeCB.imgLinesNo;
  }
  return this.TreeCB.GetImgSrc(img);
}

function TreeCBItemGetImg(opened)
{
  var img;

  if(this.items.length>0)
  {
    if(opened==1)
    {
      if(this.imgLeaf.length==0)
        img=this.TreeCB.imgOpenedDef;
      else
        img=this.imgOpened;
    }
    else
    {
      if(this.imgLeaf.length==0)
        img=this.TreeCB.imgClosedDef;
      else
        img=this.imgClosed;
    }
  }
  else
  {
    if(this.imgLeaf.length==0)
      img=this.TreeCB.imgLeafDef;
    else
      img=this.imgLeaf;
  }
  return this.TreeCB.GetImgSrc(img);
}

function TreeCBItemExpand()
{
  var text="";

  text+='<table border=0 cellspacing=0 cellpadding=0><tr>';

  var img=this.GetImg(1);
  var pre='<td height=22 width=20 ><img src=\"'+img+'\"></td>';
  
  img=this.GetLinesImg(0, 0);
  pre='<td height=22 width=20 onclick=\"parent.'+this.name+'.Collapse();\" background=\"'+img+'\" style=\"background-position:center center;background-repeat:no-repeat;\"><span style=\"width:22\"></span></td>'+pre;

  var parent=this.parent;

  back=-1;
  while(parent!=0)
  {
    img=this.GetLinesImg(back, 0);
    pre='<td height=22 width=20 background=\"'+img+'\" style=\"background-position:center center;background-repeat:no-repeat;\"><span style=\"width:22\"></span></td>'+pre;
    back--;
    parent=parent.parent;
  }
  text+=pre;
  text+="<td onclick='parent."+this.name+".Select();' style='font-family :"+this.TreeCB.FontFamily+";cursor:hand;color:"+this.TreeCB.color+"'><nobr>"+this.text+"</nobr></td>";
  text+='</tr></table>';

  for(i=0; i<this.items.length; i++)
  {
    text+='<span id=\"'+this.items[i].name+'\"><table border=0 cellspacing=0 cellpadding=0><tr>';
   
    img=this.items[i].GetImg(0);
    pre='<td height=22 width=20 ><img src=\"'+img+'\"></td>';
 
    img=this.items[i].GetLinesImg(0, 1);
    pre='<td height=22 width=20 onclick=\"parent.'+this.items[i].name+'.Expand();\" background=\"'+img+'\" style=\"background-position:center center;background-repeat:no-repeat;\"><span style=\"width:22\"></span></td>'+pre;
    parent=this.items[i].parent;

    back=-1;
    while(parent!=0)
    {
      img=this.items[i].GetLinesImg(back, 0);
      pre='<td height=22 width=20 background=\"'+img+'\" style=\"background-position:center center;background-repeat:no-repeat;\"><span style=\"width:22\"></span></td>'+pre;
      back--;
      parent=parent.parent;
    }
    text+=pre;
  
    text+="<td onclick='parent."+this.items[i].name+".Select();' style='font-family :"+this.TreeCB.FontFamily+";cursor:hand;color:"+this.TreeCB.color+"'><nobr>"+this.items[i].text+"</nobr></td>";
    text+='</tr></table></span>';
  }
  document.all[this.name].innerHTML=text;
}


function TreeCBItemCollapse()
{
  var text="";

  text+='<table border=0 cellspacing=0 cellpadding=0><tr>';

  var img;

  img=this.GetImg(0);
  pre='<td height=22 width=20 ><img src=\"'+img+'\"></td>';

  img=this.GetLinesImg(0, 1);
  pre='<td height=22 width=20 onclick=\"parent.'+this.name+'.Expand();\" background=\"'+img+'\" style=\"background-position:center center;background-repeat:no-repeat;\"><span style=\"width:22\"></span></td>'+pre;

  var parent=this.parent;

  back=-1;
  while(parent!=0)
  {
    img=this.GetLinesImg(back, 1);
    pre='<td height=22 width=20 background=\"'+img+'\" style=\"background-position:center center;background-repeat:no-repeat;\"><span style=\"width:22\"></span></td>'+pre;
    back--;
    parent=parent.parent;
  }
            
  text+=pre;
  text+="<td onclick='parent."+this.name+".Select();' style='font-family :"+this.TreeCB.FontFamily+";cursor:hand;color:"+this.TreeCB.color+"'><nobr>"+this.text+"</nobr></td>";
  text+='</tr></table>';

  document.all[this.name].innerHTML=text;
}

function TreeCBItemSelect()
{
  this.TreeCB.HideBox();
  document.all[this.TreeCB.name+'Bar'].value=this.text;
  document.all[this.TreeCB.valueName].value=this.id;
}


function TreeCBCreate()
{
  var text="<input type=hidden name='"+this.valueName+"'>";
  text+="<input type=text readonly style='font-family :"+this.FontFamily+";margin-bottom:-1;margin-top:-1;cursor:hand;padding-right:20;color:"+this.color+";border: 1px solid black;width:"+this.width+";height:"+this.height+";background-image:url("+this.GetImgSrc(this.imgOpenBtn)+");background-repeat:no-repeat;background-position:center right;background-color:"+this.backgroundColor+"' id='"+this.name+"Bar' onClick='"+this.name+".ShowBox()'>";

  text+="<span style='position:absolute;display:none;z-index:"+Layer+";background-color:transparent;' id='"+this.name+"Filler' onclick='"+this.name+".HideBox()' onkeypress='alert(1)';></span>";
  Layer++;
  text+="<span id='"+this.name+"Box' style='overflow:auto;background-color:"+this.backgroundColor+";width:"+this.boxWidth+";height:"+this.boxHeight+";z-index:"+Layer+";border:1px solid black;position:absolute;text-align:left;display:none;' onkeypress='alert(1)';>";

  for(i=0; i<this.items.length; i++)
  {
    text+="<span id='"+this.items[i].name+"'><table border=0 cellspacing=0 cellpadding=0><tr>";

    var img=this.items[i].GetLinesImg(0, 1);
    text+="<td height=22 width=20 onclick='parent."+this.items[i].name+".Expand();' background='"+img+"' style='background-position:center center;background-repeat:no-repeat;cursor:hand;'><span style=\"width:22\"></span></td>";

    img=this.items[i].GetImg(0);
    text+='<td height=22 width=20 ><img src=\"'+img+'\"></td>';

    text+="<td onclick='parent."+this.items[i].name+".Select();' style='font-family :"+this.FontFamily+";cursor:hand;color:"+this.color+"'><nobr>"+this.items[i].text+'</nobr></td>';
    text+='</tr></table></span>';
  }                                       
  
  text+="</span>";
  Layer++;

  document.write(text);
}

function TreeCBShowBox()
{
  var filler=document.all[this.name+'Filler'];
  var box=document.all[this.name+'Box'];
  var bar=document.all[this.name+'Bar'];

  var top=bar.offsetTop;
  var left=bar.offsetLeft;
  var parent=bar.offsetParent;
  while(parent.tagName.toUpperCase()!='BODY')
  {
    top+=parent.offsetTop;
    left+=parent.offsetLeft;
    parent=parent.offsetParent;
  }

  if(this.boxDown)
  {
    box.style.top  = top+1+bar.offsetHeight;
    box.style.left = left;
    box.style.width = bar.offsetWidth;
    box.style.display="block";
  }
  else
  {
    box.style.top  = top-1-this.boxHeight;
    box.style.left = left;
    box.style.width = bar.offsetWidth;
    box.style.display="block";
  }

  filler.style.top     = 0;
  filler.style.left    = 0;
  filler.style.width   = parent.scrollWidth;
  filler.style.height  = parent.scrollHeight;
  filler.style.display = "block";
}

function TreeCBHideBox()
{
  var filler=document.all[this.name+'Filler'];
  filler.style.display = "none";
  var box=document.all[this.name+'Box'];
  box.style.display="none";
}
