// Project menu handling Funcions

function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

// debug tools
function debug_trace(string) {
    if (debug) debug = confirm('DEBUG>>>'+string);
}

function trace_element(tag, element) {
    if (!debug) return;
    debug = confirm('DEBUG>>>Tracing element:' + tag + ":" +
        '\nnode=' + element.nodeName +
        '\nname=' + element.getAttribute('name') +
        '\ntype=' + element.nodeType);
     if (!debug) return;

    attlist = '';
    att = element.attributes;
    for (var i = 0; i < att.length; i++) {
        attlist += '\nattribute ' + i + ': ' + att[i].name + '=' + att[i].value;
    }
    debug = confirm("DEBUG>>>Attributes:" + attlist);
    if (!debug) return;
    
    childlist = '';
    cn = element.childNodes;
    for (var i = 0; i < cn.length; i++) {
        //childlist += '\nchildnode ' + i + ': ' + cn[i].nodeName + '=' + cn[i].childNodes[0].nodeValue;
        childlist += '\nchildnode ' + i + ': ' + cn[i].nodeName + "/" + cn[i].nodeType;
    }
    debug = confirm("DEBUG>>>Children::" + childlist);
}


function open_py(year) {
	var py = 'py'+year;
	var py_list = document.getElementById(py);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var a = document.createElement('a');
	var url = document.location.toString();
	var pref;
	var pl = proj_arr.length;
	//debug=true;
	debug_trace("Open "+py+" called from "+url);
	debug_trace("Proj Array type =" + window.proj_arr instanceof Array);
	debug_trace("Proj Array size =" + pl );
	// add year to the heading
	head1.appendChild(document.createTextNode(year));
	trace_element("H1", head1);
	li.appendChild(head1);
	// add underlines to list element
  	li.appendChild(document.createTextNode("_________________________________________________________"));
	// set link
	a.setAttribute("href","javascript:close_py(" + year + ");");
  	a.appendChild(document.createTextNode("close"));
	trace_element("A", a);
	li.appendChild(a);
	// now create the project list
	var ul = document.createElement('ul');
	pref = (url.search(/thoughts/i) < 0)?'':'/';
	debug_trace("Prefix ="+pref);
	for (i=0;i<proj_arr.length;++i) {
		debug_trace("Project Id="+proj_arr[i][0]);
		debug_trace("Project Yr="+proj_arr[i][1]);
		if (proj_arr[i][1] == year) {
			var pli = document.createElement('li');
			var pa = document.createElement('a');
			pa.setAttribute("href",pref+"project.php?projectid="+proj_arr[i][0]);
			pa.appendChild(document.createTextNode(proj_arr[i][2]));
			trace_element("PA",pa);
			pli.appendChild(pa);
			ul.appendChild(pli);
		}
	}
	trace_element("UL", ul);
	li.appendChild(ul);
	li.setAttribute("id", py);
	trace_element("LI", li);
	// replace list element with collapsed on
	py_list.parentNode.replaceChild(li, py_list);
	trace_element("PY_LIST", py_list);
}

function close_py(year) {
	var py = 'py'+year;
	var py_list = document.getElementById(py);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var a = document.createElement('a');
	debug_trace("Close py"+year);
	// add year to the heading
	head1.appendChild(document.createTextNode(year));
	trace_element("H1", head1);
	li.appendChild(head1);
	// add underlines to list element
  	li.appendChild(document.createTextNode("______"));
	// set link
	a.setAttribute("href","javascript:open_py(" + year + ");");
  	a.appendChild(document.createTextNode("open"));
	trace_element("A", a);
	li.appendChild(a);
	li.setAttribute("id", py);
	trace_element("LI", li);
	// replace list element with collapsed on
	py_list.parentNode.replaceChild(li, py_list);
	trace_element("PY_LIST", py_list);
	
}


