// M_TLIB.JS

  /////////////////////////////////////////////////////////////////
  //                                                             //
  //   CPAccounting(R) Version Apr10'05 (C)Copyright 1994-2005   //
  //                                                             //
  /////////////////////////////////////////////////////////////////

var bsUnicIndex = 0;
var allTrees = new Array();
var bcScrollInc = 16;

function BSTree(){
  this.id               = allTrees.length;
  this.root             = new BSMenuItem(this.id, null,"bsRoot", null);
  this.items            = new Array();
  allTrees[this.id]     = this;
  this.selected         = null;
  this.altColor         = false;
  this.scrollTo         = 0;
  this.addToScroll      = false;
  this.doc              = null
  this.showIcons        = true;
  this.background       = '';
  this.hideAfterChoose  = false;
  this.showToolbar      = true;
  this.searchFor        = '';
}

BSTree.prototype.checkCookies = function (){
  var cookies = getCookie(this.id);
  if(cookies){
    for(var i=0; i<cookies.length; i++){
      var item = this.items[i];
      if(item)
        item.isOpen = (item.isOpen || cookies.charAt(i)=='1');
    }
  }
}

BSTree.prototype.expand = function(flag){
  for(var i=0; i<this.items.length; i++){
    this.items[i].isOpen = flag;
    this.items[i].altColor = false;
  }
  this.show();
}

BSTree.prototype.find = function(){
  var obj = getObject( 'search', getTreeFrame().document );
  this.searchFor = obj.value;
  var answer = this.searchFor;
  if(!answer)
    return;
  this.expand(false);
  answer = answer.toUpperCase();
  var found = false;
  for(var i=0; i<this.items.length; i++){
    var item = this.items[i];
    if(item.text.toUpperCase().indexOf(answer)!=-1){
      found = true;
      BSTree_setVisibleMode( this.items[i] );
    }
  }
  if(!found)
   alert("търсете в множествено число");
  this.show();
}

function BSTree_setVisibleMode(item){
  item.isOpen = true;
  item.altColor = true;
  while(item.parent!=null){
    item = item.parent;
    item.isOpen = true;
  }
}

BSTree.prototype.switchHideAfterChoose = function(){
  this.hideAfterChoose = !this.hideAfterChoose;
}

