
function ICL_treeSetup(only_cat_edit)
{
    var p = document.getElementById("ICL_tree");
    
    ajax_request('XMLProvider.do?ingredientcategorychildren=0','ICL_treeFill', new Array( '0', only_cat_edit ));
    
    p.innerHTML = '<div id="ICL_tree_0_categories" class="ajaxTreeNode"><img src="icons/loading.gif" ></div>';
    
}

function ICL_treeOpen(id, only_cat_edit)
{
    var e = document.getElementById("ICL_tree_"+id+"_head");
    var b = document.getElementById("ICL_tree_"+id+"_boxim");
    var c = document.getElementById("ICL_tree_"+id+"_categories");
    var i = document.getElementById("ICL_tree_"+id+"_ingredients");
    
    ajax_request('XMLProvider.do?ingredientcategorychildren='+id,'ICL_treeFill', new Array( id, only_cat_edit ));
    if ( only_cat_edit == 0 )
    {
        ajax_request('XMLProvider.do?ingredientsincategory='+id,'ICL_treeFillIngredients', new Array( id ));
        i.innerHTML = '<img src="icons/loading.gif" >';
    }
    c.innerHTML = '<img src="icons/loading.gif" >';
    b.onclick = function() {  ICL_treeClose(id+"",only_cat_edit) }
    b.src = 'icons/box_minus.gif';
    createCookie("ICL_tree_"+id,"1",1);
}
function ICL_treeClose(id,only_cat_edit)
{
    var e = document.getElementById("ICL_tree_"+id+"_head");
    var b = document.getElementById("ICL_tree_"+id+"_boxim");
    var c = document.getElementById("ICL_tree_"+id+"_categories");
    var i = document.getElementById("ICL_tree_"+id+"_ingredients");
    i.innerHTML = '';
    c.innerHTML = '';
    b.onclick = function() {  ICL_treeOpen(id+"",only_cat_edit) }
    b.src = 'icons/box_plus.gif';
    ereaseCookie("ICL_tree_"+id);
}

function ICL_treeFill(req,id,only_cat_edit)
{
    var data = req.responseXML.getElementsByTagName("xml")[0];
    var ics = data.getElementsByTagName("ingredientcategory");
    var html = '';
    var delayOpen = null;
    if ( ics != null && ics.length > 0 )
    {
        var i;
        var icid;
        var icname;
        for ( i = 0 ; i<ics.length ; i++ )
        {
            var icid = ics[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
            var icname = ics[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
            
            html += '<div id="ICL_tree_'+icid+'" class="ajaxTreeNode">'
            html += '<img id="ICL_tree_'+icid+'_boxim" onClick="ICL_treeOpen(\''+icid+'\',\''+only_cat_edit+'\')" src="icons/box_plus.gif" class="ajaxTreeNodeBoxIm"/>';
            html += '<span id="ICL_tree_'+icid+'_head" class="ajaxTreeNodeHead" >';
            html += icname+'</span>';
            if ( only_cat_edit == 1 )
            {
                html += '<span class="ajaxTreeNodeIcons">';
                html += '<a href="IngredientCategoryEdit.do?id='+icid+'">';
                html += '<img src="icons/page_edit.png" border="0"/>';
                html += '</a>';
                html += '<a href="IngredientCategoryList.do?delete_ingredientcategory='+icid+'">';
                html += '<img src="icons/delete.png" border="0"/>';
                html += '</a></span>';
            }
            html += '<div id="ICL_tree_'+icid+'_categories"></div>';
            html += '<div id="ICL_tree_'+icid+'_ingredients"></div>';
            html += '</div>';
            if ( readCookie("ICL_tree_"+icid)!=null )
            {
                if ( delayOpen == null )
                {
                    delayOpen = new Array(icid+"");
                } else {
                    delayOpen[delayOpen.length] = icid+"";
                }
            }
        }
    }

    var e = document.getElementById("ICL_tree_"+id+"_categories");
    if ( e != null )
    {
        e.innerHTML = html;
        if ( delayOpen != null )
        {
            for ( i = 0 ; i<delayOpen.length ; i++ )
            {
                ICL_treeOpen(delayOpen[i],only_cat_edit);
            }
        }
    }
}
function ICL_treeFillIngredients(req,id)
{
    var data = req.responseXML.getElementsByTagName("xml")[0];
    var ics = data.getElementsByTagName("ingredient");
    var html = '';
    
    if ( ics != null && ics.length > 0 )
    {
        var i;
        var icid;
        var icname;
        for ( i = 0 ; i<ics.length ; i++ )
        {
            var icid = ics[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
            var icname = ics[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
            
            html += '<div id="ICL_tree_'+icid+'" class="ajaxTreeNode">'+icname;
            html += '<span class="ajaxTreeNodeIcons">';
            html += '<a href="IngredientEdit.do?id='+icid+'">';
            html += '<img src="icons/page_edit.png" border="0"/>';
            html += '</a>';
            html += '<a href="IngredientList.do?delete_ingredient='+icid+'">';
            html += '<img src="icons/delete.png" border="0"/>';
            html += '</a></span>';
            html += '</div>';         
        }
    }
    
    var e = document.getElementById("ICL_tree_"+id+"_ingredients");
    if ( e != null )
        e.innerHTML = html;
   
}


