﻿function clickButton(e, buttonid){
      var evt = e ? e : window.event;
      if (evt.keyCode == 13)
      {
          var bt = document.getElementById(buttonid);
          if (bt)
          {
                    bt.click();
                    return false;
          }
      }

}
// левое меню (смена картинки)
function onMouse(which)
{
    which.parentNode.firstChild.style.backgroundImage = 'url(../images/cellLeftMenuOrange.gif)';
    which.parentNode.firstChild.style.backgroundRepeat = 'repeat-y'; 
    which.style.backgroundColor = '#4f514c';
}

function onMouseOut(which)
{
    which.parentNode.firstChild.style.backgroundImage = 'url(../images/cellLeftMenuNiz.jpg)';
    which.parentNode.firstChild.style.backgroundRepeat = 'no-repeat'; 
    which.style.backgroundColor = '#7c8278';
}

function expand(node)
{
      if (node.className=="CNodeStyle" || node.className=="CNodeStyleEnd")
      {
           var callback= function(response)
           {
                if (response.error)
                {
                    if (response.request.status==403)
                        alert("У вас нет прав на выполнение данного действия");
                    else
                        alert(response.error);
                }
                else
                {
                    var nodeChildContainer=getChildsContainer(node.parentNode);
                    if (response.value.length!=0)
                    {
                       nodeChildContainer.innerHTML=response.value;
                       
                       if (nodeChildContainer.nextSibling)
                        node.className="CNodeStyleE";
                       else
                        node.className="CNodeStyleEndE";
                       changeCookie(node.parentNode.id, true);
                       nodeChildContainer.style.display="block";
                    }
                    else
                    {
                        node.className="nodeStyle";
                        node.parentNode.parentNode.removeChild(nodeChildContainer);
                    }
                }
            }
            var response=PInventGroupStruct.GetChildNodes(node.parentNode.id, callback);
      }
      else
      {
                 var nodeChildContainer=getChildsContainer(node.parentNode);
                 nodeChildContainer.style.display="none";
                 changeCookie(node.parentNode.id, false);
                  if (nodeChildContainer.nextSibling)
                      node.className="CNodeStyle";
                  else
                      node.className="CNodeStyleEnd";
      }
}
    function getChildsContainer(node)
    {
        for (var i=0; i<node.childNodes.length; i++)
        {
        
            if (node.childNodes[i]!=null)
            {
                if (node.childNodes[i].className=="СChildNode")
                {   
                    return node.childNodes[i];
               }
             }
        }
        return null;
    }
    
    function changeCookie(id, isExpand)
    {
        var curItem="";
        var regExpObj=new RegExp("^"+id+"=");
        var emap=getCookie("eigroup");
        if (emap!=null)
        {
        var itemCollection=emap.split(":");
        for (var i=0; i<itemCollection.length; i++)
        {
            if (id==itemCollection[i])
            {
                curItem=itemCollection[i];
                break;
            }
        }
        if (curItem.length!=0)
        {
            emap="";
            if (!isExpand)
            {
                for (var i=0; i<itemCollection.length; i++)
                {
                    if (id!=itemCollection[i])
                    {
                        emap+=itemCollection[i]+":";
                        
                    }
                }
           // alert(emap) ;
            document.cookie="eigroup="+emap+"; path=/";
            }
        }
        else
        {
            emap="";
           if (isExpand)
           {
                emap=id+":";
                for (var i=0; i<itemCollection.length; i++)
                {
                    if (id!=itemCollection[i])
                    {
                        emap+=itemCollection[i]+":";
                        
                    }
                }
               document.cookie="eigroup="+emap+"; path=/";
            }
          }
        }
        else
        {
         if (isExpand)
            document.cookie="eigroup="+id+":; path=/";
         }
    } 
    function getCookie(name) 
    {
	    var cookie = " " + document.cookie;
	    var search = " " + name + "=";
	    var setStr = null;
	    var offset = 0;
	    var end = 0;
	    if (cookie.length > 0)
	    {
		    offset = cookie.indexOf(search);
		    if (offset != -1) 
		    {
			    offset += search.length;
			    end = cookie.indexOf(";", offset)
			    if (end == -1) 
			    {
				    end = cookie.length;
			    }
			    setStr = unescape(cookie.substring(offset, end));
		    }
        }
    
	    return(setStr);
    }

