// Includes an automated menu system by Andrew Burnett which provides an expanded menu for the current group of pages and a pop-up menu for the others; you are welcome to make use of it, but if you do so please include an acknowledgement and a reference to this web site.

var action, action2, menu0Count;
var mnuHighlightColour="#CBCBA9";
var mnuDarkTextCoulour="#155663";
var mnuLightTextColour="white";
var mnu0BackgroundColour="#70A0D0";
var mnu1BackgroundColour="#CDE3F7";
var mnu2BackgroundColour="#EEF5FC";

function getItem(itemId){
 if (document.getElementById) return document.getElementById(itemId);
 else if (document.all) return document.all[itemId];
 else return new Object();
 }

function getChild(item){
 if (item.children) return item.children[0];
 else if (item.firstChild) return item.firstChild;
 else return new Object();
 }

function noLine(itemID){
 getItem(itemID).style.borderBottomColor="white";
 }

function setVis(itemID,isVis){
 getItem(itemID).style.display=isVis;
 }

function goTop(){
 window.scrollTo(0,0);
 }

function closeWin(){
 window.close();
 }

function setClass(item,classx){
 item.className=classx;
 }

//init calls loadImages which must be present on page (may be empty).
//If present, normally initialises srcs and calls doLoad.
//If used, page must also contain afterLoad for any additional processing.
//This code preloads images, retries failures, and displays messages during and after load.
function doLoad(){
 for(var i=0; i<srcs.length; i++){
  imgs[i]=new Image();
  imgs[i].onload=new Function("imgLoaded("+i+")");
  imgs[i].onerror=new Function("imgErr("+i+")");
  imgs[i].src=srcs[i];
  }
 }

function reLoad(imgNo){
 imgs[imgNo].src=srcs[imgNo];
 }

function imgLoaded(imgNo){
 errs[imgNo]=-1;
 getItem("note2").innerHTML+=".";
 if(isLoaded()){
  getItem("note2").style.display="none";
  getItem("note3").style.display="none";
  afterLoad();
  }
 }

function imgErr(imgNo){
 errs[imgNo]++;
 if(errs[imgNo]<=10){
  window.setTimeout("reLoad("+imgNo+")",100);
  }
 else{
  getItem("note2").innerHTML="Error loading images";
  }
 }

function isLoaded(){
 for(var i=0; i<errs.length; i++){
  if(errs[i]!=-1) return false;
  }
 return true;
 }

//Adjusts document body width, top bar width and left bar height to fit window.
//Iterative approach needed as each change changes the document dimensions.
//Called from init; where not required the page cancels it with the following:
//window.onresize=new Function();
//getItem("bar").width = 700;
//getItem("mnubar").style.height = "780px";
//(and individual elements are given fixed widths).
function setWidth(){
 var oldWidth=0;
 var oldHeight=0;
 var i=1;
 while (oldWidth!=document.body.offsetWidth && oldHeight!=getItem("main").offsetHeight && i<=5){
  i++;
  oldWidth=document.body.offsetWidth;
  oldHeight=getItem("main").offsetHeight;
  var newWidth=oldWidth - 200;
  if(newWidth<568) newWidth=568;
  getItem("main").style.width=newWidth + "px";
  getItem("bar").width=newWidth + 100;
  setMnuBar();
  }
 }

function setMnuBar(){
 var barht;
 barht=getItem("main").offsetHeight;
 if (barht<520){barht=520}
 getItem("mnubar").style.height=(barht + 20) + "px";
 }

//Menu handler - parameters are level 0/level 1 menu indexes.
//Alternatively specify just one parameter, the level 1 menu text
function init(item0,item1){
 var menusx, itemx, i, j, wth, ht
 menu0Count=menu1Count.length;
 menusx=getItem("menus");
 menusx.innerHTML=menuText;
 if (!item1) {
  for (i=0; i<menu0Count; i++){
   for (j=0; j<menu1Count[i]; j++){
    itemx=getItem("menu1_" + i + "_" + j);
    itemx=itemx.innerHTML;
    itemx=/<a.*>(.*)<\/a>/i.exec(itemx)[1];
    if (item0 == itemx) {
     item0=i;
     item1=j;
     break
    }}
   if (item0 == i) break;
  }}
 itemx=getItem("menu0_0");
 wth=itemx.offsetLeft + itemx.offsetWidth;
 noLine("menu0_" + (menu0Count - 1));
 for (i=0; i<menu0Count; i++){
  ht=getItem("menu0_" + i).offsetTop;
  noLine("menu1_" + i + "_" + (menu1Count[i]-1));
  if(i==item0){
   noLine("menu0_" + i);
   if(item1>0){
    noLine("menu1_" + i + "_" + (item1-1));
    }
   getItem("menu1_" + i + "_" + item1).className="mnu2";
   }
  else{
   itemx=getItem("menu1_" + i);
   itemx.style.position="absolute"
   itemx.style.left=(wth + 5) + "px";
   itemx.style.top=ht + "px";
   itemx.style.display="none";
   }
  itemx=getItem("menu0_" + i);
  itemx.onmouseover=new Function("showMenu0(" + item0 + "," + i + ")");
  itemx.onmouseout=new Function("cancMenu0(" + item0 + "," + i + ")");
  itemx.onclick=new Function("clickMenu(" + "'menu0_" + i +"')");
  for (j=0; j<menu1Count[i]; j++){
   itemx=getItem("menu1_" + i + "_" + j);
   itemx.onmouseover=new Function("showMenu1(" + item0 + "," + i + "," + j + ")");
   itemx.onmouseout=new Function("cancMenu1(" + item0 + "," + i + "," + j + ")");
   itemx.onclick=new Function("clickMenu(" + "'menu1_" + i + "_" + j +"')");
  }}
 setWidth();
 window.onresize=new Function("setWidth()");
 action2 = window.setTimeout("loadImages()",100);
 }