function open_client(name) {
	var client_name = (name.substring(0,16)).replace(/ /gi,"_");
	var client_id = 'c'+client_name;
	var client_list = document.getElementById(client_id);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var head2 = document.createElement('h2');
	var a = document.createElement('a');
	var url = document.location.toString();
	var pref;
	var pl = proj_arr.length;
	//debug=true;
	debug_trace("Open "+client_id+" called from "+url);
	debug_trace("Proj Array type =" + window.proj_arr instanceof Array);
	debug_trace("Proj Array size =" + pl );
	// set link
	a.setAttribute("href","javascript:close_client('" + name + "');");
	//a.setAttribute("class", "close");
  	//a.appendChild(document.createTextNode("close"));
	// add name to the heading
	a.appendChild(document.createTextNode(name));
	trace_element("A", a);
	head2.appendChild(a);
	li.appendChild(head2);
	// now create the project list
	var ul = document.createElement('ul');
	pref = (url.search(/thoughts/i) < 0)?'':'/';
	debug_trace("Prefix ="+pref);
	for (i=0;i<proj_arr.length;++i) {
		debug_trace("Project Id="+proj_arr[i][0]);
		debug_trace("Project Client="+proj_arr[i][3]);
		if (proj_arr[i][3] == name) {
			var pli = document.createElement('li');
			var pa = document.createElement('a');
			//pa.setAttribute("href",pref+"project.php?projectid="+proj_arr[i][0]);
			pa.setAttribute("href","/project.php?projectid="+proj_arr[i][0]);
			pa.appendChild(document.createTextNode(proj_arr[i][2]));
			trace_element("PA",pa);
			pli.appendChild(pa);
			ul.appendChild(pli);
		}
	}
	trace_element("UL", ul);
	li.appendChild(ul);
	li.setAttribute("id", client_id);
	trace_element("LI", li);
	// replace list element with collapsed on
	client_list.parentNode.replaceChild(li, client_list);
	trace_element("CLIENT_LIST", client_list);
}

function close_client(name) {
	var client_name = (name.substring(0,16)).replace(/ /gi,"_");
	var client_id = 'c'+client_name;
	var client_list = document.getElementById(client_id);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var head2 = document.createElement('h2');
	var a = document.createElement('a');
	//debug = true;
	debug_trace("Close client_"+client_name);
	// add name to the heading
	//li.appendChild(head1);
	// set link
	a.setAttribute("href","javascript:open_client('" + name + "');");
	//a.setAttribute("class", "open");
  	//a.appendChild(document.createTextNode("open"));
	a.appendChild(document.createTextNode(name));
	trace_element("A", a);
	head2.appendChild(a);
	li.appendChild(head2);
	li.setAttribute("id", client_id);
	trace_element("LI", li);
	// replace list element with collapsed on
	client_list.parentNode.replaceChild(li, client_list);
	trace_element("CLIENT_LIST", client_list);
	
}

function open_cat(catid, name) {
	var cat_id = 'cat'+catid;
	var cat_list = document.getElementById(cat_id);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var head2 = document.createElement('h2');
	var a = document.createElement('a');
	var url = document.location.toString();
	var pref;
	var pl = cat_arr.length;
	//debug=true;
	debug_trace("Open "+cat_id+" called from "+url);
	debug_trace("Proj Array type =" + window.cat_arr instanceof Array);
	debug_trace("Proj Array size =" + pl );
	// set link
	a.setAttribute("href","javascript:close_cat('" + catid + "','" + name + "');");
	//a.setAttribute("class", "close");
  	//a.appendChild(document.createTextNode("close"));
	// add name to the heading
	a.appendChild(document.createTextNode(name));
	trace_element("A", a);
	head2.appendChild(a);
	li.appendChild(head2);
	// now create the project list
	var ul = document.createElement('ul');
	pref = (url.search(/thoughts/i) < 0)?'':'/';
	debug_trace("Prefix ="+pref);
	for (i=0;i<cat_arr.length;++i) {
		debug_trace("Project Id="+cat_arr[i][0]);
		debug_trace("Project cat="+cat_arr[i][4]);
		if (cat_arr[i][4] == catid) {
			var pli = document.createElement('li');
			var pa = document.createElement('a');
			//pa.setAttribute("href",pref+"project.php?projectid="+cat_arr[i][0]);
			pa.setAttribute("href","/project.php?projectid="+cat_arr[i][0]);
			pa.appendChild(document.createTextNode(cat_arr[i][2]));
			trace_element("PA",pa);
			pli.appendChild(pa);
			ul.appendChild(pli);
		}
	}
	trace_element("UL", ul);
	li.appendChild(ul);
	li.setAttribute("id", cat_id);
	trace_element("LI", li);
	// replace list element with collapsed on
	cat_list.parentNode.replaceChild(li, cat_list);
	trace_element("CAT_LIST", cat_list);
}

