/************************************************************
 * Common JavaScript functions for building forum headers   *
 * and footers. The functions in this file are accessed by  *
 * the custom forum JavaScript files.                       *
 *                                                          *
 * Please note that the JavaScript files do not create the  *
 * heirachy links (e.g. Jedi Council Forums » The Star      *
 * Wars Films » Attack of the Clones »). This must be done  *
 * in the forum header IMMEDIATELY AFTER the <script> tags. *
 ************************************************************/ 




var forumName = '';
var numberOfForumAnnouncements = 0;
var numberOfForumLinks = 0;
var numberOfForumMods = 0;
var forumAnnouncements = '';
var forumLinks = '';
var modLinks = '';
var currentForumAnnouncement = 1;
var currentForumLink = 1;
var currentMod = 1;
var firstPass = true;
var PMLinkForumName = '';
var forumAnnouncementsPosition = 1;
var forumLinksPosition = 1;
var modLinksPosition = 1;
var useStylesheet = false;
var contactwho = '';

  //initialise global variables. Do not edit these values.




function outputForumHeaderFooter()
{
    //outputs the custom header or footer for the forum
    //(called from individual forum JS files).
  
  if (firstPass)
    //if the header is to be output this time...

  {
    if ((forumAnnouncementsPosition == 1) || (forumAnnouncementsPosition == 3))
      //if the forum announcement are to be shown in the header...

    {
      outputForumAnnouncements();
    }
    
    if ((forumLinksPosition == 1) || (forumLinksPosition == 3))
      //if the forum links are to be shown in the header...

    {
      outputForumLinks();
    }
    
    if ((modLinksPosition == 1) || (modLinksPosition == 3))
      //if the moderator links are to be shown in the header...

    {
      outputModLinks();
    }
    firstPass = false;
      //set the firstPass indicator to false, so the footer is output next time
  }
  else
    //else if the footer is to be output this time...

  {
    if ((forumAnnouncementsPosition == 2) || (forumAnnouncementsPosition == 3))
      //if the announcement is to be shown in the footer...

    {
      outputForumAnnouncements();
    }
    
    if ((forumLinksPosition == 2) || (forumLinksPosition == 3))
      //if the forum links are to be shown in the footer...
    
    {
      outputForumLinks();
    }
    
    if ((modLinksPosition == 2) || (modLinksPosition == 3))
      //if the mod links are to be shown in the footer...
    
    {
      outputModLinks();
    }
  }
}




function outputForumAnnouncements()
{
    //outputs the forum links
  document.write(forumAnnouncements+
                 '</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function outputForumLinks()
{
    //outputs the forum links
  document.write(forumName+' Forum Links:'+forumLinks+
                 '</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function outputModLinks()
{
    //outputs the mod links
    
  document.write(contactwho+':');
  document.write(modLinks);
  document.write('</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function addForumAnnouncement(forumAnnouncementText, forumAnnouncementColor)
{
    //builds the forum announcement HTML one at a time (called from individual forum JS files).

  if (firstPass)
    //if this is the first set of passes of this function...

  {
    forumAnnouncements = forumAnnouncements+'<span style="color: '+forumAnnouncementColor+'">'+forumAnnouncementText+'</span>';
      //build the announcement

    if (currentForumAnnouncement < numberOfForumAnnouncements)
      //if more forum announcements need to be built...

    {
      forumAnnouncements=forumAnnouncements+' <br><br>';
        //output a seperating space

      currentForumAnnouncement++;
        //increment the forum announcement number

    }
  }

}




function addForumLink(forumLinkURL, forumLinkText)
{
    //builds the forum links HTML one at a time (called from individual forum JS files).

  if (firstPass)
    //if this is the first set of passes of this function...

  {
    forumLinks = forumLinks+' <a class="MainMenuLink" href="'+forumLinkURL+'">'+
                 forumLinkText+'</a>';
      //build the link for this forum link

    if (currentForumLink < numberOfForumLinks)
      //if more forum links need to be built...

    {
      forumLinks=forumLinks+' |';
        //output a seperating pipe

      currentForumLink++;
        //increment the forum link number

    }
  }

}




function addMod(modUsername, modUserID, modUserTitle)
{
    //builds the link for one moderator at a time (called from individual forum JS files).

  if (firstPass)
    //if this is the first set of passes of this function...

  {
    if (currentMod == 1)
      //if this is the first mod to be built...

    {
      PMLinkForumName = strReplaceAll(forumName,' ','+');
        //Swap the spaces in the forum name for +s so that it is compatible with the URL syntax

      PMLinkForumName = strReplaceAll(PMLinkForumName,'&','and');
        //Swap any &s in the forum name for 'and's so that it is compatible with the URL syntax

    }
    
    if (useStylesheet == true)
      //if the mod stylesheet is to be used...

    {
      modClass='mod-'+modUserID;
        //set the modClass to use the appropriate style in the mod stylesheet
      
    }
    else
    {
      modClass='MainMenuLink';
        //set the modClass to use the default MainMenuLink class

    }
    
    modLinks=modLinks+' <a href="http://boards.theforce.net/pm_send.asp?usr='+modUserID+
                      '&message_subject='+PMLinkForumName+
                      '+Forum&message_body=Please+give+details+of+the+problem+or+question+here"'+
                      ' title="'+modUserTitle+'" class="'+modClass+'">'+modUsername+'</a>';
                      
      //build the link for this mod, and append it onto the string containing the rest of them


    if (currentMod < numberOfForumMods)
      //if more mods still need to be built...

    {
      modLinks=modLinks+' |';
        //output a seperating pipe

      currentMod++;
        //increment the mod number

    }
  }
}




function strReplaceAll(string,text,by)
{
    // Replaces text with 'by' in string

  var strLength = string.length, txtLength = text.length;
  if ((strLength == 0) || (txtLength == 0))
  {
    return string;
  }
  var i = string.indexOf(text);
  if ((!i) && (text != string.substring(0,txtLength)))
  {
    return string;
  }
  if (i == -1)
  {
    return string;
  }

  var newstr = string.substring(0,i) + by;

  if (i+txtLength < strLength)
  {
    newstr += strReplaceAll(string.substring(i+txtLength,strLength),text,by);
  }

  return newstr;
}