	var treeLineActive = 'tree_line_active';

	function mayBeAction(destination_cluster_id)
	{
		if(!actionCopyMove || !actionSource)return false;

		if(actionSource=='group')
		{
			if(!anyoneChecked)
			{
				pAlert(messages['tree_nothing_checked']);
				return false;
			}
			else
			{
				if(actionCopyMove=='m')
				{
					if(!validateGroupMove(document.forms.tree_form,destination_cluster_id))
					{
						pAlert(messages['tree_group_move_error']);
						return false;
					}
				}
				document.forms.tree_form.cluster_status.disabled=true;
				document.forms.tree_form.cluster_id_destination.value=destination_cluster_id;
				document.forms.tree_form.operation.value=(actionCopyMove=='m'?'cluster_move':'cluster_copy');
				document.forms.tree_form.submit();
			}
			return true;
		}
		else if(actionCopyMove=='m' && actionSource==destination_cluster_id)
		{
			pAlert(messages['tree_move_to_itself']);
			return false;
		}
		else if(actionCopyMove=='m' && isDescendant(destination_cluster_id,actionSource))
		{
			pAlert(messages['tree_parent_to_descendant_move']);
			return false;
		}
		else if(actionCopyMove=='m' && tree_data[actionSource]['parent']==destination_cluster_id)
		{
			pAlert(messages['tree_move_to_current_parent']);
			return false;
		}

		document.forms.copy_move_form.cluster_id.value=actionSource;
		document.forms.copy_move_form.cluster_id_destination.value=destination_cluster_id;
		document.forms.copy_move_form.operation.value=(actionCopyMove=='m'?'cluster_move':'cluster_copy');
		document.forms.copy_move_form.current_dimension.value=dimension;
		document.forms.copy_move_form.submit();
	}

	var prevColor;
	function setActionSource(act,src,lnk)
	{
		if(isPopupOpen())
			return false;

		if(actionSource)
		{
			if(actionSource!='group')
			{
				if(src!='group')
				{
					document.forms.tree_form['cluster_id['+actionSource+']'].checked = false;
					checkChildren(document.forms.tree_form['cluster_id['+actionSource+']'],true);
				}
			}
			actionLnk.style.color=prevColor;
		}
		actionSource = src;
		if(src!='group')
		{
			document.forms.tree_form['cluster_id['+actionSource+']'].checked = true;
			checkChildren(document.forms.tree_form['cluster_id['+actionSource+']'],true);
		}


		if(src=='group' && !anyoneChecked)
		{
			pAlert(messages['tree_nothing_checked']);

			if(actionLnk)
				actionLnk.style.color = prevColor;
			actionSource = false;
			actionCopyMove = false;

			return false;
		}


		actionCopyMove = act;
		actionLnk = lnk;
		if(!prevColor)prevColor = lnk.style.color;
		lnk.style.color='black';
	}

	function confirmF(mes,tr_active)
	{
		if(isPopupOpen())
			return false;

		//document.forms.tree_form['statpg_id['+tr_active+']'].checked = true;
		//checkChildren(document.forms.tree_form['cluster_id['+tr_active+']'],true);

		var ret_val = confirm(mes);


		return ret_val;
	}
	function isDescendant(clusterId1,clusterId2)
	{
		var t = clusterId1;
		while(tree_data[t]['parent'])
		{
			if(tree_data[t]['parent']==clusterId2)
				return true;
			t = tree_data[t]['parent'];
		}
		return false;
	}

	function groupAction(act,status)
	{
		if(isPopupOpen())
			return false;

		if(!anyoneChecked)
		{
			pAlert(messages['tree_nothing_checked']);
			return false;
		}
		else if(act=='delete')
		{
			if(!confirm(messages['statpg_confirm_delete']))
				return false;
		}
		document.forms.tree_form.cluster_id_destination.disabled=true;
		if(""+status=='undefined')
			document.forms.tree_form.cluster_status.disabled=true;
		else
			document.forms.tree_form.cluster_status.value=status;
		document.forms.tree_form.operation.value='statpg_'+act;
		document.forms.tree_form.submit();
	}
	function validateGroupMove(tree_form,destinationId)
	{
		var i,j,chk;
		for(i=0;i<tree_form.elements.length;i++)
		{
			if(tree_form.elements[i].type == 'checkbox' && tree_form.elements[i].checked)
			{
				chk = tree_form.elements[i];
				j = chk.name.indexOf('[');
				cluster_id = 1*chk.name.substr(j+1,chk.name.indexOf(']')-j-1);
				if(cluster_id>0)
				{
					if(cluster_id==destinationId || isDescendant(destinationId,cluster_id) || tree_data[cluster_id]['parent']==destinationId)
						return false;
				}
			}
		}
		return true;
	}
	function init_tree(ctb, onclick)
	{
		window['tree_data'] = window[ctb + 'tree_data'];

		var r_div;
		var table_arr,tr_arr
		var i,j;

		var tree_form;

		if(r_div = document.getElementById('treetable'))
		{
			table_arr = r_div.getElementsByTagName('table');
			for(i=0;i<table_arr.length;i++)
			{
				tr_arr = table_arr[i].getElementsByTagName('tr');
				for(j=0;j<tr_arr.length;j++)
				{
					tr_arr[j].onmouseover=function()
					{
							//this.style.backgroundColor = treeLineActive;
							this.className = treeLineActive;
					};
					tr_arr[j].onmouseout=function()
					{
							this.className = '';
							//this.style.backgroundColor = 'transparent';
					};

				}
			}
		}
		if(tree_form = document.forms['tree_form'])
		{
			if (tree_form.elements)
			{
				for (i = 0; i < tree_form.elements.length; i++)
				{
					if ('checkbox' == tree_form.elements[i].type)
					{
						//if (0 != tree_form.elements[i].value)
						{
							tree_form.elements[i].onclick = function()
							{
								//window['anyoneChecked'] += this.checked ? 1 : -1;
								//window['anyoneChecked'] = Math.max(0, window['anyoneChecked'])
								checkChildren(this);
								checkAncestors(this);

								if (onclick && window[onclick])
								{
									window[onclick]();
								}
							}
						}
						/*
						else
						{
							tree_form.elements[i].onclick = function()
							{
								var i;
							}
						}
						tree_form.elements[i].checked = false;
						*/

					}
				}
			}
		}
	}

	function checkChildren(chk, uncheckOthers)
	{
		var i, j, cluster_id;
		var children = [];

		if (uncheckOthers)
		{
			for (i = 0; i < chk.form.elements.length; i++)
			{
				if(chk.form.elements[i].type == 'checkbox')
				{
					if (chk != chk.form.elements[i])
					{
					 	chk.form.elements[i].checked = false;
					}
				 	chk.form.elements[i].disabled = false;
				}
			}
		}

		j = chk.name.indexOf('[');
		cluster_id = chk.name.substr(j + 1, chk.name.indexOf(']') - j - 1);

		var cur_arr = tree_data[cluster_id]['children'];
		var have_more = false;
		var next_arr;

		do
		{
			next_arr = [];
			for (i = 0; i < cur_arr.length; i++)
			{
				chk.form['cluster_id[' + cur_arr[i] + ']'].checked = chk.checked;
				//chk.form['cluster_id[' + cur_arr[i] + ']'].disabled = chk.checked;

				if (tree_data[cur_arr[i]]['children'].length)
				{
					next_arr = eval('['+next_arr.join(',')+(next_arr.length?',':'') + tree_data[cur_arr[i]]['children'].join(',') + ']');
				}
			}
			cur_arr = next_arr;
		}
		while(0 < cur_arr.length);

		//if(anyoneChecked<row_count-1 && !chk.checked)
		//	document.tree_form['cluster_id[0]'].checked = false;
	}


	function getPathToRoot(clId)
	{
		var i = clId;
		var arr = [];

		while( tree_data[i] && tree_data[i]['parent'] && 0 != tree_data[i]['parent'])
		{
			arr[arr.length] = tree_data[i]['parent'];
			i = tree_data[i]['parent'];
		}
		return arr;
	}

	function checkAncestors(chk)
	{
		var i, j, cluster_id;

		j = chk.name.indexOf('[');
		cluster_id = chk.name.substr(j + 1, chk.name.indexOf(']') - j - 1);

		var arr = getPathToRoot(cluster_id);
		var chk2;
		if (!chk.checked)
		{
			for (i = 0; i < arr.length; i++)
			{
				if (chk2 = chk.form.elements['cluster_id[' + arr[i] + ']'])
				{
					chk2.checked = false;
				}
			}
		}
		else
		{
			for(j=0; j < arr.length; j++)
			{
				if (tree_data[arr[j]])
				{
					for(k=0; k < tree_data[arr[j]]['children'].length; k++)
					{
						//examine childs
						chk2 = chk.form.elements['cluster_id[' + tree_data[arr[j]]['children'][k] + ']'];
						if ((chk2) && (false == chk2.checked))
						{
							//cancel all work
							//as at least one child is unchecked
							break;
						}
					}

					//if all children are checked then
					//we check parent
					if(k == tree_data[arr[j]]['children'].length)
					{
						if (chk2 = chk.form.elements['cluster_id[' + arr[j] + ']'])
						{
							chk2.checked =true;
						}
					}
					else
					{
						//cancel all work
						//as at least one child is unchecked
						break;
					}
				}
			}
		}
	}




	var confirm_popup;
	function confirmPopup(popup_url,popup_name,dimension,cl_id,params)
	{
		//alert(anyoneChecked);
		if(isPopupOpen())
			return false;

		if(cl_id=='group')
		{
			if(!anyoneChecked)
			{
				pAlert(messages['tree_nothing_checked']);
				return false;
			}
			var chk_arr = resetChk(true,true,false);
			var i;
			cl_id = '';
//{mine} "for in" construction is required here
			for(i in chk_arr)
			{
				cl_id+=(cl_id?'+':'')+i;
			}
			//params+='&recursively=1';
		}
		else
		{
			resetChk(true,true,true,false);
			document.forms.tree_form['cluster_id['+cl_id+']'].checked = true;
		}
		if(popup_url)
		{
			confirm_popup = window.open(popup_url+'?current_dimension='+dimension+'&cluster_id='+cl_id+'&'+params,popup_name,'left=0,top=0,toolbar=0,location=0,scrollbars=1,width=400,height=400');

			window.onmousemove = window.onfocus=function()
			{
				if(confirm_popup && !confirm_popup.closed)
				{
				}
				else
				{
					resetChk(true,false,true,false);
					window.onfocus=null;
					window.onmousemove=null;
				}
			}

			confirm_popup.onunload = function()
			{
				if(this.location && (this.location=='about:blank' || this.location==''))
				{
				}
				else if(this.opener && this.location.href==this.opener.location.href)
				{
				}
				else
				{
					if(this.opener && typeof(this.opener.resetChk)=="function")
						this.opener.resetChk(true,false,true,false);
				}
			}

			confirm_popup.focus();
			//confirm_popup['page_url'] = p_url; //doesn't work in mozilla
		}
		return false;
	}

	function resetChk(chDisabled,disabledVal,chChecked,checkedVal)
	{
		var tree_form;
		var cluster_id = {};
		var t;
		if(tree_form = document.forms['tree_form'])
		{
			for(i=0;i<tree_form.elements.length;i++)
			{
				if(tree_form.elements[i].type == 'checkbox')
				{
					if(chChecked)
					{
						tree_form.elements[i].checked = checkedVal;
					}
					else if(/*!tree_form.elements[i].disabled &&*/tree_form.elements[i].checked)
					{
						t = ""+tree_form.elements[i].name+" = "+"1;"+"";
						eval(t);
					}
					if(chDisabled)
						tree_form.elements[i].disabled = disabledVal;
				}
			}
		}
		if(chChecked && !checkedVal)anyoneChecked = 0;
		return cluster_id;
	}
	function isPopupOpen()
	{
		if(confirm_popup && !confirm_popup.closed)
		{
			pAlert(messages['popup_still_opened']);
			confirm_popup.focus();
			return true;
		}
		return false;
	}