var lineList2 = Array();
var doresize = null;
var mouseMode = 0;
var can_show = false;

window.onresize = TreeResize;
document.onresize = TreeResize;

window.onunload = TreeUnload;
document.onunload = TreeUnload;

try
{
    if (ff())
    {
        document.onmouseup = function()
        {
        	var person = isAnyPerson();
        	if (person)
        	    person.onmouseup();
        	    //alert(person.id + ' - mouse up!');
        }
    }
}
catch (e)
{
	// not loaded yet
}

function TreeResize()
{
	var div = g('person_list_div');
	if (!div)
		return;
	var new_width = parseInt(document.body.clientWidth) - 650;
	div.style.width = Math.max(new_width, 260);
	ScrollPage();
}

function TreeUnload()
{
	/*
	var b = g('save_button');
	if (!b)
		return;
	if (b.className != 'button green')
		return;

    if (confirm('Вы хотите сохранить дерево перед выходом?'))
    {
    	SaveTree();
    	document.location.href = "#";
    	return false;
    }
    else
    {

    }
    */
}

function DragPerson(person_id)
{
	var person_div = document.getElementById("person" + person_id + "_list"); // do not change!
	if (!person_div)
	{
	    //alert('no div person!');
	    return false;
	}
	var moveDiv = document.getElementById("moveDiv");
	moveDiv.innerHTML = '<div class="rect">' + person_div.innerHTML + '</rect>';
	var m = mousePageXY();
	setMove(moveDiv);
	moveDiv.style.left = parseInt(m.x); // - 50;
	moveDiv.style.top = parseInt(m.y); // - 50;
	moveDiv.style.width = "106px";
	moveDiv.style.height = "115px";
	moveDiv.style.border = "0px";
	moveDiv.style.visibility = "visible";
	moveDiv.person_id = person_id;
	moveDiv.onmouseup = function()
	{
		//DropPerson();
		// реализовано в самой форме
    }
    return true;
}

function PersonMouseDown(obj)
{
	edit_tree(true);
	var lt = mouseMode;
	if (lt == 0)
	    setMove(obj, null, null, false);
	if (lt == 1 || lt == 3 || lt == 7 || lt == 2)
	{
	    var m2 = mousePageXY();
	    moveDiv.style.left = m2.x;
	    moveDiv.style.top = m2.y;
	    moveDiv.innerHTML = "";
	    moveDiv.style.width = "0px";
	    moveDiv.style.height = "0px";
	    moveDiv.person_id = null;
	    setMove(moveDiv);
	    AddLine2(obj, moveDiv, lt);
	}
}

function PersonMouseUp(obj)
{
    if (lineList2.length > 0 && lineList2[lineList2.length - 1]['to'].id == 'moveDiv')
    {
        var exists = false;
        var cline = null;
        var from_obj = lineList2[lineList2.length - 1]['from'];
        for (var i = 0; i < lineList2.length - 1; i++)
       	{
       	    cline = lineList2[i];
       	    if (cline['from'] == from_obj && cline['to'] == obj || cline['from'] == obj && cline['to'] == from_obj)
       	    {
       	        if (mouseMode == 2)
       	        {
       	            document.getElementById('linksDropped').removeChild(lineList2[i]['line']);
       	            lineList2.deleteItem(i);
       	            document.getElementById('linksDropped').removeChild(lineList2[lineList2.length - 1]['line']);
       	            lineList2.length = lineList2.length - 1;
       	            //lineList2.deleteItem(lineList2.length - 1);
       	            SetMouseMode(0);
       	            return;
       	        }
       	        else
           	        exists = true;
       	        break;
       	    }
       	}
       	if (mouseMode == 2)
       	    return;
       	if (exists)
       	    alert("Связь между этими родственниками уже существует");
       	if (obj == from_obj)
       	    exists = true;

       	if (!exists)
       	{
            lineList2[lineList2.length - 1]['to'] = obj;
            RecombineTreeItems2();
            SetMouseMode(0);
        }
    }
    setMove(null, 0, 0);
}

function a(text)
{
	if (can_show)
		alert(text);
}

