// -------------------------------------------------

// frameit
// =======
// Called from the HEAD section of all contents pages
// Called to ensure that a "contents" page is in a frame in the HSO frameset
// If not then put the current location into the query string and load index.php

function frameit()
{
  if (window.name == "content" ||
      parent.parent != top ||
      location.search == "?frames=no")
  {
  }
  else
  {
    var newloc;
    var i;
    var href = top.location.href;

    switch (top.location.protocol)
    {
    case "http:":
      var hostname = top.location.hostname;
      if (hostname.indexOf("hitchinsymphony.org.uk") > 0)
      {
        newloc = "http://" + hostname + "?s=" + top.location.pathname.substr(1);

        window.location.replace (newloc);
      }
      break;
    case "file:":
      i = href.lastIndexOf("/");
      newloc = href.substr(0, i + 1);
      newloc = newloc + "index.php";

      window.location.replace (newloc);
      break;
    }
  }
}

// -------------------------------------------------

// frameix
// =======
// Called from the HEAD section of the frameindex page
// Called to ensure that a "frameindex" page is in a frame in the HSO frameset

function frameix()
{
  if (window.name == "index" ||
      parent.parent != top)
  {
  }
  else 
  {
    var newloc;
    var i;
    var href = top.location.href;

    switch (top.location.protocol)
    {
    case "http:":
      var hostname = top.location.hostname;
      if (hostname.indexOf("hitchinsymphony.org.uk") > 0)
      {
        newloc = "http://" + hostname;

        window.location.replace (newloc);
      }
      break;
    case "file:":
      i = href.lastIndexOf ("/");
      newloc = href.substr(0, i + 1);
      newloc = newloc + "index.php";

      window.location.replace (newloc);
      break;
    }
  }
}

// -------------------------------------------------

// framemembers
// ============
// Called from the HEAD section of the members/index page
// Called to ensure that the members.php page is in a frame in the HSO frameset

function framemembers()
{
  if (window.name == "content" ||
      parent.parent != top)
  {
  }
  else
  {
    var newloc;
    var i;
    var href = top.location.href;

    switch (top.location.protocol)
    {
    case "http:":
      var hostname = top.location.hostname;
      if (hostname.indexOf("hitchinsymphony.org.uk") > 0)
      {
        newloc = "http://" + hostname + "?s=members.php"

        window.location.replace (newloc);
      }
      break;
    case "file:":
      i = href.lastIndexOf("/");
      i = href.lastIndexOf("/", i - 1); 
      newloc = href.substr(0, i + 1);
      newloc = newloc + "index.php";

      window.location.replace (newloc);
      break;
    }
  }
}

// -------------------------------------------------

// indexloaded
// ===========
// Called when the frameindex page is loaded to do lots of exciting stuff

function indexloaded(doc)
{
  if (doc.implementation)
  {
    doQuotes (doc);

    doEMails (doc);
  }
}

// -------------------------------------------------

// contentloaded
// =============
// Called when a content page is loaded to put quotes where needed and other stuff

function contentloaded(doc)
{
  if (doc.implementation)
  {
    doQuotes (doc);

    doNicknames (doc);

    doEMails (doc);
  }
}

// -------------------------------------------------

// doQuotes
// ========
// Called from contentloaded and indexLoaded to insert single or double quotes around <q> strings

function doQuotes(doc)
{
  var quotes = doc.getElementsByTagName("q");

  for (var n=0; n<quotes.length; n++)
  {
    quoteIt (doc, quotes[n], quotes[n].parentNode.nodeName == "Q" ? 1 : 2);
  }
}

// -------------------------------------------------

// doNicknames
// ===========
// Called from contentloaded to insert single quotes around class="nickname" strings

function doNicknames(doc)
{
  var spans = doc.getElementsByTagName("span");

  for (var n=0; n<spans.length; n++)
  {
    if (spans[n].className == "nickname")
    {
      quoteIt (doc, spans[n], 1);
    }
  }
}

// -------------------------------------------------

// quoteIt
// =======
// Called from doQuotes and doTitles to insert single or double quotes around a string

function quoteIt (doc, element, num)
{
  var openquote = num == 1 ? "\u2018" : "\u201c";
  var closequote = num == 1 ? "\u2019" : "\u201d";

  element.insertBefore(doc.createTextNode(openquote), element.firstChild);
  element.appendChild(doc.createTextNode(closequote));
}

// -------------------------------------------------

// doEMails
// ========
// Called from contentLoaded and indexLoaded to set href for an "a" element with class="mailto..."

function doEMails(doc)
{
  var as = doc.getElementsByTagName("a");

  for (var n=0; n<as.length; n++)
  {
    if (as[n].className.substr(0, 6) == "mailto")
    {
      as[n].href = "mailto:" + as[n].className.substr(6) + "@hitchinsymphony.org.uk"
    }
  }
}

// -------------------------------------------------

// frameBiography
// ==============
// Included at the foot of a biography page to ensure that 
// it has a link to its programme page if called directly

function frameBiography(programme)
{
  if (window.name != "hsobiog")
  {
    document.writeln("This biography is a part of the <a href=" + programme + ">Hitchin Symphony Orchestra</a> web site");
  }
}

// -------------------------------------------------

// openBiography
// =============
// Open a new window for a Biography entry

function openBiography (pagehref) 
{
var win;
  win = window.open(pagehref, "hsobiog", "width=600,height=420,dependent=yes,resizable=yes,scrollbars=yes");
  win.focus();
}

// -------------------------------------------------

// openPicture
// ===========
// Open a new window for a Picture entry from members page

function openPicture (pichref, picwidth, picheight) 
{
  var win;

  win = window.open(pichref, "hsopic", "width=" + picwidth + ", height=" + picheight + ",dependent=yes,resizable=yes,scrollbars=yes");
  win.focus();
}

// -------------------------------------------------

// Check data entered into Mailing List request form

function checkMailingList(mailform)
{
  if (mailform.name1.value == "")
  {
    alert("Please enter your name");
    mailform.name1.focus();
    return false;
  }

  if (mailform.address1.value == "" &&
      mailform.address2.value == "" &&
      mailform.address3.value == "" &&
      mailform.postcode.value == "" &&
      mailform.email.value == "")
  {
    alert("Please enter either your address or your e-mail address");
    mailform.address1.focus();
    return false;
  }

  return true;
}
