	// getStyleSheetRule ("styleLinks", ".menItem").width = "150px";
/* -----------------------------------------------------------------------------------------------
	Linkmen.js
	Revision History
	----------------

	02-Nov-04 v07
		Converted to object array, working within original paradigm

	02-Nov-04 v08
		Started on CSS conversion of links, preparatory to placing in own screen

	05-Nov-04 v08
		More on Css function.
		Moved utilities out to DanUts

	08-Nov-04 v08
		Now Linkmen10 - remove old table rows functions

	10-Nov-04 v08
		New live version - NewDanMain => DanMain

	23-May-05
		Renamed LinkMen_mini.js - was LinkMen10.js
		"Mini" version called from main page of Dan's web page

	12-Jul-05
		Corrected %20%20.. error (introduced by DW?) - now works on "front page" of web site

	07-Sep-05
		Converted to getAtt/setAtt
		Deleted commented out stuff in process.  Previous in LinkMen_mini_pre-Att.jsold

------------------------------------------------------------------------------------------------- */

// ----------------------------------------------------------------------------------- Cookie stuff

// This is our user data cookie object
var
  userdata = new Cookie(document, "UserDetails", 240, '/');

// load data - if not there, set UserRef 0

if (!userdata.load() || !userdata.UserRefno) {
  UserRef = 0;          // no cookie - set ref to 0
}
else {             		// get data
  UserRef = userdata.UserRefno;
  UserFn = userdata.Forename;
  UserSn = userdata.Surname;
}
	workingUserRefno = 0;
  UserLinks = "UserLinks00000.html";    <!-- replace with function -->