function DropPerson(only_hide, drop_x, drop_y)
{
    var moveDiv = document.getElementById("moveDiv");
    moveDiv.style.visibility = "hidden";
    moveDiv.style.left = "0px";
    moveDiv.style.top = "0px";

    var moveDiv = document.getElementById('moveDiv');
    var lastLine = lineList2[lineList2.length - 1];
    if (lastLine != null && lastLine['to'] == moveDiv)
    {
        document.getElementById('linksDropped').removeChild(lineList2[lineList2.length - 1]['line']);
        lineList2.length = lineList2.length - 1;
    }

    //if (only_hide == true)
    //    return;

    if (moveobject != null && moveobject.person_id != null)
    {
        var m = null;
        if (drop_x && drop_y)
            m = {"x" : drop_x, "y" : drop_y};
        else
        {
            m = mousePageXY();
            // защита от переноса в зону панели
            if (m.y > parseInt(g('tree_options_block').style.top))
            	return;
            edit_tree(true);
            //alert(m.x + '\n' + m.y);
        }
	    person = document.createElement("div");
	    person.className = "rect";
	    person.id = "person" + moveobject.person_id + "_wrapper";
	    person.style.left = m.x + "px";
	    person.style.top =  m.y + "px";
	    person.style.width = 116 + "px";
	    person.style.height = 125 + "px";
	    person.style.border = "0px";
	    person.style.vertialAlign = "middle";
	    p_z_index++;
	    person.style.zIndex = p_z_index;
	    person.onmousedown = function()
	    {
            PersonMouseDown(this);
            SelectPerson(this, 2);
	    }
	    person.onmouseup = function()
	    {
            PersonMouseUp(this);
            SelectPerson(this, 1);
	    }
	    person.onselectstart = function()
	    {
	        return false;
	    }
	    person.onmouseover = function()
	    {
	        // ShowMenu(this);
	        SelectPerson(this, 1);
	    }
	    person.onmouseout = function()
	    {
	        SelectPerson(this);
	    }
	    person.innerHTML = moveDiv.innerHTML;
	    document.getElementById('personsDropped').appendChild(person);

	    if (tree_mode == 'edit')
	    {
    	    RegisterMenu('arrows_menu', g(person.id), 0, 2);
        }

        if (moveobject.person_id)
        {
	        var list_person = document.getElementById("person" + moveobject.person_id + "_list"); // do not change!
	        list_person.style.display = "none";
	        var list = document.getElementById("person_list_div");
	        list.style.visibility = "hidden";
	        list.style.visibility = "visible";
	    }
	}
    setMove(null, 0, 0);
}

function AddLine2(from_d, to_d, lt, prop)
{
    if (!from_d || !to_d)
        return;

    if (!prop)
        prop = 48;
    var prop2 = 100 - 4 - prop;
    l_z_index++;
    var line;
    var draw_from_side = line_types[lt];
    for (var i = 0; i < lineList2.length; i++)
    {
        line = lineList2[i];
        if (line['from'] == from_d && line['to'] == to_d)
            return false;
        if (line['from'] == to_d && line['to'] == from_d)
            return false;
    }

    line = document.createElement("div");
    line.className = "black_line";
    line.id = "line" + l_z_index;
    line.style.left = 0;
    line.style.top = 0;
    line.style.width = 0;
    line.style.height = 0;
    line.style.zIndex = l_z_index;
    line.style.border = "0px";
    line.onmousemove = function()
    {
        if (!doresize)
            ChangeLinesOrder();
        ResizeLine(doresize, (draw_from_side == true) ? 1 : 2, this);
    }
    line.onmouseup = function()
    {
    	DropPerson();
    }

    var part1 = document.createElement("div");
    part1.className = draw_from_side ? "leftFloat" : "topFloat";
    part1.id = line.id + "_part1";
    part1.style.width = draw_from_side ? prop + "%" : "100%";
    part1.style.height = draw_from_side ? "100%" : prop + "%";
    part1.style.border = "0px";
    line.appendChild(part1);

    var part0 = document.createElement("div");
    part0.className = draw_from_side ? "leftFloat" : "topFloat";
    part0.id = line.id + "_part0";
    part0.style.width = draw_from_side ? "1%" : "100%";
    part0.style.height = draw_from_side ? "100%" : "1%";
    part0.style.cursor = draw_from_side ? "w-resize" : "n-resize";
    part0.style.border = "0px";
    part0.onmousedown = function()
    {
        doresize = line;
    }
    line.appendChild(part0);

    var part2 = document.createElement("div");
    part2.className = draw_from_side ? "leftFloat" : "topFloat";
    part2.id = line.id + "_part2";
    part2.style.width = draw_from_side ? prop2 + "%" : "100%";
    part2.style.height = draw_from_side ? "100%" : prop2 + "%";
    part2.style.border = "0px";
    line.appendChild(part2);

    var l = lineList2.length;
    lineList2[l] = Array();
    lineList2[l]['from'] = from_d;
    lineList2[l]['to'] = to_d;
    lineList2[l]['lt'] = lt;
    lineList2[l]['line'] = line;


    document.getElementById('linksDropped').appendChild(line);
}