BSTree.prototype.show = function (){
  this.scrollTo = 0;
  if(this.selected!=null)
    this.addToScroll = true;
  var win = window.top.frames["bsTreeFrame"];
  this.doc = win.document;
  if(!this.doc)
    return;

  var cookieToSet = '';
  for(var i=0; i<this.items.length; i++){
    cookieToSet+=(this.items[i].isOpen)? '1':'0';
  }
  setCookie(this.id, cookieToSet);
 
  if (document.images)
    this.doc.open("text/html", "replace");
  else
    this.doc.open("text/html");

  this.doc.writeln( '<head>' );

  if(GECKO){
    this.doc.writeln( '<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1251">' );
  }

  this.doc.writeln( '<title>bsTree</title>' );
  this.doc.writeln( '<style type="text/css">' );
  this.doc.writeln( '* {font-size:12px;}' );
  this.doc.writeln( 'a {' );
  this.doc.writeln( 'color:black;' );
  this.doc.writeln( 'text-decoration:none;' );
  this.doc.writeln( '}' );
  this.doc.writeln( 'a.alt {' );
  this.doc.writeln( 'color:#0000aa;' );
  this.doc.writeln( '}' );
  this.doc.writeln( 'a.searchColor {' );
  this.doc.writeln( 'color:red;' );
  this.doc.writeln( '}' );
  this.doc.writeln( 'a:hover {' );
  this.doc.writeln( 'color:#1111ff;' );
  this.doc.writeln( '}' );
  this.doc.writeln( '.search {' );
  this.doc.writeln( 'background-color: rgb(229,237,242);' );
  this.doc.writeln( '}' );

  this.doc.writeln( '</style>' );
  this.doc.writeln( '<script>' );
  if(IE)
    this.doc.writeln( 'document.onkeyup=function(){window.top.keyOverTree(event);};' );
  else
    this.doc.writeln( 'document.onkeyup=window.top.keyOverTree;' );
  this.doc.writeln( '</script>' );
  this.doc.writeln( '</head>' );

  this.doc.writeln( '<body background="'+bsTreeGraph+this.background+'" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">' );
  this.doc.writeln( '<form name="s_t_form" action="javascript:window.top.bsTree.find();">' );
  this.doc.writeln( '<table border=0 cellpadding=0 cellspacing=0>' );
  this.doc.writeln('<tr><td><a href="/G0000BG/CPA-1.HTM" target="bsMainFrame"><img src="' + bsTreeGraph+ 'M_TOP.JPG" awidth=216 aheight="'+headerHeight+'" border=0  alt=" За програма CPAccounting&reg; "></td></tr>');

  if(this.showToolbar){
    this.doc.writeln('<tr><td>');
    this.doc.write( '<table border=0 cellpadding=0 cellspacing=0>' );
    this.doc.write( '<tr><td>&nbsp;<input class="search" name="search" id="search" width=185 value="'+this.searchFor+'" title="пишете в множествено число"></td>');
    this.doc.write( '<td><a href="javascript:window.top.bsTree.find();" onMouseOver="window.top.chTreeImg(\'tfind\');" onMouseOut="window.top.chTreeImg(\'tfind\');"><img width=31 name="tfind" src="'+bsTreeGraph+'T-FIND.GIF" border="0" alt="Търси в менюто (бърз клавиш:F)"></a></td></tr>' );
    this.doc.write( '</table>' );
    this.doc.writeln('</td></tr>');

    this.doc.writeln('<tr><td>');
    this.doc.write( '<a href="javascript:window.top.bsTree.expand(true);" onMouseOver="window.top.chTreeImg(\'texp\');" onMouseOut="window.top.chTreeImg(\'texp\');"><img name="texp" src="'+bsTreeGraph+'T-EXP.GIF" border="0" alt="Разгъни менюто"></a>' );
    this.doc.write( '<a href="javascript:window.top.bsTree.expand(false);" onMouseOver="window.top.chTreeImg(\'tcol\');" onMouseOut="window.top.chTreeImg(\'tcol\');"><img name="tcol" src="'+bsTreeGraph+'T-COLPS.GIF" border="0" alt="Сгъни менюто"></a>' );
    //this.doc.write( '<a href="javascript:window.top.bsTree.find();" onMouseOver="window.top.chTreeImg(\'tfind\');" onMouseOut="window.top.chTreeImg(\'tfind\');"><img name="tfind" src="'+bsTreeGraph+'T-FIND.GIF" border="0" alt="Търси в менюто"></a>' );
    this.doc.writeln('</td></tr>');
  }

  for(var i=0; i<this.root.children.length; i++){
    this.root.children[i].showMenu( '' );
  }
  this.doc.writeln( '</table>' );
  var checked = (bsTree.hideAfterChoose)? 'checked':'';
  this.doc.writeln( '<input '+checked+' type="checkbox" onclick="window.top.allTrees['+this.id+'].switchHideAfterChoose();">Скрий след избор' );
  this.doc.writeln( '</form>' );
  this.doc.writeln( '</body>' );

  this.doc.close();
  
  win.scrollBy(0, this.scrollTo-headerHeight);
}

BSTree.prototype.createItem = function(text, action, alt){
  var index = this.items.length;
  var menuItem = new BSMenuItem(this.id, index,text, action, false);
  this.items[index] = menuItem;
  if(alt==true)
    menuItem.alt = true;
  return menuItem;
}

function BSMenuItem(treeId, id, text, action){
  this.treeId     = treeId;
  this.id         = id;
  this.text       = text;
  this.children   = null;
  this.action     = action;
  this.isOpen     = false;
  this.alt        = false;
  this.parent     = null;
}

BSMenuItem.prototype.isLeaf = function (){
  return (this.children==null);
}

BSMenuItem.prototype.addMenuItem = function (menuItem){
  if(this.children==null)
    this.children = new Array();
  var index = this.children.length;
  this.children[index] = menuItem;
  menuItem.parent = this;
  return menuItem;
}