// --------------------------------------------------------------------------------------- Main declarations here

		var debugVars = 0;
		var debugState = 0;
		var debugExpand = 0;
		var debugContent = 0;
		var debugLink = 0;
		var debugS = '';

		var doBull = 1;					// TRUE if bullets for link menu
		var isLocal = 0; 				// 1 for local, 0 for web
		var	VisitCnt = 0;				// stats, and to test for reload
		var	UpdateCnt = 0;			// to test for reload
		var LinksCnt = 0;				
		var FolderCnt;

		/* New Stuff */
		var L = new Array();		// This holds our links objects
		var F = new Array();		// This holds our folders objects
		
		function sumL (i) {
			return '(' + i + ')  title: ' + L[i].title + ", lev: " + L[i].lev + ",  refno: " + L[i].refno + ",  isFolder: " + L[i].isFolder;
		}

		function sumL1 (L) {
			return 'title: ' + L.title + ", lev: " + L.lev + ",  refno: " + L.refno + ",  isFolder: " + L.isFolder;
		}

		/* Constructor function for new linksItem */
		function makeLinksItem (url, title, lev, refno, isFolder) {
			this.url = url;
			this.title = title;
			this.lev = lev;
			this.refno = refno;
			this.isFolder = isFolder;
			/* new div items  - to be defined later */
			this.item = null;		// the complete item ref
			this.expand = null;	// expand button
			//this.expandImg = null;	// expand imange
			this.link = null;		// link
			this.menu = null;		// menu button
			this.menuImg = null;	// menu imange
			// alert (sumL1(this));
		} // makeLinksItem 

		/* Constructor function for new linksFolder */
		function makeLinksFolder (title, lev, refno) {
			this.title = title;
			this.lev = lev;
			this.refno = refno;
			this.div = null
			// alert (sumL1(this));
		} // makeLinksFolder 

		/* function to add link object to L, with server string parsed */
		function addLink (linkStr) {
			var ind = L.length;
			var url;
			var title;
			var refno;
			var lev;
			var isFolder;
			  var p = linkStr.indexOf (String.fromCharCode(9));	// TAB is separator
			  url = linkStr.slice(p+1);											// 2nd 1/2 is URL
	    	
		  	linkStr = linkStr.slice(0,p);								// 0 => p-1
		  	if (linkStr.charAt(0) == '+') {
		    	isFolder = true;
		  	}
		  	p = linkStr.indexOf(',');
		  	if (p >= 0) {
					refno = Number(linkStr.slice(0,p-1));
					refno = parseInt(linkStr);
		    	linkStr = linkStr.slice(p+1);
		  	}
		  	else
		  		refno = 0;
		  	p = linkStr.indexOf(',');
		  	if (p >= 0) {
					lev = Number(linkStr.slice(0,p-1));
					lev = parseInt(linkStr);
		    	linkStr = linkStr.slice(p+1);
		  	}
		  	else
		  		lev = 0;
				title = linkStr;	// store title

				LinksCnt = ind+1;
				L[ind] = new makeLinksItem (url, title, lev, refno, isFolder); 
				if (isFolder)
					addFolder (title, lev, refno);

		} // addLink 
		
		/* function to add folder object to F, with server string parsed */
		function addFolder (title, lev, refno) {
			var ind = F.length;
			FolderCnt = ind+1;
			F[ind] = new makeLinksFolder (title, lev, refno); 
		} // addFolder 
		

		// Global values, set by various buttons and events
		var MouseX = 0;
		var MouseY = 0;
		var currentLinkInd;			// index of item selected - set on menu access
		var currentLinkRefno;		// .. and Refno
		var currentParentRefno;		// Parent of current item, whether link or folder
		var currentFolderInd;		// index of current Folder in Folders list
		var currentParentInd;		// index of parent of current folder in Folders list
		var currentFolderHeader;	// Header text ("adding link" etc)
		var currentAction;			// Action selected
		var currentIsFolder;
		var expandList = new Array();		// comma separated list of expanded refnos

		expandList.length = 0;

		document.onmousedown = moveToClick;

		function refnoToInd(refno) { // returns index in list of item refno; -1 if not found
			var N = 0;
			while ((N < LinksCnt) && (L[N].refno != refno))
				N++;
			if (N = LinksCnt)
				N = -1;
			return N;
		}

		function moveToClick(evt) {
    	evt = (evt) ? evt : event;
    	var coords = getPageEventCoords(evt);    // to be filled in with real function name
    	MouseX = coords.left;
    	MouseY = coords.top;
    } // moveToClick

		if (document.location.href.indexOf("file") == 0)
			isLocal = 1;

		if (isLocal==1) {
			document.all.LoadingFrame.src="TempLinks.html";
			UserRef = 1;
		}
		else
			document.all.LoadingFrame.src="http://www.hamm.org.uk/cgi-bin/DanMain.exe/getlinks" + String(workingUserRefno);

		/* Initialise_LinkmenMini sets various values depending on whether we are local or remote, logged in or not */
		function Initialise_LinkmenMini () {
			var s;
			var x;
			scriptName = document.location.pathname;
			var p = Math.max(scriptName.lastIndexOf('/'),scriptName.lastIndexOf('\\'));
			if (scriptName != '/')
				scriptName = scriptName.slice(p+1);
			s = scriptName.toLowerCase();
			if (s.indexOf('index.htm')==0)
				testVer = 0
			else
				testVer = 1;
			document.getElementById("spScript").innerHTML = scriptName + ", testVer: " + testVer; 
			document.getElementById("spUser").innerHTML = String(workingUserRefno);
			document.getElementById("spOther").innerHTML = ", local: " + String(isLocal) + ', LoadingFrame: ' + document.all.LoadingFrame.src;
			// document.getElementById("btnLogout").onclick="userdata.remove(); location=" + scriptName; // don't work!
			if (workingUserRefno==0) {
				document.getElementById("btnSubmit").disabled = 1;
			}
			else {
				document.getElementById("linksHead").innerHTML = UserFn + " " + UserSn + "'s links";
			}

			var Nam;
			if (UserRef > 0) {
				Nam = UserFn + ' ' + UserSn;
			}
			else
				Nam = "mr #‘Nobody";
			if (x = document.getElementById("userName"))
			  x.innerHTML = Nam;

			if (x = getEl("dateMod"))
				x.innerHTML = document.lastModified;

			if (x = getEl("visitCnt"))
				x.innerHTML = String(VisitCnt);
			if (x = getEl("updateCnt"))
				x.innerHTML = String(UpdateCnt);

			getEl("LF_Title").onchange = makeDefaultButton1;
			getEl("LF_URL").onchange = makeDefaultButton2;

		if (debugExpand) {
			addToExpandList(0,1);
			addToExpandList(10,1);
			addToExpandList(21,1);
		}
		/* now expand those items in expandList */
		s = '';
		for (var i = 0; i < LinksCnt; i++) {
		  if (debugExpand && L[i].isFolder) 
		  	s += L[i].isFolder + " " + inExpandList(i) + '<br />';
			if (L[i].isFolder && inExpandList(i))
				Expand(i,1);
		}
		if (debugExpand)
			getEl("Debug05").innerHTML = s;

		} // Initialise_LinkmenMini

		function createCssItems() {
		  var tr;
		  var td;
		  var trfolder;
		  var cssDiv;
		  var linkStr;
		  var url;
		  var title;
		  var isFolder;
		  var lev;
		  var refno;
		  var cssLinks = getEl("cssLinks");			// Main display
		  var cssFolders = getEl("cssFolders");	// Folder display
		  var span1, span2, span3;
		  var expB, menB;										// expand and menu buttons
		  var o;												// temp objects
		  var levIndent = 12;

			if (!cssLinks || !cssFolders)
				return;
		  debugS += '\n\n                                                   CSS Stuff\n' +
		  				 '                                                   =========\n\n';
			DebugS = '';
			var lf = document.getElementById("LoadingFrame").style;
			if (lf)
			  lf.display = "none";
			LinksCnt = L.length;

			// F[0] = new makeLinksFolder ("Outer level", 0, 0);
			// FolderCnt = 1;
			/* remove old rows and folders */
			while (cssLinks.lastChild) {  // delete rows in case refresh
				cssLinks.removeChild(cssLinks.lastChild)
			}
			while (cssFolders.lastChild) {  // and folders
				cssFolders.removeChild(cssFolders.lastChild)
			}

			for (var i = 0; i < LinksCnt; i++) {
			  /* Test for content */
			  url = L[i].url;
			  title = L[i].title;
			  lev = L[i].lev;
			  refno = L[i].refno;
			  isFolder = L[i].isFolder;

				linkStr = title;
				cssItem = createEl("div");
				
				if (isFolder)
					setAtt (cssItem, "className", "csitem csfld")
				else
					setAtt (cssItem, "className", "csitem cslnk");
				// ------------------------------------------------ span1
				span1 = createEl("span");
				if (isFolder) {
					o = createEl("a");
					setAtt (o, "title", "Click to expand");
					setAtt (o, "href", "javascript: void Expand(" + String(i) + ")");
					expB = createEl("img");
					setAtt(expB, "src", "tr_closed-off.gif");
					setAtt(expB, "border", "0");
					o.appendChild(expB);
					span1.appendChild(o);
				} // isFolder
				else
					o = expB = null;
				W = baseWid1 + lev*levIndent;
				span1.style.width = "" + String(W) + "px";
				span1.style.paddingLeft = String(lev*levIndent)+"px";
				if (debugLink==1)
					debugS += span1.outerHTML + '\n';
				cssItem.appendChild(span1);
				// ------------------------------------------------ span2
				span2 = createEl("span");
				W = baseWid2 - lev*levIndent;		// This works in IE but NOT in FF.
				// W = 147;	// Test to ensure it is the following line that does not work in FF and not the previous!
				span2.style.width = String(W) + "px";		// This works in IE but NOT in FF - FF ignores it.
				if (i==LinksCnt-1)
					linkStr = '<a class="displinkmen1" href="javascript: void LinkMenu(' + String(i) + ');">' + title + '</a>';
				else {
					linkStr = title;
					if (!isFolder) {
						linkStr = '<a class="lrl" title="' + url + '" href="' + url + ' "target="_blank">' + linkStr + '</a>'
					}
					if (isFolder) {
		    		// cssDiv.innerHTML = '  ' + '<a class="lrmf" href="javascript: void selectFolder(' + String(FolderCnt-1) + ');">' + linkStr + '</a>';
				  	linkStr = '<a class="lrf" title="Click to expand" href="javascript: void Expand(' + String(i) + ');"><b>' + linkStr + '</b></a>';
					}
				}
				span2.innerHTML = linkStr;
				// span2.style.position = "static" // does nothing in either model
				// <a class="lrl" title="http://www.aldaily.com" href="http://www.aldaily.com "target="_blank">Arts and Letters Daily</a>
				if (debugLink==1)
					debugS += span2.outerHTML + '\n';
				cssItem.appendChild(span2);
				// ------------------------------------------------ span3
				span3 = createEl("span");	
				if (doBull==1) {
					// span3.innerHTML = '<a class="displinkmen menButton" title="Left click for menu" href="javascript: void LinkMenu(' + String(i) + ');"><b>•</b></a>';
					if (false && isFolder)
						span3.innerHTML = '<a class="displinkmen" title="Left click for menu" href="javascript: void LinkMenu(' + String(i) + ');"><b>•</b></a>'
					else
						// span3.innerHTML = '<a class="displinkmen" title="Left click for menu" href="javascript: void LinkMenu(' + String(i) + ');">o' + span2.style.width + '</a>';
						span3.innerHTML = '<a class="displinkmen" title="Left click for menu" href="javascript: void LinkMenu(' + String(i) + ');">o' + '</a>';
					// span3.style.left = "149px" // does nothing in either model
				}
				else {
					o = createEl("a");
					setAtt (o, "title", "Left click for menu");
					setAtt (o, "href", "javascript: void LinkMenu(" + String(i) + ")");
					menB = createEl("img");
					setAtt(menB, "src", "sq-off.gif");
					setAtt(menB, "border", "0");
					o.appendChild(menB);
					span3.appendChild(o);
					span3.style.left = "49px" // does nothing in either model
				}
				if (debugLink==1)
					debugS += span3.outerHTML + '\n\n';
				cssItem.appendChild(span3);

				cssLinks.appendChild(cssItem);
				L[i].item = cssItem;
				L[i].expand = span1;
				//L[i].expandImg = expB;
				L[i].link = span2;
				L[i].menu = span3;
				L[i].menuImg = menB;
				if (lev > 0) 
				  cssItem.style.display = "none";
			} //

			/* Now create folders */
			for (var i = 0; i < FolderCnt; i++) {
				o = createEl("div");
				x = createEl("a");
				setAtt(x, "className", "lrmf");
				setAtt(x, "href", "javascript: void selectFolder(" + String(i) + ");");
				setAtt(x, "innerHTML", F[i].title);
				o.appendChild(x);

    		//o.innerHTML = '<a class="lrmf" href="javascript: void selectFolder(' + String(i) + ');">' + F[i].title + '</a>';
				o.style.paddingLeft = String((F[i].lev+((i==0)?0:1))*levIndent)+"px";
    		cssFolders.appendChild(o);
    		if (debugLink==1)
    			DebugS += o.innerText + '\n';
    		F[i].div = x;
			} //
			if (debugLink==1)
				document.getElementById("Debug01").innerText = DebugS;

			if (debugContent==1)
				document.getElementById("Debug01").innerText = DebugS;

			// document.getElementById("tblLinks").rules = "rows";  // this sets the display rules
			for (i = 0; i < LinksCnt; i++) 
				//L[i].menu.innerHTML = "x-" + L[i].link.style.width + "-x";
				;
		} // createCssItems

		function Refresh(all) {
			all = 0;
			if (all==1) {
				if (isLocal==1)
					{ location.href = scriptName; }
				else
					{ location.href = "http://www.hamm.org.uk/" + scriptName; }
				// CreateLinkRows('bdyLinks');
			}
			else {
				/* clear variables */
				if (isLocal==1)
					{ LoadingFrame.location.href = 'TempLinks.html'; }
				else {
					if (isLocal==1)
						LoadingFrame.location.href = "http://www.hamm.org.uk/cgi-bin/DanMainx.exe/getlinks" + String(workingUserRefno)
					else
						if (doneReload == 0) {
							LoadingFrame.location.href = "http://www.hamm.org.uk/cgi-bin/DanMain.exe/getlinks" + String(workingUserRefno);
							// resetTimer1();
							DiagCnt = 0;
							waitForInitId = setInterval("waitForInit()",500);
						}
						else {
							createCssItems();
							Initialise_LinkmenMini();
							setSectionTits(document);
						}
					}
			}
		} // Refresh