function RedrawLine2(l)
{
	try
	{
		var min_size = 10;

		var line = l['line'];
		var part0 = document.getElementById(line.id + "_part0");
		var part1 = document.getElementById(line.id + "_part1");
		var part2 = document.getElementById(line.id + "_part2");
		var d1 = l['from'];
		var d2 = l['to'];

		var d1c = GetCenter(d1);
		var d2c = GetCenter(d2);
		line.style.left = Math.min(d1c.x, d2c.x);
		line.style.top = Math.min(d1c.y, d2c.y);
		line.style.width = Math.abs(d1c.x - d2c.x);
		line.style.height = Math.abs(d1c.y - d2c.y);

		var w = line_widths[l['lt']];

		var color = line_colors[l['lt']];
		if (line_types[l['lt']] == true)
		{
		    var top_left = 0;
		    if (d1c.y < d2c.y && d1c.x < d2c.x || d1c.y > d2c.y && d1c.x > d2c.x)
		        top_left = w;
		    part1.style.borderTop = top_left.toString() + "px solid " + color;
		    part1.style.borderBottom = (w - top_left).toString() + "px solid " + color;
		    if (parseInt(line.style.height) > min_size)
		    {
		        part0.style.borderTop = (w - top_left).toString() + "px solid " + color;
		        part0.style.borderBottom = top_left.toString() + "px solid " + color;
		        part2.style.borderTop = (w - top_left).toString() + "px solid " + color;
		        part2.style.borderBottom = top_left.toString() + "px solid " + color;
		        part1.style.borderRight = w.toString() + "px solid " + color;
		    }
		    else
		    {
		        part0.style.borderTop = top_left.toString() + "px solid " + color;
		        part0.style.borderBottom = (w - top_left).toString() + "px solid " + color;
		        part2.style.borderTop = top_left.toString() + "px solid " + color;
		        part2.style.borderBottom = (w - top_left).toString() + "px solid " + color;
		        part1.style.borderRight = "0px";
		    }
		}
		else
		{
	        var top_left = 0;
		    if (d1c.y < d2c.y && d1c.x < d2c.x || d1c.y > d2c.y && d1c.x > d2c.x)
		        top_left = w;
		    part1.style.borderLeft = top_left.toString() + "px solid " + color;
		    part1.style.borderRight = (w - top_left).toString() + "px solid " + color;
	        if (parseInt(line.style.width) > min_size)
	        {
		        part0.style.borderLeft = (w - top_left).toString() + "px solid " + color;
		        part0.style.borderRight = top_left.toString() + "px solid " + color;
		        part2.style.borderLeft = (w - top_left).toString() + "px solid " + color;
		        part2.style.borderRight = top_left.toString() + "px solid " + color;
	            part1.style.borderBottom = w.toString() + "px solid " + color;
		    }
		    else
		    {
		        part0.style.borderLeft = top_left.toString() + "px solid " + color;
		        part0.style.borderRight = (w - top_left).toString() + "px solid " + color;
		        part2.style.borderLeft = top_left.toString() + "px solid " + color;
		        part2.style.borderRight = (w - top_left).toString() + "px solid " + color;
		        part1.style.borderBottom = "0px";
		    }
		}
	}
	catch (e) { /* some null vars */ }
}

function RecombineTreeItems2()
{
	var line = null;
	for (var i = 0; i < lineList2.length; i++)
	{
	    //line = lineList2[i];
	    //if (moveobject.style != null && moveobject.style.zIndex != null && (line['from'] == moveobject && line['from'].style.zIndex == moveobject.style.zIndex || line['to'] == moveobject && line['to'].style.zIndex == moveobject.style.zIndex))
	        RedrawLine2(lineList2[i]);
	}
}