//Highlights moused-over menu item and shows its popup if it contains more than one item.
function showMenu0(item0,item1){
 var i
 for (i=0; i<menu0Count; i++){
  if(i==item1){
   setColour("menu0_" + i);
   if(i!=item0 && menu1Count[i]>1){
    getItem("menu1_" + i).style.display="";
   }}
  else{
   restoreColour("menu0_" + i);
   if(i!=item0){
    getItem("menu1_" + i).style.display="none";
  }}}
 window.clearTimeout(action);
 }

function showMenu1(item0,item1,item2){
 setColour("menu1_" + item1 + "_" + item2);
 if(item0==item1){
  hideMenu(item0);
  }
 window.clearTimeout(action);
 }

function cancMenu0(item0,item1){
 if(item0==item1){
  restoreColour("menu0_" + item1);
  }
 else{
  action = window.setTimeout("hideMenu(" + item0 + ")",500);
  }
 }

function cancMenu1(item0,item1,item2){
 restoreColour("menu1_" + item1 + "_" + item2);
 if(item0!=item1){
  action = window.setTimeout("hideMenu(" + item0 + ")",500);
  }
 }

function clickMenu(item0){
location.href=getChild(getItem(item0)).href;
// getChild(getItem(item0)).click();
 }

function hideMenu(item0){
 var i
 for (i=0; i<menu0Count; i++){
  restoreColour("menu0_" + i);
  if(i!=item0){
   getItem("menu1_" + i).style.display="none";
   }
  }
 }

function setColour(item0){
 var itemx;
 itemx=getItem(item0);
 itemx.style.backgroundColor=mnuHighlightColour;
 getChild(itemx).style.color=mnuDarkTextCoulour;
 }

function restoreColour(item0){
 var itemx, classx;
 itemx=getItem(item0);
 classx=itemx.className;
 if(classx=="mnu0"){
  itemx.style.backgroundColor=mnu0BackgroundColour;
  getChild(itemx).style.color=mnuLightTextColour;
  }
 else if(classx=="mnu1"){
  itemx.style.backgroundColor=mnu1BackgroundColour;
  }
 else if(classx=="mnu2"){
  itemx.style.backgroundColor=mnu2BackgroundColour;
  }
 }

//Cookie procedures.
function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
  + ";domain=.logicarchitect.co.uk;path=/;expires="+expire.toGMTString();
 }

function getCookie(cookieName) {
 var dc = document.cookie;
 var prefix = cookieName + "=";
 var begin = dc.indexOf("; " + prefix);
 if (begin == -1) {
  begin = dc.indexOf(prefix);
  if (begin != 0) return null;
 } else {
  begin += 2;
 }
 var end = document.cookie.indexOf(";", begin);
 if (end == -1) {
  end = dc.length;
 }
 return unescape(dc.substring(begin + prefix.length, end));
}

//Used for validating 4-character hex strings.
function checkCode(thisCode){
 var i
 if (thisCode.length!=4){
  return false;
  }
 for(i=0;i<=3;i++){
  if (!checkChar(thisCode.charAt(i))){
   return false;
   }
  }
 return true;
 }

function checkChar(oneChar){
 if(oneChar>="0" && oneChar <="9"){
  return true;
  }
 if(oneChar>="A" && oneChar <="F"){
  return true;
  }
 return false;
 }

function toHex(val){
 var x=val.toString(16);
 if (x.length==1){
  x = "0" + x;
  }
 x = x.toUpperCase();
 return x;
 }

// returns the unescaped value of queryItem in the query string.
function param(queryItem){
 var queryString = window.location.search;
 var pos1 = queryString.indexOf("?" + queryItem + "=");
 if (pos1 < 0){
  var pos1 = queryString.indexOf("&" + queryItem + "=");
  if (pos1 < 0){
   return "";
   }
  }
 var pos2 = queryString.indexOf("&", pos1 + 1);
 if (pos2 < 0){
  return unescape(queryString.substr(pos1 + queryItem.length + 2));
  }
 return unescape(queryString.substring(pos1 + queryItem.length + 2, pos2));
 }

// to enable window.opener to work in Opera add to <a> onclick="winOpen(this); return false;"
function winOpen(item){
 window.open(item.href, item.target);
 }

// Google event tracking to track each click from one page to another.
// Saves page name in a cookie as other methods do not reliably find last page visited.
function trackEvent(){
 var lastref;
 if (getCookie("pagename")) {
  lastref = getCookie("pagename");
  }
 else {
  lastref = "(none)";
  }
 var thisref = location.href;
 var newref = /logicarchitect([.]co[.]uk)?(\x2F|\x5C)(.*).htm/.exec(thisref);
 try {
  var pagename = decodeURIComponent(newref[3]);
  }
 catch(err) {}
 if (!pagename) {
  pagename = thisref;
  }
 setCookie("pagename",pagename);
 pageTracker._trackEvent('Clicks', lastref, pagename);
 }
