/************************************************************
 * 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 numberOfForumLinks = 0;
var numberOfForumMods = 0;
var forumLinks = '';
var modLinks = '';
var currentForumLink = 1;
var currentMod = 1;
var firstPass = true;
var PMLinkForumName = '';
var modOnline = '';
var forumLinksPosition = 1;
var modLinksPosition = 1;
var useStylesheet = false;
var announcement = '';
var numberOfAnnouncements = 0;
var currentAnnouncement = 1;

  //initialise global variables. Do not edit these values.



	//outputs the custom header or footer for the forum
	//(called from individual forum JS files).
function outputForumHeaderFooter()
{

  
	//if the header is to be output this time...
	if (firstPass)
	{

		outputAnnouncements();

		//if the forum links are to be shown in the header...
		if ((forumLinksPosition == 1) || (forumLinksPosition == 3))
		{
			outputForumLinks();
		}
    
		//if the moderator links are to be shown in the header...
		if ((modLinksPosition == 1) || (modLinksPosition == 3))
		{
			outputModLinks();
		}

		//set the firstPass indicator to false, so the footer is output next time	
		firstPass = false;

	}
  else
    //else if the footer is to be output this time...

  {
    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 outputAnnouncements()
{
	//outputs the Announcements
	document.write(announcement + '</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');

}


function addAnnouncement(announcementText)
{


	//builds the announcements in HTML one at a time (called from individual forum JS files).

	// Add this announcement to the rest. 
	announcement = announcement + '<b>Announcement: </b>' + announcementText;
		

	//if more announcements need to be built...
	if (currentAnnouncement < numberOfAnnouncements)
	{
		// output a break line
		announcement = announcement + '<br>';

		//increment the announcement number
		currentAnnouncement++;
	}
	
}


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('Forum Moderators:');
  document.write(modLinks);
  document.write('</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function addForumLink(forumLinkURL, forumLinkText)
{
	//builds the forum links HTML one at a time (called from individual forum JS files).

	//if this is the first set of passes of this function...
	if (firstPass)
	{
		forumLinks = forumLinks+' <a class="MainMenuLink" href="'+forumLinkURL+'">'+
			forumLinkText+'</a>';
			//build the link for this forum link

		//if more forum links need to be built...
		if (currentForumLink < numberOfForumLinks)
		{
			//output a seperating pipe
			forumLinks=forumLinks+' |';

			//increment the forum link number
			currentForumLink++;
		}
	}
}




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 (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/user.asp?usr='+modUserID+
                      '" 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;
}