
function sendMail(url)
{
	var email = window.prompt("Please enter e-mail","");
	if (email)
	{
		window.location="mailto:" + email + "?subject=" + url + "&body=" + url;
	}
}

function showSearch(yesNo)
{
	OpenSearch.style.display=yesNo?'none':'block';
	SearchForm.style.display=yesNo?'block':'none';
	setProperty('search', yesNo?1:0);
}

function showAdd(yesNo)
{
	OpenAdd.style.display=yesNo?'none':'block';
	AddForm.style.display=yesNo?'block':'none';
	setProperty('add', yesNo?1:0);
}

function load()
{
	if (typeof(SearchForm) != 'undefined')
	{
	 	var search = getProperty('search');
	 	showSearch(search==1);
	}
}


function getProperty(name)
{
var ck = document.cookie;
var broken = ck.split(';');
  for (var i = 0; i < broken.length; i++)
  {
  	if (broken[i])
  	{	
  		var pair = broken[i].split('=');
  		if(pair.length >= 2)
  		{
  			if (strtrim(pair[0]) == name ) return strtrim(pair[1]);
  		}
  	}
  }
  return null;
}

function setProperty(name, value)
{
  	var dateExp = new Date();
  	dateExp.setMonth(dateExp.getMonth()+1)

	document.cookie = name + '=' + value + '; expires=' + dateExp.toGMTString();
}


function join(parts, separator)
{
	var s = '';
	for (var i = 0; i<parts.length; i++)
	{
		s += parts[i] + separator;
	}
	return s;
}



// --------------------------------------
//        Tree
// --------------------------------------
document.expanded = new Array();

function strtrim(value)
{
  return value.replace(/^\s+/,'').replace(/\s+$/,'');
}
function dequote(value)
{
  return value.replace(/^"+/,'').replace(/"+$/,'');
}

function findNamedChild(node, name)
{
	if (!node) return null;
	var chi = node.childNodes;
	var len = chi.length;
	for (var i=0; i<len; i++ )
	{
		if (chi[i].id == name) return chi[i];
		else
		{
			var found = findNamedChild(chi[i], name);
			if (found) return found;
		}
	}
	return null;
}

function findParentOfType(node, type)
{
	if (node == null) return null;
	var current = node;
	var parent = null;
	while((parent = current.parentNode) != null)
	{
		if (parent.tagName == type) return parent;
		current = parent;
	}
	return null;
}


function toggle(nodeId)
{
	var good = false;
	var div = document.getElementById(nodeId);
	var ul =  findNamedChild(div, 'ul');
	var img = findNamedChild(div, 'plus');
	if (good = (ul != null && img != null && ul.className != null && ul.innerHTML != null)) 
	{
		if ((ul.className == "shown") || (ul.className == ""))
		{
			ul.className = "hidden";
			img.src = "/images/treePlus.gif";
			if (a != null) a.href = "javascript:;";
			document.expanded[nodeId] = false;
		}
		else
		if (ul.className == "hidden")
		{
			img.src = "/images/treeMinus.gif";
			ul.className = "shown";
			document.expanded[nodeId] = true;
		}

		if (strtrim(ul.innerHTML) == "")
		{
			ul.innerHTML = "<div onclick='javascript:cancelLoad(\"" + nodeId + "\");' class='loadMsg'>"
				+ document.loadingMessage + "</div>";
			ul.className="";

			var a = findNamedChild(div, 'toggle');
			if ((a != null) && ("object" == typeof(a)) && (window.length > 0))
			{
				var fram =  findNamedChild(div, 'frame');
				if (fram) fram.src = a.href + '&random='+ Math.random();
			}
		}

	}
	autosave();
	return false;
}

function expand(nodeId)
{
	var good = false;
	var div = document.getElementById(nodeId);
	var ul =  findNamedChild(div, 'ul');
	var img = findNamedChild(div, 'plus');
	if (good = (ul != null && img != null && ul.className != null && ul.innerHTML != null)) 
	{
		if (ul.className == "hidden")
		{
			img.src = "/images/treeMinus.gif";
			document.expanded[nodeId] = true;

			ul.className = "shown";
			
			if (strtrim(ul.innerHTML) == "")
			{
				ul.innerHTML = "<div onclick='javascript:cancelLoad(\"" + nodeId + "\");' class='loadMsg'>"
					+ document.loadingMessage + "</div>";
				ul.className="";

				var a = findNamedChild(div, 'toggle');
				if ((a != null) && ("object" == typeof(a)) && (window.length > 0))
				{
					var fram =  findNamedChild(div, 'frame');
					if (fram) fram.src = a.href + '&random='+ Math.random();
				}
			}
		}

	}
	return false;
}

function addToExpandList(id)
{
	var cook = getProperty('expandedNodes');
	
 	if (cook)
 	{
	 	 cook = cook + '.' + id;
	}
	else
	{
		cook = id;
	}
	
	setProperty('expandedNodes', cook);
}

