function CntInit()
{
	var oSub;

	oSub = document.getElementById('cntSub');
	CntLoadLevel(oSub,'-1');
}

function CntOnLevelLoad(XMLReq)
{
	var oSub, oTable, oRow, oCell;
	var DSTable, DSRecord;
	var sImgSrc, sRef, sName;
	var i;

	oSub = XMLReq.Param;
	if(!oSub)
		return;

	oTable = document.createElement('table');
	oTable.border = '0';
	oTable.cellspacing='0';
	oTable.width='100%';

	DSTable = XMLReq.Req.responseXML.getElementsByTagName('l');
	for(i = 0; i < DSTable.length; i++)
	{
		DSRecord = DSTable[i];
		sImgSrc = GetField(DSRecord, 'ImgSrc');
		sRef = GetField(DSRecord, 'ref');
		sName = GetField(DSRecord, 'name');

		oRow = oTable.insertRow(-1);

		oCell = oRow.insertCell(-1);
		oCell.width='24';
		oCell.vAlign='top';
		oCell.innerHTML = '<img border="0" src="'+sImgSrc+'" width=24 height=20 onclick="CntToggle(this);">';

		oCell = oRow.insertCell(-1);

		if(sRef.substr(0,1) == '?')
			oCell.innerHTML = '<a id="r'+sRef.substr(1)+'" href="javascript:CntToggleById(\'r'+sRef.substr(1)+'\');" refid="'+sRef+'" style="color:#000060">'+sName+'</a><span style="display:none"></span>';
		else
			oCell.innerHTML = '<a href="'+sRef+'" target="_blank">'+sName+'</a>';
	}

	oSub.parentNode.replaceChild(oTable, oSub);
}

function CntLoadLevel(oSub, sId)
{
	var bExpandable;

	if(sId != '')
	{
		oSub.innerHTML ='<br><i>Загрузка...</i>'
		LoadXML('./?List&Id='+sId, CntOnLevelLoad, oSub);
		bExpandable = true;
	}
	else
	{
		oSub.parentNode.replaceNode(document.createElement('table'));
		bExpandable = false;
	}

	return bExpandable;
}

function CntToggleById(sElem)
{
	var oElem;

	oElem = document.getElementById(sElem);
	if(!oElem)
		retrun;

	CntToggle(oElem);
}

function CntToggle(oElem)
{
	var oRow, oExpImg, oCell, oRef, oSub;
	var sId, sSubStyleDisplay;
	var bExpandable;

	oRow = oElem;
	while(oRow.tagName.toUpperCase() != 'TR')
	{
		oRow = oRow.parentNode;
		if(!oRow)
			break;
	}
   	if(!oRow)
	{
		alert('internal error (unk Row)');
		return;
	}

	oExpImg = oRow.getElementsByTagName('img')[0];

	oCell = oRow.cells[1];
	if(!oCell)
	{
		alert('internal error (unk Cell)');
		return;
	}

	oRef = oCell.firstChild;
	sId = oRef.getAttribute('refid',0).substr(1);
	if(sId == '')
		return;
   
	oSub = oRef.nextSibling;
	if(!oSub)
	{
		alert('internal error (unk Sub)');
		return;
	}

	sSubStyleDisplay = oSub.style.display;

	if(oSub.tagName.toUpperCase() != 'TABLE')
	{
		oSub.style.display = '';
		bExpandable = CntLoadLevel(oSub, sId);
	}
	else
	{
		if(oSub.innerHTML != '')
			bExpandable = true;
		else
			bExpandable = false;
	}

	if(bExpandable)
	{
		if(sSubStyleDisplay == 'none')
		{
			oSub.style.display = '';
			oExpImg.src = oExpImg.src.replace('.gif', '_e.gif');
		}
		else
		{
			oSub.style.display = 'none';
			oExpImg.src = oExpImg.src.replace('_e.gif', '.gif');
		}
	}
}