var waitForInitId = 0;

  function waitForInit() {
		if ((currUpdateCnt < UpdateCnt) || (DiagCnt == 10)) {
			clearInterval(waitForInitId);
			createCssItems();
			Initialise_LinkmenMini();
			setSectionTits(document);
		}
		else {
			DiagCnt++;
			if (debugState == 1)
				getEl("diagRefresh").innerHTML = "Count: " + String(DiagCnt) +
						" currUpdateCnt: " + String(currUpdateCnt) +
						" UpdateCnt: " + String(UpdateCnt);
		}
  }

 		var
 			timeoutID;
		function resetTimer1() {
			clearTimeout(timeoutID);
			timeoutID = setTimeout("CreateCssItems();Initialise_LinkmenMini();setSectionTits(document);", 2000); 
		}

		function LinkMenu(N) {
			var DebugS = "Data for item: " + N + "<BR />"; 

			currentLinkInd = N;  // set global link number
			currentLinkRefno = L[N].refno;  // set global link number

	 		/* default values, for new link or base-level item */
	 		currentFolderInd = 0;
			currentParentRefno = 0;
			currentParentInd = 0;
			currentIsFolder = 0;

		  if (N == LinksCnt-1) {  // New item
		 		menSelect("menAddLink,menAddFolder");
		 	}
		 	else {								// find folder
		 		if ((L[N].lev == 0) && !L[N].isFolder) {	// level 0: outer level
					// Already done
		 		}
		 		else {							// find containing folder, if not folder already
		 			var CurrLev = L[N].lev;
		 			// step back to containing level
		 			if (CurrLev > 0) { // Bug in original - did not test for this
		 				var i = N;
		 				while ((CurrLev<=L[i].lev)) {
							i--;
						}
						DebugS += "currentParentRefno set from L[" + i + "].refno<BR />";
						DebugS += "CurrLev: " + CurrLev + "<BR />";
						DebugS += "FolderCnt: " + FolderCnt + "<BR />";
						currentParentRefno = L[i].refno;
					}
					else
						currentParentRefno = 0;
		 			
		 			currentFolderInd = FolderCnt-1;
		 			DebugS += "L[N].isFolder: " + L[N].isFolder + "<BR />";
		 			if (L[N].isFolder) {
		 				currentIsFolder = 1;
		 				while ((currentFolderInd>0) && (F[currentFolderInd].refno != currentLinkRefno))
		 					currentFolderInd--;
		 				currentParentInd = FolderCnt-1;
		 				while ((currentParentInd>0) && (F[currentParentInd].refno != currentParentRefno))
		 					currentParentInd--;
		 			}
		 			else {
		 				while ((currentFolderInd>0) && (F[currentFolderInd].refno != currentParentRefno))
		 					currentFolderInd--;
		 				void currentParentInd;	// Applies only if folder
		 			}
		 		}
		  	if (L[N].isFolder) {
		 			menSelect("menAddLink,menAddFolder,menDelFolder,menRenFolder,menMoveFolder");
		  	}
		  	else {
		 			menSelect("menAddLink,menAddFolder,menDelLink,menRenLink,menMoveLink");
		 		}
		  }
		  var x = document.getElementById("linkmen").style;
  	  // var y = document.getElementById("yyy").style;
  	  // if (y)
  	  	// y.display = "none";
	  	x.display = "block";
	  	// shiftTo("linkmen", MouseX, MouseY);
	  	x.top = String(MouseY+15) + "px";
	  	// x.left = String(MouseX-parseInt(x.width)) + "px";
	  	// x.left = String(MouseX-150) + "px"; // try right
	  	x.right = "50px";

			if ((isLocal==1) && (debugVars==1)) {
			  DebugS += "L[" + N + "].lev: " + L[N].lev + "<BR />" +
									"currentLinkInd: " + currentLinkInd + "<BR />" +
									"currentLinkRefno: " + currentLinkRefno + "<BR />" +
									"currentParentRefno: " + currentParentRefno + "<BR />" +
									"currentParentInd: " + currentParentInd + "<BR />" +
									"currentFolderInd: " + currentFolderInd + "<BR />" +
									"currentFolderHeader: " + currentFolderHeader + "<BR />" +
									"currentAction: " + currentAction;
				document.getElementById("Debug02").innerHTML = DebugS;
			}
		} // LinkMenu

		function debugExpandList() {
			if (debugExpand==1) {
				var S = '';
				for (var i = 0; i < expandList.length; i++) {
					S += String(i) + ': ' + expandList[i] + '<BR />';
				}
				getEl("Debug04").innerHTML = S;
			}
		}

		function inExpandList(N) {				// returns true if item index N in expand list
			var rn = L[N].refno;
			var i = 0;
			while ((i < expandList.length) && (expandList[i] != rn))
				i++;
			if (i < expandList.length)
				return 1
			else
				return 0
		}

		function addToExpandList(N,add) { // Add to expand list if add==1, else subtract
			if (add==1) {
				expandList[expandList.length] = L[N].refno;
			}
			else {
				var i = 0;
				var rn = L[N].refno;
				while ((i < expandList.length) && (expandList[i] != rn))
					i++;
				if (i < expandList.length)
					expandList.splice(i,1);
			}
			debugExpandList();
		}

		function Expand(N, auto) { // expand folder with list index N - if Auto, then done at start, so don't add to list
		  var lev = L[N].lev+1;
			// L[N].expandImg.setAtt("src", "tr_open-off.gif"); - can't do as lose reference after expand re-assigned.
			// DebugS = '"src" = "tr_open-off.gif"\n';
		  var s = L[N].expand.innerHTML;
		  s = s.replace(/Expand/, "Contract");
		  s = s.replace(/expand/, "contract");
		  s = s.replace(/tr_closed-off/, "tr_open-off");
		  L[N].expand.innerHTML = s;
		  L[N].link.innerHTML = L[N].link.innerHTML.replace(/Expand/, "Contract");
		  L[N].link.innerHTML = L[N].link.innerHTML.replace(/expand/, "contract");
			if (debugLink==1)
				DebugS = L[N].expand.innerHTML + '\n\n';
			if (debugContent==1)
				document.getElementById("Debug01").innerText = DebugS;
		  if (!auto)
		  	addToExpandList(N,1);		// do before N changes
		  for (N = N + 1; N < LinksCnt; N++) {
		    if (L[N].lev == lev) {
		    	L[N].item.style.display = "";
		    } else if (L[N].lev < lev) {break;}
		  }
		} // Expand
	
		function Contract(N) { // contract folder with list index N
		  var lev = L[N].lev+1;
		  var s = L[N].expand.innerHTML;
		  s = s.replace(/Contract/, "Expand");
		  s = s.replace(/contract/, "expand");
		  s = s.replace(/tr_open-off/, "tr_closed-off");
		  L[N].expand.innerHTML = s;
		  L[N].link.innerHTML = L[N].link.innerHTML.replace(/Contract/, "Expand");
		  L[N].link.innerHTML = L[N].link.innerHTML.replace(/contract/, "expand");
			if (debugLink==1)
				DebugS += L[N].expand.innerHTML + '\n\n';
			if (debugContent==1)
					document.getElementById("Debug01").innerText = DebugS;
		  addToExpandList(N,0);		// do before N changes
		  for (var NN = N + 1; NN < LinksCnt; NN++) {
		    if (L[NN].lev >= lev) {
		    	if (L[NN].isFolder) {
			    	Contract(NN)
			    }
		    	L[NN].item.style.display = "none";
		    } else {break;}
		  }
		} // Contract
	
		function ShowLoadingFrame() {
			var x = document.getElementById("LoadingFrame").style;
			if (x)
			  if (x.display == "none") {
			  	x.display = "block";
			    document.getElementById("btnB").value = "Hide loading frame";
			  }
			  else {
			    x.display = "none";
			  	document.getElementById("btnB").value = "Show loading frame";
			  }
		} // ShowLoadingFrame

		function selectFolder (N) { // select folder (for adding link, etc)
	  	for (var i = 0; i < FolderCnt; i++) {
	  		F[i].div.style.backgroundColor = "white";
	  	}
	  	F[N].div.style.backgroundColor = "gainsboro";
			currentParentRefno = F[N].refno;
		} // selectFolder 

		function menitemSelect (it, itlist, hideAll) {
		  var x = document.getElementById(it).style;
			if (x) {
		  	if (hideAll) 
		  		x.display = "none";
				else if (itlist.indexOf(it) >= 0)
					x.display = "block";
				else
					x.display = "none";
			}
		} // menitemSelect 

		function menSelect (s, hideAll) {
		  if (!hideAll) {  // hide heading and other details
		  	displayLinkmenHeading ("", 1);
			  displayLinkmenDetails ();
			  displayLinkmenButtons ();
			  displayLinkmenFolders (-1);
		  }
			menitemSelect ("menAddLink", s, hideAll);
			menitemSelect ("menAddFolder", s, hideAll);
			menitemSelect ("menDelLink", s, hideAll);
			menitemSelect ("menDelFolder", s, hideAll);
			menitemSelect ("menRenLink", s, hideAll);
			menitemSelect ("menRenFolder", s, hideAll);
			menitemSelect ("menMoveLink", s, hideAll);
			menitemSelect ("menMoveFolder", s, hideAll);
			menitemSelect ("menCancel", "menCancel", hideAll);  // Normally display this
		}

		function displayLinkmenHeading (S, hide) {
			var x = document.getElementById("linkmenHeading");
			if (x) {
				if (hide) 
					x.style.display = "none"
				else {
			  x.style.display = "block";
			  x.innerHTML = S;
			 }
			}
		} // menSelect 
		
		function displayLinkmenDetails(show) {
		  /* Display input fields.
		  	 if Show==1, then display just the title.
		  	 if Show==2, display URL also
		  */
		  x = document.getElementById("linkmenDetails").style;
		  if (x) {
		    if (show) {		// show the Div
		      x.display = "block";
		  		x = document.getElementById("LF_URL").style;
		  		if (x) {
		    		if (show==2) {	// show the URL also
		      		x.display = "block";
		  				}
		    		else
		      		x.display = "none";
		      }
		     }
		    else
		      x.display = "none";
		  }
		} // displayLinkmenDetails

		function displayLinkmenFolders(ind, Header) {
		  x = document.getElementById("linkmenFolders").style;
		  if (x)
		    if ((ind>=0) && (FolderCnt > 0)) {
		      x.display = "block";
					selectFolder(0);
					if (Header)
						document.getElementById("linkmenFoldersHeading").innerHTML = "<span>" + Header + "</span>";
					selectFolder(ind);
		    }
		    else
		      x.display = "none";
		} // displayLinkmenFolders

		function displayLinkmenButtons(show) {
		  x = document.getElementById("linkmenButtons").style;
		  if (x) {
		    if (show) {
		      x.display = "block";
		      // getEl("btnSubmit").focus() // takes away from input element
		    }
		    else
		      x.display = "none";
		  }
		} // displayLinkmenButtons

		function CancelLinkmen() {
		  var x = document.getElementById("linkmen").style;
		  	x.display = "none"
		}

		function SetupAction(Header, Details, FoldersTitle) {
		  displayLinkmenHeading (Header);
		  menSelect ("", 1);
		  if (Details>0) {
		  	displayLinkmenDetails (Details);
		  	document.getElementById("LF_Title").focus();
		  }
		  if (FoldersTitle!="") {
			  displayLinkmenFolders (currentFolderInd, FoldersTitle);
		  }
			displayLinkmenButtons (1);
  		document.getElementById("LF_Title").value = "";
  		document.getElementById("LF_URL").value = "";
		} // SetupAction

		function AddLink() {
			currentFolderHeader = "Adding link to ";
			currentAction = 1;
			SetupAction ("Add link", 2, "Select folder to contain new link");
		}

		function AddFolder() {
			currentFolderHeader = "Adding folder to ";
			currentAction = 2;
			SetupAction ("Add folder", 1, "Select folder to contain new folder");
		}

		function DelLink() {
			currentFolderHeader = "Deleting link ";
			currentAction = 3;
			SetupAction ("Delete link <span class='dlgLink'>" + L[currentLinkInd].title + "</span>",
			0, "");
		}

		function DelFolder() {
			currentFolderHeader = "Deleting folder ";
			currentAction = 3;
			SetupAction ("Delete folder <span class='dlgLink'>" + L[currentLinkInd].title + "<br />" +
									 "Embedded items will move to outer folder", 0, "");
		}

		function RenLink() {
			currentFolderHeader = "Renaming link";
			currentAction = 4;
			SetupAction ("Rename link <span class='dlgLink'>" + L[currentLinkInd].title + "</span>", 2, "");
  		document.getElementById("LF_Title").value = L[currentLinkInd].title;
  		document.getElementById("LF_URL").value = L[currentLinkInd].url;
		}

		function RenFolder() {
			currentFolderHeader = "Renaming folder";
			currentAction = 4;
			SetupAction ("Rename folder <span class='dlgLink'>" + L[currentLinkInd].title + "</span>", 1, "");
  		document.getElementById("LF_Title").value = L[currentLinkInd].title;
		}

		function MoveLink() {
			currentFolderHeader = "Moving <span class='dlgLink'>" + L[currentLinkInd].title + "</span><br /> to ";
			currentAction = 5;
			SetupAction ("Move link " + L[currentLinkInd].title, 0, "Move link to");
		}

		function MoveFolder() {
			currentFolderHeader = "Moving <span class='dlgLink'>" + L[currentLinkInd].title + "</span><br /> to ";
			currentAction = 5;
			SetupAction ("Move folder " + L[currentLinkInd].title, 0, "Move folder to");
		}

		function Help(N) {
		  var x = document.getElementById("linkhelp").style;
		  if (x) {
		    if (N==1)
		  		x.display = "block" 
		    else
		  		x.display = "none" 
		  }
		}

		function ShowFoldersFrame() {
			var x = document.getElementById("linkfolders").style;
			if (x)
			  if (x.display == "none") {
			  	x.display = "block";
			    document.getElementById("btnC").value = "Hide folders frame";
			  }
			  else {
			    x.display = "none";
			  	document.getElementById("btnC").value = "Show folders frame";
			  }
		} // ShowFoldersFrame