function cancelLoad(id)
{
	var div = document.getElementById(id);
	var ul =  findNamedChild(div, 'ul');
	var fra = findNamedChild(div, 'frame');
	if (fra != null)
		fra.src = "about:blank";
	if (ul != null)
	{
		ul.innerHTML = "";
		ul.className = "hidden";
	}

}


function loadNode(id, node)
{
	var div = document.getElementById(id);
	var ul =  findNamedChild(div, 'ul');
	
	if (ul != null && "object" == typeof(ul) && node && "object" == typeof(node)) 
	{
		ul.className = "hidden";
		ul.innerHTML = node.innerHTML;
		expand(id);
		autoexpand();
	}
}

var selection = null;

function select(id)
{
	if (document.forms[0]._linkSubmit) document.forms[0]._linkSubmit.value='';
	
	if (selection != null && selection.className != null)
	{
		selection.className = "";
	}

	var div = document.getElementById(id);
	var typeElem = findNamedChild(div, 'type');
	var type = typeElem ? typeElem.value : '';
	var node =  findNamedChild(div, 'sel');
	selection = node;
	
	if (node != null && node.className != null)
	{
		node.className = "Current";
		document.forms[0].ctype.value=type;
		document.forms[0].cid.value=id;
		setProperty('current', id);
		if (type=="organization")
		{
			showAddInToolbar(true);
		}
		else
		{
			showAddInToolbar(false);
		}
		submitTree();

	}
	else
	{
		document.forms[0].ctype.value='';
		document.forms[0].cid.value='';
	}
	return false;
}

function showAddInToolbar(onOff)
{
	var ena = window.parent.frames['TreeToolbar'].document.getElementById('addEnabled');
	var disa = window.parent.frames['TreeToolbar'].document.getElementById('addDisabled');
	if (onOff)
	{
		ena.className="shown";
		disa.className="hidden";
	}
	else
	{
		ena.className="hidden";
		disa.className="shown";
	}
}

function submitTree(action)
{
	var treeDoc = window.parent.frames['center'].document;
	treeDoc.forms[0].action.value=action;
	treeDoc.forms[0].submit();
}


function paintRecursive(root, tag, className)
{
	if (!root) return;
	if (root.tagName == tag) root.className = className;
	var chi = root.childNodes;
	if (chi)
	{
		for (var i=0; i<chi.length; i++ )
		{
			paintRecursive(chi[i], tag, className);
		}
	}
	
}


document.cut = function()
{
	if (document.cutty != null && document.cutty.className != null)
	{
		var div = findParentOfType(document.cutty, 'DIV');
		paintRecursive(div, 'A', '');
		document.cutty = null;
		document.forms[0].xtype.value='';
		document.forms[0].xid.value='';
	}


	if (selection != null && selection.className != '')
	{
		var div = findParentOfType(selection, 'DIV');
		var typeElem = findNamedChild(div, 'type');
		var type = typeElem ? typeElem.value : '';
		document.cutty = selection;
		paintRecursive(div, 'A', 'cut');
		selection = null;
		document.forms[0].xtype.value=type;
		document.forms[0].xid.value=div.id;
	}
}

function cutInTree()
{
	var treeDoc = window.parent.frames['center'].document;
	if (typeof(treeDoc.cut) == "function") treeDoc.cut();
}

function loadDone(id)
{
	if (window.parent.loadNode != null && window.parent != self)
	{
		var div = document.getElementById(id);
		var ul =  findNamedChild(div, 'ul');
		if (ul && ul.innerHTML)
		{
			window.parent.loadNode(id, ul);
			location.replace('about:blank');
		}
	}
}

function refreshTree()
{

	if (window.parent.frames.center != null)
	{
		window.parent.frames.center.location.reload();
	}
}

function refreshTopFrame()
{

	if (window.top.frames.north != null)
	{
		window.top.frames.north.location.reload();
	}
}

function autosave()
{
	var ae = new Array();
 	for (id in document.expanded)
 	{
		if ( document.expanded[id]) ae[ae.length] = id;
	}	
	var joint = join(ae, '.');
	setProperty('expandedNodes', joint);
}

document.expandPath = function(path)
{
 	for (var i = 0; i < path.length; i++)
 	{
 		if (path[i]) addToExpandList(path[i]);
	}	
	var id = path[path.length-1];
	select(id);
	setProperty('current', id);
	
	autoexpand();
}

function autoexpand()
{
	var cook = getProperty('expandedNodes');

 	if (cook)
 	{
	 	var list = cook.split('.');
	 	for (var i = 0; i < list.length; i++)
	 	{
	 		if (list[i]) expand(list[i]);
		}	
	}
	
	var sel = getProperty('current');
	if (sel)
	{
		select(sel);
	}


	window.onunload=function()
	{
		autosave();
	}

}

function skip(i)
{
	if (document.forms[0] && document.forms[0].elements['startFrom'])
	{
		document.forms[0].elements['startFrom'].value = i;
		document.forms[0].submit();
	}
}

window.onload = function()
	{
	load();
	}
