// JavaScript for the menu, loaded in menu.htm
function theparameter(allparams,paramname) {
// given a URL search string or cookie, return the value of the named parameter
  var pos = allparams.indexOf(paramname+"=");
  if(pos != -1) {
    var startpos = pos + paramname.length + 1;
    var endpos = allparams.indexOf(";",startpos);
    if(endpos == -1) endpos = allparams.indexOf("&",startpos);
    if(endpos == -1) endpos = allparams.length;
    return unescape(allparams.substring(startpos,endpos));
  } else {
    return "";
  }
}
// set the affiliate cookie 'ac' if this page was called from an affiliate site
var affiliate = theparameter(self.location.search, "ac");
if(affiliate != "") {
  // ac is on the URL
  if (theparameter(document.cookie, "ac") == "") {
    // this PC doesn't already have an ac cookie
    var expdate = new Date();
    // the cookie lasts one year
    expdate.setFullYear(expdate.getFullYear() + 1);
    document.cookie="ac=" + escape(affiliate) + "; expires=" + expdate.toGMTString() + "; path=/";
  }
}
// set the 'trust' cookie if this page was called from a trust site
var trust = theparameter(self.location.search, "trust");
if(trust != "") {
  trust = trust.replace(/^\s+|\s+$/g, ""); // trim
  document.cookie="trust=" + escape(trust) + "; path=/";
}
// get the trust cookie
trust = theparameter(document.cookie, "trust").toUpperCase();
if(trust=="") trust = "treesintrust";

// by default, the menu is enabled. See new_page
var menu_enabled = true;
  
function ElementRef(elementID) {
// called from the routines below
// get a reference to an element in the current document
  if(document.getElementById) {	//ie6+ ns6+
    this.obj=document.getElementById(elementID);
    this.style=document.getElementById(elementID).style;
  } else {						//ie5
    this.obj=document.elementID;
    this.style=document.elementID.style;
  }
}

function overmenu(cell) {
// called from the onmouseout of the cells in the table in menu
// if it isn't already selected, make it the menuover colour
  if(cell.style.backgroundImage.indexOf("menuon")==-1) {
    cell.style.backgroundImage = "url(images/trusts/" + trust + "/menuover.gif)";
  }
}
function offmenu(cell) {
// called from the onmouseover of the cells in the table in menu
// if it isn't already selected, make it transparent
  if(cell.style.backgroundImage.indexOf("menuon")==-1) {
    cell.style.backgroundImage = "" ; 
  }
}
function new_page(pagename) {
// called from the onclick of the cells in the table in menu
// also from the BON buttons to stop robots calling asp pages
// replace the current page with a new one
// this is here so that a click on anywhere in the cell will execute the link
  if (menu_enabled) {
    menu_enabled = false;
    if (location.href) {
      location.href = pagename;
    } else {
	  location.replace(pagename);
    }
  }
}
function setmenu() {
// called from advancedmenu
// the cells in the table in menu are named after the pages they link to.
// get the name of this page and make the corresponding cell blue
  var lastslash = location.href.lastIndexOf("/") + 1;
  var endofname = location.href.length - lastslash;
  var filename = location.href.substr(lastslash,endofname).toLowerCase();
  // strip any querystring off the end
  if(filename.indexOf("?") != -1) {
    endofname = filename.indexOf("?");
    if(filename.substr(endofname + 1, 6) == "demo=y") {
      // demo link, so highlight the demo menu item
	  filename="demo.aspx";
	} else {
	  // strip off the querystring
      filename = filename.substr(0,endofname);
    }
  }
  // strip any bookmark off the end
  if(filename.indexOf("#") != -1) {
    endofname = filename.indexOf("#");
    filename = filename.substr(0,endofname);
  }  
  // redirect pages with no menu item to use another menu item
  if(filename=="details.aspx") {filename="register.aspx"}
  if(filename=="dedication.aspx") {filename="register.aspx"}
  if(filename=="memorial.aspx") {filename="register.aspx"}
  if(filename=="owner.aspx") {filename="register.aspx"}
  // these don't have a menu item so select a blank one
  if(filename=="") {filename="nomenu"}
  if(filename=="piece.shtm") {filename="nomenu"}
  if(filename=="oxygen.shtm") {filename="nomenu"}
  if(filename=="acre.shtm") {filename="nomenu"}
  if(filename=="purchase.aspx") {filename="nomenu"}
  if(filename=="supply.aspx") {filename="nomenu"}
  if(filename=="paypalpdt.aspx") {filename="nomenu"}
  if(filename=="businesses.shtm") {filename="nomenu"}
  if(filename=="security.shtm") {filename="nomenu"}
  if(filename=="affiliates.shtm") {filename="nomenu"}
  if(filename=="green_christmas_gifts.shtm") {filename="nomenu"}
  if(filename=="leed.shtm") {filename="nomenu"}
  if(filename.substr(0,2)=="ca") {filename="nomenu"}
  var cell = new ElementRef(filename);
  if(filename != "nomenu") {
    cell.style.backgroundImage = "url(images/trusts/" + trust + "/menuon.gif)";
  }
  return true;
}
function advancedmenu() {
// called from menu
// some old browsers, i.e. NS4, don't support dhtml, so check that this brower can,
// then call setmenu to highlight the selected menu item
  mdv = new ElementRef("index.shtm");
  if(mdv.style) {
    setmenu();
  }
}
function setlogos() {
// called from trustlogos, the last thing to be loaded on each page
// set the colours and logos and header link to those specific to this trust
    var el;
    el = new Image();
    el.src = "images/trusts/" + trust + "/header.gif";
    document.images[0].src = el.src;
    
	//set the gap at the top of the page
	tsheight="15";
	if(trust=="INT") {tsheight="15"}
	if(trust=="NTNB") {tsheight="2"}
	if(trust=="NSNT") {tsheight="21"}
	if(trust=="ON") {tsheight="0"}
	if(trust=="MVA") {tsheight="0"}
	if(trust=="TLC") {tsheight="120"}
	tsheight=tsheight + "px";				
    el = new ElementRef("maindiv");
	el.style.top = tsheight;
    
    el = new ElementRef("menucol");
    el.style.backgroundImage = "url(images/trusts/" + trust + "/menubg.gif)";
    el = new ElementRef("sidebarL");
    el.style.backgroundImage = "url(images/trusts/" + trust + "/sidebars.gif)";
    el = new ElementRef("sidebarR");
    el.style.backgroundImage = "url(images/trusts/" + trust + "/sidebars.gif)"; 
    el = new ElementRef("sidestripL");
    el.style.backgroundImage = "url(images/trusts/" + trust + "/sidestrips.gif)";
    el = new ElementRef("sidestripR");
    el.style.backgroundImage = "url(images/trusts/" + trust + "/sidestrips.gif)"; 
    el = new ElementRef("Body1");
    el.style.backgroundImage = "url(images/trusts/" + trust + "/background.gif)";
    document.links[0].href = "trust_links/" + trust + ".map";
}