/*
function GetLinkType()
{
    if (document.getElementById("link_type_form") == null)
	{
        return 0;
	}
    for (var i = 0; i < link_type_form.link_type.length; i++)
    {
        if (link_type_form.link_type[i].checked)
            return link_type_form.link_type[i].value;
    }
    return null;
}
*/

function ResizeLine(line, rtype, where_moved)
{
	if (!doresize)
	    return;
	if (where_moved != line)
	    return;
	var min_p = 5;
	var p1 = document.getElementById(line.id + "_part1");
	var p2 = document.getElementById(line.id + "_part2");
	m = mousePageXY();
	if (rtype == 1)
	{
		var left = m.x - parseInt(line.style.left);
		var all = parseInt(line.style.width);
		left = Math.round(left/all * 100);
		left = Math.max(min_p, left);
		left = Math.min(100 - min_p, left);
		p1.style.width = (left - 2) + "%";
	    p2.style.width = (100 - left - 2) + "%";
    }
    if (rtype == 2)
	{
		var top = m.y - parseInt(line.style.top);
		var all = parseInt(line.style.height);
		top = Math.round(top/all * 100);
		top = Math.max(min_p, top);
		top = Math.min(100 - min_p, top);
		p1.style.height = (top - 2) + "%";
	    p2.style.height = (100 - top - 2) + "%";
    }
}

function isAnyLine(l)
{
    var line = l['line'];
    if (!line)
        return false;

    var m = mousePageXY();
    var x1 = m.x - parseInt(line.style.left);
    var y1 = m.y - parseInt(line.style.top);

    if (x1 > parseInt(line.style.width) && x1 < 0 && y1 < 0 && y1 > parseInt(line.style.height))
        return false;

    if (line_types[l['lt']] == true)
    {
        var part1 = document.getElementById(line.id + "_part1");
        var start_x = parseInt(part1.style.width) / 100 * parseInt(line.style.width);
        if (x1 >= start_x && x1 <= start_x + 5)
            return true;
    }

    if (line_types[l['lt']] == false)
    {
        var part1 = document.getElementById(line.id + "_part1");
        var start_y = parseInt(part1.style.height) / 100 * parseInt(line.style.height);
        if (y1 >= start_y && y1 <= start_y + 5)
            return true;
    }

    return false;
}

function ChangeLinesOrder()
{
	var line = null;
	for (var i = 0; i < lineList2.length; i++)
	{
        line = lineList2[i];
        if (line)
        {
            var founded = isAnyLine(line);
            if (founded)
            {
	            for (var j = 0; j < lineList2.length; j++)
	            {
	            	var line2 = lineList2[j]['line'];
	            	if (line2)
	            	{
	            		line2.style.zIndex = j;
	            	}
	            	if (line2 == line['line'])
	            	{
	            		line2.style.zIndex = 200;
	            	}
	            }
            }
        }
        if (founded)
            break;
	}
}

var item_prefix = '_item_';
var link_prefix = '_link_';
var sfrom = null;

function ClearSaveForm()
{
	var sform = document.getElementById("save_tree_form");
	id = 0;
	sform.innerHTML = '';
}

function AddItem(item_id, pos_x, pos_y)
{
    if (item_id <= 0)
        return false;
    if (pos_x < 0 || pos_y < 0)
        return false;
    pos_y = parseInt(pos_y); // - 151;
    var add = '<input type="hidden" name="' + item_prefix + id + '" value="' + parseInt(item_id) + '_' + parseInt(pos_x) + '_' + parseInt(pos_y) + '">';
    id++;
    sform.innerHTML = sform.innerHTML + add;
    return true;
}

function AddLink(from_id, to_id, link_type, prop)
{
    if (from_id <= 0 || to_id <= 0)
        return false;
    if (link_type < 0 || prop < 0)
        return false;
    var add = '<input type="hidden" name="' + link_prefix + id + '" value="' + parseInt(from_id) + '_' + parseInt(to_id) + '_' + parseInt(link_type) + '_' + parseInt(prop) + '">';
    id++;
    sform.innerHTML = sform.innerHTML + add;
    return true;
}