var waitForReloadId = 0;
var currUpdateCnt = 0;
var doneReload = 0;
var DiagCnt;
function setAction () {
	var N = currentAction;
  // document.getElementById("LF_Code").value = String(N);
  document.LoadingFrame.document.getElementById("UserRef").value = workingUserRefno;
  document.LoadingFrame.document.getElementById("LF_Code").value = String(N);
  document.LoadingFrame.document.getElementById("LF_Refno").value = currentLinkRefno;
  document.LoadingFrame.document.getElementById("LF_Parent").value = currentParentRefno;
  document.LoadingFrame.document.getElementById("LF_Title").value = document.getElementById("LF_Title").value;
  document.LoadingFrame.document.getElementById("LF_URL").value = document.getElementById("LF_URL").value;
	if (currentAction==5) {
		expandList[expandList.length] = currentParentRefno; // since we are moving to there!
		var ind = refnoToInd(currentParentRefno); // check that parents are expanded - another time!
		if (ind >= 0) {
			var lev = L[ind].lev;
			while (L[ind].lev > 0) {
				while ((ind > 0) && (L[ind].lev == lev))
					ind--;
				if (L[N].isFolder)
					addToExpandList(ind,1);
				lev = L[ind].lev;
			}
		}
	}
	if (Verify(N)) {
		if ((isLocal==1) && (debugVars==1)) {
  		document.getElementById("Debug03").innerHTML = 
  		"UserRef: " + workingUserRefno + "<BR />" +
  		"LF_Code: " + String(N) + "<BR />" +
  		"LF_Refno: " + currentLinkRefno + "<BR />" +
  		"LF_Parent: " + currentParentRefno + "<BR />" +
  		"LF_Title: " + document.getElementById("LF_Title").value + "<BR />" +
  		"LF_URL: " + document.getElementById("LF_URL").value
  	}
  	else
  		document.LoadingFrame.document.getElementById("userRequest").submit();
  	CancelLinkmen();

		doneReload = 0;
  	currUpdateCnt = UpdateCnt;
  	DiagCnt = 1;
		if (isLocal != 1) {
			//resetTimer();
			waitForReloadId = setInterval("waitForReload()",100);
		
		}
  } // if (Verify())
} // setAction

  function waitForReload() {
		if (debugState == 1)
			getEl("diagRefresh").innerHTML = "Count: " + String(DiagCnt) +
					" currUpdateCnt: " + String(currUpdateCnt) +
					" UpdateCnt: " + String(UpdateCnt);
		if ((currUpdateCnt < UpdateCnt) || (DiagCnt == 50)) {
			clearInterval(waitForReloadId);
			doneReload = 1;  // should be 1, bud doesn't work
			Refresh(1);
		}
		else {
			DiagCnt++;
		}
  }
  
  var
    timeoutID;
	function resetTimer() {
		clearTimeout(timeoutID);
		if (testVer==1)
			timeoutID = setTimeout("Refresh(1)", 500) 
		else
			timeoutID = setTimeout("Refresh(1)", 500); // Refresh all for now, until we sort out "local refresh"
	}


