var imgUniquePrefix = "%%";

function Initialise()
{
  SetCentralPanel('Welcome');
  SetMenuBar('MainMenu');
  ProcessPhoneNumbers();
  TrapAnchorClick();
  DelayLoads();
}

function DelayLoads()
{
	var imgID = 0;
	var imgsrc;
	var imgs = document.getElementsByTagName('img');
  for(i = 0; i < imgs.length; i++)
    {
	    if((imgs[i].src == ""))
	    {
		    imgsrc = imgs[i].name
		    // DelayLoad and Thumbnail images have their src stored in the name attribute, but the name attribute must be unique,
		    // so in case some images are used more than once, make all the name attributes unique
		    imgs[i].name = imgs[i].name + imgUniquePrefix + imgID
        imgs[i].src = imgsrc;
        imgID += 1;
      }
    }
}

function FullImage(ThisImage)
{
	ThisImage = ThisImage.replace("/Thumbnails/","/FullSize/");
	// Remove the img unique identifier that was added by the DelayLoads routine
	ThisImage = ThisImage.substring(0, ThisImage.lastIndexOf(imgUniquePrefix));
  popup = window.open('ImagePopup.html?i=' + ThisImage,'Fullsize','toolbar=no,location=no,directories=no,menuBar=no,scrollbars=yes,status=yes,resizable=no');
  popup.document.focus;
}

function ProcessPhoneNumbers()
{
  tag = document.getElementsByClassName("Phone");
  for(i in tag)
  {
    tag[i].title = "Telephone number";
  }

}

document.getElementsByClassName = function(cl)
{
var retnode = [];
var myclass = new RegExp('\\b *'+cl+' *\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
};

function TrapAnchorClick()
{
  anc = document.getElementsByTagName("a");
  for(i in anc)
  {
    Ext = GetFileExtension(anc[i].href).toLowerCase();
    switch(Ext)
    {
      case 'html' :
      case 'htm' :
      case 'shtml' :
      case 'asp' :
      case 'php' :
      DocType = "webpage";
      break;

      case 'ppt' :
      DocType = "PowerPoint file";
      break;

      case 'doc' :
      DocType = "Word file";
      break;

      case 'xls' :
      DocType = "Excel file";
      break;

      case 'pdf' :
      DocType = "PDF file";
      break;

      case 'swf' :
      DocType = "Flash file";
      break;

      case 'dcr' :
      DocType = "Shockwave file";
      break;

      case 'jnlp' :
      case 'jar' :
      DocType = "Java file";
      break;

      case 'vbs' :
      case 'js' :
      DocType = "script file";
      break;

      case 'zip' :
      DocType = "Zip file";
      break;

      case 'bmp' :
      case 'gif' :
      case 'jpg' :
      case 'jpeg' :
      DocType = 'image file';
      break;

      case 'wav' :
      DocType = 'sound file';
      break;

      case 'avi' :
      case 'mov' :
      case 'wmv' :
      case 'mpg' :
      DocType = 'movie file';
      break;

      default :
      DocType = "page";
    }
    switch (anc[i].className)
    {
      case 'Toggle' :
        anc[i].title = "Show/hide details";
        break;

      case 'Offsite' :
        anc[i].className = "Offsite " + Ext;
        anc[i].title = "Off-site " + DocType;
        break;

      case 'Onsite' :
        anc[i].className = "Onsite " + Ext;
        anc[i].title = "On-site " + DocType;
    }
  }
}

function GetFileExtension(url)
{
  if (!url) {return "";}
  dot = url.lastIndexOf(".");
  if ( dot == -1 ) {return "";}
  return url.substr(dot + 1, url.length);
}

function AnchorClick()
{
 BigWindow();
}

function SizeAndPosition()
{
  window.moveTo(0, 0);
  window.resizeTo(screen.availWidth * 0.99, screen.availHeight * 0.98);
}

function ToggleDiv(ID)
{
  oDiv = document.getElementById(ID);
  switch(oDiv.style.display)
  {
  case '' :
    oDiv.style.display = 'block';
    writeSessionCookie("div"+ID, "open");
    break;
  case 'none' :
    oDiv.style.display = 'block';
    writeSessionCookie("div"+ID, "open");
    break;
  case 'block' :
    oDiv.style.display = 'none';
    writeSessionCookie("div"+ID, "closed");
    break;
  }
  window.scrollBy(0,getCookieValue("MouseY") - 15);
}

function RestoreDivState()
{
  div = document.getElementsByTagName("div");
  for(i in div) {
    if (div[i].className == "ToggleDiv") {
      if (getCookieValue(div[i].id) == "open" )
      {
        ToggleDiv(div[i].id);
      }
    }
  }
}


function xlat(img, ext, dom, acc)
{
  document.write("<a href='mai" + "lto:" + acc + "&#64;" + dom + ext + "'><img alt='' class='Icon' title='Email address' src='" + img + "' /> Email</a>");
}

function SetCentralPanel(src)
{
	var params;
	params = "dbWrite.php";
	params += "?db=stthomas_pagelogs";
	params += "&tbl=pages";
	params += "&fld=page";
	params += "&val=" + src;
	document.getElementById("phpLink").src=params;

  document.getElementById('CentralPanel').innerHTML=document.getElementById(src).innerHTML;
}

function SetMenuBar(src)
{
  document.getElementById('MenuBar').innerHTML=document.getElementById(src).innerHTML;
}

function writeSessionCookie (cookieName, cookieValue) {
  if (testSessionCookie()) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else
  {
    return false;
  }
}

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else
  {
    return false;
  }
}

function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
  {
    return true;
  }
  else
  {
    return false;
  }
}
