////
// Default settings
        
        // show checkboxes
        var gbChowCheckboxes = true;
        // link template to use
        var giLinkTemplate = 2;
        // open preloaded nodes 
        var gbOpenLoadedNodes = true;
        // last level , will be displayed without [+]
        var giMaxLevel = 4;
//
////

var gaReq = [];
var goSearchRequest;
var gaCallers = [];

function cNode(sID, sName, iLevel, sDefaultCheck)
{
        this.sID        = sID;
        this.sOID   = 'o' + this.sID;
        this.sName      = sName;
        this.iLevel = iLevel;
        this.sInstanceName = this.sOID + '_inst';
        this.sDefaultCheck = sDefaultCheck;
        this.bSelfChecked = (this.sDefaultCheck == 'checked');
        this.bInit      = false;

        this.getHTML            = cNode_getHTML;
        this.doPlaceHTML        = cNode_doPlaceHTML;
        this.doInit             = cNode_doInit;
        this.doSwitch           = cNode_doSwitch;
        this.doOpen             = cNode_doOpen;
        this.doClose            = cNode_doClose;
        this.doCheck            = cNode_doCheck;
        this.doInheritCheckbox = cNode_doInheritCheckbox;
        this.doReceiveChildren = cNode_doReceiveChildren;

}

function cNode_getHTML()
{
        var sHTML = '';
        var aLinkHTML = [];
        
        aLinkHTML[0] = '<a href="index.php?cg=search&go_chapter=1&sr_chapter=' + this.sID + '">' + this.sName + '</a>';
        aLinkHTML[1] = '<a href="scriphtname2.php?id=' + this.sID + '&var=val" title="' + this.sName + '">' + this.sName + '</a>';
        aLinkHTML[2] = this.sName;
        
        
        sHTML += '<table border="0" cellpadding="0" cellspacing="5">';
        sHTML += '<tr>';
                sHTML += '<td valign="top"><nobr>';
                if (this.iLevel < giMaxLevel)
                {
                        sHTML += '<a href="javascript:;" onclick="' + this.sInstanceName + '.doSwitch()"><img id="' + this.sOID + '_swimg" src="img/ul_catalog.gif" alt="" title="" width="11" height="14" border="0"/></a>';
                        sHTML += '<input type="hidden" name="open_'+ this.sID + '" id="'+ this.sOID + '_open"  value="">';
        }
                
                sHTML += '<input type="hidden" name="loaded_'+ this.sID + '" id="'+ this.sOID + '_loaded"  value="">';
                
                if (gbChowCheckboxes)
                {
                        sHTML += ' <input type="checkbox" name="tr_cCodes[]" id="' + this.sOID + '_id" onclick="' + this.sInstanceName + '.doCheck()" value="'+ this.sID + '" ' + this.sDefaultCheck + '>';
                }
                sHTML += '</nobr></td>';
                sHTML += '<td>' + aLinkHTML[giLinkTemplate] + '</td>';
        sHTML += '</tr>';
        sHTML += '<tr id="' + this.sOID + '_ChildrenTR" style="display:none">';
                sHTML += '<td></td>';
                sHTML += '<td id="' + this.sOID + '_ChildrenTD">идет загрузка...</td>';
                sHTML += '</td>';
        sHTML += '</tr></table>';
        return sHTML;
}

function cNode_doInit()
{
        this.oSwitcherImage = document.getElementById(this.sOID + '_swimg');
        this.oChildrenTR        = document.getElementById(this.sOID + '_ChildrenTR');
        this.oChildrenTD        = document.getElementById(this.sOID + '_ChildrenTD');
        this.oCheckbox          = document.getElementById(this.sOID + '_id');
        this.oOpenInput         = document.getElementById(this.sOID + '_open');
        this.oLoadedInput       = document.getElementById(this.sOID + '_loaded');
        
        if (this.bLoadedInput)
        this.oLoadedInput.value = this.bLoadedInput;

        window[this.sInstanceName] = this;
}

function cNode_doPlaceHTML()
{
        document.write(this.getHTML());
        
        this.doInit();
}

function cNode_doSwitch()
{
        if (this.oOpenInput.value == "1")
        {
                this.doClose();
        }
        else
        {
                this.doOpen();
        }
}

function cNode_doOpen()
{
        if (this.isEmpty || !this.oOpenInput) return;

        this.oChildrenTR.style.display = '';
        
        if (this.bLoadedInput)
        {
                if (this.oSwitcherImage)
                        this.oSwitcherImage.src = 'img/ul_catalogAct.gif';

                if (this.oOpenInput)
                        this.oOpenInput.value = 1;
                        
                this.doInheritCheckbox();
        }
        else
        {
                doLoadChildren(this);
        }
}

function cNode_doClose()
{
        this.oSwitcherImage.src = 'img/ul_catalog.gif';
        this.oChildrenTR.style.display = 'none';
        this.oOpenInput.value = 0;
}

function cNode_doCheck(_bInherited, _bValue, bSuperValue)
{
        if (!this.oCheckbox)
                return;
        
        if (bSuperValue)
        {
                this.oCheckbox.checked = _bValue;
                _bInherited = false;
        }
                
        if (_bInherited)
        {
                this.oCheckbox.checked = _bValue || this.bSelfChecked;
        }
        else
        {
                this.bSelfChecked = this.oCheckbox.checked;
        }
        
        this.doInheritCheckbox(bSuperValue);
}

function cNode_doInheritCheckbox(bSuperValue)
{
        var i;
        
        if (!this.oCheckbox)
                return;
        
        for (i in this.aChildrenObj)
        {
                this.aChildrenObj[i].doCheck(true, this.oCheckbox.checked, bSuperValue);
        }
}

