// Escape from frames - currently disabled 19/12/07
// if (window != top) top.location.href = location.href;
var popupWin;

// #############################################################################

function IEVersion() {
   if (navigator.appName != "Microsoft Internet Explorer") return null;
   var version = navigator.appVersion;
   version = parseFloat(version.substr(version.indexOf("MSIE")+5));
   return version;
}

// #############################################################################

function checkForUpLink(baseDir) {

   // This function stopped working after switching to absolute positioned DIVs
   // for menu and content. The call in footer.php has been disabled until a
   // solution is found.

   // Check length of document, if significantly more than window height
   // activate the upLink button.
   // There seems to be a lack of documentation for these properties - more
   // work may be needed here.
   // Don't even think about it with Netscape 4.x.
   if (navigator.appName == "Netscape" &&
         parseInt(navigator.appVersion) <= 4) return;
//   var docHeight = document.body.scrollHeight;
   var docHeight = document.documentElement.scrollHeight;
   if (window.opera && parseInt(navigator.appVer) == 6) {
      // Opera 6.x does it differently :-(
      docHeight=document.body.clientHeight;
   }
   if (docHeight > 1.5*windowHeight()) upLinkButton(baseDir);
}

//##############################################################################
function ImageWindow(sx,sy,imageName,title) {
/*##############################################################################

*/

  if (popupWin) {
    if ( ! popupWin.closed) {
      popupWin.close();
    }
  }
  imageUrl = "/tools/popup-image.html\?name="+imageName+"&title="+title;
  var scrollBars = 'no';
  var w = sx+64;
  if (w > screen.availWidth) {
    w = screen.availWidth;
    scrollBars = "yes";
  }
  var h = sy+130;
  if (h > screen.availHeight-32) {
    h = screen.availHeight-32;
    scrollBars = "yes";
  }
  var x = (screen.availWidth-w)/2;
  var y = (screen.availHeight-h)/2 -30;
  if (y < 0) y = 0;
  var popupFeatures='resizable=yes,scrollbars='+scrollBars+',width='+w+',height='+h;
  popupWin=window.open(imageUrl,"sedberghextra",popupFeatures);
  popupWin.moveTo(x,y);
  popupWin.focus();
}

// #############################################################################

function windowBottom() {
   if (document.documentElement && document.documentElement.scrollTop) {
      var top = document.documentElement.scrollTop
   } else top = document.body.scrollTop
   if (window.innerHeight) {
      var height = window.innerHeight
   } else if (document.documentElement && document.documentElement.clientHeight) {
      height = document.documentElement.clientHeight
   } else if (document.body) {
      height = document.body.clientHeight
   }
   return top + height;
}

// #############################################################################

function windowRight() {
   if (document.documentElement && document.documentElement.scrollLeft) {
      var left = document.documentElement.scrollLeft
   } else left = document.body.scrollLeft
   if (window.innerWidth) {
      var width = window.innerWidth
   } else if (document.documentElement && document.documentElement.clientWidth) {
      width = document.documentElement.clientWidth
   } else if (document.body) {
      width = document.body.clientWidth
   }   return left + width;
}

// #############################################################################

function windowHeight() {
   if (window.innerHeight) {
      var height = window.innerHeight
   } else if (document.documentElement && document.documentElement.clientHeight) {
      height = document.documentElement.clientHeight
   } else if (document.body) {
      height = document.body.clientHeight
   }
   return height;
}

// #############################################################################

function get_style(name) {
   if (document.getElementById) return document.getElementById(name).style;
   else if (document.all) return document.all(name).style;
   else return false;
}

// #############################################################################

function contactLink() {
   // This just provides a means of generating email links in a page without
   // them appearing as obvious email addresses in the HTML source. This should
   // help to reduce the risk of addresses being harvested for junk mail.
   this.symbol = String.fromCharCode(64);
}

contactLink.prototype.c = function(addr) {
   this.place = addr.rot13();
}

contactLink.prototype.b = function(addr) {
   this.person = addr.rot13();
}

contactLink.prototype.link = function(subject) {
   url = this.person+this.symbol+this.place;
   var link = '<a href=mailto:'+url;
   if (subject) {
      link += '?Subject='+subject.replace(/ /g,'%20');
   }
   link += '>'+url+'</a>';
   return link;
}

// #############################################################################

function upLinkButton() {
   // Displays a link at bottom RHS of window to tahe user back to top
   if ((navigator.appName == "Netscape" &&
         parseInt(navigator.appVersion) <= 4)||
         parseInt(IEVersion()) <= 4) {
      // Netscape  4x and IE4 just can't cope with this
      return;
   }
   var baseDir = arguments[0];
   // Need to specify width & height here instead of in style sheet
   // otherwise things break with IE5 :-(
   document.write('<div id="uplink" style="width: 50px; height: 15px;">');
   document.writeln('<a class=blacklink'+
       ' href="javascript:window.scrollTo(0,0)">Top&nbsp;');
   document.writeln('<img src="'+baseDir+'/images/pageup.gif" width="13"'+
       ' height="9" border="0"></a></div>');
   if(document.all && navigator.appName == "Microsoft Internet Explorer" &&
      navigator.platform == "Win32") {
//      alert("windowBottom "+windowBottom());
      uplink.style.position = "absolute";
      uplink.style.setExpression("top",
         "windowBottom() - uplink.style.pixelHeight - 2");
      uplink.style.setExpression("left",
         "windowRight() - uplink.style.pixelWidth - 2");
      uplink.getElementsByTagName("a")[0].href =
         "javascript:void(window.scrollTo(0,0));"
      document.getElementsByTagName("body")[0].style.marginBottom = "0px";
      window.scrollTo(0,0);
   }
}

// #############################################################################

function displayPage(extraUrl) {

// Displays the given URL in a seperate window. To avoid screen clutter the same
// named window (sedberghExtra) is always used and is brought forward in case it
// already existed and was obscured by other windows
   extraWindow = window.open(extraUrl,"sedberghExtra");
   if (extraWindow) extraWindow.focus();
}

// #############################################################################

String.prototype.rot13 = function () {

// Perform rot13 encoding on an ASCII character string. Characters outside the
//   ASCII range (65-90 & 97-122) are left unchanged.

   var result = "";
   for (var count=0; count < this.length; count++) {
      var code = this.charCodeAt(count);
      if ((code  >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
         code += 13;
         if ((code > 90 && code < 104 ) || code > 122) code -= 26;
      }
      result += String.fromCharCode(code)
   }
   return result;
}