function close_cat(catid, name) {
	var cat_id = 'cat'+catid;
	var cat_list = document.getElementById(cat_id);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var head2 = document.createElement('h2');
	var a = document.createElement('a');
	//debug = true;
	debug_trace("Close cat_"+name);
	// add name to the heading
	//li.appendChild(head1);
	// set link
	a.setAttribute("href","javascript:open_cat('" + catid + "','" + name + "');");
	//a.setAttribute("class", "open");
  	//a.appendChild(document.createTextNode("open"));
	a.appendChild(document.createTextNode(name));
	trace_element("A", a);
	head2.appendChild(a);
	li.appendChild(head2);
	li.setAttribute("id", cat_id);
	trace_element("LI", li);
	// replace list element with collapsed on
	cat_list.parentNode.replaceChild(li, cat_list);
	trace_element("CAT_LIST", cat_list);
	
}


function open_clients(name) {
	var client_list = document.getElementById(name);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var head2 = document.createElement('h2');
	var a = document.createElement('a');
	var url = document.location.toString();
	//debug=true;
	// set link
	a.setAttribute("href","javascript:close_clients('" + name + "');");
	// add name to the heading
	a.appendChild(document.createTextNode("Projects by client"));
	trace_element("A", a);
	head2.appendChild(a);
	li.appendChild(head2);
	// now create the project list
	var ul = document.createElement('ul');
	for (i=0;i<proj_arr.length;++i) {
		var pli = document.createElement('li');
		var pa = document.createElement('a');
		var pid = proj_arr[i][0];	// project id
		var pt = proj_arr[i][2];	// project title
		var pc = proj_arr[i][3];	// project client
		debug_trace("Project Id="+pid);
		debug_trace("Project Title="+pt);
		debug_trace("Project Client="+pc);
		pa.setAttribute("href","/project.php?projectid="+pid);	
		if (pt != pc) {pt = pc + ": " + pt;}	// add cient name if not same as title
		pa.appendChild(document.createTextNode(pt));
		trace_element("PA",pa);
		pli.appendChild(pa);
		ul.appendChild(pli);
	}
	trace_element("UL", ul);
	li.appendChild(ul);
	li.setAttribute("id", name);
	trace_element("LI", li);
	// replace list element with collapsed on
	client_list.parentNode.replaceChild(li, client_list);
	trace_element("CLIENT_LIST", client_list);
}

function close_clients(name) {
	var client_list = document.getElementById(name);
	var li = document.createElement('li');
	var head1 = document.createElement('h1');
	var head2 = document.createElement('h2');
	var a = document.createElement('a');
	//debug = true;
	debug_trace("Close clients "+name);
	// set link
	a.setAttribute("href","javascript:open_clients('" + name + "');");
	a.appendChild(document.createTextNode("Projects by client"));
	trace_element("A", a);
	head2.appendChild(a);
	li.appendChild(head2);
	li.setAttribute("id", name);
	trace_element("LI", li);
	// replace list element with collapsed on
	client_list.parentNode.replaceChild(li, client_list);
	trace_element("CLIENT_LIST", client_list);
	
}