function SaveTree()
{
	edit_tree(false);
	sform = document.getElementById("save_tree_form");
	ClearSaveForm();
	var l = null;
	var prop = null;
	for (var i = 0; i < lineList2.length; i++)
	{
		l = lineList2[i];
		prop = document.getElementById(l['line'].id + '_part1').style;
		AddLink(l['from'].id.substring(6), l['to'].id.substring(6), l['lt'], line_types[l['lt']] == true ? prop.width : prop.height);
	}
	var all = document.getElementById('personsDropped').getElementsByTagName('*');
	for (var i = 0; i < all.length; i++)
	{
		if (all[i].className.toLowerCase() == 'rect' && all[i].id.substring(0, 6).toLowerCase() == 'person')
		{
            AddItem(all[i].id.substring(6), all[i].style.left, all[i].style.top);
  		}
	}
	sform.submit();
}

function ShowTree(items, lines)
{
    var max_top = 0;
    var min_top = 5000;

    for (var i = 0; i < items.length; i++)
    {
    	if (DragPerson(items[i][0]))
    	{
    		DropPerson(false, items[i][1], items[i][2]);
	    	max_top = Math.max(max_top, items[i][2]);
	    	min_top = Math.min(min_top, items[i][2]);
	  	}
    }

    if (tree_mode == 'edit' || tree_mode == 'manual')
    {
        if (min_top > level_h + footer_h)
        {
    		var move_levels = Math.round((min_top - footer_h) / level_h);
    		MovePersons(-1 * move_levels * level_h);
    		min_top -= move_levels * level_h;
        	max_top -= move_levels * level_h;
    	}
    }

    var need_levels = Math.round((max_top - footer_h) / level_h) + 4 + ((items.length == 0) ? 1 : 0);
    var layersPanel = document.getElementById('layersPanel');
    layersPanel.innerHTML = '';
    for (var l = 0; l < need_levels; l++)
    {
        var d = document.createElement("div");
        d.className = "level" + ((l % 2) + 1);
        layersPanel.appendChild(d);
    }

    for (var i = 0; i < lines.length; i++)
    {
    	AddLine2(document.getElementById('person' + lines[i][0] + '_wrapper'), document.getElementById('person' + lines[i][1] + '_wrapper'), lines[i][2], lines[i][3]);
    }

    RecombineTreeItems2();
    RegisterAllMenus();
}

function ClearTree()
{
   	var plist = document.getElementById('personsDropped');
   	if (ff()) plist.all = plist.getElementsByTagName('*');
   	for (var i = plist.all.length - 1; i >= 0; i--)
   	{
   		if (plist.all[i].className == 'rect' && plist.all[i].id.substring(0,6) == 'person')
   		    plist.removeChild(plist.all[i]);
   	}

   	var plist = document.getElementById('person_list_div');
   	if (ff()) plist.all = plist.getElementsByTagName('*');
   	for (var i = plist.all.length - 1; i >= 0; i--)
   	{
   		if (plist.all[i].className == 'rect' && plist.all[i].id.substring(0,6) == 'person')
   		    plist.all[i].style.display = 'block';
   	}

   	var lineContainer = document.getElementById('linksDropped');
   	for (var i = 0; i < lineList2.length; i++)
   	{
        lineContainer.removeChild(lineList2[i]['line']);
   	}

   	lineList2.length = 0;
   	l_z_index = 100;
}

function RollBackTree(items, lines)
{
    if (confirm('Вы уверены, что хотите отменить все внесённые изменения?'))
    {
        ClearTree();
   	    ShowTree(items, lines);
   	    edit_tree(false);
    }
}

function ResetTree()
{
    if (confirm('Вы уверены, что хотите очистить дерево и начать его создание заново?'))
    {
        ClearTree();
        edit_tree(true);
    }
}

function SelectPerson(person, type)
{
	if (!person)
	    return false;

    var colors = new Array('', '#FF0000', '#00FF00');
    var b_colors = new Array('', '#cdf350', 'lime');
    if (type)
    {
        //person.style.border = '1px solid ' + colors[type];
        person.style.backgroundColor = b_colors[type];
        person.style.borderWidth = '1px';
    }
    else
        //person.style.border = '0px';
        person.style.backgroundColor = '';
    return true;
}