function cNode_doReceiveChildren(aChildrenObj, bOpen)
{
        var sChildrenHTML = '';
        var i;
        
        this.bLoadedInput = 1;
        if (this.oLoadedInput)
        this.oLoadedInput.value = this.bLoadedInput;
        
        if (bOpen == null)
                bOpen = gbOpenLoadedNodes;
        
        this.aChildrenObj = aChildrenObj;
        this.iChildrenNum = 0;
        
        for (i in this.aChildrenObj)
        {
                this.iChildrenNum ++;
                sChildrenHTML += this.aChildrenObj[i].getHTML();
        }
        
        this.oChildrenTD.innerHTML = sChildrenHTML;
        
        for (i in this.aChildrenObj)
        {
                this.aChildrenObj[i].doInit();
        }
        
        if (bOpen)
                this.doOpen();
                
        if (this.oSwitcherImage && !this.iChildrenNum)
        {
                this.isEmpty = true;
                this.doClose();
                this.oSwitcherImage.style.visibility = 'hidden';
        }
}

function cRootNode()
{
        this.Parent = cNode;
        this.Parent('root', 'root', 0, '');
        
        this.getHTML = cRootNode_getHTML;
        this.doInit = cRootNode_doInit;
        this.doOpen = cRootNode_doOpen;
        this.doShowSearch = cRootNode_doShowSearch;
        this.fnChildrenID = cRootNode_fnChildrenID;
}

function cRootNode_getHTML()
{
        var sHTML = '';
        var aLinkHTML = [];
        
        sHTML += '<div id="' + this.sOID + '_ChildrenTD"></div>';
        return sHTML;
}

function cRootNode_doInit()
{
        this.oChildrenTD        = document.getElementById(this.sOID + '_ChildrenTD');

        window[this.sInstanceName] = this;
}

function cRootNode_doOpen(){}
function cRootNode_doShowSearch()
{
        this.oChildrenTD.innerHTML = "идет поиск...";
}

function cRootNode_fnChildrenID(sSeparator)
{
        var sList ='' ,i;
        
        for (i in this.aChildrenObj)
        {
                sList += (sList.length) ? sSeparator: '';
                sList += this.aChildrenObj[i].sID;
        }
        
        return sList;
}


function doLoadCallBack(i)
{
        var oReq = gaReq[i];

        // only if req shows "loaded"
    if (oReq.readyState == 4) 
        {
        // only if "OK"
        if (oReq.status == 200) 
                {
            var aChildrenStrings = [];
                        var aChildProps = [];
                        var aChildrenObj = [];
                        var i;
                        
                        aChildrenStrings = oReq.responseText.split('{o}');
                        for (j in aChildrenStrings)
                        {
                                aChildProps = aChildrenStrings[j].split('{p}');
                                if (aChildProps[0].length > 0)
                                {
                                        aChildrenObj[aChildrenObj.length] = new cNode(aChildProps[0], aChildProps[1], aChildProps[2]);
                                }
                        }

                        var oCaller = gaCallers[i];
                        
                        oCaller.doReceiveChildren(aChildrenObj, true);
                        
                        delete gaReq[i];
                        delete gaCallers[i];
                        
        }
    }
}

function fnGetRequest()
{
    var oReq;
        
        // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
        try {
                        oReq = new XMLHttpRequest();
        } catch(e) {
                        oReq = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
        try {
                oReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
                try {
                        oReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                        oReq = false;
                }
                }
    }
        
        return oReq;
}

function doLoadChildren(oCaller) 
{
        var oReq, i, sURL;
        
        i = gaReq.length;
        
        oReq = fnGetRequest();

        if(oReq) {
                eval('oReq.onreadystatechange = function (){doLoadCallBack('+i+')};');
                
                gaReq[i] = oReq;
                gaCallers[i] = oCaller;
                sURL = '/ajax/treeEngine/get_children.php?id='+oCaller.sID;
                gaReq[i].open("GET", sURL , true);
                gaReq[i].send("");
        }
}

function doSearch(sText, bInStart)
{
        doInitialState();
        
        if (sText == '')
        {
                return;
        }
        
        goSearchRequest = fnGetRequest();
        if (goSearchRequest)
        {
                oRoot.doShowSearch();
                
                goSearchRequest.onreadystatechange = doSearchCallback;
                goSearchRequest.open("GET", '/ajax/treeEngine/get_children.php?search='+encodeURI(sText)+'&instart=' + (bInStart+0) + '&roots=' + oRoot.fnChildrenID(';'), true);
                goSearchRequest.send("");
                
        }
}

function doSearchCallback()
{
        // only if req shows "loaded"
    if (goSearchRequest.readyState == 4) 
        {
        // only if "OK"
        if (goSearchRequest.status == 200) 
                {
                        if (goSearchRequest.responseText.length > 0)
                        {
                                //document.getElementById('debug').innerHTML = goSearchRequest.responseText;
                                eval(goSearchRequest.responseText);
                        }
                        else
                        {
                                alert('По вашему запросу ничего не найдено');
                                doInitialState();
                        }
        }
    }
}

function doPreloadPic(url)
{
        n=new Image();
        n.src=url;
}
doPreloadPic('img/ul_catalog.gif');
doPreloadPic('img/ul_catalogAct.gif');

function doResetAll()
{
        var i;
        
        for(i in aNodes['root'])
        {
                aNodes['root'][i].doCheck(false, false, true);
        }
}