function $() 
 {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
     return element;
      
    elements.push(element);
  } elements=(elements.length==0)? elements: elements[0];
    return elements;
 }


 function set_ed_status(id)
  {
    if (typeof(id)=="undefined") id=window.event;

     if ( id.target || id.srcElement )
     {
       id=fixEvent(id);
        id=id.target;
         id=test_parent(id,["layer","window","rb"],function (obj,id){  for (var i=0; i<id.length; i++){ if (obj.id.indexOf(id[i])>-1 || 
                                                                                                       obj.className.indexOf(id[i])>-1 )
                                                                                                           return obj;  }  return false; });
          if (id===false) return false;
      }

    switch ( typeof(id)) 
     { 
      case "string":
       id=$(id);
       break;
      case "number":
       id=$("layer"+id);
       break; 
     }

     $(id.id).status=1;
  }


function show_download_img()
  {
   var dn;

    dn=$("download");
     if (dn==null)
      { 
      dn=document.createElement("div");
      dn.id="download";
      dn.style.zIndex=9999999999999;
      dn.style.width="32px";
      dn.style.height="32px";
      dn.style.position="absolute";
      im=document.createElement("img");
      im.src="/WinInterface/jslib/winlib/window/download1.gif";
      im.style.width="32px";
      im.style.height="32px";
      im.alt="Processing!!";
      im.border=0;
      dn.appendChild(im);
      if ($("tmp_bl")!=null)
       $("tmp_bl").appendChild(dn);
       else
        document.getElementsByTagName('body')[0].appendChild(dn);
     }
     win_func.showwin(dn);
 }

 function hide_download_img()
  {
    var dn=$("download");
     if (dn)
     win_func.hidewin(dn);
  }

 function clone(obj)
  {
    var cloneObj = {};
     for (var i in obj) 
      {
       if (typeof obj[i] == 'object') 
        {
          cloneObj[i] = clone(obj[i]);
        } else 
            {
            cloneObj[i] = obj[i];
            }
   }
    return cloneObj;
  }

  function include(file) 
   {
     if (document.createElement && document.getElementsByTagName) 
      {
      var head = document.getElementsByTagName('head')[0];
      var script = document.createElement('script');
           script.setAttribute('type', 'text/javascript');
           script.setAttribute('src', file);
           head.appendChild(script);
          } else 
              {
                alert('Your browser can\'t deal with the DOM standard. That means it\'s old. Go fix it!');
              }
   }

    function showhelp(e,hstr)
     {
           if ( typeof( tb_menu_create)=="undefined")
           {
               alert("not included js mudule cooltree.js");
                return;
            }
               e=fixEvent(e);
               e.cancelBubble = true;
                if (e.target)
                {
            var xy=GetAbsCoords (e.target);
                 xy[0]+=e.target.offsetWidth-e.target.offsetWidth/3;
                 xy[1]+=e.target.offsetHeight/2;
                } else xy=[e.pageX,e.pageY];
            var menu_arr=[{innerHTML: hstr, onclick: function (e) {

            e=fixEvent(e);
                                     e.cancelBubble = true;
                            if (e.target.id.indexOf("help_line")==-1)
                             tb_menu_create.close_menu();   }}];
       tb_menu_create.create_menu(menu_arr,xy[0],xy[1]);
  }