function SetMouseMode(mode)
{
    if (mode >= 0)
        mouseMode = mode;
    var help = new Array(
        'Для перемещения человека нажмите и удерживайте на нём кнопку мыши и переместите в новое место',
        'Наведите мышь на одного из родителей, нажмите и переместите на сына/дочь, удерживая кнопку мыши нажатой, затем отпустите',
        'Наведите мышь на одного из людей, связь с которым надо разорвать, нажмите и переместите на второго, удерживая кнопку нажатой',
        'Наведите мышь на брата/сестру, нажмите и переместите на другого брата/сестру, удерживая кнопку мыши нажатой, затем отпустите',
        '', '', '',
        'Наведите мышь на одного из супругов, нажмите и переместите на второго супруга, удерживая кнопку мыши нажатой, затем отпустите'
    )
    var helpSpan = document.getElementById('helpSpan');
    if (helpSpan)
        helpSpan.innerHTML = help[mode];
    if (document.getElementById('link_type_form') != null)
    {
        for (var i = 0; i < link_type_form.link_type.length; i++)
        {
    	    if (link_type_form.link_type[i].value == mode)
    	    {
    		    link_type_form.link_type[i].checked = true;
    		    return;
    	    }
        }
    }
}

function AddLevel(top)
{
	var layersPanel = document.getElementById('layersPanel');

	if (ff()) layersPanel.all = layersPanel.getElementsByTagName('*');
	var default_class = "level1";
	{
    	if (top)
    	{
    		for (var i = 0; i < layersPanel.all.length; i++)
            {
                var layer = layersPanel.all[i];
                switch (layer.className)
                {
                	case "level1":
                	    layer.className = "level2";
                	    break;
                	case "level2":
                	    layer.className = "level1";
                	    break;
                }
            }
            MovePersonsDown(level_h);
        }
	}
	var new_layer = document.createElement("div");
	var last_layer = layersPanel.all[layersPanel.all.length - 1];
	if (last_layer)
	    default_class = (last_layer.className == "level1") ? "level2" : "level1";
	new_layer.className = default_class;
	layersPanel.appendChild(new_layer);
}

function edit_tree(mode)
{
	var b = g('save_button');
	var mar = g('options_marquee');
	if (!b || !mar)
		return;
	b.className = (mode == true) ? 'button green' : 'button gray';
	mar.style.display = (mode == true) ? 'block' : 'none';
    return;
}

function isAnyPerson()
{
    var m = mousePageXY();

    var personsDropped = document.getElementById('personsDropped');
    if (!personsDropped)
        return;
    var persons = personsDropped.getElementsByTagName('*');
    for (var i = 0; i < persons.length; i++)
    {
    	var person = persons[i];
    	if (!person.style)
    	    continue;
    	var l = parseInt(person.style.left);
    	var t = parseInt(person.style.top);
    	var w = parseInt(person.style.width);
    	var h = parseInt(person.style.height);

    	if (m.x <= l+w && m.x >= l && m.y <= t+h && m.y >= t)
    	    return person;
    }

    return false;
}

function SendAddRequest()
{
	g('popup1_img').style.display = 'block';
	g('popup1_status').innerHTML = '';
	var url = 'action=add_new_user&add_for=' + current_person_id;
	var params = Array('add_type', 'first_name', 'last_name', 'middle_name', 'bd_year', 'bd_month', 'bd_day', 'email');
	for (var i = 0; i < params.length; i++)
	{
		url += '&' + params[i] + '=' + g('popup1_' + params[i]).value;
	}
	url += (g('popup1_died').checked) ? '&died=checked' : '&died=no';
    var res = MakeAjaxRequest(url, false);
    g('popup1_img').style.display = 'none';
    if (res.substring(0,1) == '1')
    {
        //g('popup1_status').innerHTML = 'Родственник успешно добавлен';
        var p_type = g('popup1_add_type').value;
        HideForm();
        var person_list_div = g('person_list_div');
        var new_div = document.createElement('div');
        new_div.innerHTML = res.substring(1);
        var new_id = ParseId(new_div.getElementsByTagName('*')[0].id);
        person_list_div.innerHTML = person_list_div.innerHTML + new_div.innerHTML;
        DragPerson(new_id);
        var m = mousePageXY();
        DropPerson(false, m.x, m.y);
        setMove(g('person' + new_id + '_wrapper'));
        AddLine2(g('person' + current_person_id + '_wrapper'), g('person' + new_id + '_wrapper'), p_type);
        RecombineTreeItems2();
    }
    else
    {
    	g('popup1_status').innerHTML = '<br>' + res.substring(1);
    }
}