BSMenuItem.prototype.showMenu = function (inFront){
  var rowSrc = '';
  var treeObj = allTrees[this.treeId];
  rowSrc += '<tr valign="top"><td nowrap title="'+this.text+'">';
  var isLeaf = this.isLeaf();
  var toggle = ' onClick="return window.top.bsToggle('+this.treeId+','+this.id+');" ';
  var mouseAction = ' onMouseOver="return window.top.bsMOver(\''+this.text+'\');" ';
     mouseAction += ' onMouseOut="return window.top.bsMOut(\''+this.text+'\');" ';
  rowSrc += inFront;

  if(!isLeaf && treeObj.showIcons){
    rowSrc += '<a href="javascript:void(0);" '+toggle+mouseAction+'>';
    rowSrc += addImg( (this.isOpen)? tree_plus_ico:tree_minus_ico , this.text);
    rowSrc += '</a>';
  }
  
//var href = (this.action)? '/G0000BG/M_HOME.HTM':'#';
  var href = (this.action) ? ( this.action > 0 ) ? '/G0000BG/HELP.HTM#h'+this.action : this.action : '#';
  var tmpClass = (this.alt)? 'alt':'';
  if(this.altColor){
    tmpClass += ' searchColor';
  }
  
  rowSrc += '<a class="'+tmpClass+'" href="'+href+'" target="bsMainFrame" '+toggle+mouseAction+'>';

  var leafIco ='';
  if(/*treeObj.showIcons*/true){
    if(!isLeaf){
      leafIco = (this.isOpen)? 
        (this.alt)? tree_open1_ico:tree_open_ico:
        (this.alt)? tree_close1_ico:tree_close_ico;
        rowSrc += addImg(leafIco, this.text);
    } else {
      leafIco = tree_leaf_ico;
      if(treeObj.showIcons)
        rowSrc += addImg(leafIco, this.text);
    }
  }
  
  rowSrc += '<font face="MS Sans Serif,Arial,Helvetica" size="1"';
  if(treeObj.selected==this.id){
    rowSrc += 'style="background-color:navy;color:white;"';
    treeObj.addToScroll = false;
  }
  if(treeObj.addToScroll){
    treeObj.scrollTo += bcScrollInc;
  }
  rowSrc += '>&nbsp;';
  rowSrc += this.text;
  rowSrc += '&nbsp;</font>';

  rowSrc += '</a>';
  rowSrc += '</td></tr>';
  
  treeObj.doc.writeln(rowSrc);
  if(!isLeaf){
    if(this.isOpen){
      for(var i=0; i<this.children.length; i++){
        var item = this.children[i];
        item.showMenu( inFront + addImg(tree_blank_ico) );
      }
    }
  }
  return null;
}

function addImg(src, toolTip){
  if(!toolTip)
    toolTip = '';
  var ret = '<img alt="'+toolTip+'" src="'+bsTreeGraph+src+'" align=left border=0 vspace=0 hspace=0>';
  return ret;
}

function bsToggle(treeId,id){
  var treeObj = allTrees[treeId];
  var item = treeObj.items[id];
  treeObj.selected = id;
  item.isOpen = !item.isOpen;
  setTimeout('allTrees['+treeId+'].show()', 5);
  if(item.action && treeObj.hideAfterChoose){
    window.top.show_hide_tree();
  }
  return (item.action)? true:false;
}

function bsMOver(text){
  window.status = text;
  return true;
}

function bsMOut(text){
  window.status = '';
  return true;
}

function getCookie(id){
  var search;
  search = 'CPA_Help_'+id+"="
  offset = document.cookie.indexOf(search) 
  if (offset != -1) {
    offset += search.length ;
    end = document.cookie.indexOf(";", offset) ;
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}

function setCookie(id, value) {
  var expires = new Date ();
  expires.setTime (expires.getTime() + (86400 * 1000 * 7/* days */));
  document.cookie = 'CPA_Help_'+id+"="+ escape(value)+"; expires=" + expires.toGMTString() +  "; path=/";
}

function chTreeImg(name){
  chImg(name, getTreeFrame().document);
}

function keyOverTree(evnt){
  var keyCode = evnt.keyCode;
  if(!keyCode)
    keyCode = evnt.charCode;
  var shiftKey = evnt.shiftKey;
  var altKey = evnt.altKey;
  var ctrlKey = evnt.ctrlKey;
  keyCode = (NS4)? ( (evnt.which==70 || evnt.which==102 || evnt.which==212 || evnt.which==244)? 70:0):keyCode;
  if(keyCode==70){
    var treeFrame = window.top.getTreeFrame();
    var obj = window.top.getObject( 'search', treeFrame.document );
    if(obj!=null && obj!=undefined)
      obj.focus();
  }
}