var win_func={
       win_on_tray:{length:0}, 
       orientation:"horizontal",
    //     orientation:"vertical",

       tray_obj:null,
       windows_op: {},
       but_styles:{
                    normal:{
                             0:{backgroundImage:'url("/WinInterface/jslib/winlib/window/taskbutton.gif")',backgroundRepeat:"no-repeat",
                                backgroundPosition:"0 0px",cursor:"pointer",width:"2px",height:"28px"}, 
                             1:{backgroundImage:'url("/WinInterface/jslib/winlib/window/taskbutton.gif")',
                                backgroundPosition:"0 -56px",cursor:"pointer",width:"25px",height:"28px"},
                             2:{backgroundImage:'url("/WinInterface/jslib/winlib/window/taskbutton.gif")',backgroundRepeat:"no-repeat",
                                backgroundPosition:"0 -28px",cursor:"pointer",width:"2px",height:"28px"}
                           },
                  selected:{
                             0:{backgroundImage:'url("/WinInterface/jslib/winlib/window/taskbutton.gif")',backgroundRepeat:"no-repeat",
                                 backgroundPosition:"0 -252px",cursor:"pointer",width:"2px",height:"28px"}, 
                             1:{backgroundImage:'url("/WinInterface/jslib/winlib/window/taskbutton.gif")',
                                  backgroundPosition:"0 -308px",cursor:"pointer",width:"25px",height:"28px"}, 
                             2:{backgroundImage:'url("/WinInterface/jslib/winlib/window/taskbutton.gif")',backgroundRepeat:"no-repeat",
                                  backgroundPosition:"0 -280px",cursor:"pointer",width:"2px",height:"28px"}
                           }
                  },

     addEvent:function(obj, type, fn)
      {
         try 
          {
             Event.add(obj, type, fn);
          }
              catch(e)
               {
                 alert("Not Set Object Event!!");
               }
      },

     addEvent_m : function(obj, type, fn,method)
      {
        var func=function(event) { fn[method](event,fn); };
                         try 
                          {
                            Event.add(obj, type, func);
                          }
                            catch(e)
                             {
                               alert("Not Set Object Event!!");
                             }
      },

     delEvent_m: function (obj, type)
      {
         try 
          {
             Event.remove(obj, type);
          }
              catch(e)
               {
                 alert("Not Set Object Event!!");
               }
      },

    set_style: function (obj,st,t)
     {
     var val=[{m:"color",dv:"#000000"},
              {m:"fontWeight",dv:"normal"},
              {m:"borderLeft", dv:""},
              {m:"borderBottom",dv:""} ];

      if (!t)
      {
        for (var key in st)
         {
          obj[key]=st[key];
         }
           return;
      }
       for (var i=0; i<val.length; i++)
        {
         obj[val[i].m]=st[val[i].m] || val[i].dv;
        }
     },

      set_cell_style: function (but_id,type)
       {
         type=(!type)? "normal" : type;
          switch ( typeof(but_id) )
           {
              case "string": but_id=$(but_id); 
               break;        
           }
              for (var i=0; i<but_id.rows.item(0).cells.length; i++)
               {
                 this.set_style(but_id.rows.item(0).cells.item(i).style,this.but_styles[type][i]);
               }
       },

set_center_coord : function(obj)
 {
       var html = document.documentElement, body = document.body;
       var scrollX = (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0);
       var scrollY = (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0);

      var  w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
       var  h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
      var cw = w/2;
       var ch = h/2;

       var cx = scrollX + cw;
       var cy = scrollY + ch;

       var  tw = Math.min(w, obj.clientWidth);
       var  th = Math.min(h, obj.clientHeight);

       var tx = cx - tw/2;
       var ty = cy - th/2;

       obj.style.left = tx+ "px";
       obj.style.top = ty+ "px";
 },

 float_win: function()
  {
    var max=0;
      var i=0;
    for (var key in this.windows_op)
     {
       i++;
       var zIind=this.windows_op[key].zIndex;
         
         if (zIind>max) max=zIind;
       $(key).style.zIndex=i;
           
//       alert(key+" "+zIind);
     }

  return (max>0)? (parseInt(max)+1) : 1;

  },

 restore_float_win: function()
  {

    for (var key in this.windows_op)
     {
       $(key).style.zIndex=this.windows_op[key].zIndex;
     }

 },

 set_offset_frcenter: function(obj)
   {
      var xy=[parseInt(obj.style.left),parseInt(obj.style.top) ];
        for (var key in this.windows_op)
          {
             if ( parseInt($(key).style.left)==xy[0]  )
              {
                       obj.style.left=(xy[0]+18)+ "px";
              }
             if ( parseInt($(key).style.top)==xy[1]  )
              {
                    obj.style.top=(xy[1]+18) +"px";
              }
         }
    },

 showwin: function (id,title,type,xy)
  {

    if (typeof(id)=="undefined") id=window.event;
 
     if ( id.target || id.srcElement )
      {
       id=fixEvent(id);
        id=id.target;
         id=test_parent(id,["layer","window","rb"],function (obj,id){  for (var i=0; i<id.length; i++){ if (obj.id.indexOf(id[i])>-1 || 
                                                                                                       obj.className.indexOf(id[i])>-1 ) 
                                                                                                           return obj;  }  return false; });
         if (id===false) return false;
      }

   switch ( typeof(id)) 
    {
     case "string":
      var obj=$(id);
       break; 
     case "number":
       var obj=$("layer"+id);
        break; 
      default:
       var obj=id;
        break;
     }
         if (obj.style.display!="block" || !this.windows_op[obj.id])
         {
           obj.style.display="block";

            if (!xy || ( !xy[0] || ExtractNumber(obj.style.left)<0 )  || ( !xy[1]  || ExtractNumber(obj.style.top)<0 )  )
             {
               this.set_center_coord(obj);
                this.set_offset_frcenter(obj);
                obj.startX=null;
                obj.startY=null;
              }
                else 
                 {
                    obj.style.left=(xy[0]) ? xy[0] : obj.style.left;
                    obj.style.top=(xy[1]) ? xy[1] : obj.style.top;
                    obj.startX=ExtractNumber(obj.style.left);
                    obj.startY=ExtractNumber(obj.style.top);
                  }
         this.windows_op[obj.id]={};
       }

         this.windows_op[obj.id]['type']=type;

         obj.style.zIndex=this.float_win();

    if (type=="modal" )
    {

  var  w = (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
  var  h = (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
  var dialogmask = $(obj.id+'-dialog-mask');

              if (dialogmask==null)
              { 
               dialogmask = document.createElement('div');
               dialogmask.id = obj.id+'-dialog-mask';
               dialogmask.className='block_win-dialog-mask';
               document.body.appendChild(dialogmask);
               }
   
          dialogmask.title=obj.title || obj.id+": Modal Dialog Background";

                if (typeof(getDocumentHeight)!="undefined" )
                {  
                 dialogmask.style.height = getDocumentHeight()+ 'px';
                  dialogmask.style.width = getDocumentWidth()+ 'px';
                } else
                    alert("Not include dialog_box.js module!");

      dialogmask.style.zIndex =obj.style.zIndex;

         dialogmask.style.visibility = "visible";

     if (typeof(DragDrop)!="undefined" && DragDrop._ZIndex>=obj.style.zIndex)
      {
        DragDrop._ZIndex++;
         dialogmask.style.zIndex = DragDrop._ZIndex;
      } 

        obj.style.zIndex=(parseInt(dialogmask.style.zIndex)+1);           
        DragDrop._ZIndex++;
       //  alert(dialogmask.style.zIndex);
    }  

       if ( typeof(DragDrop)!="undefined"  )
        {
            if (DragDrop._ZIndex>=parseInt(obj.style.zIndex))
             {
               DragDrop._ZIndex++;
               obj.style.zIndex=DragDrop._ZIndex;
             }
            else
             DragDrop._ZIndex=parseInt(obj.style.zIndex);

              var j=DragDrop.test_child_recip(obj);
                if  (j!=false)
                 {
                   DragDrop._ZIndex++;
                    for (var i_j=0; i_j<j.length; i_j++)
                   DragDrop.set_style_recip(j[i_j],{display:"block",zIndex:DragDrop._ZIndex});
                   DragDrop.refresh_id_con(j[0]);
                 }
                 
       }
                                
         this.windows_op[obj.id]['zIndex']=parseInt(obj.style.zIndex);
          // alert(this.windows_op.toSource());
          if (title)
           $(obj.id+"_header").value=title;

//       if (this.tray_obj==null) return;
     
  },

 max_win: function (id) 
  {
    if (typeof(id)=="undefined") id=window.event;

     if ( id.target || id.srcElement )
      {
       id=fixEvent(id);
        id.cancelBubble=true;
         id=id.target;
      }

     switch ( typeof(id)) 
       { 
     case "string":
      id=$(id);
       break; 
      }

         if (id.className.indexOf("min_win")!=-1 )
          {
            this.min_win(id);
            return false;
          }
 
       id.className='min_win';
         id=test_parent(id,["layer","window","rb"],function (obj,id){  for (var i=0; i<id.length; i++){ if (obj.id.indexOf(id[i])>-1 || 
                                                                                                       obj.className.indexOf(id[i])>-1 ) 
                                                                                                           return obj;  }  return false;  });
        if (id===false) return false;

    var _clX=pageWidth();
    var _clY=pageHeight();

    var _x=ExtractNumber(id.style.left);
    var _y=ExtractNumber(id.style.top);
    
    var _maxX=ExtractNumber(id.style.maxWidth);
    var _maxY=ExtractNumber(id.style.maxHeight);

    var  max_step=9;

        _maxX=Math.min( _clX-max_step ,( _maxX==0 ) ? _clX-max_step: _maxX);    
        _maxY=Math.min( _clY-max_step ,( _maxY==0 ) ? _clY-max_step : _maxY);

       id.style.width=_maxX+"px";
       id.style.height=_maxY+"px";


           if (id.resizefunc ) id.resizefunc("max_win");

               if (!id.startX && !id.startY)
               {
              this.set_center_coord(id);
              this.set_offset_frcenter(id);
               }

          if ( typeof (DragDrop) !="undefined" )
           {
                DragDrop.refresh_parent(id.id);
 
       if ( typeof(DragDrop)!="undefined"  )
        {
      if (DragDrop._ZIndex>=parseInt(id.style.zIndex))
       {
          DragDrop._ZIndex++;
           id.style.zIndex=DragDrop._ZIndex;
        }
        else
       DragDrop._ZIndex=parseInt(id.style.zIndex);

        var j=DragDrop.test_child_recip(id);
             if  (j!=false)
             { 
                DragDrop._ZIndex++;
                for (var i_j=0; i_j<j.length; i_j++)
                DragDrop.set_style_recip(j[i_j],{display:"block",zIndex:DragDrop._ZIndex});
                DragDrop.refresh_id_con(j[0]);
             }

               } 

           }
 

    return false;
 },
  

 tray_win:  function(id)
  {


   if (typeof(id)=="undefined") id=window.event;

     if ( id.target || id.srcElement )
     {
       id=fixEvent(id);
        id.cancelBubble=true;  
       this.cleare_bg(id);
         id=id.target;
        }

     switch ( typeof(id)) 
       { 
     case "string":
      id=$(id);
       break; 
      case "number":
       id=$("layer"+id);
        break;
      }

         id=test_parent(id,["layer","window","rb"],function (obj,id) { for (var i=0; i<id.length; i++){ if (obj.id.indexOf(id[i])>-1 || 
                                                                                                       obj.className.indexOf(id[i])>-1 ) 
                                                                                                           return obj; }  return false;  });

          if (id.style.display=="none") return false;
        
     if (id==false || this.tray_obj==null  )  return false;
  
      var  tray_key=id.id.replace(/^wintray/,"");

      if ( typeof(this.win_on_tray["tray"+tray_key])=="undefined" )
       {
            tray_key=this.added_window(id.id);
        }

     this.win_on_tray["tray"+tray_key].xy=[$(id.id).style.left,$(id.id).style.top];
          this.hidewin(id.id);  
           return false;
      },
 
min_win: function (id) 
  {
 
    if (typeof(id)=="undefined") id=window.event;

    if ( id.target || id.srcElement )
     {
       id=fixEvent(id);
        id.cancelBubble=true;
        id=id.target;
     }

     switch ( typeof(id)) 
       { 
     case "string":
      id=$(id);
       break; 
      case "number":
       id=$("layer"+id);
        break;
      }

         if (id.className.indexOf("max_win")!=-1 )
          {
            this.max_win(id);
            return false;
          }

        id.className='max_win';

         id=test_parent(id,["layer","window","rb"],function (obj,id){  for (var i=0; i<id.length; i++){ if (obj.id.indexOf(id[i])>-1 || 
                                                                                                       obj.className.indexOf(id[i])>-1 ) 
                                                                                                           return obj;  }  return false;  });
 
     //   alert(id.id);
         if (id===false) return false;




    var _clX=100;
    var _clY=100;

    var _x=ExtractNumber(id.style.left);
    var _y=ExtractNumber(id.style.top);
    
    var _minX=ExtractNumber(id.style.minWidth);
    var _minY=ExtractNumber(id.style.minHeight);

           if (_minX==0) _minX=100; 
           if (_minY==0) _minY=100;

    //    _minX=Math.max( 100 ,( _minX==0 ) ? 100 : _minX);    
    //    _minY=Math.max( 100 ,( _minY==0 ) ? 100 : _minY);

       id.style.width= _minX+"px";
       id.style.height= _minY+"px";

        if (id.resizefunc ) id.resizefunc("min_win");

               if (!id.startX && !id.startY)
               {
 
               this.set_center_coord(id);
               this.set_offset_frcenter(id);
               }

          if ( typeof (DragDrop) !="undefined" )
           {
                DragDrop.refresh_parent(id.id);
           }


//   alert("min_win");
    return false;
  },

 closewin: function (id)
  {

    if (typeof(id)=="undefined") id=window.event;

     if ( id.target || id.srcElement )
      {
       id=fixEvent(id);
        id=id.target;

         var name=id.id.split('_');
          
          name=$(name[0]);
            if ( name==null)
              {
         id=test_parent(id,["layer","window","rb"],function (obj,id) { return  ( id.in_array(obj, function (comp,value) {
                                                            return  (value.id.indexOf(comp)>-1 || 
                                                                     value.className.indexOf(comp)>-1 );
                                                                     })===false ) ? false : obj; 

                                                                     });
               }
              else
               {
                id=name;
                }
         if (id===false){ alert("Not Set Parent Element"); return false;}
      }

      switch ( typeof(id)) 
       { 
     case "string":
      
      id=$(id);
    
       break; 
      case "number":
       id=$("layer"+id);
        break;
      }

 var save_obj=test_child(id,["edsave"],function (obj,id){    

         return  ( id.in_array(obj, function (comp,value) {
                                                            return  ( (value.id && value.id.indexOf(comp)>-1) || 
                                                                     (value.className &&
                                                                     value.className.indexOf(comp)>-1) );
                                                                     })===false ) ? false : obj; 

                                                           });

    save_obj=(save_obj.length==1)?save_obj[0]: false;
      
     var save_func=save_obj.onclick; 

      if ( !(!save_obj) && typeof(save_func)!="undefined" )
       {


        if (id.status && id.status==1 )
         {
             var t=this;

     showDialog('Prompt',"<br>Save Changes??",'Prompt',[{txt:'Save', act:function () 
                                                                      {
                                                                         $("dialog-button0").id=id.id;
                                                                          
                                                                          save_func();
                                                                           hideDialog();
                                                                         if (!save_status) return;
                                                                 //         t.hidewin(id);
                                                                        if (t.tray_obj==null) return;
                                                                        t.delete_win("tray"+id.id.replace(/^wintray/,""));
  
                                                                      } }, 
                                                    {txt: 'Don\'t Save',act: function ()
                                                                              { hideDialog();
                                                                         id.status=0;
                                                                         t.hidewin(id);
                                                                         if (t.tray_obj==null) return;
                                                                         t.delete_win("tray"+id.id.replace(/^wintray/,""));
                                                                              } },
                                                    {txt:'Cancel'}]); 

            return; 
         } 

       }

     this.hidewin(id);

         if (this.tray_obj==null) return false;
          this.delete_win("tray"+id.id.replace(/^wintray/,""));


        return false;
   },

  hidewin: function (id)
  {
    if (typeof(id)=="undefined") id=window.event;
 
     if ( id.target || id.srcElement )
      {

       id=fixEvent(id);
        id=id.target;

         id=test_parent(id,["layer","window","rb"],function (obj,id) {  return  ( id.in_array(obj, function (comp,value) {
                                                            return  (value.id.indexOf(comp)>-1 || 
                                                                     value.className.indexOf(comp)>-1 );
                                                                     })===false ) ? false : obj; 

                                                                     });
         if (id===false) return false;
      }

      switch ( typeof(id)) 
       { 
     case "string":
      
      id=$(id);
    
       break; 
      case "number":
       id=$("layer"+id);
        break;
      }

        id.style.display="none";
     
    if ( this.windows_op[id.id])
          {
         switch ( this.windows_op[id.id]['type'] )
          {
            case 'modal':
             $(id.id+"-dialog-mask").style.visibility="hidden";
              break;
          }

         delete this.windows_op[id.id];
          }
          this.restore_float_win();

 
       if ( typeof(DragDrop)!="undefined" )
        {
         var j=DragDrop.test_child_recip(id);

          if  (  j!=false)
           for (var i_j=0; i_j<j.length; i_j++)
                 {
                 DragDrop.set_style_recip(j[i_j],{display:"none"});
                 }
        }

            return id;
   },

//   winid={name: , title: , style:,resize: true, sys_but:{}, dragdrop: true )

  create_win: function (winid)
   {
     var div=document.createElement("div");

         div.id=winid.name;
         div.className="layer";
/*
      "position:absolute; 
        display:none; 
            top:5;
           left:20;
      min-width:250px; 
      max-width:2050px;
          width:250px; 
         height:250px; 
     min-height:250px; 
     max-height:3050px;"
*/

  this.set_style(div.style,winid.style );
     var div1=document.createElement("div");

         div1.className="header_s";
         div1.style.position="relative;"
         
     var b=document.createElement("b");

         b.id=winid.name+"_header";
         b.innerHTML=winid.title;

      this.set_style(b.style,{position:"absolute",top:5,left:5,color:"#FFFFFF", fontFamily:"Arial",fontSize:"8pt",fontWeight: "bold" } );

      div1.appendChild(b);

      var table=document.createElement("table");
          table.style.borderCollapse="collapse";
   
      var row=table.insertRow(-1);

      for (var key in winid.sys_but)
       {
         var cel=row.insertCell(-1);
             cel.className=key;
             cel.onclick=winid.sys_but[key].onclick;
       }
            div1.appendChild(table);

             div.appendChild(div1);

    if (winid.resize)
     {
       var div1=document.createElement("div");
            div1.id="resize";  
             this.set_style(b.style,{ position:"absolute",bottom:"0px",right:"0px",width:"10px",height:"10px",cursor:"se-resize"  } );
              div.appendChild(div1);
     }

 },

  set_bg: function (e)
   {
     e=fixEvent(e);
      if (e.target.className.indexOf("_over")!=-1) return;
       e.target.className+="_over";
         return false;
   },

  cleare_bg: function (e)
   {
      e=fixEvent(e);
       var newcl=e.target.className.replace(/_over/,"");
        if (newcl!=e.target.className) 
         e.target.className=newcl;
          return false;
    }, 

  init: function (stobj)
   {            
    stobj=(!stobj)? document.body: stobj;

     this.tray_obj=$("tray_recip");

     var set_but_obj={close_win:
             {click:"closewin",mouseover:"set_bg", mouseout:"cleare_bg" }, 
               min_win:{click:"min_win" ,mouseover:"set_bg", mouseout:"cleare_bg"  }, 
               max_win:{click:"max_win" ,mouseover:"set_bg", mouseout:"cleare_bg"}, 
               tray_win:{click:"tray_win" ,mouseover:"set_bg", mouseout:"cleare_bg"} };

        for (var k in set_but_obj)
         {
           var but=getElementsByClassName(stobj,k);
            for (var i=0; i<but.length; i++)
            {
              
              for (var kb in set_but_obj[k] )           
               {

                if (but[i]['on'+kb]==null && (!but[i].events || !but[i].events[kb] ))

                  this.addEvent_m(but[i],kb,this,set_but_obj[k][kb] );
                }
            } 

         }

   },

     create_win_clone: function (id_w,id_n)
      {
         var _div_1=$(id_w)

             var _div_2=_div_1.cloneNode(true);
  
         _div_2.id="wintray"+id_n;

         _div_1.parentNode.appendChild(_div_2);


      var newTa= _div_2.getElementsByTagName('textarea');
    var newInpt= _div_2.getElementsByTagName('input');	
     var newSel= _div_2.getElementsByTagName('select');

         for (i=0; i < newInpt.length; i++)
          {
         //   var newName = newInpt[i].name.substring(0,newInpt[i].name.search(/\d/)) + nameNum;
               if (newInpt[i].id.indexOf(_div_1.id)==-1)
                 newInpt[i].setAttribute('id',newInpt[i].id+"_"+id_n);
         //         alert(newInpt[i].id);
          }

          for (i=0; i < newSel.length; i++)
           {
           //  var newName = newSel[i].name.substring(0,newSel[i].name.search(/\d/)) + nameNum;

             if (newSel[i].id.indexOf(_div_1.id)==-1)

              newSel[i].setAttribute('id',newSel[i].id+"_"+id_n);
            }

          for (i=0; i < newTa.length; i++)
           {
// var newName = newTa[i].name.substring(0,newTa[i].name.search(/\d/)) + nameNum;

             if (newTa[i].id.indexOf(_div_1.id)==-1)
  newTa[i].setAttribute('id',newTa[i].id+"_"+id_n);

  newTa[i].setAttribute('value',_div_1.getElementsByTagName('textarea')[i].value);
  newTa[i].defaultValue = _div_1.getElementsByTagName('textarea')[i].value;
           }


  
          test_child(_div_2,[id_w,_div_2.id],function (obj,id){ if (  obj.id!=null && obj.id.indexOf(id[0]+"_")==0) { 
                                                             obj.id= obj.id.replace( new RegExp ('^'+id[0], '') ,id[1]);
                   }  return false;   }  );
 
   if(navigator.appVersion.indexOf("MSIE") == -1)
     this.init(_div_2);

            if (typeof(DragDrop)!="undefined" ) DragDrop.dragpanels[_div_2.id]=""; 

          return _div_2; 
      },

       added_window: function (id_w)
        {

         id_w=(!id_w) ? null : id_w;
         
          switch ( typeof(id_w) )
           {
              case "string": but_id=$(id_w); break;        
           }
      
          this.win_on_tray.length++;

       //  var id_n=((id_w!=null)?(id_w): this.win_on_tray.length);

           var id_n=this.win_on_tray.length;

              this.win_on_tray["tray"+id_n]={};
     
              this.win_on_tray["tray"+id_n].status=0;
              this.win_on_tray["tray"+id_n].win_name=id_w;
              this.win_on_tray["tray"+id_n].title=( (id_w!=null) ? ( $(id_w +"_header" ).innerHTML ): "Element "+this.win_on_tray.length );

              var table=document.createElement("table");

               table.id="tray" + id_n;

               table.title=this.win_on_tray["tray"+id_n].title;

               table.style.borderCollapse="collapse";

           var  oRow = table.insertRow(-1);
           var  oCell=oRow.insertCell(-1);

                oCell.id="tray" + id_n;
                oCell=oRow.insertCell(-1);
                oCell.id="tray" + id_n;
                oCell.align="center";
                oCell.valign="center";
                var  img=document.createElement("img"); 
                     img.id="tray" + id_n;
                     var ico=  $(id_w +"_icon" );
                     img.src=( (id_w!=null && ico!=null) ?  ico.src : "/WinInterface/jslib/winlib/window/edit_16.png" );  

                oCell.appendChild(img);       

                oCell=oRow.insertCell(-1);

                oCell.id="tray" + id_n;
                this.set_cell_style(table);      

       this.addEvent_m(table,"mouseover",this,"select_but" );
       this.addEvent_m(table,"mouseout",this,"unselect_but");
       this.addEvent_m(table,"click",this,"restore_win" );

             var insTable=this.tray_obj;

              if (this.orientation=="horizontal")
               {
                   oRow=insTable.rows.item(0);
                  oCell=oRow.insertCell(-1);
                  oCell.appendChild(table);
                 
               } else 
                  {
                       oRow = insTable.insertRow(-1);
                       oCell=oRow.insertCell(-1);
                       oCell.appendChild(table);
                  }
                 
 if (id_w!=null)
  {
   this.win_on_tray["tray"+id_n].winclone=this.create_win_clone(id_w,id_n);
    this.hidewin("wintray"+id_n);
  }

      return id_n;
            },

           select_but:function (e) 
            {
              if (e == null)
               e = window.event; 
            	// IE uses srcElement, others use target
                var target = e.target != null ? e.target : e.srcElement;

                 if  ( this.win_on_tray[target.id].status==0 )
                  {
                   this.set_cell_style($(target.id),"selected");
                    this.win_on_tray[target.id].status=1;
                  }
             },

           unselect_but:function (e) 
            {
              if (e == null)
               e = window.event; 
            	// IE uses srcElement, others use target
                var target = e.target != null ? e.target : e.srcElement;

                 if  ( this.win_on_tray[target.id].status==1 )
                  {
                   this.set_cell_style($(target.id),"normal");
                   this.win_on_tray[target.id].status=0;
                  }

             },


            restore_win: function (e)
             {
                switch (  typeof(e) )
                  {
                       case "string":
                          var target=$(e);
                         break;
                           default:
                            if (e == null)
                             e=window.event; 
                               e=fixEvent(e);
                // IE uses srcElement, others use target
                              var target = e.target;
                               break;
                   }

                var sh_w=false;

                 for (var key in this.win_on_tray )
                   {
                         if (key==target.id || this.win_on_tray[key].win_name ==target.id )
                          { 
                                sh_w=true;          
                                 
                                  break;
                           }
                    }

                 if (!sh_w)
                  {
                      this.showwin(target);
                        return 'new';
                   }

                 if ( $( this.win_on_tray[key].win_name )==null)
                  {
                    this.delete_win(key);
                     return 'del';    
                  }  
             //      this.showwin(this.win_on_tray[target.id].win_name);
                     
                          if (this.win_on_tray[key].winclone.style.display=="block")
                          { 
                             this.tray_win(this.win_on_tray[key].winclone);
                               return 'tray';
                          } 
                      this.showwin(this.win_on_tray[key].winclone,null,null,this.win_on_tray[key].xy);
                              return 'show';
              },

           delete_win: function (id_w)
            {
              if (typeof(this.win_on_tray[id_w])=="undefined" ) return false;

               var d_obj=$(id_w);

                    if (d_obj==null ) { alert("Not Found Window Indetifer "+id_w);  return false;}

                         try 
                          {
                          (d_obj.parentNode||body).removeChild(d_obj);
                          }
                             catch(e)
                              {
                                alert("Error deleting Element "+id_w );
                                   return false;
                              }



                        var d_obj=$("win"+id_w);
                         try 
                          {
                          (d_obj.parentNode||body).removeChild(d_obj);
                          }
                             catch(e)
                              {
                                alert("Error deleting Element "+d_obj.id);
                                   return false;
                              }
                delete this.win_on_tray[id_w];
                  
                delete DragDrop.dragpanels["win"+id_w]; 

               return true;
            },

 master_wind:{},
  cur_active:[],
  act_master:"",

  Show_allmastwins: function ( status )
    {
        var wins= this.master_wind[this.act_master];

         for (var i=0; i<wins.length; i++)
          {
            $(wins[i].name).style.zIndex=(100000001+i);
                                     //     100000000     
             if ( !wins[i].shwin )
              {
                $(wins[i].name).style.display=(!status)? 'block': status;
              }
          }
    },

  Start_master: function (act_master)
   {
  key=this.master_wind[act_master][0];
      this.cur_active=0;
      this.act_master=act_master;
      this.Show_allmastwins();

      var dialogmask=$("master-dialog-mask");

      if (dialogmask==null)
       {
          dialogmask=document.createElement("div");
          dialogmask.id="master-dialog-mask";
          document.body.appendChild(dialogmask);
       }
   
      dialogmask.style.height = getDocumentHeight()+ 'px';

      dialogmask.style.width = getDocumentWidth()+ 'px';

     dialogmask.style.zIndex = 100000000;

     dialogmask.style.visibility = "visible";

     $(key.name+"_back").disabled=false;
     $(key.name+"_next").disabled=false;

     this.Arrange_master_wind();

      if ( key.shwin) 
       this.showwin(key.name,key.title);
   },

  Show_masterNextWin: function ()
   {
       if (this.act_master=="")
        {
          alert("Not Set Currient Master!!");
          return; 
        }
     if (this.cur_active==this.master_wind[this.act_master].length)
      {
        return;
      }

     var key=this.master_wind[this.act_master][this.cur_active];

      if (key.save_func!=null) 
 
       if (! key.save_func() ) return;

        $(key.name+"_next").disabled=true;

         $(key.name+"_back").disabled=true;

//          if ( key.shwin)
           this.closewin(key.name);
               
             if (this.cur_active==(this.master_wind[this.act_master].length-1) )
              { 
              this.Show_allmastwins("none");
               $("master-dialog-mask").style.visibility="hidden";
               return;
              }  
      this.cur_active++;

            key=this.master_wind[this.act_master][this.cur_active];
             if ( key.shwin) 
              this.showwin(key.name,key.title);
               $(key.name+"_next").disabled=false;
                $(key.name+"_back").disabled=false;
   },

 Show_masterBackWin: function ()
  {
       if (this.act_master=="")
        {
          alert("Not Set Currient Master!!");
          return; 
        }

         var key=this.master_wind[this.act_master][this.cur_active];

/*    if (key.save_func!=null) 
       if (! key.save_func() ) 
        return;
*/

     $(key.name+"_next").disabled=true;
     $(key.name+"_back").disabled=true;

      if (key.shwin)
       this.closewin(key.name);

     if (this.cur_active==0)
      {      
        this.Show_allmastwins("none");
         $("master-dialog-mask").style.visibility="hidden";

          return;
      }

    this.cur_active--;
     key=this.master_wind[this.act_master][this.cur_active];


      if (key.shwin)
       this.showwin(key.name,key.title); else
        $(key.name).style.display="block";

    //    this.Arrange_master_wind(null,this.cur_active);

        $(key.name+"_next").disabled=false;
          $(key.name+"_back").disabled=false;
  },

  Arrange_master_wind: function(act_master,st_win)
   {

var html = document.documentElement, body = document.body;
	    var scrollX = (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0);
	    var scrollY = (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0);

    this.act_master=(!act_master) ? this.act_master : act_master;
     st_win=(!st_win) ? 0: st_win;

     if (this.act_master=="")
      {
        alert("Not Set Currient Master!!");
         return; 
      }
        var wins= this.master_wind[this.act_master];
            
         for (var i=(wins.length-1); i>=st_win; i--)
          {
            $(wins[i].name).style.zIndex=(100000001+(wins.length)-i);

            $(wins[i].name).style.top=scrollY+(30+((wins.length-1)-i)*25)+"px";

            $(wins[i].name).style.left=scrollX+(30+((wins.length-1)-i)*25)+"px";

          //     100000000
          //      $(wins[i].name).style.display=(!status)? 'block': status;
          }
   },


/* winds=[ { name:"window_name", 
            title:"description of window",
            shwin:true,
        save_func: function (){  save window for go to next level  },
           },

          { 
          },
          {
          },  ,  ,  ,  ,  ,  ,  ,  ,  ,  , ];
*/

  Init_master: function (mast_name, winds)
   {
     this.master_wind[mast_name]=winds;

      for (var i=0; i<winds.length; i++)
       {
        var key=winds[i];

            var next=$(key.name+"_next");
                if (next==null ) { alert("Not Found Next Button For Window "+key.name);  continue;}
                this.addEvent_m(next,"click",this, "Show_masterNextWin" );
                next.disabled = true;

//   if ( i!=0 ) {

                 var back=$(key.name+"_back");
                  back.disabled =true;

              if (back==null)
                 { alert("Not Found Back Button For Window "+key.name); 
                    continue;
                 }           
                   this.addEvent_m(back,"click",this, "Show_masterBackWin" );

//    }

        }

     }

  }



function  tabs_control(set_arr,id,but_panel,tab_win,set_par)
 {

   this.id=id;
 
   this.but_panel=(!but_panel)? this.but_panel : but_panel;

   this.tab_win=(!tab_win)? this.tab_win : tab_win;
 
    this.tab_but=set_arr;

    if (set_par)
     {
       for (var key in set_par)
        {
            this[key]=new Object(set_par[key]);
        }
    }
      this.init_tab_button();
 }

  tabs_control.prototype={

        but_panel:"tabbut",

          tab_win:"tab",

       classnames:new Array("tab_l","tab_c","tab_r"),

   classnames_sel:new Array("tab_l_sel","tab_c_sel","tab_r_sel"),  

      tab_style:[{},{},{}],

      tab_style_sel:[{},{},{}],

      old_act:"",
      cur_act:"",
      tab_but:{  },
      max_tabs:0,  
      id:"1",
      orientation:"horizontal",

      find_tab: function(id)
       {


       },

      set_select: function(id)
       {
            if (this.tab_sel_user_func)  
             if ( !this.tab_sel_user_func(id) )  return false;
         
             if (this.cur_act==id) return false;

               if (this.cur_act!="" && this.tab_but[this.cur_act] )
               {
                 var i=this.cur_act;
                 var selObj=$("tabbut_"+this.id+"_"+i);
                      this.tab_but[this.cur_act].selected=false;
                      this.set_tab_style(selObj);
              try
                {
              $(this.tab_win+"_"+this.id+"_"+i).style.display="none";
                 }
                   catch(e) {}
           }

         this.old_act=this.cur_act;

          this.cur_act=id;

          if (this.cur_act!="" && this.tab_but[this.cur_act] )
           {
         
              var i=this.cur_act;

              var selObj=$("tabbut_"+this.id+"_"+i);


               this.tab_but[this.cur_act].selected=true;
               this.set_tab_style(selObj,true);          
              try
                {
                     $(this.tab_win+"_"+this.id+"_"+i).style.display=( $(this.tab_win+"_"+this.id+"_"+i).tagName.toLowerCase()=="table" )?  "" : "block";
                }
                 catch(e)
                  {
                  }
           } else this.cur_act="";

       },

    tab_sel_user_func: function (id)
     {

          return true;

     },

    delete_user_func:    function (id)
     {
          return true;
     },

    removetab: function (id,wsel)
     {
       
     if  (! this.delete_user_func(id) ) return; 

    this.closetab(id,wsel);

         var butObj=$("tabbut_"+this.id+"_"+id).parentNode;           
          var winObj=$(this.tab_win+"_"+this.id+"_"+id);
  
           try 
            {
             (winObj.parentNode||body).removeChild(winObj);
            }
               catch(e)
                {
                      
                }

                    try 
                     {
                          (butObj.parentNode||body).removeChild(butObj);
                          }
                             catch(e)
                              {
                         
                              }

     try
         { 
        delete this.tab_but[id];
                 
         } 
          catch (e) {alert("This tab Id "+id+" not present.");}

     },

    addtab: function (add_arr)
     {
        for (var key in add_arr)
         {
            if (typeof( this.tab_but[key] )!="undefined")
             {
               alert("This tab id "+key+" already exists!!"); 
                 continue;
             }

             this.tab_but[key]=add_arr[key];
         }
         this.init_tab_button(add_arr);

     },

    showtab: function (id)
     {
   

     },

    closetab: function(id,wsel)
       {
          if (id!="" && this.tab_but[id] )
           {
             var selObj=$("tabbut_"+this.id+"_"+id).style.display="none";           
              try
                {
               $(this.tab_win+"_"+this.id+"_"+id).style.display="none";
                 }
                   catch(e) {}
 
           }

   if (!wsel && this.cur_act==id)
    {
        var i=0;
         var prev="";  
          var next="";
          var ext=false;
          for (var key in this.tab_but)
           {
             if (this.tab_but[key].status=="closed" ) continue;
             var next=key;
              if (ext) break;
                 
              if (key==id) {ext=true; }
               else       
              var prev=key;
               i++;
           }
 
             if (prev!="")
              {
               this.set_select(prev);
              } else
                  if (next!=id ) 
                 this.set_select(next);
                    else  
                      this.cur_act="";           

            this.tab_but[id].status="closed";
     }

       }, 
 
      set_tab_style: function (selObj,type)
       {       
        var set_style=this.tab_style;
        var set_class=this.classnames;
          if (type)
           {
        var set_style=this.tab_style_sel;
        var set_class=this.classnames_sel;
           }
                    selObj.rows.item(0).cells.item(0).className=set_class[0];
                    selObj.rows.item(0).cells.item(1).className=set_class[1];
                    selObj.rows.item(0).cells.item(2).className=set_class[1];
                    selObj.rows.item(0).cells.item(3).className=set_class[1];
                    selObj.rows.item(0).cells.item(4).className=set_class[2];

                    set_obj(selObj.rows.item(0).cells.item(0).style,set_style[0]);
                    set_obj(selObj.rows.item(0).cells.item(1).style,set_style[1]);
                    set_obj(selObj.rows.item(0).cells.item(2).style,set_style[1]);
                    set_obj(selObj.rows.item(0).cells.item(3).style,set_style[1]);
                    set_obj(selObj.rows.item(0).cells.item(4).style,set_style[2]);

       },  

   restore_tab_button : function ( el )
     {
        var oTable=$("tabbut_"+this.id+"_"+el);
             oTable.title=this.tab_but[el].title;

             $("cell_"+this.id+"_"+el).innerHTML=this.tab_but[el].name;

             $("img_"+this.id+"_"+el).src=this.tab_but[el].img;

      },

     init_tab_button: function(add_el,del_t)
      {
         var tobj=this; 

          var insTable=$(this.but_panel);

            if (!insTable  || !insTable.rows)
             {
               alert("Not Found button panel "+this.but_panel+"\r\nfor init tab controller.");
                 return;
              }
  
            if ( !add_el || del_t)
             {
                while(insTable.rows.length>1)
                 insTable.deleteRow(insTable.rows.length-1);
             }

        if ( !add_el )
              add_el=this.tab_but;

          for(var key in add_el)
           {  
             var i=key;

  var table=document.createElement("table");

               table.id="tabbut_"+this.id+"_"+i;

               table.title=(add_el[key].title)? add_el[key].title : table.id;

               table.style.borderCollapse="collapse";

           var  oRow = table.insertRow(-1);
           var  oCell=oRow.insertCell(-1);
                oCell=oRow.insertCell(-1);
   
             if ( add_el[key].img )
              {
                var  img=document.createElement("img"); 
                     img.src=add_el[key].img;
                     img.id=   "img_"+this.id+"_"+i;
                oCell.appendChild(img); 

                 if  (add_el[key].rmenu)
                  {
                     img.rmenu=[];

                        img.rmenu=add_el[key].rmenu;

                        

                      oCell.onclick=function (e) 
                       {
         e=fixEvent(e);
          var name=e.target.id.split('_');  tobj.set_select(name[2]);
          e.cancelBubble = true;

          var xy=GetAbsCoords (e.target);
               xy[0]+=2;
               xy[1]+=e.target.offsetHeight-1;
              tb_menu_create.cl_st=true;
               var rmenu=e.target.rmenu;

                  for(var m_i=0; m_i<rmenu.length; m_i++)
                   {
                      rmenu[m_i].tab_id=tobj.id;
                   }

                 tb_menu_create.create_menu(rmenu,xy[0],xy[1]);
                        return false;
                                   } 
                  }
              }

              oCell=oRow.insertCell(-1);
              oCell.innerHTML=add_el[key].name;
              oCell.id="cell_"+this.id+"_"+i;

              oCell.onclick=function (e) { e=fixEvent(e); var name=e.target.id.split('_');  tobj.set_select(name[2]); };

              oCell.oncontextmenu=function (e) { e=fixEvent(e);  var name=e.target.id.split('_');

                                                                   var title=$("tabbut_"+name[1]+"_"+name[2]).title;
                                                                          if (title!="") 
                                                                            showhelp(e,title);  e.cancelBubble=true; return false; };

                oCell=oRow.insertCell(-1);

                oCell.align="right";
                oCell.valign="top";

          if ( add_el[key].close )
              {
                var  img=document.createElement("img"); 
                     img.id="tabclose_"+this.id + "_"+i;
                     img.src="/WinInterface/jslib/winlib/window/tab-close.gif";
                   
   
                  img.onclick=function (e) { e=fixEvent(e);   var name=e.target.id.split('_');   tobj.removetab(name[2]); // tobj.closetab(name[2]); 
                                           } 

                oCell.appendChild(img);
              }

                oCell=oRow.insertCell(-1);
                  this.max_tabs++;

               this.set_tab_style(table);

              if (this.orientation=="horizontal")
               {
                   oRow=insTable.rows.item(0);
                    if (oRow==null) oRow = insTable.insertRow(-1);
                  oCell=oRow.insertCell(-1);
                  oCell.appendChild(table);
                  oCell.align="right";
               } else 
                  {
                       oRow = insTable.insertRow(-1);
                       oCell=oRow.insertCell(-1);
                       oCell.appendChild(table);
                       oCell.align="left";
                  }

  /*  this.addEvent_m(table,"mouseover",this,"select_but" );
       this.addEvent_m(table,"mouseout",this,"unselect_but");
   */

                 if ( add_el[key].selected )
                  { 
                     this.set_select(key);
                  }
                 
           }
       }


    }