function Verify(N) {
  var Title = document.getElementById("LF_Title").value;
  var URL = document.getElementById("LF_URL").value;

	if ((N==1) || (N==2) || (N==4)) {
		var FieldsComplete = 0;
		if ((Title) && (Title > ""))
			FieldsComplete = 1;
		if ((N==1) || ((N==4) && (currentIsFolder==0)))
			if ((URL) && (URL > ""))
				FieldsComplete = 1;
  	if (FieldsComplete==1)
  		return true
  	else {
  		alert ('All fields must be completed');
  		return false;
  	}
  }
  else
    return true;
} // Verify

// -------------------------------------------------------------------------------------- Event initialisation
document.onkeydown = trapEsc;

// getEl("LF_Title").onchange = makeDefaultButton;	// must do in Initialise_LinkmenMini, when form created!

// decipher key down codes
function trapEsc(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
		if (evt && (evt.keyCode==27)) {
			cancelMenu()
		}
}

function giveSubmitButtonFocus() {
	if (getEl("linkmen").style.display=="block")
		getEl("btnSubmit").focus() 
}

/* must be better way, when we understand model better.
	 need to detect caller identity */
function makeDefaultButton1() { // called from Title
	if (currentAction!=1)
		giveSubmitButtonFocus();
}
function makeDefaultButton2() {	// called from URL
	giveSubmitButtonFocus();
}

